concinnity-engine 0.19.53

Runtime engine for Concinnity: ECS schedule, graphics, spawn, streaming
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
// The HitRegion pass: hover styling and click / confirm dispatch for every
// region, with the clicks that need the whole system (group toggles, rebind
// capture, dropdown opens) returned for the caller to apply.

use concinnity_core::ecs::Ref;
use std::collections::{HashMap, HashSet};

use concinnity_core::components::{FrameInput, SettingVerb, SpriteFit, TextLabel, UiAction};
use concinnity_core::ecs::asset_id::AssetId;
use concinnity_core::ecs::{FrameVec, PipelineContext, StepResult};
use concinnity_core::gfx::overlay::OverlayTransform;
use concinnity_core::settings::SettingKey;

use super::intent::UiIntent;
use super::{
    OpenRequest, UiInputSystem, fire_action, point_in_rect, region_covers_canvas, region_rect,
    set_label_style,
};

// The per-frame state every region's hit-test reads.
pub(super) struct RegionFrame<'a> {
    // The topmost input-capturing screen; only its regions (or, with none,
    // the screen-less regions) react.
    active_screen: Option<AssetId>,
    // The scrollbar thumb is being dragged, so no region reacts.
    thumb_active: bool,
    // Reference-canvas mappings for screen-owned regions, chosen by `fit`.
    overlay: OverlayTransform,
    overlay_bottom: OverlayTransform,
    overlay_cover: OverlayTransform,
    // Each panel's content band (reference space), indexed like `panels`.
    panel_bands: FrameVec<'a, [f32; 4]>,
}

impl<'a> RegionFrame<'a> {
    // `overlay` is the default fit mapping; the bottom-anchored and cover
    // mappings a region may opt into derive from the same viewport.
    pub(super) fn new(
        viewport: [f32; 2],
        active_screen: Option<AssetId>,
        thumb_active: bool,
        overlay: OverlayTransform,
        panel_bands: FrameVec<'a, [f32; 4]>,
    ) -> Self {
        Self {
            active_screen,
            thumb_active,
            overlay,
            overlay_bottom: OverlayTransform::bottom_anchored_from_viewport(viewport),
            overlay_cover: OverlayTransform::cover_from_viewport(viewport),
            panel_bands,
        }
    }

    // The cursor in a region's own space: window pixels for a screen-less
    // region, reference space through its `fit` otherwise.
    fn cursor_for(&self, screen: Option<AssetId>, fit: SpriteFit, mx: f32, my: f32) -> (f32, f32) {
        if screen.is_none() {
            return (mx, my);
        }
        match fit {
            SpriteFit::Bottom => self.overlay_bottom.inverse(mx, my),
            SpriteFit::Cover => self.overlay_cover.inverse(mx, my),
            SpriteFit::Fit => self.overlay.inverse(mx, my),
        }
    }
}

// What the region pass asks the caller to do after it releases the regions.
#[derive(Default)]
pub(super) struct RegionOutcome {
    // A group header was clicked: flip that group on the active panel.
    pub(super) toggle_group: Option<usize>,
    // A rebind row was clicked: capture for this setting key and value label.
    pub(super) start_capture: Option<(SettingKey, Option<AssetId>)>,
    // A dropdown row was clicked: open its floating list.
    pub(super) start_open: Option<OpenRequest>,
    // A fired action ended the step (e.g. Quit); nothing else applies.
    pub(super) fired: Option<StepResult>,
}

// Why a region may be unable to hover or fire this frame.
#[derive(Debug, Default, Clone, Copy)]
struct RegionGate {
    thumb_active: bool,
    slider: bool,
    // The focus cursor is on this region (a focused slider still highlights).
    pad_focused: bool,
    screen_matches: bool,
    // Its scroll-content row is collapsed.
    collapsed_row: bool,
    // The engine disabled its setting row at runtime.
    disabled: bool,
    // It follows a label that is empty or gone.
    follow_inert: bool,
}

// Whether a region neither hovers nor fires this frame.
fn region_inert(gate: RegionGate) -> bool {
    gate.thumb_active
        || (gate.slider && !gate.pad_focused)
        || !gate.screen_matches
        || gate.collapsed_row
        || gate.disabled
        || gate.follow_inert
}

// A follow-label region's synced y and whether it is inert: it tracks its
// label and goes inert while the label is empty or missing.
fn follow_sync(
    follow: Option<(AssetId, f32)>,
    labels: &HashMap<AssetId, (f32, bool)>,
) -> (Option<f32>, bool) {
    let Some((label_id, offset)) = follow else {
        return (None, false);
    };
    match labels.get(&label_id) {
        Some(&(ly, empty)) => (Some(ly + offset), empty),
        None => (None, true),
    }
}

// Whether a region's setting row is in the runtime-disabled set.
pub(super) fn setting_row_disabled(
    disabled_rows: &HashSet<SettingKey>,
    action: Option<&UiAction>,
) -> bool {
    matches!(action, Some(UiAction::Setting { key, .. }) if disabled_rows.contains(key))
}

// A region's `(hovered, fire)`. While the focus cursor is set it owns the hover
// slot: the focused region styles and fires on confirm, and the mouse's region
// does neither. Otherwise the mouse hovers, a click fires, and an unfocused
// confirm may fall through to a full-canvas region.
fn hover_and_fire(
    has_focus: bool,
    pad_focused: bool,
    mouse_hovered: bool,
    clicked: bool,
    confirm: bool,
    fallback_fire: bool,
) -> (bool, bool) {
    if has_focus {
        (pad_focused, pad_focused && confirm)
    } else {
        (mouse_hovered, (mouse_hovered && clicked) || fallback_fire)
    }
}

impl UiInputSystem {
    // Refresh the owned copy of `DisabledSettingRows` only when the published
    // set changed, so the region pass reads it without a per-frame clone.
    pub(super) fn refresh_disabled_rows(&mut self, ctx: &PipelineContext) {
        let changed = match ctx.resource::<crate::ecs::DisabledSettingRows>() {
            Some(d) => d.0 != self.disabled_rows_cache,
            None => !self.disabled_rows_cache.is_empty(),
        };
        if changed {
            self.disabled_rows_cache = ctx
                .resource::<crate::ecs::DisabledSettingRows>()
                .map(|d| d.0.clone())
                .unwrap_or_default();
        }
    }

    // Resolve each followed label's (y, is-empty) once, so the region pass
    // reads a map instead of looking a label up per region.
    fn resolve_follow_labels(&mut self, ctx: &PipelineContext) {
        self.follow_labels.clear();
        for &id in &self.follow_label_ids {
            if let Some(l) = ctx.get_by_id::<TextLabel>(id) {
                self.follow_labels.insert(id, (l.y, l.content.is_empty()));
            }
        }
    }

    // Hover-style and fire every region for one frame. An inert region that was
    // hovered has its label style restored so the hover never strands. A fired
    // action that ends the step returns at once, leaving later regions as they
    // were.
    pub(super) fn hit_test_regions(
        &mut self,
        input: &FrameInput,
        intent: &UiIntent,
        frame: RegionFrame,
        ctx: &mut PipelineContext,
    ) -> RegionOutcome {
        self.resolve_follow_labels(ctx);
        let (mx, my) = (input.mouse_x, input.mouse_y);
        let [vw, vh] = input.viewport;
        let focus_index = self.focus.as_ref().map(|f| f.index);
        let confirm_fallback = intent.confirm && focus_index.is_none();
        let mut confirm_used = false;
        let mut outcome = RegionOutcome::default();

        for (i, entry) in self.regions.iter_mut().enumerate() {
            let (follow_y, follow_inert) = follow_sync(entry.follow, &self.follow_labels);
            if let Some(y) = follow_y {
                entry.region.y = y;
            }
            let pad_focused = focus_index == Some(i);
            let gate = RegionGate {
                thumb_active: frame.thumb_active,
                slider: entry.slider_key.is_some(),
                pad_focused,
                screen_matches: entry.screen == frame.active_screen,
                collapsed_row: entry.scroll_row.is_some() && entry.hidden,
                disabled: setting_row_disabled(
                    &self.disabled_rows_cache,
                    entry.region.action.as_ref(),
                ),
                follow_inert,
            };
            if region_inert(gate) {
                if entry.was_hovered {
                    set_label_style(
                        ctx,
                        entry.region.label.map(Ref::id),
                        entry.original_color,
                        entry.original_scale,
                    );
                    entry.was_hovered = false;
                }
                continue;
            }

            // A region spanning the whole reference canvas covers the full
            // window, so a full-canvas advance region catches letterbox clicks.
            let full_window = entry.screen.is_some() && region_covers_canvas(&entry.region);
            let (qx, qy) = frame.cursor_for(entry.screen, entry.fit, mx, my);
            let r = &entry.region;
            let mut mouse_hovered = if full_window {
                mx >= 0.0 && mx < vw && my >= 0.0 && my < vh
            } else {
                point_in_rect(qx, qy, region_rect(r))
            };
            // A scroll-content region only counts as hovered inside its band, so
            // a row scrolled past the edge does not catch clicks over the chrome.
            if let Some((pi, _)) = entry.scroll_row
                && let Some(band) = frame.panel_bands.get(pi)
            {
                mouse_hovered = mouse_hovered && point_in_rect(qx, qy, *band);
            }
            let fallback_fire = confirm_fallback && full_window && !confirm_used;
            let (hovered, fire) = hover_and_fire(
                focus_index.is_some(),
                pad_focused,
                mouse_hovered,
                input.left_click,
                intent.confirm,
                fallback_fire,
            );

            if hovered && !entry.was_hovered {
                set_label_style(ctx, r.label.map(Ref::id), r.hover_color, r.hover_scale);
            } else if !hovered && entry.was_hovered {
                set_label_style(
                    ctx,
                    r.label.map(Ref::id),
                    entry.original_color,
                    entry.original_scale,
                );
            }
            entry.was_hovered = hovered;

            if !fire {
                continue;
            }
            if fallback_fire {
                confirm_used = true;
            }
            match &r.action {
                None => {}
                Some(UiAction::GroupToggle(gid)) => outcome.toggle_group = Some(*gid),
                Some(UiAction::Setting {
                    key,
                    verb: SettingVerb::Rebind,
                }) => outcome.start_capture = Some((*key, r.label.map(Ref::id))),
                Some(UiAction::Setting {
                    key,
                    verb: SettingVerb::Open,
                }) => {
                    // Snapshot the control rect and the row's un-hovered value style.
                    outcome.start_open = Some(OpenRequest {
                        setting: *key,
                        value_label: r.label.map(Ref::id),
                        anchor: region_rect(r),
                        screen: entry.screen,
                        color: entry.original_color,
                        scale: entry.original_scale,
                    });
                }
                Some(action) => {
                    if let Some(result) = fire_action(action, r.label.map(Ref::id), ctx) {
                        outcome.fired = Some(result);
                        return outcome;
                    }
                }
            }
        }
        outcome
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // A live region: on the active screen and gated by nothing.
    fn live() -> RegionGate {
        RegionGate {
            screen_matches: true,
            ..Default::default()
        }
    }

    #[test]
    fn a_live_region_is_not_inert() {
        assert!(!region_inert(live()));
    }

    #[test]
    fn each_gate_makes_a_region_inert() {
        let gates = [
            RegionGate {
                thumb_active: true,
                ..live()
            },
            RegionGate {
                slider: true,
                ..live()
            },
            RegionGate {
                screen_matches: false,
                ..live()
            },
            RegionGate {
                collapsed_row: true,
                ..live()
            },
            RegionGate {
                disabled: true,
                ..live()
            },
            RegionGate {
                follow_inert: true,
                ..live()
            },
        ];
        for gate in gates {
            assert!(region_inert(gate), "{gate:?}");
        }
    }

    #[test]
    fn a_focused_slider_stays_live() {
        let gate = RegionGate {
            slider: true,
            pad_focused: true,
            ..live()
        };
        assert!(!region_inert(gate));
    }

    #[test]
    fn follow_sync_tracks_the_label_and_goes_inert_when_empty_or_missing() {
        let labels = HashMap::from([(AssetId(1), (40.0, false)), (AssetId(2), (60.0, true))]);
        assert_eq!(follow_sync(None, &labels), (None, false));
        assert_eq!(
            follow_sync(Some((AssetId(1), 5.0)), &labels),
            (Some(45.0), false)
        );
        assert_eq!(
            follow_sync(Some((AssetId(2), 5.0)), &labels),
            (Some(65.0), true)
        );
        assert_eq!(follow_sync(Some((AssetId(3), 5.0)), &labels), (None, true));
    }

    #[test]
    fn setting_row_disabled_matches_the_setting_key() {
        let rows = HashSet::from([SettingKey::ShowFps]);
        let row = |key| UiAction::Setting {
            key,
            verb: SettingVerb::Next,
        };
        assert!(setting_row_disabled(&rows, Some(&row(SettingKey::ShowFps))));
        assert!(!setting_row_disabled(&rows, Some(&row(SettingKey::Vsync))));
        assert!(!setting_row_disabled(&rows, Some(&UiAction::Quit)));
        assert!(!setting_row_disabled(&rows, None));
        assert!(!setting_row_disabled(
            &HashSet::new(),
            Some(&row(SettingKey::ShowFps))
        ));
    }

    #[test]
    fn mouse_hovers_and_clicks_without_focus() {
        assert_eq!(
            hover_and_fire(false, false, true, false, false, false),
            (true, false)
        );
        assert_eq!(
            hover_and_fire(false, false, true, true, false, false),
            (true, true)
        );
        assert_eq!(
            hover_and_fire(false, false, false, true, false, false),
            (false, false)
        );
    }

    #[test]
    fn unfocused_confirm_fires_only_through_the_fallback() {
        assert_eq!(
            hover_and_fire(false, false, true, false, true, false),
            (true, false)
        );
        assert_eq!(
            hover_and_fire(false, false, false, false, true, true),
            (false, true)
        );
    }

    #[test]
    fn focus_owns_the_hover_slot() {
        // The mouse's region neither hovers nor fires while focus is elsewhere.
        assert_eq!(
            hover_and_fire(true, false, true, true, true, true),
            (false, false)
        );
        assert_eq!(
            hover_and_fire(true, true, false, false, false, false),
            (true, false)
        );
        assert_eq!(
            hover_and_fire(true, true, false, false, true, false),
            (true, true)
        );
    }
}