1use super::*;
2
3pub fn mount_game_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
14 let host: GameEngineHost = state.get_game_host().clone();
15 let host_btn: GameEngineHost = host.clone();
16 let host_wave: GameEngineHost = host.clone();
17 let viewport: NodeId = host.mount_viewport(
18 Style::default()
19 .width(Length::Percent(100.0))
20 .height(Length::Percent(100.0)),
21 );
22 let vp_sig: Signal<Size> = use_viewport_size();
23 let vp_frame_style = move || -> Style {
24 let live: ThemeTokens = use_live_tokens(tokens);
25 let h: f32 = adaptive_viewport_height(vp_sig.get().get_height());
26 Style::default()
27 .column()
28 .width(Length::Percent(100.0))
29 .height(Length::Px(h))
30 .overflow(Overflow::Hidden)
31 .border(
32 live.get_geometry().get_border_width(),
33 live.get_palette().get_border(),
34 )
35 .radius(live.get_geometry().get_radius_md())
36 };
37 let viewport_frame: NodeId = view! {
38 column {
39 bind_style: vp_frame_style
40 { viewport }
41 }
42 };
43 let fs_2d_open: Signal<bool> = Signal::new(false);
44 let fs_2d_portal: NodeId =
45 mount_game_fullscreen_portal(fs_2d_open, &host, viewport, None, GAME_CARD_TITLE, tokens);
46 let on_step_physics = move |_: &UiEvent| {
47 let _: u32 = host_btn.step(0.051);
48 };
49 let on_spawn_wave = move |_: &UiEvent| {
50 host_wave.ingest_event(&WindowEvent::KeyDown(
51 WindowId::new(1),
52 KeyEvent::new(KeyCode::Character('r'), Modifiers::default(), false),
53 ));
54 let _: u32 = host_wave.step(1.0 / 60.0);
55 host_wave.ingest_event(&WindowEvent::KeyUp(
56 WindowId::new(1),
57 KeyEvent::new(KeyCode::Character('r'), Modifiers::default(), false),
58 ));
59 };
60 let on_open_fs = move |_: &UiEvent| {
61 let _: bool = fs_2d_open.set_if_changed(true);
62 };
63 let hint_style = move || -> Style {
64 let live: ThemeTokens = use_live_tokens(tokens);
65 Style::default()
66 .width(Length::Percent(100.0))
67 .font_size(12.0)
68 .font_family(FontFamily::Monospace)
69 .color(live.get_palette().get_muted_foreground())
70 };
71 let ctrl_left: NodeId =
72 mount_game_control_button(BTN_GAME_LEFT, &host, KeyCode::ArrowLeft, false, tokens);
73 let ctrl_up: NodeId =
74 mount_game_control_button(BTN_GAME_UP, &host, KeyCode::ArrowUp, false, tokens);
75 let ctrl_down: NodeId =
76 mount_game_control_button(BTN_GAME_DOWN, &host, KeyCode::ArrowDown, false, tokens);
77 let ctrl_right: NodeId =
78 mount_game_control_button(BTN_GAME_RIGHT, &host, KeyCode::ArrowRight, false, tokens);
79 let ctrl_dash: NodeId =
80 mount_game_control_button(BTN_GAME_DASH, &host, KeyCode::Character('e'), true, tokens);
81 let arena_card: NodeId = view! {
82 EuvCard {
83 title: GAME_CARD_TITLE.to_string()
84 tokens: tokens
85 subtitle: GAME_CARD_SUB
86 { viewport_frame }
87 text {
88 bind_style: hint_style
89 { GAME_HUD_CONTROLS_HINT }
90 }
91 }
92 };
93 let sim_card: NodeId = view! {
94 EuvCard {
95 title: GAME_SIM_CARD_TITLE.to_string()
96 tokens: tokens
97 subtitle: GAME_SIM_CARD_SUB
98 EuvActionRow {
99 EuvButton {
100 label: BTN_STEP_PHYSICS.to_string()
101 tokens: tokens
102 sm()
103 on_click: on_step_physics
104 }
105 EuvButton {
106 label: BTN_SPAWN_WAVE.to_string()
107 tokens: tokens
108 outline()
109 sm()
110 on_click: on_spawn_wave
111 }
112 EuvButton {
113 label: BTN_GAME_FULLSCREEN.to_string()
114 tokens: tokens
115 secondary()
116 sm()
117 on_click: on_open_fs
118 }
119 }
120 }
121 };
122 let input_card: NodeId = view! {
123 EuvCard {
124 title: GAME_INPUT_CARD_TITLE.to_string()
125 tokens: tokens
126 subtitle: GAME_INPUT_CARD_SUB
127 EuvActionRow {
128 { ctrl_left }
129 { ctrl_up }
130 { ctrl_down }
131 { ctrl_right }
132 { ctrl_dash }
133 }
134 }
135 };
136 let rail: NodeId = view! {
137 column {
138 style: {
139 width: Length::Percent(100.0)
140 gap: SPACE_MD
141 }
142 { sim_card }
143 { input_card }
144 }
145 };
146 let studio: NodeId = mount_studio_layout(arena_card, rail);
147 view! {
148 EuvPage {
149 title: GAME_TITLE.to_string()
150 subtitle: GAME_SUBTITLE.to_string()
151 tokens: tokens
152 { studio }
153 { fs_2d_portal }
154 }
155 }
156}
157
158pub(crate) fn mount_game_fullscreen_portal(
173 open: Signal<bool>,
174 host: &GameEngineHost,
175 inline_viewport: NodeId,
176 cam_mode_opt: Option<Signal<u8>>,
177 title: &str,
178 tokens: ThemeTokens,
179) -> NodeId {
180 let ctx: Context = current_context();
181 bind_fullscreen_context(&ctx);
182 let fs_viewport: NodeId = host.mount_viewport(
183 Style::default()
184 .flex_grow(1.0)
185 .flex_shrink(1.0)
186 .min_height(Length::Px(0.0))
187 .width(Length::Percent(100.0))
188 .overflow(Overflow::Hidden)
189 .stop_scroll_propagation(true)
190 .stop_event_propagation(true),
191 );
192 let ctrl_left: NodeId =
193 mount_game_control_button(FS_CTRL_LEFT, host, KeyCode::ArrowLeft, false, tokens);
194 let ctrl_up: NodeId = mount_game_control_button(
195 if cam_mode_opt.is_some() {
196 FS_CTRL_DRIVE
197 } else {
198 FS_CTRL_UP
199 },
200 host,
201 KeyCode::ArrowUp,
202 true,
203 tokens,
204 );
205 let ctrl_down: NodeId = mount_game_control_button(
206 if cam_mode_opt.is_some() {
207 FS_CTRL_REVERSE
208 } else {
209 FS_CTRL_DOWN
210 },
211 host,
212 KeyCode::ArrowDown,
213 false,
214 tokens,
215 );
216 let ctrl_right: NodeId =
217 mount_game_control_button(FS_CTRL_RIGHT, host, KeyCode::ArrowRight, false, tokens);
218 let extra_ctrl: NodeId = if let Some(cam_sig) = cam_mode_opt {
219 let on_cam = move |_: &UiEvent| {
220 let next: u8 = (cam_sig.get_untracked() + 1) % 4;
221 let _: bool = cam_sig.set_if_changed(next);
222 };
223 view! {
224 EuvButton {
225 label: FS_CTRL_CAMERA.to_string()
226 tokens: tokens
227 secondary()
228 sm()
229 on_click: on_cam
230 }
231 }
232 } else {
233 mount_game_control_button(FS_CTRL_DASH, host, KeyCode::Character('e'), false, tokens)
234 };
235 let on_exit = move |_: &UiEvent| {
236 let _: bool = open.set_if_changed(false);
237 };
238 let paint_exit_icon = move |bounds: Rect, dl: &mut DisplayList| {
239 draw_euv_icon(EuvIconName::Minimize, bounds, dl, Color::hex(0xFAFAFA));
240 };
241 let title_str: String = title.to_string();
242 let mut top_bar_id: NodeId = NodeId::default();
243 let mut bottom_bar_id: NodeId = NodeId::default();
244 let portal: NodeId = view! {
245 portal {
246 style: {
247 Style::default().column()
248 display: Display::None
249 position: Position::Fixed
250 inset: (Some(0.0), Some(0.0), Some(0.0), Some(0.0))
251 width: Length::Percent(100.0)
252 height: Length::Percent(100.0)
253 padding: Edges::all(0.0)
254 margin: Edges::all(0.0)
255 background: Color::hex(0x09090B)
256 border: (0.0, COLOR_TRANSPARENT)
257 radius: 0.0
258 overflow: Overflow::Hidden
259 pointer_events: PointerEvents::None
260 stop_scroll_propagation: true
261 stop_event_propagation: true
262 z_index: Z_INDEX_FULLSCREEN
263 }
264 row {
265 node_ref: top_bar_id
266 style: {
267 width: Length::Percent(100.0)
268 flex_shrink: 0.0
269 justify: JustifyContent::SpaceBetween
270 align: AlignItems::Center
271 padding: Edges::symmetric(SPACE_SM, SPACE_MD)
272 background: Color::rgba(9, 9, 11, 236)
273 }
274 text {
275 style: {
276 flex_shrink: 1.0
277 min_width: Length::Px(0.0)
278 font_size: 14.0
279 font_weight: FontWeight::SemiBold
280 color: Color::hex(0xFAFAFA)
281 text_overflow: TextOverflow::Ellipsis
282 }
283 { title_str }
284 }
285 row {
286 style: {
287 width: Length::Px(FS_EXIT_BTN_SIZE)
288 height: Length::Px(FS_EXIT_BTN_SIZE)
289 justify: JustifyContent::Center
290 align: AlignItems::Center
291 radius: tokens.get_geometry().get_radius_md()
292 background: Color::rgba(255, 255, 255, 24)
293 border: (1.0, Color::rgba(255, 255, 255, 44))
294 cursor: CursorIcon::Pointer
295 }
296 role: A11yRole::Button
297 a11y_label: BTN_EXIT_FULLSCREEN.to_string()
298 on_click: on_exit
299 canvas {
300 caching: CanvasCaching::RetainedStatic
301 style: {
302 width: Length::Px(FS_EXIT_ICON_SIZE)
303 height: Length::Px(FS_EXIT_ICON_SIZE)
304 }
305 painter: paint_exit_icon
306 }
307 }
308 }
309 { fs_viewport }
310 row {
311 node_ref: bottom_bar_id
312 style: {
313 width: Length::Percent(100.0)
314 flex_shrink: 0.0
315 padding: Edges::symmetric(SPACE_SM, SPACE_MD)
316 background: Color::rgba(9, 9, 11, 232)
317 }
318 EuvActionRow {
319 { ctrl_left }
320 { ctrl_up }
321 { ctrl_down }
322 { ctrl_right }
323 { extra_ctrl }
324 }
325 }
326 }
327 };
328 let handle: SceneHandle = ctx.get_scene().clone();
329 let safe_sig: Signal<SafeAreaInsets> = ctx.get_safe_area();
330 let prev_fs: Rc<Cell<bool>> = Rc::new(Cell::new(false));
331 let fs_acquired: Rc<Cell<bool>> = Rc::new(Cell::new(false));
332 let fs_cleanup: Rc<Cell<bool>> = Rc::clone(&fs_acquired);
333 let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
334 on_cleanup(move || {
335 if fs_cleanup.replace(false) {
336 exit_fullscreen();
337 }
338 });
339 let _: Effect = EuvOverlayStack::bind_signal(open, FS_OVERLAY_NAME, Z_INDEX_FULLSCREEN);
340 Effect::new(move || {
341 let is_open: bool = open.get();
342 let safe: SafeAreaInsets = safe_sig.get();
343 if is_open != prev_fs.replace(is_open) {
344 if is_open {
345 if !fs_acquired.replace(true) {
346 enter_fullscreen();
347 }
348 } else if fs_acquired.replace(false) {
349 exit_fullscreen();
350 }
351 }
352 handle.with_write(
353 |tree: &mut SceneTree| {
354 tree.cancel_subtree_transitions(portal);
355 if let Some(n) = tree.get_node_mut(inline_viewport) {
356 let mut st: Style = n.get_style();
357 st.get_mut_layout().set_display(if is_open {
358 Display::None
359 } else {
360 Display::Flex
361 });
362 n.set_style(st);
363 tree.mark_dirty(inline_viewport, DIRTY_LAYOUT | DIRTY_PAINT);
364 }
365 if let Some(n) = tree.get_node_mut(portal) {
366 let mut st: Style = n.get_style();
367 st.get_mut_layout().set_display(if is_open {
368 Display::Flex
369 } else {
370 Display::None
371 });
372 st.get_mut_paint()
373 .set_pointer_events(if is_open {
374 PointerEvents::Auto
375 } else {
376 PointerEvents::None
377 })
378 .set_opacity(1.0)
379 .set_transform(Transform2D::default());
380 n.set_style(st);
381 }
382 if let Some(n) = tree.get_node_mut(top_bar_id) {
383 let mut st: Style = n.get_style();
384 st.get_mut_layout().set_padding(Edges::new(
385 SPACE_SM + safe.get_top(),
386 SPACE_MD + safe.get_right(),
387 SPACE_SM,
388 SPACE_MD + safe.get_left(),
389 ));
390 n.set_style(st);
391 tree.mark_dirty(top_bar_id, DIRTY_LAYOUT | DIRTY_PAINT);
392 }
393 if let Some(n) = tree.get_node_mut(bottom_bar_id) {
394 let mut st: Style = n.get_style();
395 st.get_mut_layout().set_padding(Edges::new(
396 SPACE_SM,
397 SPACE_MD + safe.get_right(),
398 SPACE_SM + safe.get_bottom(),
399 SPACE_MD + safe.get_left(),
400 ));
401 n.set_style(st);
402 tree.mark_dirty(bottom_bar_id, DIRTY_LAYOUT | DIRTY_PAINT);
403 }
404 let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
405 tree.mark_dirty(portal, dirty_l | DIRTY_PAINT);
406 },
407 (),
408 );
409 })
410 });
411 ctx.get_scene().with_write(
412 |tree: &mut SceneTree| {
413 tree.attach_scope(portal, scope);
414 },
415 (),
416 );
417 portal
418}
419
420pub(crate) fn mount_game_control_button(
434 label: &str,
435 host: &GameEngineHost,
436 code: KeyCode,
437 primary: bool,
438 tokens: ThemeTokens,
439) -> NodeId {
440 let host_down: GameEngineHost = host.clone();
441 let host_up: GameEngineHost = host.clone();
442 let host_click: GameEngineHost = host.clone();
443 let on_click = move |_: &UiEvent| {
444 pulse_game_key(&host_click, code);
445 };
446 let node: NodeId = if primary {
447 view! {
448 EuvButton {
449 label: label.to_string()
450 tokens: tokens
451 sm()
452 on_click: on_click
453 }
454 }
455 } else {
456 view! {
457 EuvButton {
458 label: label.to_string()
459 tokens: tokens
460 outline()
461 sm()
462 on_click: on_click
463 }
464 }
465 };
466 let mut ctx: Context = current_context();
467 ctx.on_pointer_down(node, move |_: &UiEvent| {
468 host_down.press_key(code);
469 });
470 ctx.on_pointer_up(node, move |_: &UiEvent| {
471 host_up.release_key(code);
472 });
473 node
474}
475
476pub(crate) fn pulse_game_key(host: &GameEngineHost, code: KeyCode) {
483 host.press_key(code);
484 let _: u32 = host.step(0.08);
485 host.release_key(code);
486}