dndgamerolls 0.1.10

DnD Game Rolls - D&D dice roller with CLI and 3D visualization using Bevy
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! Settings UI systems
//!
//! This module contains systems for the settings button, modal, and color picker.

use bevy::prelude::*;

use crate::dice3d::types::{
    ColorComponent, ColorPreview, ColorSetting, ColorSlider, ColorSliderHandle, ColorSliderTrack,
    ColorTextInput, ColorValueLabel, DiceRollerRoot, IconAssets, IconType, SettingsButton,
    SettingsCancelButton, SettingsModal, SettingsModalOverlay, SettingsOkButton, SettingsState,
};

// ============================================================================
// Constants
// ============================================================================

const MODAL_BG: Color = Color::srgba(0.0, 0.0, 0.0, 0.8);
const PANEL_BG: Color = Color::srgb(0.15, 0.15, 0.2);
const BUTTON_BG: Color = Color::srgb(0.25, 0.25, 0.3);
const BUTTON_HOVER: Color = Color::srgb(0.35, 0.35, 0.4);
const BUTTON_OK: Color = Color::srgb(0.2, 0.5, 0.3);
const BUTTON_CANCEL: Color = Color::srgb(0.5, 0.2, 0.2);
const TEXT_PRIMARY: Color = Color::srgb(0.9, 0.9, 0.9);
const TEXT_SECONDARY: Color = Color::srgb(0.7, 0.7, 0.7);
const SLIDER_TRACK_BG: Color = Color::srgb(0.1, 0.1, 0.1);
const INPUT_BG: Color = Color::srgb(0.1, 0.1, 0.12);

// ============================================================================
// Setup Systems
// ============================================================================

/// Spawn the settings button (gear icon) in the dice view
pub fn spawn_settings_button(commands: &mut Commands, icon_assets: Res<IconAssets>) {
    let icon_handle = icon_assets.icons.get(&IconType::Settings).cloned();

    commands
        .spawn((
            ButtonBundle {
                style: Style {
                    position_type: PositionType::Absolute,
                    top: Val::Px(50.0),
                    right: Val::Px(10.0), // Rightmost side
                    width: Val::Px(36.0),
                    height: Val::Px(36.0),
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    border: UiRect::all(Val::Px(1.0)),
                    ..default()
                },
                background_color: BackgroundColor(BUTTON_BG),
                border_color: BorderColor(Color::srgb(0.4, 0.4, 0.45)),
                border_radius: BorderRadius::all(Val::Px(4.0)),
                ..default()
            },
            SettingsButton,
            DiceRollerRoot,
        ))
        .with_children(|parent| {
            if let Some(handle) = icon_handle {
                // Use icon image
                parent.spawn(ImageBundle {
                    image: UiImage::new(handle),
                    style: Style {
                        width: Val::Px(24.0),
                        height: Val::Px(24.0),
                        ..default()
                    },
                    ..default()
                });
            } else {
                // Fallback to unicode
                parent.spawn(TextBundle::from_section(
                    "âš™",
                    TextStyle {
                        font_size: 22.0,
                        color: TEXT_PRIMARY,
                        ..default()
                    },
                ));
            }
        });
}

/// Spawn the settings modal
pub fn spawn_settings_modal(commands: &mut Commands, settings_state: &SettingsState) {
    let editing_color = &settings_state.editing_color;

    // Modal overlay (darkens background)
    commands
        .spawn((
            NodeBundle {
                style: Style {
                    position_type: PositionType::Absolute,
                    left: Val::Px(0.0),
                    top: Val::Px(0.0),
                    width: Val::Percent(100.0),
                    height: Val::Percent(100.0),
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    ..default()
                },
                background_color: BackgroundColor(MODAL_BG),
                z_index: ZIndex::Global(100),
                ..default()
            },
            SettingsModalOverlay,
        ))
        .with_children(|overlay| {
            // Modal window
            overlay
                .spawn((
                    NodeBundle {
                        style: Style {
                            width: Val::Px(450.0),
                            min_height: Val::Px(400.0),
                            flex_direction: FlexDirection::Column,
                            padding: UiRect::all(Val::Px(20.0)),
                            row_gap: Val::Px(15.0),
                            border: UiRect::all(Val::Px(2.0)),
                            ..default()
                        },
                        background_color: BackgroundColor(PANEL_BG),
                        border_color: BorderColor(Color::srgb(0.4, 0.4, 0.5)),
                        border_radius: BorderRadius::all(Val::Px(8.0)),
                        ..default()
                    },
                    SettingsModal,
                ))
                .with_children(|modal| {
                    // Title
                    modal.spawn(TextBundle::from_section(
                        "Settings",
                        TextStyle {
                            font_size: 24.0,
                            color: TEXT_PRIMARY,
                            ..default()
                        },
                    ));

                    // Background Color section
                    modal.spawn(TextBundle::from_section(
                        "Background Color",
                        TextStyle {
                            font_size: 18.0,
                            color: TEXT_SECONDARY,
                            ..default()
                        },
                    ));

                    // Color preview and sliders container
                    modal
                        .spawn(NodeBundle {
                            style: Style {
                                flex_direction: FlexDirection::Row,
                                column_gap: Val::Px(20.0),
                                ..default()
                            },
                            ..default()
                        })
                        .with_children(|row| {
                            // Color preview box
                            row.spawn((
                                NodeBundle {
                                    style: Style {
                                        width: Val::Px(80.0),
                                        height: Val::Px(120.0),
                                        border: UiRect::all(Val::Px(2.0)),
                                        ..default()
                                    },
                                    background_color: BackgroundColor(editing_color.to_color()),
                                    border_color: BorderColor(Color::srgb(0.5, 0.5, 0.5)),
                                    border_radius: BorderRadius::all(Val::Px(4.0)),
                                    ..default()
                                },
                                ColorPreview,
                            ));

                            // Sliders column
                            row.spawn(NodeBundle {
                                style: Style {
                                    flex_direction: FlexDirection::Column,
                                    flex_grow: 1.0,
                                    row_gap: Val::Px(10.0),
                                    ..default()
                                },
                                ..default()
                            })
                            .with_children(|sliders| {
                                // Alpha slider
                                spawn_color_slider(
                                    sliders,
                                    ColorComponent::Alpha,
                                    "A",
                                    editing_color.a,
                                    Color::WHITE,
                                );
                                // Red slider
                                spawn_color_slider(
                                    sliders,
                                    ColorComponent::Red,
                                    "R",
                                    editing_color.r,
                                    Color::srgb(1.0, 0.3, 0.3),
                                );
                                // Green slider
                                spawn_color_slider(
                                    sliders,
                                    ColorComponent::Green,
                                    "G",
                                    editing_color.g,
                                    Color::srgb(0.3, 1.0, 0.3),
                                );
                                // Blue slider
                                spawn_color_slider(
                                    sliders,
                                    ColorComponent::Blue,
                                    "B",
                                    editing_color.b,
                                    Color::srgb(0.3, 0.3, 1.0),
                                );
                            });
                        });

                    // Text input section
                    modal.spawn(TextBundle::from_section(
                        "Enter color (hex, ARGB, or labeled):",
                        TextStyle {
                            font_size: 14.0,
                            color: TEXT_SECONDARY,
                            ..default()
                        },
                    ));

                    // Text input field
                    modal
                        .spawn((
                            NodeBundle {
                                style: Style {
                                    width: Val::Percent(100.0),
                                    height: Val::Px(32.0),
                                    padding: UiRect::horizontal(Val::Px(10.0)),
                                    align_items: AlignItems::Center,
                                    border: UiRect::all(Val::Px(1.0)),
                                    ..default()
                                },
                                background_color: BackgroundColor(INPUT_BG),
                                border_color: BorderColor(Color::srgb(0.3, 0.3, 0.35)),
                                border_radius: BorderRadius::all(Val::Px(4.0)),
                                ..default()
                            },
                            ColorTextInput,
                        ))
                        .with_children(|input| {
                            input.spawn(TextBundle::from_section(
                                editing_color.to_hex(),
                                TextStyle {
                                    font_size: 16.0,
                                    color: TEXT_PRIMARY,
                                    ..default()
                                },
                            ));
                        });

                    // Format hints
                    modal.spawn(TextBundle::from_section(
                        "Formats: #AARRGGBB, A:1 R:0.5 G:0.3 B:0.2, or 1,0.5,0.3,0.2",
                        TextStyle {
                            font_size: 12.0,
                            color: Color::srgb(0.5, 0.5, 0.5),
                            ..default()
                        },
                    ));

                    // Spacer
                    modal.spawn(NodeBundle {
                        style: Style {
                            flex_grow: 1.0,
                            ..default()
                        },
                        ..default()
                    });

                    // Buttons row
                    modal
                        .spawn(NodeBundle {
                            style: Style {
                                flex_direction: FlexDirection::Row,
                                justify_content: JustifyContent::FlexEnd,
                                column_gap: Val::Px(10.0),
                                ..default()
                            },
                            ..default()
                        })
                        .with_children(|buttons| {
                            // Cancel button
                            buttons
                                .spawn((
                                    ButtonBundle {
                                        style: Style {
                                            width: Val::Px(100.0),
                                            height: Val::Px(36.0),
                                            justify_content: JustifyContent::Center,
                                            align_items: AlignItems::Center,
                                            border: UiRect::all(Val::Px(1.0)),
                                            ..default()
                                        },
                                        background_color: BackgroundColor(BUTTON_CANCEL),
                                        border_color: BorderColor(Color::srgb(0.6, 0.3, 0.3)),
                                        border_radius: BorderRadius::all(Val::Px(4.0)),
                                        ..default()
                                    },
                                    SettingsCancelButton,
                                ))
                                .with_children(|btn| {
                                    btn.spawn(TextBundle::from_section(
                                        "Cancel",
                                        TextStyle {
                                            font_size: 16.0,
                                            color: TEXT_PRIMARY,
                                            ..default()
                                        },
                                    ));
                                });

                            // OK button
                            buttons
                                .spawn((
                                    ButtonBundle {
                                        style: Style {
                                            width: Val::Px(100.0),
                                            height: Val::Px(36.0),
                                            justify_content: JustifyContent::Center,
                                            align_items: AlignItems::Center,
                                            border: UiRect::all(Val::Px(1.0)),
                                            ..default()
                                        },
                                        background_color: BackgroundColor(BUTTON_OK),
                                        border_color: BorderColor(Color::srgb(0.3, 0.6, 0.4)),
                                        border_radius: BorderRadius::all(Val::Px(4.0)),
                                        ..default()
                                    },
                                    SettingsOkButton,
                                ))
                                .with_children(|btn| {
                                    btn.spawn(TextBundle::from_section(
                                        "OK",
                                        TextStyle {
                                            font_size: 16.0,
                                            color: TEXT_PRIMARY,
                                            ..default()
                                        },
                                    ));
                                });
                        });
                });
        });
}

/// Helper to spawn a color slider row
fn spawn_color_slider(
    parent: &mut ChildBuilder,
    component: ColorComponent,
    label: &str,
    value: f32,
    track_color: Color,
) {
    parent
        .spawn(NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                column_gap: Val::Px(10.0),
                height: Val::Px(24.0),
                ..default()
            },
            ..default()
        })
        .with_children(|row| {
            // Label
            row.spawn(TextBundle::from_section(
                label,
                TextStyle {
                    font_size: 16.0,
                    color: track_color,
                    ..default()
                },
            ));

            // Slider track
            row.spawn((
                NodeBundle {
                    style: Style {
                        width: Val::Px(180.0),
                        height: Val::Px(16.0),
                        border: UiRect::all(Val::Px(1.0)),
                        ..default()
                    },
                    background_color: BackgroundColor(SLIDER_TRACK_BG),
                    border_color: BorderColor(Color::srgb(0.3, 0.3, 0.35)),
                    border_radius: BorderRadius::all(Val::Px(3.0)),
                    ..default()
                },
                ColorSliderTrack { component },
                ColorSlider { component },
                Interaction::None,
            ))
            .with_children(|track| {
                // Slider handle
                track.spawn((
                    NodeBundle {
                        style: Style {
                            position_type: PositionType::Absolute,
                            left: Val::Percent(value * 100.0 - 4.0), // Center the 14px handle
                            top: Val::Px(1.0),
                            width: Val::Px(14.0),
                            height: Val::Px(14.0),
                            ..default()
                        },
                        background_color: BackgroundColor(track_color),
                        border_radius: BorderRadius::all(Val::Px(7.0)),
                        ..default()
                    },
                    ColorSliderHandle { component },
                ));
            });

            // Value label
            row.spawn((
                TextBundle::from_section(
                    format!("{:.2}", value),
                    TextStyle {
                        font_size: 14.0,
                        color: TEXT_SECONDARY,
                        ..default()
                    },
                ),
                ColorValueLabel { component },
            ));
        });
}

// ============================================================================
// Interaction Systems
// ============================================================================

/// Handle settings button click
pub fn handle_settings_button_click(
    interaction_query: Query<
        (&Interaction, &mut BackgroundColor),
        (Changed<Interaction>, With<SettingsButton>),
    >,
    mut settings_state: ResMut<SettingsState>,
) {
    for (interaction, _bg) in interaction_query.iter() {
        if *interaction == Interaction::Pressed {
            settings_state.show_modal = true;
            settings_state.editing_color = settings_state.settings.background_color.clone();
            settings_state.color_input_text = settings_state.editing_color.to_hex();
        }
    }
}

/// Handle settings button hover
pub fn handle_settings_button_hover(
    mut interaction_query: Query<
        (&Interaction, &mut BackgroundColor),
        (Changed<Interaction>, With<SettingsButton>),
    >,
) {
    for (interaction, mut bg) in interaction_query.iter_mut() {
        *bg = BackgroundColor(match interaction {
            Interaction::Hovered => BUTTON_HOVER,
            _ => BUTTON_BG,
        });
    }
}

/// Spawn/despawn settings modal based on state
pub fn manage_settings_modal(
    mut commands: Commands,
    settings_state: Res<SettingsState>,
    modal_query: Query<Entity, With<SettingsModalOverlay>>,
) {
    if !settings_state.is_changed() {
        return;
    }

    if settings_state.show_modal {
        // Spawn modal if not exists
        if modal_query.is_empty() {
            spawn_settings_modal(&mut commands, &settings_state);
        }
    } else {
        // Despawn modal
        for entity in modal_query.iter() {
            commands.entity(entity).despawn_recursive();
        }
    }
}

/// Handle OK button click
pub fn handle_settings_ok_click(
    interaction_query: Query<&Interaction, (Changed<Interaction>, With<SettingsOkButton>)>,
    mut settings_state: ResMut<SettingsState>,
    mut clear_color: ResMut<ClearColor>,
) {
    for interaction in interaction_query.iter() {
        if *interaction == Interaction::Pressed {
            // Apply the editing color
            settings_state.settings.background_color = settings_state.editing_color.clone();

            // Update the clear color
            clear_color.0 = settings_state.settings.background_color.to_color();

            // Save to file
            if let Err(e) = settings_state.settings.save() {
                eprintln!("Failed to save settings: {}", e);
            }

            // Close modal
            settings_state.show_modal = false;
        }
    }
}

/// Handle Cancel button click
pub fn handle_settings_cancel_click(
    interaction_query: Query<&Interaction, (Changed<Interaction>, With<SettingsCancelButton>)>,
    mut settings_state: ResMut<SettingsState>,
) {
    for interaction in interaction_query.iter() {
        if *interaction == Interaction::Pressed {
            // Discard changes and close modal
            settings_state.show_modal = false;
        }
    }
}

/// Handle color slider interaction
pub fn handle_color_slider_drag(
    mut interaction_query: Query<
        (&Interaction, &ColorSlider, &Node, &GlobalTransform),
        Changed<Interaction>,
    >,
    mut settings_state: ResMut<SettingsState>,
    windows: Query<&Window>,
) {
    let Ok(window) = windows.get_single() else {
        return;
    };

    let Some(cursor_pos) = window.cursor_position() else {
        return;
    };

    for (interaction, slider, _node, _transform) in interaction_query.iter_mut() {
        if *interaction == Interaction::Pressed {
            settings_state.dragging_slider = Some(slider.component);
        }
    }

    // Handle ongoing drag
    if let Some(component) = settings_state.dragging_slider {
        // Get track bounds from any slider with matching component
        for (_interaction, slider, node, transform) in interaction_query.iter() {
            if slider.component == component {
                let rect = node.logical_rect(transform);
                let relative_x = (cursor_pos.x - rect.min.x) / rect.width();
                let value = relative_x.clamp(0.0, 1.0);

                match component {
                    ColorComponent::Alpha => settings_state.editing_color.a = value,
                    ColorComponent::Red => settings_state.editing_color.r = value,
                    ColorComponent::Green => settings_state.editing_color.g = value,
                    ColorComponent::Blue => settings_state.editing_color.b = value,
                }
            }
        }
    }
}

/// Release slider drag on mouse release
pub fn handle_slider_release(
    mouse_button: Res<ButtonInput<MouseButton>>,
    mut settings_state: ResMut<SettingsState>,
) {
    if mouse_button.just_released(MouseButton::Left) {
        settings_state.dragging_slider = None;
    }
}

/// Continue slider drag while mouse is held
pub fn handle_slider_drag_continuous(
    mut settings_state: ResMut<SettingsState>,
    slider_query: Query<(&ColorSlider, &Node, &GlobalTransform)>,
    windows: Query<&Window>,
    mouse_button: Res<ButtonInput<MouseButton>>,
) {
    if !mouse_button.pressed(MouseButton::Left) {
        return;
    }

    let Some(component) = settings_state.dragging_slider else {
        return;
    };

    let Ok(window) = windows.get_single() else {
        return;
    };

    let Some(cursor_pos) = window.cursor_position() else {
        return;
    };

    for (slider, node, transform) in slider_query.iter() {
        if slider.component == component {
            let rect = node.logical_rect(transform);
            let relative_x = (cursor_pos.x - rect.min.x) / rect.width();
            let value = relative_x.clamp(0.0, 1.0);

            match component {
                ColorComponent::Alpha => settings_state.editing_color.a = value,
                ColorComponent::Red => settings_state.editing_color.r = value,
                ColorComponent::Green => settings_state.editing_color.g = value,
                ColorComponent::Blue => settings_state.editing_color.b = value,
            }
            break;
        }
    }
}

/// Update color preview and slider handles when editing color changes
pub fn update_color_ui(
    settings_state: Res<SettingsState>,
    mut preview_query: Query<&mut BackgroundColor, With<ColorPreview>>,
    mut handle_query: Query<(&ColorSliderHandle, &mut Style)>,
    mut label_query: Query<(&ColorValueLabel, &mut Text)>,
    input_query: Query<&Children, With<ColorTextInput>>,
    mut text_query: Query<&mut Text, Without<ColorValueLabel>>,
) {
    if !settings_state.is_changed() {
        return;
    }

    let color = &settings_state.editing_color;

    // Update preview
    for mut bg in preview_query.iter_mut() {
        bg.0 = color.to_color();
    }

    // Update slider handles
    for (handle, mut style) in handle_query.iter_mut() {
        let value = match handle.component {
            ColorComponent::Alpha => color.a,
            ColorComponent::Red => color.r,
            ColorComponent::Green => color.g,
            ColorComponent::Blue => color.b,
        };
        // Position handle (track is 180px, handle is 14px)
        style.left = Val::Px(value * 166.0);
    }

    // Update value labels
    for (label, mut text) in label_query.iter_mut() {
        let value = match label.component {
            ColorComponent::Alpha => color.a,
            ColorComponent::Red => color.r,
            ColorComponent::Green => color.g,
            ColorComponent::Blue => color.b,
        };
        if let Some(section) = text.sections.first_mut() {
            section.value = format!("{:.2}", value);
        }
    }

    // Update text input if not currently being edited
    if settings_state.dragging_slider.is_some() {
        for children in input_query.iter() {
            for child in children.iter() {
                if let Ok(mut text) = text_query.get_mut(*child) {
                    if let Some(section) = text.sections.first_mut() {
                        section.value = color.to_hex();
                    }
                }
            }
        }
    }
}

/// Handle keyboard input for color text field
pub fn handle_color_text_input(
    mut settings_state: ResMut<SettingsState>,
    mut keyboard_events: EventReader<bevy::input::keyboard::KeyboardInput>,
    input_query: Query<&Children, With<ColorTextInput>>,
    mut text_query: Query<&mut Text>,
) {
    if !settings_state.show_modal {
        return;
    }

    let mut text_changed = false;

    // Handle keyboard input
    for event in keyboard_events.read() {
        if event.state != bevy::input::ButtonState::Pressed {
            continue;
        }

        match event.key_code {
            KeyCode::Backspace => {
                settings_state.color_input_text.pop();
                text_changed = true;
            }
            KeyCode::Enter => {
                if let Some(parsed) = ColorSetting::parse(&settings_state.color_input_text) {
                    settings_state.editing_color = parsed;
                }
            }
            _ => {
                // Try to get the character from logical_key
                if let bevy::input::keyboard::Key::Character(ref s) = event.logical_key {
                    for c in s.chars() {
                        if c.is_ascii() && !c.is_control() {
                            settings_state.color_input_text.push(c);
                            text_changed = true;
                        }
                    }
                }
            }
        }
    }

    // Update display
    if text_changed {
        for children in input_query.iter() {
            for child in children.iter() {
                if let Ok(mut text) = text_query.get_mut(*child) {
                    if let Some(section) = text.sections.first_mut() {
                        section.value = settings_state.color_input_text.clone();
                    }
                }
            }
        }

        // Try to parse and update preview (but don't commit yet)
        if let Some(parsed) = ColorSetting::parse(&settings_state.color_input_text) {
            settings_state.editing_color = parsed;
        }
    }
}

/// Apply settings on startup
pub fn apply_initial_settings(
    settings_state: Res<SettingsState>,
    mut clear_color: ResMut<ClearColor>,
) {
    clear_color.0 = settings_state.settings.background_color.to_color();
}