Simplify gui creation

This commit is contained in:
hodasemi 2025-02-28 06:26:57 +01:00
parent 79879d1ae7
commit 17dd562408
4 changed files with 56 additions and 86 deletions

View file

@ -54,10 +54,10 @@ pub struct ButtonInfo {
background_fill_type: DisplayableFillType, background_fill_type: DisplayableFillType,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
pub click_sound: AssetPath, pub click_sound: Option<AssetPath>,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
pub hover_sound: AssetPath, pub hover_sound: Option<AssetPath>,
pub text: RwLock<String>, pub text: RwLock<String>,
pub text_color: Color, pub text_color: Color,
@ -98,16 +98,12 @@ impl ButtonInfo {
background_fill_type: DisplayableFillType::Expand, background_fill_type: DisplayableFillType::Expand,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
click_sound: match Self::find_click_sound(grid) { click_sound: Self::find_click_sound(grid)
Some(sound) => sound, .or(gui_handler.click_sound().map(|p| p.clone())),
None => gui_handler.click_sound().clone(),
},
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
hover_sound: match Self::find_hover_sound(grid) { hover_sound: Self::find_hover_sound(grid)
Some(sound) => sound, .or(gui_handler.hover_sound().map(|p| p.clone())),
None => gui_handler.hover_sound().clone(),
},
text: RwLock::default(), text: RwLock::default(),
text_color: Color::Black, text_color: Color::Black,
@ -143,10 +139,10 @@ impl ButtonInfo {
} }
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
b"click_sound" => button_info.click_sound = cow_to_path(attribute.value), b"click_sound" => button_info.click_sound = Some(cow_to_path(attribute.value)),
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
b"hover_sound" => button_info.hover_sound = cow_to_path(attribute.value), b"hover_sound" => button_info.hover_sound = Some(cow_to_path(attribute.value)),
b"select_mode" => { b"select_mode" => {
button_info.select_mode = cow_to_button_select_mode(attribute.value)? button_info.select_mode = cow_to_button_select_mode(attribute.value)?
@ -176,7 +172,7 @@ impl ButtonInfo {
return Err(anyhow::Error::msg(format!( return Err(anyhow::Error::msg(format!(
"Unsupported attribute in Button: {}", "Unsupported attribute in Button: {}",
from_utf8(attribute.key.into_inner())? from_utf8(attribute.key.into_inner())?
))) )));
} }
} }
} }

View file

@ -3,23 +3,23 @@ use crate::{
gui_handler::gui::iconizable::IconizablePositioning, prelude::*, gui_handler::gui::iconizable::IconizablePositioning, prelude::*,
}; };
use anyhow::Result; use anyhow::{Result, anyhow};
use assetpath::AssetPath; use assetpath::AssetPath;
use utilities::prelude::*; use utilities::prelude::*;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use super::{ use super::{
IconBuilderType,
fill_type::{FillType, InnerFillType}, fill_type::{FillType, InnerFillType},
wrapper::{IconizableWrapper, TextableWrapper}, wrapper::{IconizableWrapper, TextableWrapper},
IconBuilderType,
}; };
use cgmath::{vec2, vec4}; use cgmath::{vec2, vec4};
use std::{ use std::{
ops::Deref, ops::Deref,
sync::{ sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc, Mutex, Arc, Mutex,
atomic::{AtomicBool, Ordering::SeqCst},
}, },
}; };
@ -121,18 +121,14 @@ impl ButtonBuilder {
let normal = FillType::new( let normal = FillType::new(
framable.clone(), framable.clone(),
match self.normal { self.normal
Some(info) => info, .ok_or(anyhow!("normal button layout not set!"))?,
None => FillTypeInfo::from(gui_handler.menu_button().clone()),
},
)?; )?;
let selected = FillType::new( let selected = FillType::new(
framable.clone(), framable.clone(),
match self.selected { self.selected
Some(info) => info, .ok_or(anyhow!("selected button layout not set!"))?,
None => FillTypeInfo::from(gui_handler.menu_button_selected().clone()),
},
)?; )?;
let click_executable = Executable::new(&gui_handler); let click_executable = Executable::new(&gui_handler);
@ -393,13 +389,13 @@ impl Button {
} }
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
if !button_info.click_sound.is_empty() { if let Some(click_sound) = &button_info.click_sound {
button_builder = button_builder.set_click_sound(button_info.click_sound.clone()); button_builder = button_builder.set_click_sound(click_sound.clone());
} }
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
if !button_info.hover_sound.is_empty() { if let Some(hover_sound) = &button_info.hover_sound {
button_builder = button_builder.set_hover_sound(button_info.hover_sound.clone()); button_builder = button_builder.set_hover_sound(hover_sound.clone());
} }
button_builder = button_builder.set_icon( button_builder = button_builder.set_icon(

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View file

@ -37,42 +37,34 @@ impl Default for Font<'_> {
#[derive(Deserialize, Serialize, Clone, Debug, Default)] #[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct GuiHandlerCreateInfo<'a> { pub struct GuiHandlerCreateInfo<'a> {
// default button textures
pub menu_button: AssetPath,
pub menu_button_selected: AssetPath,
// path to the alphabet image // path to the alphabet image
#[serde(borrow)] #[serde(borrow)]
pub font: Font<'a>, pub font: Font<'a>,
// sound info // sound info
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
pub click_sound: AssetPath, pub click_sound: Option<AssetPath>,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
pub hover_sound: AssetPath, pub hover_sound: Option<AssetPath>,
// resource base directory // resource base directory
pub resource_directory: AssetPath, pub resource_directory: Option<AssetPath>,
} }
impl<'a> GuiHandlerCreateInfo<'a> { impl<'a> GuiHandlerCreateInfo<'a> {
pub const fn new() -> Self { pub const fn new() -> Self {
GuiHandlerCreateInfo { GuiHandlerCreateInfo {
// default button textures
menu_button: AssetPath::new(),
menu_button_selected: AssetPath::new(),
// path to the alphabet image // path to the alphabet image
font: Font::Path(AssetPath::new()), font: Font::Bytes(include_bytes!("default_font.png")),
// sound info // sound info
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
click_sound: AssetPath::new(), click_sound: None,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
hover_sound: AssetPath::new(), hover_sound: None,
// resource base directory // resource base directory
resource_directory: AssetPath::new(), resource_directory: None,
} }
} }
} }
@ -232,14 +224,11 @@ pub struct GuiHandler {
on_selected: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>, on_selected: RwLock<Option<Box<dyn Fn() -> Result<()> + Send + Sync>>>,
menu_button: AssetPath, #[cfg(feature = "audio")]
menu_button_selected: AssetPath, click_sound: Option<AssetPath>,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
click_sound: AssetPath, hover_sound: Option<AssetPath>,
#[cfg(feature = "audio")]
hover_sound: AssetPath,
// ----- gui handling ----- // ----- gui handling -----
layers: Mutex<Vec<(i32, Elements)>>, layers: Mutex<Vec<(i32, Elements)>>,
@ -315,38 +304,35 @@ impl GuiHandler {
width: AtomicU32::new(context.width()), width: AtomicU32::new(context.width()),
height: AtomicU32::new(context.height()), height: AtomicU32::new(context.height()),
menu_button: {
let mut menu_button = gui_handler_create_info.menu_button;
menu_button.set_prefix(&gui_handler_create_info.resource_directory.full_path());
menu_button
},
menu_button_selected: {
let mut menu_button_selected = gui_handler_create_info.menu_button_selected;
menu_button_selected
.set_prefix(&gui_handler_create_info.resource_directory.full_path());
menu_button_selected
},
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
click_sound: { click_sound: gui_handler_create_info.click_sound.map(|mut click_sound| {
let mut click_sound = gui_handler_create_info.click_sound; click_sound.set_prefix(
click_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); &gui_handler_create_info
.resource_directory
.as_ref()
.expect("missing resource directory")
.full_path(),
);
click_sound click_sound
}, }),
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
hover_sound: { hover_sound: gui_handler_create_info.hover_sound.map(|mut hover_sound| {
let mut hover_sound = gui_handler_create_info.hover_sound; hover_sound.set_prefix(
hover_sound.set_prefix(&gui_handler_create_info.resource_directory.full_path()); &gui_handler_create_info
.resource_directory
.as_ref()
.expect("missing resource directory")
.full_path(),
);
hover_sound hover_sound
}, }),
resource_base_path: gui_handler_create_info.resource_directory, resource_base_path: gui_handler_create_info
.resource_directory
.expect("missing resource directory"),
top_ui: RwLock::new(None), top_ui: RwLock::new(None),
tooltip_ui: RwLock::new(None), tooltip_ui: RwLock::new(None),
@ -589,22 +575,14 @@ impl GuiHandler {
&self.resource_base_path &self.resource_base_path
} }
pub fn menu_button(&self) -> &AssetPath { #[cfg(feature = "audio")]
&self.menu_button pub fn click_sound(&self) -> Option<&AssetPath> {
} self.click_sound.as_ref()
pub fn menu_button_selected(&self) -> &AssetPath {
&self.menu_button_selected
} }
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
pub fn click_sound(&self) -> &AssetPath { pub fn hover_sound(&self) -> Option<&AssetPath> {
&self.click_sound self.hover_sound.as_ref()
}
#[cfg(feature = "audio")]
pub fn hover_sound(&self) -> &AssetPath {
&self.hover_sound
} }
pub fn set_on_selected_event<F>(&self, f: F) -> Result<()> pub fn set_on_selected_event<F>(&self, f: F) -> Result<()>