euv-example 0.48.1

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
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
use super::*;

/// Mounts the 2D physics & sprite engine studio with a wide arena viewport beside the simulation and arcade-control rail.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted game engine showcase page node.
pub fn mount_game_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let host: GameEngineHost = state.get_game_host().clone();
    let host_btn: GameEngineHost = host.clone();
    let host_wave: GameEngineHost = host.clone();
    let viewport: NodeId = host.mount_viewport(
        Style::default()
            .width(Length::Percent(100.0))
            .height(Length::Percent(100.0)),
    );
    let vp_sig: Signal<Size> = use_viewport_size();
    let vp_frame_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let h: f32 = adaptive_viewport_height(vp_sig.get().get_height());
        Style::default()
            .column()
            .width(Length::Percent(100.0))
            .height(Length::Px(h))
            .overflow(Overflow::Hidden)
            .border(
                live.get_geometry().get_border_width(),
                live.get_palette().get_border(),
            )
            .radius(live.get_geometry().get_radius_md())
    };
    let viewport_frame: NodeId = view! {
        column {
            bind_style: vp_frame_style
            { viewport }
        }
    };
    let fs_2d_open: Signal<bool> = Signal::new(false);
    let fs_2d_portal: NodeId =
        mount_game_fullscreen_portal(fs_2d_open, &host, viewport, None, GAME_CARD_TITLE, tokens);
    let on_step_physics = move |_: &UiEvent| {
        let _: u32 = host_btn.step(0.051);
    };
    let on_spawn_wave = move |_: &UiEvent| {
        host_wave.ingest_event(&WindowEvent::KeyDown(
            WindowId::new(1),
            KeyEvent::new(KeyCode::Character('r'), Modifiers::default(), false),
        ));
        let _: u32 = host_wave.step(1.0 / 60.0);
        host_wave.ingest_event(&WindowEvent::KeyUp(
            WindowId::new(1),
            KeyEvent::new(KeyCode::Character('r'), Modifiers::default(), false),
        ));
    };
    let on_open_fs = move |_: &UiEvent| {
        let _: bool = fs_2d_open.set_if_changed(true);
    };
    let hint_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        Style::default()
            .width(Length::Percent(100.0))
            .font_size(12.0)
            .font_family(FontFamily::Monospace)
            .color(live.get_palette().get_muted_foreground())
    };
    let ctrl_left: NodeId =
        mount_game_control_button(BTN_GAME_LEFT, &host, KeyCode::ArrowLeft, tokens);
    let ctrl_up: NodeId = mount_game_control_button(BTN_GAME_UP, &host, KeyCode::ArrowUp, tokens);
    let ctrl_down: NodeId =
        mount_game_control_button(BTN_GAME_DOWN, &host, KeyCode::ArrowDown, tokens);
    let ctrl_right: NodeId =
        mount_game_control_button(BTN_GAME_RIGHT, &host, KeyCode::ArrowRight, tokens);
    let ctrl_dash: NodeId =
        mount_game_control_button(BTN_GAME_DASH, &host, KeyCode::Character('e'), tokens);
    let arena_card: NodeId = view! {
        EuvCard {
            title: GAME_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME_CARD_SUB
            { viewport_frame }
            text {
                bind_style: hint_style
                { GAME_HUD_CONTROLS_HINT }
            }
        }
    };
    let sim_card: NodeId = view! {
        EuvCard {
            title: GAME_SIM_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME_SIM_CARD_SUB
            EuvActionRow {
                EuvButton {
                    label: BTN_STEP_PHYSICS.to_string()
                    tokens: tokens
                    outline()
                    sm()
                    on_click: on_step_physics
                }
                EuvButton {
                    label: BTN_SPAWN_WAVE.to_string()
                    tokens: tokens
                    outline()
                    sm()
                    on_click: on_spawn_wave
                }
                EuvButton {
                    label: BTN_GAME_FULLSCREEN.to_string()
                    tokens: tokens
                    sm()
                    on_click: on_open_fs
                }
            }
        }
    };
    let input_card: NodeId = view! {
        EuvCard {
            title: GAME_INPUT_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: GAME_INPUT_CARD_SUB
            EuvActionRow {
                { ctrl_left }
                { ctrl_up }
                { ctrl_down }
                { ctrl_right }
                { ctrl_dash }
            }
        }
    };
    let rail: NodeId = view! {
        column {
            style: {
                width: Length::Percent(100.0)
                gap: SPACE_MD
            }
            { sim_card }
            { input_card }
        }
    };
    let studio: NodeId = mount_studio_layout(arena_card, rail);
    view! {
        EuvPage {
            title: GAME_TITLE.to_string()
            subtitle: GAME_SUBTITLE.to_string()
            tokens: tokens
            { studio }
            { fs_2d_portal }
        }
    }
}

/// Mounts a reusable fullscreen portal overlay hosting any 2D/3D `GameEngineHost` with safe exit, Back support, and touch controls.
///
/// # Arguments
///
/// - `Signal<bool>` - Fullscreen visibility toggle signal.
/// - `&GameEngineHost` - Target game engine host.
/// - `NodeId` - Embedded inline viewport hidden while fullscreen is active to avoid redundant rendering.
/// - `Option<Signal<u8>>` - Optional 3D camera mode signal for fullscreen camera toggling.
/// - `&str` - Title shown in the fullscreen HUD bar.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted fullscreen portal handle.
pub(crate) fn mount_game_fullscreen_portal(
    open: Signal<bool>,
    host: &GameEngineHost,
    inline_viewport: NodeId,
    cam_mode_opt: Option<Signal<u8>>,
    title: &str,
    tokens: ThemeTokens,
) -> NodeId {
    let ctx: Context = current_context();
    bind_fullscreen_context(&ctx);
    let fs_viewport: NodeId = host.mount_viewport(
        Style::default()
            .flex_grow(1.0)
            .flex_shrink(1.0)
            .min_height(Length::Px(0.0))
            .width(Length::Percent(100.0))
            .overflow(Overflow::Hidden)
            .stop_scroll_propagation(true)
            .stop_event_propagation(true),
    );
    let ctrl_left: NodeId =
        mount_game_control_button(FS_CTRL_LEFT, host, KeyCode::ArrowLeft, tokens);
    let ctrl_up: NodeId = mount_game_control_button(
        if cam_mode_opt.is_some() {
            FS_CTRL_DRIVE
        } else {
            FS_CTRL_UP
        },
        host,
        KeyCode::ArrowUp,
        tokens,
    );
    let ctrl_down: NodeId = mount_game_control_button(
        if cam_mode_opt.is_some() {
            FS_CTRL_REVERSE
        } else {
            FS_CTRL_DOWN
        },
        host,
        KeyCode::ArrowDown,
        tokens,
    );
    let ctrl_right: NodeId =
        mount_game_control_button(FS_CTRL_RIGHT, host, KeyCode::ArrowRight, tokens);
    let extra_ctrl: NodeId = if let Some(cam_sig) = cam_mode_opt {
        let on_cam = move |_: &UiEvent| {
            let next: u8 = (cam_sig.get_untracked() + 1) % 4;
            let _: bool = cam_sig.set_if_changed(next);
        };
        view! {
            EuvButton {
                label: FS_CTRL_CAMERA.to_string()
                tokens: tokens
                outline()
                sm()
                on_click: on_cam
            }
        }
    } else {
        mount_game_control_button(FS_CTRL_DASH, host, KeyCode::Character('e'), tokens)
    };
    let on_exit = move |_: &UiEvent| {
        let _: bool = open.set_if_changed(false);
    };
    let init_dark: ThemeTokens =
        ThemeTokens::resolve(live_tokens_untracked(tokens).get_scheme(), ThemeMode::Dark);
    let init_dark_p: ThemePalette = init_dark.get_palette();
    let init_dark_g: ThemeGeometry = init_dark.get_geometry();
    let paint_exit_icon = move |bounds: Rect, dl: &mut DisplayList| {
        let dark_tok: ThemeTokens =
            ThemeTokens::resolve(live_tokens_untracked(tokens).get_scheme(), ThemeMode::Dark);
        draw_euv_icon(
            EuvIconName::Minimize,
            bounds,
            dl,
            dark_tok.get_palette().get_foreground(),
        );
    };
    let exit_btn_style = move || -> Style {
        let dark_tok: ThemeTokens =
            ThemeTokens::resolve(use_live_tokens(tokens).get_scheme(), ThemeMode::Dark);
        let dark_p: ThemePalette = dark_tok.get_palette();
        let dark_g: ThemeGeometry = dark_tok.get_geometry();
        Style::default()
            .row()
            .width(Length::Px(FS_EXIT_BTN_SIZE))
            .height(Length::Px(FS_EXIT_BTN_SIZE))
            .justify(JustifyContent::Center)
            .align(AlignItems::Center)
            .radius(dark_g.get_radius_md())
            .background(dark_p.get_card())
            .border(dark_g.get_border_width(), dark_p.get_border())
            .cursor(CursorIcon::Pointer)
    };
    let title_str: String = title.to_string();
    let mut top_bar_id: NodeId = NodeId::default();
    let mut bottom_bar_id: NodeId = NodeId::default();
    let portal: NodeId = view! {
        portal {
            style: {
                Style::default().column()
                display: Display::None
                position: Position::Fixed
                inset: (Some(0.0), Some(0.0), Some(0.0), Some(0.0))
                width: Length::Percent(100.0)
                height: Length::Percent(100.0)
                padding: Edges::all(0.0)
                margin: Edges::all(0.0)
                background: init_dark_p.get_background()
                border: (0.0, COLOR_TRANSPARENT)
                radius: 0.0
                overflow: Overflow::Hidden
                pointer_events: PointerEvents::None
                stop_scroll_propagation: true
                stop_event_propagation: true
                z_index: Z_INDEX_FULLSCREEN
            }
            row {
                node_ref: top_bar_id
                style: {
                    width: Length::Percent(100.0)
                    flex_shrink: 0.0
                    justify: JustifyContent::SpaceBetween
                    align: AlignItems::Center
                    padding: Edges::symmetric(SPACE_SM, SPACE_MD)
                    background: init_dark_p.get_background().with_alpha(236)
                }
                text {
                    style: {
                        flex_shrink: 1.0
                        min_width: Length::Px(0.0)
                        font_size: 14.0
                        font_weight: FontWeight::SemiBold
                        color: init_dark_p.get_foreground()
                        text_overflow: TextOverflow::Ellipsis
                    }
                    { title_str }
                }
                row {
                    bind_style: exit_btn_style
                    role: A11yRole::Button
                    a11y_label: BTN_EXIT_FULLSCREEN.to_string()
                    on_click: on_exit
                    canvas {
                        caching: CanvasCaching::RetainedStatic
                        style: {
                            width: Length::Px(FS_EXIT_ICON_SIZE)
                            height: Length::Px(FS_EXIT_ICON_SIZE)
                        }
                        painter: paint_exit_icon
                    }
                }
            }
            { fs_viewport }
            row {
                node_ref: bottom_bar_id
                style: {
                    width: Length::Percent(100.0)
                    flex_shrink: 0.0
                    padding: Edges::symmetric(SPACE_SM, SPACE_MD)
                    background: init_dark_p.get_background().with_alpha(232)
                    border: (init_dark_g.get_border_width(), init_dark_p.get_border())
                }
                EuvActionRow {
                    { ctrl_left }
                    { ctrl_up }
                    { ctrl_down }
                    { ctrl_right }
                    { extra_ctrl }
                }
            }
        }
    };
    let handle: SceneHandle = ctx.get_scene().clone();
    let safe_sig: Signal<SafeAreaInsets> = ctx.get_safe_area();
    let prev_fs: Rc<Cell<bool>> = Rc::new(Cell::new(false));
    let fs_acquired: Rc<Cell<bool>> = Rc::new(Cell::new(false));
    let fs_cleanup: Rc<Cell<bool>> = Rc::clone(&fs_acquired);
    let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        on_cleanup(move || {
            if fs_cleanup.replace(false) {
                exit_fullscreen();
            }
        });
        let _: Effect = EuvOverlayStack::bind_signal(open, FS_OVERLAY_NAME, Z_INDEX_FULLSCREEN);
        Effect::new(move || {
            let is_open: bool = open.get();
            let safe: SafeAreaInsets = safe_sig.get();
            if is_open != prev_fs.replace(is_open) {
                if is_open {
                    if !fs_acquired.replace(true) {
                        enter_fullscreen();
                    }
                } else if fs_acquired.replace(false) {
                    exit_fullscreen();
                }
            }
            handle.with_write(
                |tree: &mut SceneTree| {
                    tree.cancel_subtree_transitions(portal);
                    if let Some(n) = tree.get_node_mut(inline_viewport) {
                        let mut st: Style = n.get_style();
                        st.get_mut_layout().set_display(if is_open {
                            Display::None
                        } else {
                            Display::Flex
                        });
                        n.set_style(st);
                        tree.mark_dirty(inline_viewport, DIRTY_LAYOUT | DIRTY_PAINT);
                    }
                    if let Some(n) = tree.get_node_mut(portal) {
                        let mut st: Style = n.get_style();
                        st.get_mut_layout().set_display(if is_open {
                            Display::Flex
                        } else {
                            Display::None
                        });
                        st.get_mut_paint()
                            .set_pointer_events(if is_open {
                                PointerEvents::Auto
                            } else {
                                PointerEvents::None
                            })
                            .set_opacity(1.0)
                            .set_transform(Transform2D::default());
                        n.set_style(st);
                    }
                    if let Some(n) = tree.get_node_mut(top_bar_id) {
                        let mut st: Style = n.get_style();
                        st.get_mut_layout().set_padding(Edges::new(
                            SPACE_SM + safe.get_top(),
                            SPACE_MD + safe.get_right(),
                            SPACE_SM,
                            SPACE_MD + safe.get_left(),
                        ));
                        n.set_style(st);
                        tree.mark_dirty(top_bar_id, DIRTY_LAYOUT | DIRTY_PAINT);
                    }
                    if let Some(n) = tree.get_node_mut(bottom_bar_id) {
                        let mut st: Style = n.get_style();
                        st.get_mut_layout().set_padding(Edges::new(
                            SPACE_SM,
                            SPACE_MD + safe.get_right(),
                            SPACE_SM + safe.get_bottom(),
                            SPACE_MD + safe.get_left(),
                        ));
                        n.set_style(st);
                        tree.mark_dirty(bottom_bar_id, DIRTY_LAYOUT | DIRTY_PAINT);
                    }
                    let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
                    tree.mark_dirty(portal, dirty_l | DIRTY_PAINT);
                },
                (),
            );
        })
    });
    ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(portal, scope);
        },
        (),
    );
    portal
}

/// Mounts a press-and-hold cross-platform touch/mouse game control button driving `code` on `host`.
///
/// # Arguments
///
/// - `&str` - Button label.
/// - `&GameEngineHost` - Target game host.
/// - `KeyCode` - Virtual key bound to this button.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted holdable button node.
pub(crate) fn mount_game_control_button(
    label: &str,
    host: &GameEngineHost,
    code: KeyCode,
    tokens: ThemeTokens,
) -> NodeId {
    let host_down: GameEngineHost = host.clone();
    let host_up: GameEngineHost = host.clone();
    let host_click: GameEngineHost = host.clone();
    let on_click = move |_: &UiEvent| {
        pulse_game_key(&host_click, code);
    };
    let node: NodeId = view! {
        EuvButton {
            label: label.to_string()
            tokens: tokens
            outline()
            sm()
            on_click: on_click
        }
    };
    let mut ctx: Context = current_context();
    ctx.on_pointer_down(node, move |_: &UiEvent| {
        host_down.press_key(code);
    });
    ctx.on_pointer_up(node, move |_: &UiEvent| {
        host_up.release_key(code);
    });
    node
}

/// Pulses a single directional or action key on `host` for mobile touch/button game controls.
///
/// # Arguments
///
/// - `&GameEngineHost` - Target game engine host.
/// - `KeyCode` - Virtual key code to pulse.
pub(crate) fn pulse_game_key(host: &GameEngineHost, code: KeyCode) {
    host.press_key(code);
    let _: u32 = host.step(0.08);
    host.release_key(code);
}