Skip to content

Commit

Permalink
Functioning resume/quit buttons in pause menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Jun 10, 2024
1 parent efcc7ae commit 7ad5f1d
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 116 deletions.
12 changes: 6 additions & 6 deletions rs/engine/src/mats/fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub type Matter = ExtendedMaterial<StandardMaterial, DistanceDither>;
pub const BAYER_HANDLE: Handle<Image> =
Handle::weak_from_u128(92299220200241619468604683494190943784);


/// Function instead of a constant because it uses floating-point math.
#[inline(always)]
pub fn max_fog_distance() -> f32 {
Expand Down Expand Up @@ -133,22 +132,23 @@ impl AsMut<DistanceDither> for DistanceDither {
impl<B, E> AsRef<DistanceDither> for ExtendedMaterial<B, E>
where
B: Material,
E: MaterialExtension + AsRef<DistanceDither> {
E: MaterialExtension + AsRef<DistanceDither>,
{
fn as_ref(&self) -> &DistanceDither {
self.extension.as_ref()
}
}

impl<B, E> AsMut<DistanceDither> for ExtendedMaterial<B, E>
where
B: Material,
E: MaterialExtension + AsMut<DistanceDither> {
where
B: Material,
E: MaterialExtension + AsMut<DistanceDither>,
{
fn as_mut(&mut self) -> &mut DistanceDither {
self.extension.as_mut()
}
}


impl Default for DistanceDither {
fn default() -> Self {
Self::new(
Expand Down
121 changes: 62 additions & 59 deletions rs/engine/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
anim::{AnimationPlugin, AnimationController, ComponentDelta, StartAnimation},
anim::{AnimationController, AnimationPlugin, ComponentDelta, StartAnimation},
entity_tree,
input::InputState,
mats::{
Expand Down Expand Up @@ -27,6 +27,7 @@ use bevy::{
ecs::{
query::{QueryEntityError, QuerySingleError},
schedule::SystemConfigs,
system::EntityCommands,
},
input::common_conditions::input_toggle_active,
pbr::ExtendedMaterial,
Expand All @@ -43,9 +44,11 @@ use leafwing_input_manager::{prelude::*, Actionlike};
use meshtext::{MeshGenerator, QualitySettings};
use rapier3d::{geometry::SharedShape, math::Point};
use serde::{Deserialize, Serialize};
use std::{collections::VecDeque, f64::consts::TAU};
use std::ops::{Add, Mul};
use bevy::ecs::system::EntityCommands;
use std::{
collections::VecDeque,
f64::consts::TAU,
ops::{Add, Mul},
};

pub mod a11y;
#[cfg(feature = "debugging")]
Expand Down Expand Up @@ -82,50 +85,49 @@ impl Plugin for UiPlugin {
},
);

app
.add_plugins((
InputManagerPlugin::<UiAction>::default(),
AnimationPlugin::<Fade>::default(),
))
.register_type::<MenuStack>()
.register_type::<UiCam>()
.register_type::<Fade>()
.init_resource::<ActionState<UiAction>>()
.insert_resource(UiAction::default_mappings())
.init_resource::<UiHovered>()
.init_resource::<TextMeshCache>()
.init_asset::<Font3d>()
.register_asset_loader(Font3dLoader)
.add_systems(Startup, setup)
.add_systems(Update, (reset_hovered, show_fps, focus::resolve_focus))
.add_systems(
PostUpdate,
(
layout::apply_constraints,
highlight_focus::<GLOBAL_UI_LAYER>,
widgets::InteractHandlers::system,
),
)
.add_systems(
Last,
(
Prev::<Button3d>::update_component,
hide_orphaned_popups,
propagate_fade::<UiMat>.before(Fade::hide_faded_out),
Fade::hide_faded_out,
anchor_follow_menu,
CuboidPanel::<UiMat>::sync,
Text3d::sync,
),
)
.insert_gizmo_group(
focus::FocusGizmos::<GLOBAL_UI_LAYER>,
GizmoConfig {
line_width: 6.0,
render_layers: GLOBAL_UI_RENDER_LAYERS,
..default()
},
);
app.add_plugins((
InputManagerPlugin::<UiAction>::default(),
AnimationPlugin::<Fade>::default(),
))
.register_type::<MenuStack>()
.register_type::<UiCam>()
.register_type::<Fade>()
.init_resource::<ActionState<UiAction>>()
.insert_resource(UiAction::default_mappings())
.init_resource::<UiHovered>()
.init_resource::<TextMeshCache>()
.init_asset::<Font3d>()
.register_asset_loader(Font3dLoader)
.add_systems(Startup, setup)
.add_systems(Update, (reset_hovered, show_fps, focus::resolve_focus))
.add_systems(
PostUpdate,
(
layout::apply_constraints,
highlight_focus::<GLOBAL_UI_LAYER>,
widgets::InteractHandlers::system,
),
)
.add_systems(
Last,
(
Prev::<Button3d>::update_component,
hide_orphaned_popups,
propagate_fade::<UiMat>.before(Fade::hide_faded_out),
Fade::hide_faded_out,
anchor_follow_menu,
CuboidPanel::<UiMat>::sync,
Text3d::sync,
),
)
.insert_gizmo_group(
focus::FocusGizmos::<GLOBAL_UI_LAYER>,
GizmoConfig {
line_width: 6.0,
render_layers: GLOBAL_UI_RENDER_LAYERS,
..default()
},
);
}

fn finish(&self, app: &mut App) {
Expand Down Expand Up @@ -336,6 +338,7 @@ impl UiAction {
// KB & Mouse
(Ok, Space.into()),
(Ok, Backspace.into()),
(Ok, Enter.into()),
(Back, Backspace.into()),
(MoveCursor, VirtualDPad::wasd().into()),
(MoveCursor, VirtualDPad::arrow_keys().into()),
Expand Down Expand Up @@ -611,14 +614,16 @@ pub fn anchor_follow_menu(
}
}

use crate::{
anim::{AnimationHandle, Delta, DynAnimation},
ui::widgets::new_unlit_material,
};
#[cfg(feature = "debugging")]
use bevy_inspector_egui::{
inspector_options::std_options::NumberDisplay::Slider,
prelude::{InspectorOptions, ReflectInspectorOptions},
};
use web_time::Duration;
use crate::anim::{AnimationHandle, Delta, DynAnimation};
use crate::ui::widgets::new_unlit_material;

/// Component that starts a new branch of a tree of entities that can be
/// faded in an out together.
Expand Down Expand Up @@ -647,10 +652,8 @@ pub struct Fade(
impl Fade {
pub const ZERO: Self = Self(0.0);
pub const ONE: Self = Self(1.0);

pub fn hide_faded_out(
mut q: Query<(&Self, &mut Visibility), Changed<Self>>,
) {

pub fn hide_faded_out(mut q: Query<(&Self, &mut Visibility), Changed<Self>>) {
for (fade, mut vis) in &mut q {
let new = if fade.0 <= 0.0 {
Visibility::Hidden
Expand All @@ -672,23 +675,23 @@ impl Default for Fade {

impl Diff for Fade {
type Delta = Self;

fn delta_from(&self, rhs: &Self) -> Self::Delta {
Self(self.0 - rhs.0)
}
}

impl Mul<f32> for Fade {
type Output = Self;

fn mul(self, rhs: f32) -> Self::Output {
Self(self.0 * rhs)
}
}

impl Add for Fade {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
Expand Down Expand Up @@ -751,7 +754,7 @@ pub fn propagate_fade<M: Asset + AsMut<DitherFade>>(
pub trait FadeCommands {
fn fade_in(&mut self, duration: Duration) -> AnimationHandle<DynAnimation<Fade>>;
fn fade_out(&mut self, duration: Duration) -> AnimationHandle<DynAnimation<Fade>>;

fn fade_in_secs(&mut self, secs: f32) -> AnimationHandle<DynAnimation<Fade>> {
self.fade_in(Duration::from_secs_f32(secs))
}
Expand All @@ -776,7 +779,7 @@ impl FadeCommands for EntityCommands<'_> {
ComponentDelta::diffable(id, t, Fade(t))
})
}

fn fade_out(&mut self, duration: Duration) -> AnimationHandle<DynAnimation<Fade>> {
let mut elapsed = Duration::ZERO;
let duration = duration.as_secs_f32();
Expand Down
12 changes: 7 additions & 5 deletions rs/engine/src/ui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl LineUpChildren {
}
}

pub fn with_additional_spacing(self, spacing: f32) -> Self {
pub fn with_spacing(self, spacing: f32) -> Self {
Self {
relative_positions: self.relative_positions.normalize() * (1.0 + spacing),
align: self.align,
Expand Down Expand Up @@ -84,7 +84,7 @@ pub fn apply_constraints(
1 => {
match transforms.get_mut(children[0]) {
Ok(mut child) => child.1.translation = Vec3::ZERO,
Err(e) => cmds.debug_components(id, e),
Err(e) => cmds.debug_components(children[0], e),
}
continue;
}
Expand All @@ -95,7 +95,8 @@ pub fn apply_constraints(
let [a, b] = match transforms.get_many([pair[0], pair[1]]) {
Ok(pair) => pair,
Err(e) => {
cmds.debug_components(id, e);
cmds.debug_components(pair[0], e);
cmds.debug_components(pair[1], e);
continue;
}
};
Expand All @@ -116,7 +117,7 @@ pub fn apply_constraints(
let mut first_child = match transforms.get_mut(children[0]) {
Ok(child) => child,
Err(e) => {
cmds.debug_components(id, e);
cmds.debug_components(children[0], e);
continue;
}
};
Expand All @@ -125,7 +126,8 @@ pub fn apply_constraints(
let [a, mut b] = match transforms.get_many_mut([pair[0], pair[1]]) {
Ok(pair) => pair,
Err(e) => {
cmds.debug_components(id, e);
cmds.debug_components(pair[0], e);
cmds.debug_components(pair[1], e);
continue;
}
};
Expand Down
11 changes: 6 additions & 5 deletions rs/engine/src/ui/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{
mats::{fade::DitherFade, fog::DistanceDither},
todo_warn,
ui::{a11y::AKNode, TextMeshCache, UiAction, UiCam, UiMat, GLOBAL_UI_RENDER_LAYERS},
util::Prev,
};
use bevy::{
a11y::accesskit::{NodeBuilder, Role},
ecs::system::{EntityCommand, EntityCommands},
pbr::ExtendedMaterial,
prelude::*,
render::{
mesh::{Indices, PrimitiveTopology::TriangleList},
Expand All @@ -24,10 +26,7 @@ use std::{
ops::ControlFlow,
sync::Arc,
};
use bevy::pbr::ExtendedMaterial;
use web_time::Duration;
use crate::mats::fade::DitherFade;
use crate::mats::fog::DistanceDither;

#[derive(Component, Clone, Deref, DerefMut)]
pub struct WidgetShape(pub SharedShape);
Expand Down Expand Up @@ -424,6 +423,7 @@ impl Text3d {
) {
let mut to_try = to_retry.drain().chain(&changed_text).collect::<Vec<_>>();
for id in to_try {
debug!(?id);
let Ok((this, font, mut ak_node)) = q.get_mut(id) else {
to_retry.insert(id);
continue;
Expand All @@ -446,7 +446,7 @@ impl Text3d {
if !fonts.contains(font) {
warn!("{font:?} does not (yet) exist. Retrying next frame...");
to_retry.insert(id);
return;
continue;
}
let Some((mesh, shape)) = cache
.entry((text.clone(), xform_key, font.clone()))
Expand Down Expand Up @@ -496,9 +496,10 @@ impl Text3d {
})
.clone()
else {
error!("Failed to generate text mesh");
error!(?text, "Failed to generate text mesh");
continue;
};
debug!(?id, ?mesh, ?shape);
cmds.insert((mesh, shape));
}
}
Expand Down
25 changes: 18 additions & 7 deletions rs/engine/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,20 @@ macro_rules! state_matches {
/// ```
#[macro_export]
macro_rules! entity_tree {
($cmds:ident; ( $($bundles:expr),* $(,)? $(; #children: $($children:tt),* $(,)?)? )) => {
$cmds.spawn((
$($bundles),*
))$(.with_children(|cmds| {
$(entity_tree!(cmds; $children);)*
}))?
($cmds:ident; ( $($bundles:expr),* $(,)? $(=> |$then_cmds:ident| $then:block)? $(; #children: $($children:tt),* $(,)?)? )) => {
{
let mut cmds = $cmds.spawn((
$($bundles),*
));
$({
let mut $then_cmds = &mut cmds;
$then;
};)?
$(cmds.with_children(|cmds| {
$(entity_tree!(cmds; $children);)*
});)?
cmds
}
}
}

Expand All @@ -972,7 +980,7 @@ pub fn debug_component_names(In((id, msg)): In<(Entity, String)>, world: &mut Wo
.into_iter()
.map(ComponentInfo::name)
.collect::<Vec<_>>();
debug!(?components, "{msg}");
debug!(?id, ?components, "{msg}");
}

/// [error_component_names]
Expand All @@ -988,6 +996,9 @@ pub fn error_component_names(In((id, msg)): In<(Entity, String)>, world: &mut Wo
error!(?components, "{msg}");
}

// TODO: These could be macros for more flexibility and so the target
// isn't always `sond_has_engine::util`, but it seems like the arguments
// would be moderately complicated.
pub trait LogComponentNames {
/// Run [debug_component_names]
fn debug_components(&mut self, id: Entity, msg: impl std::fmt::Display);
Expand Down
Loading

0 comments on commit 7ad5f1d

Please sign in to comment.