dash and wgui sounds

This commit is contained in:
Aleksander
2026-01-03 15:00:31 +01:00
parent 383bf3b11f
commit feba52c28f
34 changed files with 258 additions and 96 deletions

View File

@@ -10,6 +10,7 @@ use crate::{
text::{FontWeight, TextStyle, custom_glyph::CustomGlyphData},
util::centered_matrix,
},
sound::WguiSoundType,
widget::{
self, ConstructEssentials, EventResult, WidgetData,
label::{WidgetLabel, WidgetLabelParams},
@@ -270,6 +271,7 @@ fn register_event_mouse_enter(
listeners.register(
EventListenerKind::MouseEnter,
Box::new(move |common, event_data, (), ()| {
common.alterables.play_sound(WguiSoundType::ButtonMouseEnter);
common.alterables.trigger_haptics();
common.alterables.mark_redraw();
common
@@ -332,6 +334,7 @@ fn register_event_mouse_press(state: Rc<RefCell<State>>, listeners: &mut EventLi
);
common.alterables.trigger_haptics();
common.alterables.play_sound(WguiSoundType::ButtonPress);
common.alterables.mark_redraw();
if state.hovered {
@@ -362,6 +365,7 @@ fn register_event_mouse_release(
}
common.alterables.trigger_haptics();
common.alterables.play_sound(WguiSoundType::ButtonRelease);
common.alterables.mark_redraw();
if state.down {

View File

@@ -11,6 +11,7 @@ use crate::{
animation::{self, Animation},
i18n::I18n,
layout::{LayoutState, LayoutTask, WidgetID},
sound::WguiSoundType,
stack::{ScissorStack, Transform, TransformStack},
widget::{EventResult, WidgetData, WidgetObj},
};
@@ -140,6 +141,10 @@ impl EventAlterables {
pub fn animate(&mut self, animation: Animation) {
self.animations.push(animation);
}
pub fn play_sound(&mut self, sound_type: WguiSoundType) {
self.tasks.push(LayoutTask::PlaySound(sound_type));
}
}
pub struct CallbackDataCommon<'a> {

View File

@@ -1,6 +1,6 @@
use std::{
cell::{RefCell, RefMut},
collections::{HashMap, HashSet, VecDeque},
collections::{HashMap, HashSet},
io::Write,
rc::{Rc, Weak},
};
@@ -11,6 +11,8 @@ use crate::{
drawing::{self, ANSI_BOLD_CODE, ANSI_RESET_CODE, Boundary, push_scissor_stack, push_transform_stack},
event::{self, CallbackDataCommon, EventAlterables},
globals::WguiGlobals,
sound::WguiSoundType,
task::Tasks,
widget::{self, EventParams, EventResult, WidgetObj, WidgetState, WidgetStateFlags, div::WidgetDiv},
};
@@ -119,25 +121,24 @@ pub struct ModifyLayoutStateData<'a> {
pub layout: &'a mut Layout,
}
pub struct LayoutUpdateParams {
pub size: Vec2, // logical resolution
pub timestep_alpha: f32,
}
pub struct LayoutUpdateResult {
pub sounds_to_play: Vec<WguiSoundType>,
}
pub type ModifyLayoutStateFunc = Box<dyn Fn(ModifyLayoutStateData) -> anyhow::Result<()>>;
pub enum LayoutTask {
RemoveWidget(WidgetID),
ModifyLayoutState(ModifyLayoutStateFunc),
PlaySound(WguiSoundType),
}
#[derive(Clone)]
pub struct LayoutTasks(pub Rc<RefCell<VecDeque<LayoutTask>>>);
impl LayoutTasks {
fn new() -> Self {
Self(Rc::new(RefCell::new(VecDeque::new())))
}
pub fn push(&self, task: LayoutTask) {
self.0.borrow_mut().push_back(task);
}
}
pub type LayoutTasks = Tasks<LayoutTask>;
pub struct Layout {
pub state: LayoutState,
@@ -146,6 +147,7 @@ pub struct Layout {
components_to_refresh_once: HashSet<Component>,
registered_components_to_refresh: HashMap<taffy::NodeId, Component>,
sounds_to_play_once: Vec<WguiSoundType>,
pub widgets_to_tick: Vec<WidgetID>,
@@ -266,15 +268,12 @@ impl Layout {
}
pub fn get_parent(&self, widget_id: WidgetID) -> Option<(WidgetID, taffy::NodeId)> {
let Some(node_id) = self.state.nodes.get(widget_id) else {
return None;
};
let node_id = self.state.nodes.get(widget_id)?;
self.state.tree.parent(*node_id).map(|parent_id| {
let parent_widget_id = self.state.tree.get_node_context(parent_id).unwrap();
(*parent_widget_id, parent_id)
}
)
let parent_widget_id = self.state.tree.get_node_context(parent_id).unwrap();
(*parent_widget_id, parent_id)
})
}
fn collect_children_ids_recursive(&self, widget_id: WidgetID, out: &mut Vec<(WidgetID, taffy::NodeId)>) {
@@ -575,6 +574,7 @@ impl Layout {
registered_components_to_refresh: HashMap::new(),
widgets_to_tick: Vec::new(),
tasks: LayoutTasks::new(),
sounds_to_play_once: Vec::new(),
})
}
@@ -659,12 +659,17 @@ impl Layout {
Ok(())
}
pub fn update(&mut self, size: Vec2, timestep_alpha: f32) -> anyhow::Result<()> {
pub fn update(&mut self, params: &mut LayoutUpdateParams) -> anyhow::Result<LayoutUpdateResult> {
let mut alterables = EventAlterables::default();
self.animations.process(&self.state, &mut alterables, timestep_alpha);
self
.animations
.process(&self.state, &mut alterables, params.timestep_alpha);
self.process_alterables(alterables)?;
self.try_recompute_layout(size)?;
Ok(())
self.try_recompute_layout(params.size)?;
Ok(LayoutUpdateResult {
sounds_to_play: std::mem::take(&mut self.sounds_to_play_once),
})
}
pub fn tick(&mut self) -> anyhow::Result<()> {
@@ -677,8 +682,7 @@ impl Layout {
}
pub fn process_tasks(&mut self) -> anyhow::Result<()> {
let tasks = self.tasks.clone();
let mut tasks = tasks.0.borrow_mut();
let mut tasks = self.tasks.drain();
while let Some(task) = tasks.pop_front() {
match task {
LayoutTask::RemoveWidget(widget_id) => {
@@ -687,6 +691,11 @@ impl Layout {
LayoutTask::ModifyLayoutState(callback) => {
(*callback)(ModifyLayoutStateData { layout: self })?;
}
LayoutTask::PlaySound(sound) => {
if !self.sounds_to_play_once.contains(&sound) {
self.sounds_to_play_once.push(sound);
}
}
}
}

View File

@@ -34,6 +34,7 @@ pub mod i18n;
pub mod layout;
pub mod parser;
pub mod renderer_vk;
pub mod sound;
pub mod stack;
pub mod task;
pub mod widget;

6
wgui/src/sound.rs Normal file
View File

@@ -0,0 +1,6 @@
#[derive(Clone, Eq, PartialEq)]
pub enum WguiSoundType {
ButtonMouseEnter,
ButtonPress,
ButtonRelease,
}