rusting_engine 1.0.3

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Shared modern editor palette and component styles.

use super::{
    button, button_with_sense, combo_box, BorderStyle, ButtonProps,
    ComboBoxProps, ComboBoxResponse, Edges, ElementStyle, ShadowStyle,
};
use crate::editor::icons::paint_editor_icon;
use crate::editor::EditorIcon;

/// Colors and ready-to-use styles for the RustingEngine editor.
pub struct EditorTheme;

impl EditorTheme {
    pub const BACKGROUND: egui::Color32 = egui::Color32::from_rgb(12, 15, 22);
    pub const PANEL: egui::Color32 = egui::Color32::from_rgb(20, 24, 34);
    pub const PANEL_RAISED: egui::Color32 = egui::Color32::from_rgb(27, 32, 45);
    pub const INPUT: egui::Color32 = egui::Color32::from_rgb(15, 19, 28);
    pub const BORDER: egui::Color32 = egui::Color32::from_rgb(45, 52, 68);
    pub const BORDER_SOFT: egui::Color32 = egui::Color32::from_rgb(34, 40, 54);
    pub const TEXT: egui::Color32 = egui::Color32::from_rgb(226, 231, 241);
    pub const TEXT_MUTED: egui::Color32 =
        egui::Color32::from_rgb(145, 154, 173);
    pub const ACCENT: egui::Color32 = egui::Color32::from_rgb(70, 125, 255);
    pub const ACCENT_HOVER: egui::Color32 =
        egui::Color32::from_rgb(88, 140, 255);
    pub const ACCENT_ACTIVE: egui::Color32 =
        egui::Color32::from_rgb(54, 105, 225);
    pub const BUTTON: egui::Color32 = egui::Color32::from_rgb(31, 37, 51);
    pub const BUTTON_HOVER: egui::Color32 = egui::Color32::from_rgb(42, 50, 68);

    /// Style shared by controls placed on the main editor toolbar.
    fn toolbar_control_style(selected: bool) -> ElementStyle {
        ElementStyle {
            height: super::Length::Px(28.0),
            margin: Edges::symmetric(2.0, 1.0),
            padding: Edges::symmetric(4.0, 10.0),
            background: Some(if selected {
                Self::ACCENT
            } else {
                Self::BUTTON
            }),
            hover_background: Some(Self::ACCENT_HOVER),
            active_background: Some(Self::ACCENT_ACTIVE),
            text_color: Some(Self::TEXT),
            border: BorderStyle {
                width: 1.0,
                color: if selected {
                    Self::ACCENT_HOVER
                } else {
                    Self::BORDER
                },
                radius: 5.0,
            },
            shadow: selected.then_some(ShadowStyle {
                offset: [0, 2],
                blur: 8,
                spread: 0,
                color: egui::Color32::from_black_alpha(80),
            }),
            ..ElementStyle::default()
        }
    }

    /// Applies the palette to native egui controls and windows.
    pub fn apply(context: &egui::Context) {
        let mut style = (*context.style()).clone();
        style.spacing.item_spacing = egui::vec2(8.0, 7.0);
        style.spacing.button_padding = egui::vec2(11.0, 6.0);
        style.spacing.indent = 18.0;
        style.visuals = egui::Visuals::dark();
        style.visuals.override_text_color = Some(Self::TEXT);
        style.visuals.panel_fill = Self::BACKGROUND;
        style.visuals.window_fill = Self::PANEL;
        style.visuals.extreme_bg_color = Self::INPUT;
        style.visuals.faint_bg_color = Self::PANEL_RAISED;
        style.visuals.code_bg_color = Self::INPUT;
        style.visuals.selection.bg_fill = Self::ACCENT;
        style.visuals.selection.stroke = egui::Stroke::new(1.0_f32, Self::TEXT);
        style.visuals.window_corner_radius = egui::CornerRadius::same(8);
        style.visuals.menu_corner_radius = egui::CornerRadius::same(7);
        style.visuals.window_stroke = egui::Stroke::new(1.0_f32, Self::BORDER);
        style.visuals.window_shadow = egui::epaint::Shadow {
            offset: [0, 8],
            blur: 24,
            spread: 2,
            color: egui::Color32::from_black_alpha(120),
        };
        style.visuals.popup_shadow = egui::epaint::Shadow {
            offset: [0, 6],
            blur: 18,
            spread: 1,
            color: egui::Color32::from_black_alpha(110),
        };
        style.visuals.widgets.inactive.weak_bg_fill = Self::BUTTON;
        style.visuals.widgets.inactive.bg_fill = Self::BUTTON;
        style.visuals.widgets.inactive.bg_stroke =
            egui::Stroke::new(1.0_f32, Self::BORDER);
        style.visuals.widgets.inactive.corner_radius =
            egui::CornerRadius::same(5);
        style.visuals.widgets.hovered.weak_bg_fill = Self::BUTTON_HOVER;
        style.visuals.widgets.hovered.bg_fill = Self::BUTTON_HOVER;
        style.visuals.widgets.hovered.bg_stroke =
            egui::Stroke::new(1.0_f32, Self::ACCENT);
        style.visuals.widgets.hovered.corner_radius =
            egui::CornerRadius::same(5);
        style.visuals.widgets.active.weak_bg_fill = Self::ACCENT_ACTIVE;
        style.visuals.widgets.active.bg_fill = Self::ACCENT_ACTIVE;
        style.visuals.widgets.active.bg_stroke =
            egui::Stroke::new(1.0_f32, Self::ACCENT_HOVER);
        style.visuals.widgets.active.corner_radius =
            egui::CornerRadius::same(5);
        style.visuals.widgets.noninteractive.bg_stroke =
            egui::Stroke::new(1.0_f32, Self::BORDER_SOFT);
        style.visuals.collapsing_header_frame = true;
        style.visuals.indent_has_left_vline = false;
        context.set_style(style);
    }

    /// Standard toolbar button with optional blue selected state.
    pub fn toolbar_button(
        ui: &mut egui::Ui,
        text: &str,
        selected: bool,
        enabled: bool,
    ) -> egui::Response {
        let style = Self::toolbar_control_style(selected);
        button(
            ui,
            ButtonProps {
                text,
                tooltip: None,
                enabled,
                style,
            },
        )
    }

    /// Toolbar button with a code-drawn icon before its text.
    pub fn toolbar_icon_button(
        ui: &mut egui::Ui,
        text: &str,
        icon: EditorIcon,
        width: f32,
        enabled: bool,
    ) -> egui::Response {
        let label = format!("    {text}");
        let response = button(
            ui,
            ButtonProps {
                text: &label,
                tooltip: None,
                enabled,
                style: ElementStyle {
                    width: super::Length::Px(width),
                    ..Self::toolbar_control_style(false)
                },
            },
        );
        let icon_rect = egui::Rect::from_center_size(
            egui::pos2(response.rect.left() + 14.0, response.rect.center().y),
            egui::vec2(15.0, 15.0),
        );
        paint_editor_icon(
            ui.painter(),
            icon,
            icon_rect,
            ui.style().interact(&response).text_color(),
        );
        response
    }

    /// ComboBox that aligns exactly with buttons in the main toolbar.
    pub fn toolbar_combo_box<R>(
        ui: &mut egui::Ui,
        id_salt: impl std::hash::Hash,
        selected_text: &str,
        width: f32,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> ComboBoxResponse<R> {
        combo_box(
            ui,
            ComboBoxProps {
                style: ElementStyle {
                    width: super::Length::Px(width),
                    ..Self::toolbar_control_style(false)
                },
                popup_min_width: width,
                ..ComboBoxProps::new(id_salt, selected_text)
            },
            add_contents,
        )
    }

    /// ComboBox with separate widths for its closed control and opened panel.
    pub fn toolbar_combo_box_with_popup<R>(
        ui: &mut egui::Ui,
        id_salt: impl std::hash::Hash,
        selected_text: &str,
        control_width: f32,
        popup_width: f32,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> ComboBoxResponse<R> {
        combo_box(
            ui,
            ComboBoxProps {
                style: ElementStyle {
                    width: super::Length::Px(control_width),
                    ..Self::toolbar_control_style(false)
                },
                popup_min_width: popup_width,
                ..ComboBoxProps::new(id_salt, selected_text)
            },
            add_contents,
        )
    }

    /// Toolbar ComboBox with a code-drawn icon before its label.
    pub fn toolbar_icon_combo_box_with_popup<R>(
        ui: &mut egui::Ui,
        id_salt: impl std::hash::Hash,
        selected_text: &str,
        icon: super::super::EditorIcon,
        control_width: f32,
        popup_width: f32,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> ComboBoxResponse<R> {
        combo_box(
            ui,
            ComboBoxProps {
                leading_icon: Some(icon),
                style: ElementStyle {
                    width: super::Length::Px(control_width),
                    ..Self::toolbar_control_style(false)
                },
                popup_min_width: popup_width,
                ..ComboBoxProps::new(id_salt, selected_text)
            },
            add_contents,
        )
    }

    /// Dropdown menu used to group less frequent toolbar actions.
    pub fn toolbar_menu<R>(
        ui: &mut egui::Ui,
        id_salt: impl std::hash::Hash,
        title: &str,
        control_width: f32,
        popup_width: f32,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> ComboBoxResponse<R> {
        Self::toolbar_combo_box_with_popup(
            ui,
            id_salt,
            title,
            control_width,
            popup_width,
            add_contents,
        )
    }

    /// Quiet label that separates groups inside one dropdown menu.
    pub fn menu_section(ui: &mut egui::Ui, text: &str) {
        ui.add_space(5.0);
        ui.label(
            egui::RichText::new(text)
                .size(11.0)
                .color(Self::TEXT_MUTED)
                .strong(),
        );
        ui.add_space(2.0);
    }

    /// Full-width action with the same styling in every dropdown menu.
    pub fn menu_action(
        ui: &mut egui::Ui,
        text: &str,
        enabled: bool,
    ) -> egui::Response {
        Self::menu_item(ui, text, false, enabled)
    }

    /// Full-width selectable value used inside styled dropdowns.
    pub fn menu_choice(
        ui: &mut egui::Ui,
        text: &str,
        selected: bool,
        enabled: bool,
    ) -> egui::Response {
        Self::menu_item(ui, text, selected, enabled)
    }

    /// Shared renderer for normal actions and selected menu values.
    fn menu_item(
        ui: &mut egui::Ui,
        text: &str,
        selected: bool,
        enabled: bool,
    ) -> egui::Response {
        Self::menu_item_with_sense(
            ui,
            text,
            selected,
            enabled,
            egui::Sense::click(),
        )
    }

    fn menu_item_with_sense(
        ui: &mut egui::Ui,
        text: &str,
        selected: bool,
        enabled: bool,
        sense: egui::Sense,
    ) -> egui::Response {
        button_with_sense(
            ui,
            ButtonProps {
                text,
                tooltip: None,
                enabled,
                style: ElementStyle {
                    width: super::Length::Fill,
                    height: super::Length::Px(32.0),
                    margin: Edges::symmetric(1.0, 0.0),
                    padding: Edges::symmetric(6.0, 12.0),
                    background: Some(if selected {
                        Self::ACCENT_ACTIVE
                    } else {
                        Self::PANEL_RAISED
                    }),
                    hover_background: Some(Self::ACCENT),
                    active_background: Some(Self::ACCENT_ACTIVE),
                    text_color: Some(Self::TEXT),
                    text_align: egui::Align2::LEFT_CENTER,
                    border: BorderStyle {
                        width: 1.0,
                        color: if selected {
                            Self::ACCENT_HOVER
                        } else {
                            Self::BORDER_SOFT
                        },
                        radius: 5.0,
                    },
                    ..ElementStyle::default()
                },
            },
            sense,
        )
    }

    /// One full-width row in a parent-child tree.
    pub fn tree_row(
        ui: &mut egui::Ui,
        text: &str,
        depth: usize,
        selected: bool,
    ) -> egui::Response {
        const INDENT: f32 = 18.0;
        let response = ui
            .horizontal(|ui| {
                ui.spacing_mut().item_spacing.x = 0.0;
                ui.add_space(depth as f32 * INDENT);
                Self::menu_item_with_sense(
                    ui,
                    text,
                    selected,
                    true,
                    egui::Sense::click_and_drag(),
                )
            })
            .inner;

        // Lines make parent depth visible without depending on icon fonts.
        if depth > 0 {
            let painter = ui.painter();
            for level in 0..depth {
                let x = response.rect.left() - (depth - level) as f32 * INDENT
                    + INDENT * 0.5;
                painter.line_segment(
                    [
                        egui::pos2(x, response.rect.top()),
                        egui::pos2(x, response.rect.bottom()),
                    ],
                    egui::Stroke::new(1.0_f32, Self::BORDER),
                );
            }
            let branch_x = response.rect.left() - INDENT * 0.5;
            painter.line_segment(
                [
                    egui::pos2(branch_x, response.rect.center().y),
                    egui::pos2(
                        response.rect.left() - 3.0,
                        response.rect.center().y,
                    ),
                ],
                egui::Stroke::new(1.0_f32, Self::BORDER),
            );
        }
        response
    }

    /// Compact button used by dock headers.
    pub fn dock_button(
        ui: &mut egui::Ui,
        symbol: &str,
        tooltip: &str,
    ) -> egui::Response {
        let style = ElementStyle {
            padding: Edges::all(0.0),
            font_size: Some(11.0),
            background: Some(Self::BUTTON),
            hover_background: Some(Self::ACCENT),
            active_background: Some(Self::ACCENT_ACTIVE),
            border: BorderStyle {
                width: 1.0,
                color: Self::BORDER,
                radius: 4.0,
            },
            ..ElementStyle::fixed(22.0, 20.0)
        };
        button(
            ui,
            ButtonProps {
                text: symbol,
                tooltip: Some(tooltip),
                enabled: true,
                style,
            },
        )
    }
}