use super::*;
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, false, tokens);
let ctrl_up: NodeId =
mount_game_control_button(BTN_GAME_UP, &host, KeyCode::ArrowUp, false, tokens);
let ctrl_down: NodeId =
mount_game_control_button(BTN_GAME_DOWN, &host, KeyCode::ArrowDown, false, tokens);
let ctrl_right: NodeId =
mount_game_control_button(BTN_GAME_RIGHT, &host, KeyCode::ArrowRight, false, tokens);
let ctrl_dash: NodeId =
mount_game_control_button(BTN_GAME_DASH, &host, KeyCode::Character('e'), true, 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
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
secondary()
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 }
}
}
}
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, false, 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,
true,
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,
false,
tokens,
);
let ctrl_right: NodeId =
mount_game_control_button(FS_CTRL_RIGHT, host, KeyCode::ArrowRight, false, 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
secondary()
sm()
on_click: on_cam
}
}
} else {
mount_game_control_button(FS_CTRL_DASH, host, KeyCode::Character('e'), false, tokens)
};
let on_exit = move |_: &UiEvent| {
let _: bool = open.set_if_changed(false);
};
let paint_exit_icon = move |bounds: Rect, dl: &mut DisplayList| {
draw_euv_icon(EuvIconName::Minimize, bounds, dl, Color::hex(0xFAFAFA));
};
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: Color::hex(0x09090B)
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: Color::rgba(9, 9, 11, 236)
}
text {
style: {
flex_shrink: 1.0
min_width: Length::Px(0.0)
font_size: 14.0
font_weight: FontWeight::SemiBold
color: Color::hex(0xFAFAFA)
text_overflow: TextOverflow::Ellipsis
}
{ title_str }
}
row {
style: {
width: Length::Px(FS_EXIT_BTN_SIZE)
height: Length::Px(FS_EXIT_BTN_SIZE)
justify: JustifyContent::Center
align: AlignItems::Center
radius: tokens.get_geometry().get_radius_md()
background: Color::rgba(255, 255, 255, 24)
border: (1.0, Color::rgba(255, 255, 255, 44))
cursor: CursorIcon::Pointer
}
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: Color::rgba(9, 9, 11, 232)
}
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
}
pub(crate) fn mount_game_control_button(
label: &str,
host: &GameEngineHost,
code: KeyCode,
primary: bool,
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 = if primary {
view! {
EuvButton {
label: label.to_string()
tokens: tokens
sm()
on_click: on_click
}
}
} else {
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
}
pub(crate) fn pulse_game_key(host: &GameEngineHost, code: KeyCode) {
host.press_key(code);
let _: u32 = host.step(0.08);
host.release_key(code);
}