concinnity-dev 0.19.119

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
//! Shared chrome for the editor's simple "row list" floating panels (Preview,
//! View, Templates). Each is one rounded surface: a draggable title area with a
//! close button over a vertical stack of fixed-height rows, and each row is a
//! hover / selection highlight plus a label with an optional checkbox. The three
//! panels differ only in their ids, their width, and how a row index maps to
//! their own action; the row geometry, the per-row draw, and the hit test are
//! identical, so they live here once. (The Assets and Template detail panels
//! use the richer grouped list in `asset_list.rs` instead.)

use concinnity_core::ecs::World;
use concinnity_core::ecs::asset_id::AssetId;

use crate::editor::theme;
use crate::editor::widget::{self, place_rounded, point_in};

// Row geometry, in window pixels.
pub(crate) const ROW_H: f32 = 28.0;
const BOX_SIZE: f32 = 16.0;
const PAD: f32 = 8.0;
// A checkbox row insets its label past the box.
const CHECK_LABEL_INSET: f32 = 32.0;
// The right-aligned value strip a row may carry (its own click target).
const VALUE_W: f32 = 64.0;
const LABEL_TOP: f32 = ROW_H * 0.5 - theme::TEXT_HALF;
// Breathing room between the last row and the panel's rounded bottom edge.
const BOTTOM_PAD: f32 = 6.0;

// Row highlight tints: nothing while idle (the panel surface shows through),
// the shared hover / selected highlights otherwise. A checkbox reflects its own
// on / off state separately.
const ROW_TINT: [f32; 4] = [0.0, 0.0, 0.0, 0.0];

// The ids a row-list panel draws into, declared by the panel itself. A panel
// whose rows never carry a checkbox or a value has no ids for them.
#[derive(Clone, Copy)]
pub(crate) struct ListIds {
    pub panel_bg: AssetId,
    pub title: AssetId,
    pub close_bg: AssetId,
    pub close_label: AssetId,
    pub row_bg: fn(usize) -> AssetId,
    pub row_label: fn(usize) -> AssetId,
    pub check_box: Option<fn(usize) -> AssetId>,
    pub value_label: Option<fn(usize) -> AssetId>,
}

// The "X" close button at the title bar's right end.
pub(crate) fn close_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    widget::close_rect(widget::title_rect(o, w))
}

// Row `i`, stacked below the title bar.
pub(crate) fn row_rect(o: [f32; 2], w: f32, i: usize) -> [f32; 4] {
    [o[0], o[1] + widget::TITLE_H + i as f32 * ROW_H, w, ROW_H]
}

// The value strip at row `i`'s right end. A caller with a value row resolves a
// click here separately from the rest of the row (cycle vs toggle).
pub(crate) fn value_rect(o: [f32; 2], w: f32, i: usize) -> [f32; 4] {
    let r = row_rect(o, w, i);
    [r[0] + r[2] - VALUE_W, r[1], VALUE_W, ROW_H]
}

// The panel's footprint (title bar plus `rows` rows plus the bottom pad).
pub(crate) fn size(w: f32, rows: usize) -> [f32; 2] {
    [w, widget::TITLE_H + rows as f32 * ROW_H + BOTTOM_PAD]
}

// The panel outer rect at origin `o`.
pub(crate) fn panel_rect(o: [f32; 2], w: f32, rows: usize) -> [f32; 4] {
    widget::outer_rect(o, size(w, rows))
}

// One row to draw: its caption, an optional checkbox (`Some(on)` draws a box
// tinted by its state; `None` is a label-only row), an optional right-aligned
// value, and whether it is the currently selected row (highlighted even
// without a hover).
pub(crate) struct Row {
    pub caption: String,
    pub check: Option<bool>,
    pub value: Option<String>,
    pub selected: bool,
}

impl Row {
    // A label-only row (Templates).
    pub(crate) fn label(caption: impl Into<String>) -> Self {
        Self {
            caption: caption.into(),
            check: None,
            value: None,
            selected: false,
        }
    }

    // A checkbox row reflecting `on` (Preview / View).
    pub(crate) fn checkbox(caption: impl Into<String>, on: bool) -> Self {
        Self {
            caption: caption.into(),
            check: Some(on),
            value: None,
            selected: false,
        }
    }

    // Attach a right-aligned value (drawn in the `value_rect` strip).
    pub(crate) fn with_value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }

    // Mark this row selected (drawn highlighted while idle).
    pub(crate) fn select(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }
}

// Position + show the whole panel at origin `o` and effective size `s`: the
// rounded panel surface (grown to `s`, so a resized panel's extra height is
// padding below the last row), the heading, the hover-tinted close button, and
// each row's highlight, optional checkbox, and label. `mouse` drives the hover
// highlight and the close-button tint.
pub(crate) fn place(
    world: &mut World,
    ids: &ListIds,
    o: [f32; 2],
    s: [f32; 2],
    heading: &str,
    rows: &[Row],
    mouse: [f32; 2],
) {
    let w = s[0];
    widget::place_panel(world, ids.panel_bg, widget::outer_rect(o, s));
    let title = widget::title_rect(o, w);
    widget::place_heading(world, ids.title, title, heading);
    let close_hover = point_in(mouse[0], mouse[1], close_rect(o, w));
    widget::place_close(world, ids.close_bg, ids.close_label, title, close_hover);
    for (i, row) in rows.iter().enumerate() {
        let r = row_rect(o, w, i);
        let hovered = point_in(mouse[0], mouse[1], r);
        let tint = if hovered {
            theme::HOVER_TINT
        } else if row.selected {
            theme::SELECTED_TINT
        } else {
            ROW_TINT
        };
        place_rounded(
            world,
            (ids.row_bg)(i),
            theme::highlight_rect(r),
            tint,
            theme::CONTROL_RADIUS,
            true,
        );
        let label_x = match row.check {
            Some(on) => {
                let box_tint = if on {
                    theme::CHECK_ON_TINT
                } else {
                    theme::CHECK_OFF_TINT
                };
                if let Some(check_box) = ids.check_box {
                    place_rounded(
                        world,
                        check_box(i),
                        [
                            r[0] + PAD,
                            r[1] + (ROW_H - BOX_SIZE) * 0.5,
                            BOX_SIZE,
                            BOX_SIZE,
                        ],
                        box_tint,
                        4.0,
                        true,
                    );
                }
                r[0] + CHECK_LABEL_INSET
            }
            None => r[0] + PAD,
        };
        let label_right = match &row.value {
            Some(_) => r[0] + r[2] - VALUE_W,
            None => r[0] + r[2] - PAD,
        };
        widget::place_message(
            world,
            (ids.row_label)(i),
            [
                label_x,
                r[1] + LABEL_TOP,
                (label_right - label_x).max(0.0),
                widget::LINE_H,
            ],
            &row.caption,
            theme::LABEL,
            true,
        );
        let Some(value_label) = ids.value_label else {
            continue;
        };
        match &row.value {
            Some(v) => {
                let vr = value_rect(o, w, i);
                widget::place_message(
                    world,
                    value_label(i),
                    [vr[0], vr[1] + LABEL_TOP, vr[2] - PAD, widget::LINE_H],
                    v,
                    theme::LABEL_DIM,
                    true,
                );
            }
            None => widget::set_label_visible(world, value_label(i), false),
        }
    }
}

// The row index at `(mx, my)`, or `None` if the point is outside every row. A
// caller then falls back to a panel-wide "consume" hit if it wants to swallow
// clicks that land on the panel but miss a row.
pub(crate) fn hit_row(mx: f32, my: f32, o: [f32; 2], w: f32, rows: usize) -> Option<usize> {
    (0..rows).find(|&i| point_in(mx, my, row_rect(o, w, i)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::editor::hud_ids::hud_ids;
    use concinnity_core::components::{Sprite, TextLabel};

    const ROWS: usize = 2;

    hud_ids! {
        base: 0;
        sprites: [PANEL_BG, CLOSE_BG, row_bg[ROWS], check_box[ROWS]];
        labels: [TITLE_LABEL, CLOSE_LABEL, row_label[ROWS], value_label[ROWS]];
    }

    const IDS: ListIds = ListIds {
        panel_bg: PANEL_BG,
        title: TITLE_LABEL,
        close_bg: CLOSE_BG,
        close_label: CLOSE_LABEL,
        row_bg,
        row_label,
        check_box: Some(check_box),
        value_label: Some(value_label),
    };

    fn injected_world() -> World {
        ids().test_world()
    }

    fn sprite(world: &World, id: AssetId) -> Sprite {
        world.get_by_id::<Sprite>(id).cloned().unwrap()
    }

    #[test]
    fn geometry_stacks_rows_below_the_title_bar() {
        let o = [40.0, 60.0];
        let w = 200.0;
        assert_eq!(widget::title_rect(o, w), [40.0, 60.0, w, widget::TITLE_H]);
        assert_eq!(row_rect(o, w, 0)[1], 60.0 + widget::TITLE_H);
        assert_eq!(row_rect(o, w, 1)[1], 60.0 + widget::TITLE_H + ROW_H);
        assert_eq!(size(w, 3)[1], widget::TITLE_H + 3.0 * ROW_H + BOTTOM_PAD);
        assert_eq!(
            panel_rect(o, w, 2)[3],
            widget::TITLE_H + 2.0 * ROW_H + BOTTOM_PAD
        );
    }

    // The whole panel is one rounded chrome surface; rows highlight over it.
    #[test]
    fn place_draws_the_rounded_panel_surface() {
        let mut world = injected_world();
        place(
            &mut world,
            &IDS,
            [20.0, 30.0],
            size(200.0, 1),
            "Panel",
            &[Row::label("a")],
            [0.0, 0.0],
        );
        let bg = sprite(&world, PANEL_BG);
        assert!(bg.visible);
        assert_eq!((bg.x, bg.y), (20.0, 30.0));
        assert_eq!((bg.width, bg.height), (200.0, size(200.0, 1)[1]));
        assert_eq!(bg.corner_radius, theme::PANEL_RADIUS);
    }

    // A resized (taller / wider) panel grows its surface to the given size; the
    // rows stay anchored below the title bar and the extra space is padding.
    #[test]
    fn place_grows_the_surface_to_the_effective_size() {
        let mut world = injected_world();
        let o = [20.0, 30.0];
        let tall_wide = [size(200.0, 1)[0] + 80.0, size(200.0, 1)[1] + 120.0];
        place(
            &mut world,
            &IDS,
            o,
            tall_wide,
            "Panel",
            &[Row::label("a")],
            [0.0, 0.0],
        );
        let bg = sprite(&world, PANEL_BG);
        assert_eq!((bg.width, bg.height), (tall_wide[0], tall_wide[1]));
        // The lone row still sits directly under the title bar.
        assert_eq!(row_rect(o, tall_wide[0], 0)[1], o[1] + widget::TITLE_H);
    }

    #[test]
    fn hit_row_resolves_a_row_or_misses() {
        let o = [10.0, 10.0];
        let w = 200.0;
        let r1 = row_rect(o, w, 1);
        assert_eq!(hit_row(r1[0] + 5.0, r1[1] + 5.0, o, w, 3), Some(1));
        // Above the first row (in the title bar) and below the last row: misses.
        assert_eq!(hit_row(o[0] + 5.0, o[1] + 5.0, o, w, 3), None);
        assert_eq!(hit_row(o[0] + 5.0, 10_000.0, o, w, 3), None);
    }

    #[test]
    fn place_draws_title_close_and_a_checkbox_row() {
        let mut world = injected_world();
        let o = [20.0, 20.0];
        let w = 200.0;
        place(
            &mut world,
            &IDS,
            o,
            size(w, 1),
            "Panel",
            &[Row::checkbox("Toggle", true)],
            [0.0, 0.0],
        );
        let title = world.get_by_id::<TextLabel>(TITLE_LABEL).unwrap();
        assert!(title.visible && title.content == "Panel");
        // The close button always shows its "X".
        let close = world.get_by_id::<TextLabel>(CLOSE_LABEL).unwrap();
        assert!(close.visible && close.content == "X");
        // The checkbox is green while on and the label is inset past the box.
        assert_eq!(sprite(&world, check_box(0)).tint, theme::CHECK_ON_TINT);
        let label = world.get_by_id::<TextLabel>(row_label(0)).unwrap();
        assert_eq!(label.content, "Toggle");
        assert_eq!(label.x, o[0] + CHECK_LABEL_INSET);
        // Off flips the checkbox tint.
        place(
            &mut world,
            &IDS,
            o,
            size(w, 1),
            "Panel",
            &[Row::checkbox("Toggle", false)],
            [0.0, 0.0],
        );
        assert_eq!(sprite(&world, check_box(0)).tint, theme::CHECK_OFF_TINT);
    }

    #[test]
    fn label_only_row_insets_by_pad_not_a_box() {
        // A label-only panel declares no checkbox ids, so its rows have nothing
        // to inset past: the label sits at PAD.
        let mut world = crate::test_support::injected_world(
            &[PANEL_BG, CLOSE_BG, row_bg(0)],
            &[TITLE_LABEL, CLOSE_LABEL, row_label(0)],
            &[],
        );
        let labels_only = ListIds {
            check_box: None,
            value_label: None,
            ..IDS
        };
        let o = [20.0, 20.0];
        place(
            &mut world,
            &labels_only,
            o,
            size(200.0, 1),
            "Panel",
            &[Row::label("Just text")],
            [0.0, 0.0],
        );
        let label = world.get_by_id::<TextLabel>(row_label(0)).unwrap();
        assert_eq!(
            label.x,
            o[0] + PAD,
            "label-only rows inset by PAD, not a box"
        );
    }

    #[test]
    fn hovered_and_selected_rows_are_tinted() {
        let mut world = injected_world();
        let o = [20.0, 20.0];
        let w = 200.0;
        let r0 = row_rect(o, w, 0);
        place(
            &mut world,
            &IDS,
            o,
            size(w, 2),
            "Panel",
            &[Row::label("a"), Row::label("b").select(true)],
            [r0[0] + 5.0, r0[1] + 5.0],
        );
        let hovered = sprite(&world, row_bg(0));
        assert_eq!(hovered.tint, theme::HOVER_TINT);
        assert!(
            hovered.height < ROW_H && hovered.corner_radius > 0.0,
            "the highlight is an inset rounded pill, not a full-width band"
        );
        assert_eq!(
            sprite(&world, row_bg(1)).tint,
            theme::SELECTED_TINT,
            "the selected row is highlighted without a hover"
        );
        // An idle, unselected row draws no highlight at all.
        place(
            &mut world,
            &IDS,
            o,
            size(w, 2),
            "Panel",
            &[Row::label("a"), Row::label("b")],
            [0.0, 0.0],
        );
        assert_eq!(sprite(&world, row_bg(0)).tint[3], 0.0);
    }
}