sgl-2d 0.4.2

Caller-driven 2D rendering, assets, text, lighting, and immediate UI for SGL games
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
//! Press-activated controls: buttons, icon and toggle buttons, radios,
//! checkboxes and collapsing headers.

use super::{UiFrame, UiKey, widget_id};
use crate::canvas::draw::Rect;
use crate::canvas::text::{HAlign, TextStyle, VAlign};

/// Compact text-glyph button states. The hit rectangle is independent of
/// glyph size. Disabled controls remain focusable so their tooltip explains why.
#[derive(Debug, Clone, Copy, Default)]
pub struct IconButton {
    pub selected: bool,
    pub disabled: bool,
}

impl UiFrame<'_> {
    /// A push button. Returns `true` on **press-down** over the button
    /// (PR-9 semantics — the action fires the frame the mouse goes down).
    pub fn button(&mut self, name: &str, rect: Rect, label: &str, px: f32) -> bool {
        let (fired, held, hover) = self.button_behavior(name, &rect);
        let bg = if held {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(rect, bg);
        let fg = if held {
            self.ui.theme.text_pressed
        } else if hover {
            self.ui.theme.text_hovered
        } else {
            self.ui.theme.text
        };
        let style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
        self.label(rect, label, &style, HAlign::Center, VAlign::Center);
        fired
    }

    /// Draw a compact glyph button using the shared button palette. `name`
    /// is its stable focus identity; attach an explanatory [`Self::tooltip_for`]
    /// with the same name. Selected controls have an inset geometric marker.
    /// Disabled controls never act, including Enter/Space, but retain focus.
    pub fn icon_button(
        &mut self,
        name: &str,
        rect: Rect,
        glyph: &str,
        px: f32,
        options: IconButton,
    ) -> bool {
        let (fired, held, hover) = self.button_behavior(name, &rect);
        let bg = if options.disabled {
            self.ui.theme.disabled
        } else if held || options.selected {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(rect, bg);
        let fg = if options.disabled {
            self.ui.theme.disabled_text
        } else {
            self.ui.theme.text
        };
        self.label(
            rect,
            glyph,
            &TextStyle::new(px, fg),
            HAlign::Center,
            VAlign::Center,
        );
        if options.selected {
            self.border(
                Rect::new(
                    rect.min.x + 3.0,
                    rect.min.y + 3.0,
                    (rect.size().x - 6.0).max(0.0),
                    (rect.size().y - 6.0).max(0.0),
                ),
                1.0,
                fg,
            );
        }
        if self.ui.focus == Some(widget_id(name)) {
            self.border(rect, 1.0, self.ui.theme.accent);
        }
        fired && !options.disabled
    }

    /// A toggle button (Godot `Button` with `toggle_mode`): draws the
    /// pressed style + accent border while `selected`. Returns `true` on
    /// press-down (the caller flips/sets its state).
    pub fn toggle_button(
        &mut self,
        name: &str,
        rect: Rect,
        label: &str,
        px: f32,
        selected: bool,
    ) -> bool {
        let (fired, held, hover) = self.button_behavior(name, &rect);
        if selected {
            self.border(rect, 2.0, self.ui.theme.accent);
        }
        let bg = if selected || held {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(rect, bg);
        let fg = if selected || held {
            self.ui.theme.text_pressed
        } else {
            self.ui.theme.text
        };
        let style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
        self.label(rect, label, &style, HAlign::Center, VAlign::Center);
        fired
    }

    /// A radio member (PR-9 fire/water selection): a toggle button that
    /// stores `value` into `current` when pressed. Returns `true` when it
    /// fired this frame.
    pub fn radio<T: Copy + PartialEq>(
        &mut self,
        name: &str,
        rect: Rect,
        label: &str,
        px: f32,
        value: T,
        current: &mut T,
    ) -> bool {
        let fired = self.toggle_button(name, rect, label, px, *current == value);
        if fired {
            *current = value;
        }
        fired
    }

    /// Shared button interaction: press-on-down fire + held/hover visuals.
    pub(super) fn button_behavior(&mut self, name: &str, rect: &Rect) -> (bool, bool, bool) {
        let id = widget_id(name);
        let hover = self.hit(rect);
        let eligible = self.register_focus(id, rect);
        let keyboard = eligible
            && self.ui.focus == Some(id)
            && !self.input.mouse_pressed
            && !self.key_used
            && self
                .input
                .keys
                .iter()
                .any(|key| matches!(key, UiKey::Enter | UiKey::Space));
        let fired = (hover && self.input.mouse_pressed) || keyboard;
        if keyboard {
            self.key_used = true;
            self.ui.keyboard_captured = true;
        }
        if fired {
            self.ui.focus = Some(id);
            self.edit_clicked = true;
        }
        if eligible && self.ui.focus == Some(id) {
            self.border(*rect, 1.0, self.ui.theme.accent);
        }
        if fired {
            self.ui.active = Some(id);
        }
        let held = self.ui.active == Some(id) && self.input.mouse_down;
        (fired, held, hover)
    }

    /// A checkbox (Godot `CheckBox`): a square at the left edge of `rect`
    /// (its side is the rect's height) with `label` beside it. The whole
    /// `rect` is the hit area. Flips `checked` on **press-down** and returns
    /// `true` that frame.
    pub fn checkbox(
        &mut self,
        name: &str,
        rect: Rect,
        label: &str,
        px: f32,
        checked: &mut bool,
    ) -> bool {
        let (fired, held, hover) = self.button_behavior(name, &rect);
        if fired {
            *checked = !*checked;
        }
        let side = rect.size().y;
        let square = Rect::new(rect.min.x, rect.min.y, side, side);
        let bg = if held {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(square, bg);
        self.border(
            square,
            1.0,
            if *checked {
                self.ui.theme.accent
            } else {
                self.ui.theme.text
            },
        );
        if *checked {
            // The mark: an inset square in the accent color.
            let inset = (side * 0.25).max(2.0);
            self.rect(
                Rect::new(
                    square.min.x + inset,
                    square.min.y + inset,
                    (side - 2.0 * inset).max(1.0),
                    (side - 2.0 * inset).max(1.0),
                ),
                self.ui.theme.accent,
            );
        }
        let fg = if hover {
            self.ui.theme.text_hovered
        } else {
            self.ui.theme.text
        };
        let style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
        let pad = 6.0;
        let text_rect = Rect::new(
            square.max.x + pad,
            rect.min.y,
            (rect.size().x - side - pad).max(0.0),
            side,
        );
        self.label(text_rect, label, &style, HAlign::Left, VAlign::Center);
        fired
    }

    /// A collapsing section header: a full-width button showing a `v` /
    /// `>` marker and `label`. Flips `open` on **press-down** and returns
    /// `true` that frame; the caller lays out the section body itself
    /// while `open` is `true` (this widget draws only the header row).
    pub fn collapsing_header(
        &mut self,
        name: &str,
        rect: Rect,
        label: &str,
        px: f32,
        open: &mut bool,
    ) -> bool {
        let (fired, held, hover) = self.button_behavior(name, &rect);
        if fired {
            *open = !*open;
        }
        let bg = if held {
            self.ui.theme.pressed
        } else if hover {
            self.ui.theme.hovered
        } else {
            self.ui.theme.control
        };
        self.rect(rect, bg);
        let fg = if held {
            self.ui.theme.text_pressed
        } else if hover {
            self.ui.theme.text_hovered
        } else {
            self.ui.theme.text
        };
        let style = TextStyle::new(px, fg).with_outline(self.ui.theme.text_outline);
        let pad = 8.0;
        let marker_w = rect.size().y;
        let marker = Rect::new(rect.min.x + pad, rect.min.y, marker_w, rect.size().y);
        self.label(
            marker,
            if *open { "v" } else { ">" },
            &style,
            HAlign::Left,
            VAlign::Center,
        );
        let text_rect = Rect::new(
            marker.max.x,
            rect.min.y,
            (rect.size().x - marker_w - 2.0 * pad).max(0.0),
            rect.size().y,
        );
        self.label(text_rect, label, &style, HAlign::Left, VAlign::Center);
        fired
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canvas::draw::DrawList;
    use crate::ui::UiInput;
    use crate::ui::test_ui::{BTN, ROW, fixture, hover_at, press_at, release_at};
    use sgl_core::math::Vec2;
    use wasm_bindgen_test::wasm_bindgen_test;

    #[wasm_bindgen_test(unsupported = test)]
    fn disabled_icon_keeps_focus_explanation_without_activation() {
        let (mut ui, mut text, _assets) = fixture();
        let rect = Rect::new(10.0, 10.0, 24.0, 24.0);
        for input in [
            UiInput::default(),
            UiInput {
                keys: vec![UiKey::Tab],
                ..UiInput::default()
            },
            UiInput {
                keys: vec![UiKey::Enter, UiKey::Space],
                ..UiInput::default()
            },
            UiInput {
                mouse_pos: Vec2::new(12.0, 12.0),
                mouse_pressed: true,
                mouse_down: true,
                ..UiInput::default()
            },
        ] {
            let mut list = DrawList::new();
            let mut f = ui.begin(&mut text, &mut list, input);
            assert!(!f.icon_button(
                "locked",
                rect,
                "X",
                12.0,
                IconButton {
                    disabled: true,
                    selected: true
                }
            ));
            let focused = f.is_focused("locked");
            let before = f.list.screen.len();
            f.tooltip_for(
                "locked",
                rect,
                Rect::new(0.0, 0.0, 100.0, 80.0),
                "Unavailable",
                12.0,
            );
            if focused {
                assert!(f.list.screen.len() > before);
            }
            f.end();
        }
        let mut list = DrawList::new();
        let mut f = ui.begin(&mut text, &mut list, UiInput::default());
        f.set_clip(Some(Rect::new(80.0, 0.0, 10.0, 10.0)));
        f.tooltip_for(
            "locked",
            rect,
            Rect::new(0.0, 0.0, 100.0, 80.0),
            "Unavailable",
            12.0,
        );
        assert!(
            f.list.screen.is_empty(),
            "clipped focus cannot show a tooltip"
        );
        f.set_clip(None);
        f.tooltip_for(
            "locked",
            rect,
            Rect::new(80.0, 0.0, 10.0, 10.0),
            "Unavailable",
            12.0,
        );
        assert!(
            f.list.screen.is_empty(),
            "offscreen focus cannot show a tooltip"
        );
        f.end();
    }

    /// PR-9: buttons fire on the press-down frame — not on release, not
    /// while held, not when pressed elsewhere.
    #[wasm_bindgen_test(unsupported = test)]
    fn button_fires_on_press_down_only() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();

        // Frame 1: press inside → fires immediately.
        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 30.0));
        assert!(f.button("b", BTN, "OK", 24.0));
        f.end();

        // Frame 2: still held → no re-fire.
        let mut f = ui.begin(&mut text, &mut list, hover_at(50.0, 30.0, true));
        assert!(!f.button("b", BTN, "OK", 24.0));
        f.end();

        // Frame 3: release → no fire.
        let input = UiInput {
            mouse_pos: Vec2::new(50.0, 30.0),
            mouse_released: true,
            ..UiInput::default()
        };
        let mut f = ui.begin(&mut text, &mut list, input);
        assert!(!f.button("b", BTN, "OK", 24.0));
        f.end();
        assert!(ui.active.is_none(), "release clears the active widget");

        // Frame 4: press outside → no fire.
        let mut f = ui.begin(&mut text, &mut list, press_at(500.0, 300.0));
        assert!(!f.button("b", BTN, "OK", 24.0));
        f.end();
    }

    /// Holding a press keeps the pressed visual state on that widget only.
    #[wasm_bindgen_test(unsupported = test)]
    fn active_widget_tracks_the_pressed_button() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();

        let mut f = ui.begin(&mut text, &mut list, press_at(50.0, 30.0));
        f.button("b", BTN, "OK", 24.0);
        f.end();
        assert_eq!(ui.active, Some(widget_id("b")));

        // Held next frame — stays active until release.
        let mut f = ui.begin(&mut text, &mut list, hover_at(50.0, 30.0, true));
        f.button("b", BTN, "OK", 24.0);
        f.end();
        assert_eq!(ui.active, Some(widget_id("b")));
    }

    /// Radio group: pressing a member selects it and deselects the rest
    /// (fire/water, PR-9).
    #[wasm_bindgen_test(unsupported = test)]
    fn radio_selects_on_press() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let water = Rect::new(120.0, 10.0, 80.0, 80.0);

        #[derive(Clone, Copy, PartialEq, Debug)]
        enum Element {
            Fire,
            Water,
        }
        let mut sel = Element::Fire;

        let mut f = ui.begin(&mut text, &mut list, press_at(150.0, 40.0));
        f.radio(
            "fire",
            Rect::new(10.0, 10.0, 80.0, 80.0),
            "F",
            24.0,
            Element::Fire,
            &mut sel,
        );
        f.radio("water", water, "W", 24.0, Element::Water, &mut sel);
        f.end();
        assert_eq!(sel, Element::Water);
    }

    /// A checkbox flips on the press-down frame anywhere in its rect — the
    /// label counts — and not while held or on release.
    #[wasm_bindgen_test(unsupported = test)]
    fn checkbox_flips_on_press_anywhere_in_its_rect() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let mut checked = false;
        // Press on the label, far from the square.
        let mut f = ui.begin(&mut text, &mut list, press_at(200.0, 116.0));
        assert!(f.checkbox("c", ROW, "Show grid", 20.0, &mut checked));
        f.end();
        assert!(checked);
        let mut f = ui.begin(&mut text, &mut list, hover_at(200.0, 116.0, true));
        assert!(!f.checkbox("c", ROW, "Show grid", 20.0, &mut checked));
        f.end();
        assert!(checked, "held → unchanged");
        let mut f = ui.begin(&mut text, &mut list, release_at(200.0, 116.0));
        assert!(!f.checkbox("c", ROW, "Show grid", 20.0, &mut checked));
        f.end();
        assert!(checked, "release → unchanged");
        let mut f = ui.begin(&mut text, &mut list, press_at(20.0, 116.0));
        assert!(f.checkbox("c", ROW, "Show grid", 20.0, &mut checked));
        f.end();
        assert!(!checked, "second press flips back");
    }

    /// A collapsing header flips `open` on press-down only.
    #[wasm_bindgen_test(unsupported = test)]
    fn collapsing_header_toggles_open_on_press() {
        let (mut ui, mut text, _assets) = fixture();
        let mut list = DrawList::new();
        let mut open = true;
        let mut f = ui.begin(&mut text, &mut list, press_at(100.0, 116.0));
        assert!(f.collapsing_header("h", ROW, "Transform", 20.0, &mut open));
        f.end();
        assert!(!open);
        let mut f = ui.begin(&mut text, &mut list, press_at(100.0, 200.0));
        assert!(!f.collapsing_header("h", ROW, "Transform", 20.0, &mut open));
        f.end();
        assert!(!open, "a press elsewhere leaves it");
    }
}