Start reworking ui to use ecs

This commit is contained in:
hodasemi 2025-03-03 20:01:17 +01:00
parent 83ab6d73c6
commit 6d19f9bceb
4 changed files with 45 additions and 38 deletions

View file

@ -116,7 +116,7 @@ impl ButtonBuilder {
self
}
pub fn build(self, gui_handler: Arc<GuiHandler>) -> Result<Arc<Button>> {
pub fn build(self, gui_handler: Arc<GuiHandler<'_>>) -> Result<Arc<Button<'_>>> {
let framable = Framable::new(gui_handler.clone(), false)?;
let normal = FillType::new(
@ -150,7 +150,7 @@ impl ButtonBuilder {
let clickable = Clickable::new(framable.clone(), click_executable.clone());
let selectable = Selectable::new(
gui_handler,
&gui_handler,
click_executable.clone(),
select_executable.clone(),
on_select_executable.clone(),
@ -253,10 +253,10 @@ pub enum ButtonSelectMode {
Bigger,
}
pub struct Button {
pub struct Button<'a> {
clickable: Arc<Clickable>,
hoverable: Arc<Hoverable>,
selectable: Arc<Selectable>,
selectable: Arc<Selectable<'a>>,
framable: Arc<Framable>,
iconizable: IconizableWrapper,
textable: TextableWrapper,
@ -280,7 +280,7 @@ pub struct Button {
visible: AtomicBool,
}
impl Button {
impl<'a> Button<'a> {
pub fn builder() -> ButtonBuilder {
ButtonBuilder {
icon: None,
@ -366,7 +366,10 @@ impl Button {
self.textable.set_text_color(text_color)
}
pub fn try_from(button_info: &ButtonInfo, gui_handler: &Arc<GuiHandler>) -> Result<Arc<Self>> {
pub fn try_from(
button_info: &ButtonInfo,
gui_handler: &Arc<GuiHandler<'a>>,
) -> Result<Arc<Self>> {
let mut button_builder = Button::builder()
.set_select_mode(button_info.select_mode)
.set_isolate(button_info.isolate);
@ -423,7 +426,7 @@ impl Button {
}
}
impl GuiElementTraits for Button {
impl<'a> GuiElementTraits for Button<'a> {
fn gridable(&self) -> Option<&dyn Gridable> {
Some(self)
}
@ -432,12 +435,12 @@ impl GuiElementTraits for Button {
Some(self)
}
fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
fn downcast<'b>(&'b self) -> Option<GuiElement<'b>> {
Some(GuiElement::Button(self))
}
}
impl Visibility for Button {
impl<'a> Visibility for Button<'a> {
fn visible(&self) -> bool {
self.visible.load(SeqCst)
}
@ -469,7 +472,7 @@ impl Visibility for Button {
}
}
impl Gridable for Button {
impl<'a> Gridable for Button<'a> {
fn set_frame(
&self,
x: i32,
@ -494,7 +497,7 @@ impl Gridable for Button {
Ok(())
}
fn selectable(&self) -> Option<&Arc<Selectable>> {
fn selectable(&self) -> Option<&Arc<Selectable<'_>>> {
Some(&self.selectable)
}
@ -522,7 +525,7 @@ impl Gridable for Button {
}
}
impl Drop for Button {
impl<'a> Drop for Button<'a> {
fn drop(&mut self) {
if self.visible.load(SeqCst) {
self.disable_base().unwrap();
@ -531,7 +534,7 @@ impl Drop for Button {
}
// private
impl Button {
impl<'a> Button<'a> {
// fn create_hovered_changed_callback(button: Arc<Button>) -> Result<()> {
// let button_weak = Arc::downgrade(&button);
@ -554,7 +557,7 @@ impl Button {
// .set_hovered_changed_callback(Some(hovered_changed))
// }
fn create_clicked_changed_callback(button: Arc<Button>) {
fn create_clicked_changed_callback(button: Arc<Button<'a>>) {
let button_weak = Arc::downgrade(&button);
let clicked_changed = Box::new(move || {
@ -576,7 +579,7 @@ impl Button {
.set_clicked_changed_callback(Some(clicked_changed));
}
fn create_selected_changed_callback(button: Arc<Button>) {
fn create_selected_changed_callback(button: Arc<Button<'a>>) {
let button_weak = Arc::downgrade(&button);
let selected_changed = move |selected| {
@ -697,8 +700,8 @@ impl Button {
}
}
impl Deref for Button {
type Target = Arc<Selectable>;
impl<'a> Deref for Button<'a> {
type Target = Arc<Selectable<'a>>;
fn deref(&self) -> &Self::Target {
&self.selectable

View file

@ -12,8 +12,8 @@ use std::sync::{
/// `Selectable` gives the ability to navigate per button or controller to
/// optionally adjacent neighbour Selectables and to execute a closure
/// when the current Selectable is pressed
pub struct Selectable {
gui_handler: Arc<GuiHandler>,
pub struct Selectable<'a> {
selected: AtomicBool,
@ -32,33 +32,33 @@ pub struct Selectable {
isolate: bool,
// used when clicked
executable: Arc<Executable<()>>,
executable: Arc<Executable<&'a mut World>>,
// used internally by button
selected_changed_executable: Arc<Executable<bool>>,
selected_changed_executable: Arc<Executable<(&'a mut World, bool)>>,
// exposed externally for event
on_select_executable: Arc<Executable<bool>>,
on_select_executable: Arc<Executable<(&'mut World, bool)>>,
// used for custom buttons
custom_callback: RwLock<Option<Box<dyn Fn(ControllerButton) -> Result<bool> + Send + Sync>>>,
custom_callback: RwLock<Option<Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>>>,
}
impl Selectable {
impl<'a> Selectable<'a> {
/// Factory method for `Selectable`, returns `Arc<Selectable>`.
///
/// # Arguments
///
/// * `executable` is a `Arc<Executable>` instance
pub fn new(
gui_handler: Arc<GuiHandler>,
gui_handler: &'a GuiHandler<'a>,
executable: Arc<Executable<()>>,
selected_changed_executable: Arc<Executable<bool>>,
on_select_executable: Arc<Executable<bool>>,
isolate: bool,
) -> Arc<Self> {
Arc::new(Selectable {
gui_handler,
selected: AtomicBool::new(false),

View file

@ -182,7 +182,7 @@ impl TextToScreen {
}
}
pub struct GuiHandler {
pub struct GuiHandler<'a> {
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
@ -241,18 +241,18 @@ pub struct GuiHandler {
current_writeable: RwLock<Option<Arc<Writeable>>>,
current_hoverable: RwLock<Option<Arc<Hoverable>>>,
current_clickable: RwLock<Option<Arc<Clickable>>>,
current_selectable: RwLock<Option<Arc<Selectable>>>,
current_selectable: RwLock<Option<Arc<Selectable<'a>>>>,
text_sample_count: VkSampleCountFlags,
callback_list: Mutex<Vec<Box<dyn FnOnce() -> Result<()> + Send + Sync>>>,
}
impl GuiHandler {
impl<'a> GuiHandler<'a> {
pub fn new(
gui_handler_create_info: GuiHandlerCreateInfo<'_>,
context: &impl ContextInterface,
) -> Result<Arc<GuiHandler>> {
) -> Result<Arc<Self>> {
let device = context.device();
let queue = context.queue();
@ -724,7 +724,7 @@ impl GuiHandler {
Ok(false)
}
pub fn current_selectable(&self) -> Result<Option<Arc<Selectable>>> {
pub fn current_selectable(&self) -> Result<Option<Arc<Selectable<'a>>>> {
match self.current_selectable.read().unwrap().as_ref() {
Some(selectable) => Ok(Some(selectable.clone())),
None => Ok(None),
@ -1064,7 +1064,7 @@ macro_rules! remove_element {
}
// object handling
impl GuiHandler {
impl<'a> GuiHandler<'a> {
// framable
pub(crate) fn add_framable(&self, layer: i32, framable: Arc<Framable>) -> Result<()> {
add_element!(self.layers, layer, framable);
@ -1101,13 +1101,17 @@ impl GuiHandler {
}
// selectable
pub(crate) fn add_selectable(&self, layer: i32, selectable: Arc<Selectable>) -> Result<()> {
pub(crate) fn add_selectable(&self, layer: i32, selectable: Arc<Selectable<'a>>) -> Result<()> {
add_element!(self.layers, layer, selectable);
Ok(())
}
pub(crate) fn delete_selectable(&self, layer: i32, selectable: &Arc<Selectable>) -> Result<()> {
pub(crate) fn delete_selectable(
&self,
layer: i32,
selectable: &Arc<Selectable<'a>>,
) -> Result<()> {
let mut current_selectable = self.current_selectable.write().unwrap();
if current_selectable.is_some() {
// unwrap is safe, just tested for `is_some`
@ -1232,7 +1236,7 @@ impl GuiHandler {
Ok(())
}
pub(crate) fn set_selectable(&self, selectable: Option<Arc<Selectable>>) -> Result<()> {
pub(crate) fn set_selectable(&self, selectable: Option<Arc<Selectable<'a>>>) -> Result<()> {
let mut current_selectable = self.current_selectable.write().unwrap();
if let Some(selectable) = current_selectable.as_ref() {
@ -1268,7 +1272,7 @@ impl GuiHandler {
}
// private - create rendering stuff
impl GuiHandler {
impl<'a> GuiHandler<'a> {
fn create_render_targets(
device: &Arc<Device>,
target_images: &TargetMode<Vec<Arc<Image>>>,
@ -1594,7 +1598,7 @@ impl GuiHandler {
}
}
impl GuiHandler {
impl<'a> GuiHandler<'a> {
pub fn process(
&self,
buffer_recorder: &mut CommandBufferRecorder<'_>,

View file

@ -32,7 +32,7 @@ pub struct Keyboard {
}
impl Keyboard {
pub fn new(gui_handler: &Arc<GuiHandler>) -> Result<Arc<Self>> {
pub fn new(gui_handler: &Arc<GuiHandler<'a>>) -> Result<Arc<Self>> {
let text_field_gui: Arc<GuiBuilder> =
GuiBuilder::from_str(gui_handler, include_str!("text_field.xml"))?;