pinch-points 0.1.0

A fast, kid-friendly crab-routing game: route streams of crabs into your sandcastle before the tide comes in
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
//! The settings screen: the row ladder, its input, and its renderer.
//!
//! Split from the settings *model* next door, which the whole game reads
//! and which has no reason to depend on Bevy UI.

use super::{GameSettings, SeatInput, UI_SCALE_MAX, UI_SCALE_MIN};
use crate::app::Screen;
use crate::app::cycle::Cycle;
use crate::app::i18n::fill;
use crate::app::menu_ui::{self, Half};
use crate::app::palette;
use bevy::prelude::*;

/// One settings row. Both the input ladder and the renderer match on
/// this, so inserting a row can never desynchronize them.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Row {
    /// What drives P1, and P2: keyboard, a named controller, or whatever
    /// turns up.
    InputP1,
    InputP2,
    CommitKeys,
    /// Opens the per-key rebinding screen.
    KeyBindings,
    RepeatDelay,
    RepeatRate,
    Music,
    Sfx,
    Speed,
    VersusMode,
    Rumble,
    Deadzone,
    Palette,
    UiScale,
    ReducedMotion,
    /// How many finished rounds the shelf keeps.
    ReplayCap,
    Language,
    /// Whether start-up asks GitHub for a newer release.
    UpdateCheck,
    /// Forgets every cleared stage. Last, and behind a confirmation.
    ResetProgress,
}

impl Row {
    /// Display and navigation order, which is [`SECTIONS`] flattened. The
    /// rows used to run in the order they were added, which put the pad
    /// deadzone six rows below the keys it belongs with.
    pub const ALL: [Row; 19] = [
        // Controls
        Row::InputP1,
        Row::InputP2,
        Row::CommitKeys,
        Row::KeyBindings,
        Row::RepeatDelay,
        Row::RepeatRate,
        Row::Rumble,
        Row::Deadzone,
        // Sound
        Row::Music,
        Row::Sfx,
        // The round
        Row::Speed,
        Row::VersusMode,
        Row::ReplayCap,
        // Presentation
        Row::Palette,
        Row::UiScale,
        Row::ReducedMotion,
        Row::Language,
        // The game itself
        Row::UpdateCheck,
        // And the one that throws something away
        Row::ResetProgress,
    ];
}

/// A heading on the settings card. Every dial in one ladder (there are
/// `Row::ALL.len()` of them) is a list to be read from the top every time;
/// a handful of named groups is a place to look things up.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Group {
    Controls,
    Sound,
    Round,
    Look,
    /// The game as a program: the one setting about the copy on disk
    /// rather than about playing it.
    Game,
    Danger,
}

impl Group {
    fn label(self, tr: &crate::app::i18n::Tr) -> &'static str {
        match self {
            Group::Controls => tr.set_group_controls,
            Group::Sound => tr.set_group_sound,
            Group::Round => tr.set_group_round,
            Group::Look => tr.set_group_look,
            Group::Game => tr.set_group_game,
            Group::Danger => tr.set_group_danger,
        }
    }
}

/// The card, left column then right. Flattening this must give
/// [`Row::ALL`]; a test says so.
pub const SECTIONS: [&[(Group, &[Row])]; 2] = [
    &[
        (
            Group::Controls,
            &[
                Row::InputP1,
                Row::InputP2,
                Row::CommitKeys,
                Row::KeyBindings,
                Row::RepeatDelay,
                Row::RepeatRate,
                Row::Rumble,
                Row::Deadzone,
            ],
        ),
        (Group::Sound, &[Row::Music, Row::Sfx]),
    ],
    &[
        (Group::Round, &[Row::Speed, Row::VersusMode, Row::ReplayCap]),
        (
            Group::Look,
            &[
                Row::Palette,
                Row::UiScale,
                Row::ReducedMotion,
                Row::Language,
            ],
        ),
        (Group::Game, &[Row::UpdateCheck]),
        (Group::Danger, &[Row::ResetProgress]),
    ],
];

const ROWS: usize = Row::ALL.len();

/// How far through the reset the player is. Wiping a campaign on one
/// keypress, on a row the cursor passes over on its way to the language, is
/// not a thing to do to somebody.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ResetPrompt {
    /// Offering to reset.
    #[default]
    Idle,
    /// Enter pressed once; a second press does it.
    Armed,
    /// Done, and saying so until the cursor moves on.
    Done,
}

/// What Enter on the reset row leads to. Pure, so the two-press rule is
/// testable without a window, or a test writing over the real save file.
fn next_reset_prompt(current: ResetPrompt) -> ResetPrompt {
    match current {
        ResetPrompt::Armed => ResetPrompt::Done,
        ResetPrompt::Idle | ResetPrompt::Done => ResetPrompt::Armed,
    }
}

/// Which row the settings cursor is on.
#[derive(Resource, Default)]
pub struct SettingsMenu {
    pub selected: usize,
    /// State of the reset row; cleared whenever the cursor leaves it.
    pub reset: ResetPrompt,
}

#[derive(Component)]
pub struct SettingsRow(pub usize);

/// One half of one row.
#[derive(Component)]
pub struct SettingsCell(pub usize, pub Half);

#[derive(Component)]
pub struct SettingsUi;

/// Label gutter and value gutter, the value including the `< >` the
/// selected row wears. Two columns of these have to fit in
/// [`DESIGN_W`](crate::app::settings::DESIGN_W), which is what keeps them
/// honest - `the_card_fits_the_window_it_was_drawn_for` holds them to it.
///
/// Both cells clip what overruns rather than wrapping it, so a row that
/// does not fit loses its tail with no warning anywhere -
/// `every_row_fits_its_cell_in_every_language` is what says so instead.
/// That check measures pixels rather than characters, because two faces
/// draw these rows: DejaVu Sans Mono at 0.602 em and the Japanese subset
/// at a full one.
const LABEL_W: f32 = 252.0;
const VALUE_W: f32 = 322.0;
const ROW_FONT: f32 = 18.0;

/// The language row's flag chip: 3:2, the ratio the art is drawn at, and
/// short enough to sit inside an 18px line rather than growing the row.
const FLAG_W: f32 = 21.0;
const FLAG_H: f32 = 14.0;
const FLAG_GAP: f32 = 7.0;

/// The flag chip on the language row, so the dial can repaint it without
/// finding it by position among the cells.
#[derive(Component)]
pub struct LanguageFlag;

pub fn enter_settings(
    mut commands: Commands,
    settings: Res<GameSettings>,
    art: Res<crate::app::art::Art>,
    mut menu: ResMut<SettingsMenu>,
) {
    let tr = settings.tr();
    menu.selected = 0;
    menu.reset = ResetPrompt::Idle;
    let mut index = 0usize;
    commands
        .spawn((
            SettingsUi,
            // Centred between the bars, like every other list screen.
            Node {
                position_type: PositionType::Absolute,
                top: Val::Px(52.0),
                bottom: Val::Px(52.0),
                left: Val::Px(0.0),
                right: Val::Px(0.0),
                flex_direction: FlexDirection::Column,
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                row_gap: Val::Px(10.0),
                ..default()
            },
        ))
        .with_children(|wrap| {
            wrap.spawn((
                Node {
                    column_gap: Val::Px(28.0),
                    padding: UiRect::axes(Val::Px(22.0), Val::Px(16.0)),
                    border: UiRect::all(Val::Px(1.0)),
                    border_radius: BorderRadius::all(Val::Px(16.0)),
                    ..default()
                },
                BackgroundColor(palette::CARD_FILL),
                BorderColor::all(palette::CARD_EDGE),
            ))
            .with_children(|card| {
                for column in SECTIONS {
                    card.spawn(Node {
                        flex_direction: FlexDirection::Column,
                        row_gap: Val::Px(2.0),
                        ..default()
                    })
                    .with_children(|side| {
                        for (group, rows) in column {
                            side.spawn((
                                Text::new(group.label(tr)),
                                TextFont {
                                    font_size: FontSize::Px(14.0),
                                    ..default()
                                },
                                TextColor(palette::GOLD.with_alpha(0.55)),
                                Node {
                                    margin: UiRect::top(Val::Px(if index == 0 {
                                        0.0
                                    } else {
                                        10.0
                                    }))
                                    .with_bottom(Val::Px(3.0))
                                    .with_left(Val::Px(10.0)),
                                    ..default()
                                },
                            ));
                            for row in *rows {
                                let flag =
                                    (*row == Row::Language).then(|| art.flag(settings.language));
                                spawn_setting_row(side, index, flag);
                                index += 1;
                            }
                        }
                    });
                }
            });
            // Read-only controller mapping reference, under the card.
            for line in [tr.pad_help1, tr.pad_help2] {
                wrap.spawn((
                    Text::new(line),
                    TextFont {
                        font_size: FontSize::Px(15.0),
                        ..default()
                    },
                    TextLayout::no_wrap(),
                    TextColor(palette::PARCHMENT.with_alpha(0.40)),
                ));
            }
        });
}

/// One dial: its name in a fixed gutter and its value in another, so every
/// value on the card starts at the same x and the column can be read down.
///
/// The language row is the one exception: its value carries a flag chip in
/// front of the name, which pushes that one value right by the width of the
/// chip. A blank slot on every other row would keep the column
/// perfectly straight and cost every one of them the same width for
/// nothing, so the indent stands.
fn spawn_setting_row(side: &mut ChildSpawnerCommands, index: usize, flag: Option<Handle<Image>>) {
    side.spawn((
        SettingsRow(index),
        Node {
            align_items: AlignItems::Center,
            padding: UiRect::axes(Val::Px(10.0), Val::Px(3.0)),
            border_radius: BorderRadius::all(Val::Px(7.0)),
            ..default()
        },
        BackgroundColor(Color::NONE),
    ))
    .with_children(|line| {
        for (half, width) in [(Half::Label, LABEL_W), (Half::Value, VALUE_W)] {
            line.spawn(Node {
                width: Val::Px(width),
                flex_shrink: 0.0,
                align_items: AlignItems::Center,
                column_gap: Val::Px(FLAG_GAP),
                overflow: Overflow::clip_x(),
                ..default()
            })
            .with_children(|cell| {
                if let (Half::Value, Some(flag)) = (half, flag.clone()) {
                    cell.spawn((
                        LanguageFlag,
                        ImageNode::new(flag),
                        Node {
                            width: Val::Px(FLAG_W),
                            height: Val::Px(FLAG_H),
                            flex_shrink: 0.0,
                            ..default()
                        },
                    ));
                }
                cell.spawn((
                    SettingsCell(index, half),
                    Text::new(""),
                    TextFont {
                        font_size: FontSize::Px(ROW_FONT),
                        ..default()
                    },
                    TextLayout::no_wrap(),
                    TextColor(palette::IDLE_ROW),
                ));
            });
        }
    });
}

pub fn settings_input(
    keys: Res<ButtonInput<KeyCode>>,
    mut menu: ResMut<SettingsMenu>,
    mut settings: ResMut<GameSettings>,
    mut progress: ResMut<crate::app::progress::Progress>,
    mut next_screen: ResMut<NextState<Screen>>,
) {
    // Enter works the doors, the key-binding screen and the reset; on every
    // dial it means the same as Escape: done here.
    if keys.just_pressed(KeyCode::Enter) {
        match Row::ALL[menu.selected] {
            Row::KeyBindings => next_screen.set(Screen::Controls),
            Row::ResetProgress => {
                menu.reset = next_reset_prompt(menu.reset);
                if menu.reset == ResetPrompt::Done {
                    progress.clear_all();
                    crate::app::progress::save(&progress);
                }
            }
            Row::InputP1
            | Row::InputP2
            | Row::CommitKeys
            | Row::RepeatDelay
            | Row::RepeatRate
            | Row::Music
            | Row::Sfx
            | Row::Speed
            | Row::VersusMode
            | Row::Rumble
            | Row::Deadzone
            | Row::Palette
            | Row::UiScale
            | Row::ReducedMotion
            | Row::ReplayCap
            | Row::Language
            | Row::UpdateCheck => next_screen.set(Screen::Menu),
        }
        return;
    }
    if keys.just_pressed(KeyCode::Escape) {
        next_screen.set(Screen::Menu);
        return;
    }
    let was = menu.selected;
    menu.selected = menu_ui::nav(&keys, menu.selected, ROWS);
    if menu.selected != was {
        // Stepping off the row takes the offer back with you.
        menu.reset = ResetPrompt::Idle;
    }
    let Some(right) = menu_ui::left_right(&keys) else {
        return;
    };
    let step = if right { 1.0 } else { -1.0 };
    match Row::ALL[menu.selected] {
        Row::InputP1 | Row::InputP2 => {
            let seat = usize::from(Row::ALL[menu.selected] == Row::InputP2);
            settings.seat_input[seat] = settings.seat_input[seat].cycled(right);
        }
        Row::CommitKeys => settings.ijkl_commits = !settings.ijkl_commits,
        Row::RepeatDelay => {
            settings.repeat_delay = f32::clamp(settings.repeat_delay + step * 0.05, 0.1, 0.5);
        }
        Row::RepeatRate => {
            settings.repeat_interval =
                f32::clamp(settings.repeat_interval + step * 0.02, 0.03, 0.2);
        }
        Row::Music => {
            let volume = i32::from(settings.music_volume) + if right { 10 } else { -10 };
            settings.music_volume = volume.clamp(0, 100) as u8;
        }
        Row::Sfx => {
            let volume = i32::from(settings.sfx_volume) + if right { 10 } else { -10 };
            settings.sfx_volume = volume.clamp(0, 100) as u8;
        }
        Row::VersusMode => settings.team_mode = settings.team_mode.cycled(right),
        Row::Speed => {
            settings.puzzle_speed = match (settings.puzzle_speed, right) {
                (100, false) | (50, true) => 75,
                (75, false) => 50,
                (75, true) => 100,
                (other, _) => other,
            };
        }
        Row::Rumble => settings.rumble = !settings.rumble,
        Row::Deadzone => {
            let dz = i32::from(settings.pad_deadzone) + if right { 10 } else { -10 };
            settings.pad_deadzone = dz.clamp(20, 80) as u8;
        }
        Row::Palette => settings.colorblind = !settings.colorblind,
        Row::UiScale => {
            let scale = i32::from(settings.ui_scale) + if right { 10 } else { -10 };
            settings.ui_scale = scale.clamp(i32::from(UI_SCALE_MIN), i32::from(UI_SCALE_MAX)) as u8;
        }
        Row::ReducedMotion => settings.reduced_motion = !settings.reduced_motion,
        Row::ReplayCap => {
            let cap = i32::from(settings.replay_cap) + if right { 5 } else { -5 };
            settings.replay_cap = cap.clamp(
                i32::from(crate::app::settings::REPLAY_CAP_MIN),
                i32::from(crate::app::settings::REPLAY_CAP_MAX),
            ) as u8;
        }
        // Doors: worked with Enter, not adjusted with A/D.
        Row::KeyBindings | Row::ResetProgress => {}
        Row::Language => settings.language = settings.language.cycled(right),
        Row::UpdateCheck => settings.check_updates = !settings.check_updates,
    }
}

/// What one row reads as: its label and the value beside it, or the whole
/// line for the rows that are doors rather than dials.
///
/// Whether the cursor puts `< >` around this row's value. Doors do not
/// step through anything, so they wear none - and the widest a value ever
/// draws depends on the answer, which is why this is one rule rather than
/// one in the screen and another in the test that measures it.
fn has_arrows(row: Row) -> bool {
    !matches!(row, Row::KeyBindings | Row::ResetProgress)
}

/// Pure, so the wording of every setting can be checked without a window -
/// this is the screen a player spends the most time reading.
/// A row's name and its current value, undecorated. The screen adds the
/// `< >` arrows to the row the cursor is on, so the arrows read as "these
/// keys, on this row" rather than as part of every value.
pub(super) fn row_text(
    tr: &crate::app::i18n::Tr,
    settings: &GameSettings,
    row: Row,
    reset: ResetPrompt,
) -> (String, String) {
    let on_off = |on: bool| if on { tr.val_on } else { tr.val_off }.to_string();
    let (label, value) = match row {
        Row::InputP1 | Row::InputP2 => {
            let seat = usize::from(row == Row::InputP2);
            return (
                fill(tr.set_seat_input, &[("n", &(seat + 1).to_string())]),
                match settings.seat_input[seat] {
                    SeatInput::Auto => tr.val_input_auto.to_string(),
                    SeatInput::Keys => tr.val_input_keys.to_string(),
                    SeatInput::Pad(n) => fill(tr.val_input_pad, &[("n", &(n + 1).to_string())]),
                },
            );
        }
        Row::CommitKeys => (
            tr.set_commit_keys,
            if settings.ijkl_commits {
                tr.val_ijkl.to_string()
            } else {
                tr.val_arrows.to_string()
            },
        ),
        Row::RepeatDelay => (
            tr.set_repeat_delay,
            format!("{:.2}s", settings.repeat_delay),
        ),
        Row::RepeatRate => (
            tr.set_repeat_rate,
            format!("{:.2}s", settings.repeat_interval),
        ),
        Row::Music => (tr.set_music, format!("{}%", settings.music_volume)),
        Row::Sfx => (tr.set_sfx, format!("{}%", settings.sfx_volume)),
        Row::Speed => (tr.set_speed, format!("{}%", settings.puzzle_speed)),
        Row::VersusMode => (
            tr.set_versus_mode,
            tr.team_modes[settings.team_mode.index()].to_string(),
        ),
        Row::Rumble => (tr.set_rumble, on_off(settings.rumble)),
        Row::Deadzone => (tr.set_deadzone, format!("{}%", settings.pad_deadzone)),
        Row::Palette => (
            tr.set_palette,
            if settings.colorblind {
                tr.val_palette_safe
            } else {
                tr.val_palette_classic
            }
            .to_string(),
        ),
        Row::UiScale => (tr.set_ui_scale, format!("{}%", settings.ui_scale)),
        Row::ReducedMotion => (tr.set_reduced_motion, on_off(settings.reduced_motion)),
        Row::ReplayCap => (tr.set_replay_cap, settings.replay_cap.to_string()),
        Row::Language => (tr.set_language, settings.language.native_name().to_string()),
        Row::UpdateCheck => (tr.set_update_check, on_off(settings.check_updates)),
        // Doors, not dials: no `< value >` decoration.
        Row::KeyBindings => {
            return (tr.set_key_bindings.to_string(), tr.val_open.to_string());
        }
        Row::ResetProgress => {
            let state = match reset {
                ResetPrompt::Idle => tr.val_reset,
                ResetPrompt::Armed => tr.val_reset_confirm,
                ResetPrompt::Done => tr.val_reset_done,
            };
            return (tr.set_reset_progress.to_string(), state.to_string());
        }
    };
    (label.to_string(), value)
}

pub fn update_settings_ui(
    settings: Res<GameSettings>,
    menu: Res<SettingsMenu>,
    art: Res<crate::app::art::Art>,
    mut cells: Query<(&SettingsCell, &mut Text, &mut TextColor)>,
    mut rows: Query<(&SettingsRow, &mut BackgroundColor)>,
    mut flags: Query<&mut ImageNode, With<LanguageFlag>>,
) {
    let tr = settings.tr();
    // The chip follows the dial. Assigning only on a change keeps this off
    // Bevy's change detection every frame, which the image would otherwise
    // trip sixty times a second for a picture that almost never moves.
    let wanted = art.flag(settings.language);
    for mut flag in &mut flags {
        if flag.image != wanted {
            flag.image = wanted.clone();
        }
    }
    for (cell, mut text, mut color) in &mut cells {
        let row = Row::ALL[cell.0];
        let picked = cell.0 == menu.selected;
        let (label, value) = row_text(tr, &settings, row, menu.reset);
        let line = match cell.1 {
            Half::Label => label,
            Half::Value if picked && has_arrows(row) => format!("< {value} >"),
            Half::Value => value,
        };
        let target = match (cell.1, picked) {
            (Half::Label, true) => Color::WHITE,
            (Half::Label, false) => palette::PARCHMENT.with_alpha(0.62),
            (Half::Value, true) => palette::GOLD,
            (Half::Value, false) => palette::PARCHMENT.with_alpha(0.92),
        };
        menu_ui::set_text(&mut text, &line);
        menu_ui::set_color(&mut color, target);
    }
    for (row, mut fill) in &mut rows {
        let ground = if row.0 == menu.selected {
            palette::GOLD.with_alpha(0.16)
        } else {
            Color::NONE
        };
        if fill.0 != ground {
            fill.0 = ground;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::i18n::EN;

    /// Every row says what it is and what it is set to, in every language.
    /// A row added without wording would otherwise show an empty dial.
    /// The card is built by walking [`SECTIONS`] with a running index, and
    /// every row's label is looked up by that index in [`Row::ALL`]. If the
    /// two ever disagree the screen shows one dial's name over another's
    /// value, silently. The comment on `Row::ALL` says a test keeps them in
    /// step; this is it.
    #[test]
    fn the_sections_are_the_row_order() {
        let flat: Vec<Row> = SECTIONS
            .iter()
            .flat_map(|column| column.iter())
            .flat_map(|(_, rows)| rows.iter().copied())
            .collect();
        assert_eq!(flat, Row::ALL.to_vec());
    }

    #[test]
    fn every_settings_row_reads_as_a_setting() {
        for lang in crate::app::i18n::ALL_LANGS {
            let settings = GameSettings {
                language: lang,
                ..GameSettings::default()
            };
            for row in Row::ALL {
                let (label, value) = row_text(settings.tr(), &settings, row, ResetPrompt::Idle);
                assert!(!label.trim().is_empty(), "{row:?} in {lang:?} has no name");
                assert!(!value.trim().is_empty(), "{row:?} in {lang:?} has no value");
            }
        }
    }

    /// Every row fits the two cells that hold it, in every language, on
    /// every stop of every dial.
    ///
    /// Both cells clip rather than wrap, so a row that overruns loses its
    /// tail with no warning anywhere - the widest of them lost the closing
    /// bracket off the paired team mode in five languages, and had done
    /// since the mode was added. The header on [`LABEL_W`] used to say the
    /// budget in characters and there was nothing to check it against;
    /// characters are the wrong unit besides, since Japanese is drawn in a
    /// face with a different advance.
    ///
    /// Every dial is walked to its widest stop rather than read at its
    /// default, because the default is not what a player leaves it on.
    #[test]
    fn every_row_fits_its_cell_in_every_language() {
        use crate::app::i18n::metrics::text_px;
        for lang in crate::app::i18n::ALL_LANGS {
            // The widest stop on each dial: three digits where the value is
            // a percentage, the longest of the named options, and the pad
            // seat, whose value counts a controller in.
            let settings = GameSettings {
                language: lang,
                seat_input: [SeatInput::Pad(9); crate::app::binds::BOUND_SEATS],
                music_volume: 100,
                sfx_volume: 100,
                puzzle_speed: 100,
                pad_deadzone: 100,
                ui_scale: 150,
                replay_cap: 99,
                colorblind: true,
                ijkl_commits: true,
                rumble: true,
                reduced_motion: true,
                check_updates: true,
                repeat_delay: 0.88,
                repeat_interval: 0.88,
                ..GameSettings::default()
            };
            let tr = settings.tr();
            for row in Row::ALL {
                for reset in [ResetPrompt::Idle, ResetPrompt::Armed, ResetPrompt::Done] {
                    for team_mode in crate::app::teams::TeamMode::ALL {
                        let settings = GameSettings {
                            team_mode,
                            ..settings.clone()
                        };
                        let (label, value) = row_text(tr, &settings, row, reset);
                        let label_w = text_px(&label, ROW_FONT);
                        assert!(
                            label_w <= LABEL_W,
                            "{row:?} in {lang:?}: label {label:?} is {label_w:.1}px, \
                             and the cell holds {LABEL_W}"
                        );
                        // The cursor's row wears the arrows, so the widest
                        // this value ever draws is with them on - on the
                        // rows that have them. The language row also gives
                        // up the head of its cell to the flag chip.
                        let decorated = if has_arrows(row) {
                            format!("< {value} >")
                        } else {
                            value
                        };
                        let chip = if row == Row::Language {
                            FLAG_W + FLAG_GAP
                        } else {
                            0.0
                        };
                        let value_w = text_px(&decorated, ROW_FONT) + chip;
                        assert!(
                            value_w <= VALUE_W,
                            "{row:?} in {lang:?}: value {decorated:?} is {value_w:.1}px, \
                             and the cell holds {VALUE_W}"
                        );
                    }
                }
            }
        }
    }

    /// The cells are the card, and the card has to fit the window the
    /// interface was drawn for. This is the other half of the budget: a
    /// row too long for its cell clips, and a cell too wide for the card
    /// runs the whole thing off the edge of a 1280-wide screen. Widening
    /// one to fix the first is what would cause the second.
    #[test]
    fn the_card_fits_the_window_it_was_drawn_for() {
        // Two columns of rows, each row a label and a value inside its own
        // padding, the columns spaced apart, all of it inside the card's
        // padding and border.
        let column = LABEL_W + VALUE_W + 2.0 * 10.0;
        let card = 2.0 * column + 28.0 + 2.0 * 22.0 + 2.0 * 1.0;
        assert!(
            card <= crate::app::settings::DESIGN_W,
            "the settings card is {card}px of the {}px it is allowed",
            crate::app::settings::DESIGN_W
        );
    }

    /// The rows that carry a number say the number, and it moves with the
    /// setting rather than being baked into the label.
    #[test]
    fn a_dial_shows_the_value_it_is_set_to() {
        let mut settings = GameSettings {
            music_volume: 42,
            ..GameSettings::default()
        };
        let at = |s: &GameSettings, row| row_text(&EN, s, row, ResetPrompt::Idle).1;
        assert!(at(&settings, Row::Music).contains("42%"));
        settings.ui_scale = 130;
        assert!(at(&settings, Row::UiScale).contains("130%"));
        settings.colorblind = true;
        assert!(at(&settings, Row::Palette).contains(EN.val_palette_safe));
        settings.team_mode = crate::app::teams::TeamMode::Trios;
        assert!(at(&settings, Row::VersusMode).contains(EN.team_modes[2]));
    }

    /// Wiping a campaign takes two presses, and the row says which one it is
    /// waiting for. One press must never be enough: the cursor passes over
    /// this row on its way down the list.
    #[test]
    fn resetting_progress_asks_twice() {
        assert_eq!(next_reset_prompt(ResetPrompt::Idle), ResetPrompt::Armed);
        assert_eq!(next_reset_prompt(ResetPrompt::Armed), ResetPrompt::Done);
        // Having done it once, the row offers again rather than firing.
        assert_eq!(next_reset_prompt(ResetPrompt::Done), ResetPrompt::Armed);

        let settings = GameSettings::default();
        let line = |reset| row_text(&EN, &settings, Row::ResetProgress, reset).1;
        assert!(line(ResetPrompt::Idle).contains(EN.val_reset));
        assert!(line(ResetPrompt::Armed).contains(EN.val_reset_confirm));
        assert!(line(ResetPrompt::Done).contains(EN.val_reset_done));
        // The three states have to read differently, or the confirmation is
        // invisible and the row is a one-press wipe after all.
        for lang in crate::app::i18n::ALL_LANGS {
            let tr = lang.tr();
            let settings = GameSettings {
                language: lang,
                ..GameSettings::default()
            };
            let say = |reset| row_text(tr, &settings, Row::ResetProgress, reset).1;
            assert_ne!(say(ResetPrompt::Idle), say(ResetPrompt::Armed), "{lang:?}");
            assert_ne!(say(ResetPrompt::Armed), say(ResetPrompt::Done), "{lang:?}");
        }
    }

    /// Enter on the reset row arms it and *stays* on the screen. Enter on
    /// every other row means "done here", so this row has to hold its own
    /// against that.
    ///
    /// Exactly one `update()`, and that is load-bearing: this app has no
    /// `InputPlugin`, so nothing clears `just_pressed` between frames and a
    /// second update would press Enter again, confirm the reset, and write
    /// over the real `progress.txt` in the tester's data directory. The
    /// seeded mark below is the tripwire if anyone adds one.
    #[test]
    fn arming_the_reset_does_not_leave_the_screen() {
        use crate::app::CampaignKind;
        use crate::app::progress::Progress;

        let mut progress = Progress::default();
        progress.mark(CampaignKind::TidePool, "Welcome Ashore");

        let mut app = App::new();
        app.add_plugins(bevy::state::app::StatesPlugin);
        app.init_state::<Screen>();
        app.init_resource::<ButtonInput<KeyCode>>();
        app.init_resource::<SettingsMenu>();
        app.insert_resource(progress);
        app.insert_resource(GameSettings::default());
        app.add_systems(Update, settings_input);
        app.insert_resource(State::new(Screen::Settings));

        let reset_row = Row::ALL.iter().position(|&r| r == Row::ResetProgress);
        app.world_mut().resource_mut::<SettingsMenu>().selected =
            reset_row.expect("the row is in the ladder");

        app.world_mut()
            .resource_mut::<ButtonInput<KeyCode>>()
            .press(KeyCode::Enter);
        app.update();

        assert_eq!(
            app.world().resource::<SettingsMenu>().reset,
            ResetPrompt::Armed,
            "the first press arms it"
        );
        assert_eq!(
            *app.world().resource::<State<Screen>>().get(),
            Screen::Settings,
            "and does not fall through to leaving the screen"
        );
        assert!(
            app.world()
                .resource::<Progress>()
                .is_cleared(CampaignKind::TidePool, "Welcome Ashore"),
            "arming alone must not clear anything"
        );
    }

    /// The reset clears both lists, since they share one record.
    #[test]
    fn resetting_progress_clears_both_campaigns() {
        use crate::app::CampaignKind;
        use crate::app::progress::Progress;

        let mut progress = Progress::default();
        progress.mark(CampaignKind::TidePool, "Welcome Ashore");
        progress.mark(CampaignKind::BeachDay, "First Flood");
        progress.clear_all();
        assert!(!progress.is_cleared(CampaignKind::TidePool, "Welcome Ashore"));
        assert!(!progress.is_cleared(CampaignKind::BeachDay, "First Flood"));
        assert!(progress.to_text().is_empty(), "and nothing to write back");
    }
}