nightshade 0.8.0

A cross-platform data-oriented game engine.
Documentation
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
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
    pub name: String,
    pub dark_mode: bool,

    pub override_text_color: Option<[u8; 4]>,
    pub hyperlink_color: Option<[u8; 4]>,
    pub faint_bg_color: Option<[u8; 4]>,
    pub extreme_bg_color: Option<[u8; 4]>,
    pub code_bg_color: Option<[u8; 4]>,
    pub warn_fg_color: Option<[u8; 4]>,
    pub error_fg_color: Option<[u8; 4]>,
    pub window_fill: Option<[u8; 4]>,
    pub panel_fill: Option<[u8; 4]>,

    pub window_stroke_color: Option<[u8; 4]>,
    pub window_stroke_width: Option<f32>,
    pub window_corner_radius: Option<f32>,
    pub window_shadow_color: Option<[u8; 4]>,
    pub window_shadow_size: Option<f32>,

    pub popup_shadow_size: Option<f32>,

    pub selection_bg_fill: Option<[u8; 4]>,
    pub selection_stroke_color: Option<[u8; 4]>,
    pub selection_stroke_width: Option<f32>,

    pub noninteractive_bg_fill: Option<[u8; 4]>,
    pub noninteractive_weak_bg_fill: Option<[u8; 4]>,
    pub noninteractive_bg_stroke_color: Option<[u8; 4]>,
    pub noninteractive_bg_stroke_width: Option<f32>,
    pub noninteractive_corner_radius: Option<f32>,
    pub noninteractive_fg_stroke_color: Option<[u8; 4]>,
    pub noninteractive_fg_stroke_width: Option<f32>,
    pub noninteractive_expansion: Option<f32>,

    pub inactive_bg_fill: Option<[u8; 4]>,
    pub inactive_weak_bg_fill: Option<[u8; 4]>,
    pub inactive_bg_stroke_color: Option<[u8; 4]>,
    pub inactive_bg_stroke_width: Option<f32>,
    pub inactive_corner_radius: Option<f32>,
    pub inactive_fg_stroke_color: Option<[u8; 4]>,
    pub inactive_fg_stroke_width: Option<f32>,
    pub inactive_expansion: Option<f32>,

    pub hovered_bg_fill: Option<[u8; 4]>,
    pub hovered_weak_bg_fill: Option<[u8; 4]>,
    pub hovered_bg_stroke_color: Option<[u8; 4]>,
    pub hovered_bg_stroke_width: Option<f32>,
    pub hovered_corner_radius: Option<f32>,
    pub hovered_fg_stroke_color: Option<[u8; 4]>,
    pub hovered_fg_stroke_width: Option<f32>,
    pub hovered_expansion: Option<f32>,

    pub active_bg_fill: Option<[u8; 4]>,
    pub active_weak_bg_fill: Option<[u8; 4]>,
    pub active_bg_stroke_color: Option<[u8; 4]>,
    pub active_bg_stroke_width: Option<f32>,
    pub active_corner_radius: Option<f32>,
    pub active_fg_stroke_color: Option<[u8; 4]>,
    pub active_fg_stroke_width: Option<f32>,
    pub active_expansion: Option<f32>,

    pub open_bg_fill: Option<[u8; 4]>,
    pub open_weak_bg_fill: Option<[u8; 4]>,
    pub open_bg_stroke_color: Option<[u8; 4]>,
    pub open_bg_stroke_width: Option<f32>,
    pub open_corner_radius: Option<f32>,
    pub open_fg_stroke_color: Option<[u8; 4]>,
    pub open_fg_stroke_width: Option<f32>,
    pub open_expansion: Option<f32>,

    pub resize_corner_size: Option<f32>,
    pub text_cursor_width: Option<f32>,
    pub clip_rect_margin: Option<f32>,
    pub button_frame: Option<bool>,
    pub collapsing_header_frame: Option<bool>,
    pub indent_has_left_vline: Option<bool>,
    pub striped: Option<bool>,
    pub slider_trailing_fill: Option<bool>,
}

impl Default for ThemeConfig {
    fn default() -> Self {
        Self {
            name: "Dark".to_string(),
            dark_mode: true,
            override_text_color: None,
            hyperlink_color: None,
            faint_bg_color: None,
            extreme_bg_color: None,
            code_bg_color: None,
            warn_fg_color: None,
            error_fg_color: None,
            window_fill: None,
            panel_fill: None,
            window_stroke_color: None,
            window_stroke_width: None,
            window_corner_radius: None,
            window_shadow_color: None,
            window_shadow_size: None,
            popup_shadow_size: None,
            selection_bg_fill: None,
            selection_stroke_color: None,
            selection_stroke_width: None,
            noninteractive_bg_fill: None,
            noninteractive_weak_bg_fill: None,
            noninteractive_bg_stroke_color: None,
            noninteractive_bg_stroke_width: None,
            noninteractive_corner_radius: None,
            noninteractive_fg_stroke_color: None,
            noninteractive_fg_stroke_width: None,
            noninteractive_expansion: None,
            inactive_bg_fill: None,
            inactive_weak_bg_fill: None,
            inactive_bg_stroke_color: None,
            inactive_bg_stroke_width: None,
            inactive_corner_radius: None,
            inactive_fg_stroke_color: None,
            inactive_fg_stroke_width: None,
            inactive_expansion: None,
            hovered_bg_fill: None,
            hovered_weak_bg_fill: None,
            hovered_bg_stroke_color: None,
            hovered_bg_stroke_width: None,
            hovered_corner_radius: None,
            hovered_fg_stroke_color: None,
            hovered_fg_stroke_width: None,
            hovered_expansion: None,
            active_bg_fill: None,
            active_weak_bg_fill: None,
            active_bg_stroke_color: None,
            active_bg_stroke_width: None,
            active_corner_radius: None,
            active_fg_stroke_color: None,
            active_fg_stroke_width: None,
            active_expansion: None,
            open_bg_fill: None,
            open_weak_bg_fill: None,
            open_bg_stroke_color: None,
            open_bg_stroke_width: None,
            open_corner_radius: None,
            open_fg_stroke_color: None,
            open_fg_stroke_width: None,
            open_expansion: None,
            resize_corner_size: None,
            text_cursor_width: None,
            clip_rect_margin: None,
            button_frame: None,
            collapsing_header_frame: None,
            indent_has_left_vline: None,
            striped: None,
            slider_trailing_fill: None,
        }
    }
}

pub(crate) fn rgba(color: [u8; 4]) -> egui::Color32 {
    egui::Color32::from_rgba_unmultiplied(color[0], color[1], color[2], color[3])
}

pub(crate) fn to_rgba(color: egui::Color32) -> [u8; 4] {
    [color.r(), color.g(), color.b(), color.a()]
}

struct WidgetOverrides {
    bg_fill: Option<[u8; 4]>,
    weak_bg_fill: Option<[u8; 4]>,
    bg_stroke_color: Option<[u8; 4]>,
    bg_stroke_width: Option<f32>,
    corner_radius: Option<f32>,
    fg_stroke_color: Option<[u8; 4]>,
    fg_stroke_width: Option<f32>,
    expansion: Option<f32>,
}

fn apply_widget_overrides(visuals: &mut egui::style::WidgetVisuals, overrides: WidgetOverrides) {
    if let Some(color) = overrides.bg_fill {
        visuals.bg_fill = rgba(color);
    }
    if let Some(color) = overrides.weak_bg_fill {
        visuals.weak_bg_fill = rgba(color);
    }
    if let Some(color) = overrides.bg_stroke_color {
        visuals.bg_stroke.color = rgba(color);
    }
    if let Some(width) = overrides.bg_stroke_width {
        visuals.bg_stroke.width = width;
    }
    if let Some(radius) = overrides.corner_radius {
        visuals.corner_radius = egui::CornerRadius::same(radius as u8);
    }
    if let Some(color) = overrides.fg_stroke_color {
        visuals.fg_stroke.color = rgba(color);
    }
    if let Some(width) = overrides.fg_stroke_width {
        visuals.fg_stroke.width = width;
    }
    if let Some(value) = overrides.expansion {
        visuals.expansion = value;
    }
}

impl ThemeConfig {
    pub fn to_visuals(&self) -> egui::Visuals {
        let mut visuals = if self.dark_mode {
            egui::Visuals::dark()
        } else {
            egui::Visuals::light()
        };

        if let Some(color) = self.override_text_color {
            visuals.override_text_color = Some(rgba(color));
        }
        if let Some(color) = self.hyperlink_color {
            visuals.hyperlink_color = rgba(color);
        }
        if let Some(color) = self.faint_bg_color {
            visuals.faint_bg_color = rgba(color);
        }
        if let Some(color) = self.extreme_bg_color {
            visuals.extreme_bg_color = rgba(color);
        }
        if let Some(color) = self.code_bg_color {
            visuals.code_bg_color = rgba(color);
        }
        if let Some(color) = self.warn_fg_color {
            visuals.warn_fg_color = rgba(color);
        }
        if let Some(color) = self.error_fg_color {
            visuals.error_fg_color = rgba(color);
        }
        if let Some(color) = self.window_fill {
            visuals.window_fill = rgba(color);
        }
        if let Some(color) = self.panel_fill {
            visuals.panel_fill = rgba(color);
        }
        if let Some(color) = self.window_stroke_color {
            visuals.window_stroke.color = rgba(color);
        }
        if let Some(width) = self.window_stroke_width {
            visuals.window_stroke.width = width;
        }
        if let Some(radius) = self.window_corner_radius {
            visuals.window_corner_radius = egui::CornerRadius::same(radius as u8);
        }
        if let Some(color) = self.window_shadow_color {
            visuals.window_shadow.color = rgba(color);
        }
        if let Some(size) = self.window_shadow_size {
            visuals.window_shadow.spread = size as u8;
        }
        if let Some(size) = self.popup_shadow_size {
            visuals.popup_shadow.spread = size as u8;
        }
        if let Some(color) = self.selection_bg_fill {
            visuals.selection.bg_fill = rgba(color);
        }
        if let Some(color) = self.selection_stroke_color {
            visuals.selection.stroke.color = rgba(color);
        }
        if let Some(width) = self.selection_stroke_width {
            visuals.selection.stroke.width = width;
        }

        apply_widget_overrides(
            &mut visuals.widgets.noninteractive,
            WidgetOverrides {
                bg_fill: self.noninteractive_bg_fill,
                weak_bg_fill: self.noninteractive_weak_bg_fill,
                bg_stroke_color: self.noninteractive_bg_stroke_color,
                bg_stroke_width: self.noninteractive_bg_stroke_width,
                corner_radius: self.noninteractive_corner_radius,
                fg_stroke_color: self.noninteractive_fg_stroke_color,
                fg_stroke_width: self.noninteractive_fg_stroke_width,
                expansion: self.noninteractive_expansion,
            },
        );
        apply_widget_overrides(
            &mut visuals.widgets.inactive,
            WidgetOverrides {
                bg_fill: self.inactive_bg_fill,
                weak_bg_fill: self.inactive_weak_bg_fill,
                bg_stroke_color: self.inactive_bg_stroke_color,
                bg_stroke_width: self.inactive_bg_stroke_width,
                corner_radius: self.inactive_corner_radius,
                fg_stroke_color: self.inactive_fg_stroke_color,
                fg_stroke_width: self.inactive_fg_stroke_width,
                expansion: self.inactive_expansion,
            },
        );
        apply_widget_overrides(
            &mut visuals.widgets.hovered,
            WidgetOverrides {
                bg_fill: self.hovered_bg_fill,
                weak_bg_fill: self.hovered_weak_bg_fill,
                bg_stroke_color: self.hovered_bg_stroke_color,
                bg_stroke_width: self.hovered_bg_stroke_width,
                corner_radius: self.hovered_corner_radius,
                fg_stroke_color: self.hovered_fg_stroke_color,
                fg_stroke_width: self.hovered_fg_stroke_width,
                expansion: self.hovered_expansion,
            },
        );
        apply_widget_overrides(
            &mut visuals.widgets.active,
            WidgetOverrides {
                bg_fill: self.active_bg_fill,
                weak_bg_fill: self.active_weak_bg_fill,
                bg_stroke_color: self.active_bg_stroke_color,
                bg_stroke_width: self.active_bg_stroke_width,
                corner_radius: self.active_corner_radius,
                fg_stroke_color: self.active_fg_stroke_color,
                fg_stroke_width: self.active_fg_stroke_width,
                expansion: self.active_expansion,
            },
        );
        apply_widget_overrides(
            &mut visuals.widgets.open,
            WidgetOverrides {
                bg_fill: self.open_bg_fill,
                weak_bg_fill: self.open_weak_bg_fill,
                bg_stroke_color: self.open_bg_stroke_color,
                bg_stroke_width: self.open_bg_stroke_width,
                corner_radius: self.open_corner_radius,
                fg_stroke_color: self.open_fg_stroke_color,
                fg_stroke_width: self.open_fg_stroke_width,
                expansion: self.open_expansion,
            },
        );

        if let Some(size) = self.resize_corner_size {
            visuals.resize_corner_size = size;
        }
        if let Some(width) = self.text_cursor_width {
            visuals.text_cursor.stroke.width = width;
        }
        if let Some(margin) = self.clip_rect_margin {
            visuals.clip_rect_margin = margin;
        }
        if let Some(value) = self.button_frame {
            visuals.button_frame = value;
        }
        if let Some(value) = self.collapsing_header_frame {
            visuals.collapsing_header_frame = value;
        }
        if let Some(value) = self.indent_has_left_vline {
            visuals.indent_has_left_vline = value;
        }
        if let Some(value) = self.striped {
            visuals.striped = value;
        }
        if let Some(value) = self.slider_trailing_fill {
            visuals.slider_trailing_fill = value;
        }

        visuals
    }
}

pub struct ThemeState {
    pub current_config: ThemeConfig,
    pub presets: Vec<ThemeConfig>,
    pub selected_preset_index: Option<usize>,
    pub preview_theme_index: Option<usize>,
    pub show_theme_editor: bool,
}

impl Default for ThemeState {
    fn default() -> Self {
        let presets = ThemeConfig::all_presets().to_vec();
        Self {
            current_config: presets[0].clone(),
            presets,
            selected_preset_index: Some(0),
            preview_theme_index: None,
            show_theme_editor: false,
        }
    }
}

impl ThemeState {
    pub fn select_preset_by_name(&mut self, name: &str) -> bool {
        for (index, preset) in self.presets.iter().enumerate() {
            if preset.name == name {
                self.current_config = preset.clone();
                self.selected_preset_index = Some(index);
                return true;
            }
        }
        false
    }
}

pub fn apply_theme(ui_context: &egui::Context, theme_state: &ThemeState) {
    ui_context.set_visuals(get_active_theme_visuals(theme_state));
}

pub fn get_active_theme_visuals(theme_state: &ThemeState) -> egui::Visuals {
    if let Some(preview_index) = theme_state.preview_theme_index
        && let Some(preset) = theme_state.presets.get(preview_index)
    {
        return preset.to_visuals();
    }
    theme_state.current_config.to_visuals()
}