From 672c0f00ba70bd551f46da75d27709b84e9a15dd Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 19 Jan 2023 13:05:48 +0100 Subject: [PATCH] Improve setting background --- src/elements/fill_type.rs | 15 ++++++++ src/elements/grid.rs | 14 +------- src/elements/icon.rs | 14 +------- src/elements/label.rs | 5 +-- src/elements/mod.rs | 36 ++++++++++++++++++- src/elements/textfield.rs | 57 +++++++------------------------ src/guihandler/gui/displayable.rs | 10 +++++- 7 files changed, 74 insertions(+), 77 deletions(-) diff --git a/src/elements/fill_type.rs b/src/elements/fill_type.rs index 6d11525..3eb7b88 100644 --- a/src/elements/fill_type.rs +++ b/src/elements/fill_type.rs @@ -145,6 +145,21 @@ impl FillType { } } +impl PartialEq for FillType { + fn eq(&self, info: &FillTypeInfo) -> bool { + match (&self.inner, info) { + (InnerFillType::Image(inner_image), FillTypeInfo::Image(info_image)) => { + inner_image.path() == info_image + } + (InnerFillType::Color(inner_color), FillTypeInfo::Color(info_color)) => { + inner_color.color() == *info_color + } + + _ => false, + } + } +} + impl Drop for FillType { fn drop(&mut self) { if self.visible.load(SeqCst) { diff --git a/src/elements/grid.rs b/src/elements/grid.rs index f46cef5..49e4277 100644 --- a/src/elements/grid.rs +++ b/src/elements/grid.rs @@ -149,19 +149,7 @@ impl Grid { } pub fn set_background(&self, background: impl Into) -> Result<()> { - let background = FillType::new(self.framable.clone(), background.into())?; - - if self.framable.is_framed() { - background.update_frame()?; - } - - if self.visible.load(SeqCst) { - background.enable()?; - } - - *self.background.write().unwrap() = Some(background); - - Ok(()) + super::set_background(self.visible(), &self.framable, &self.background, background) } pub(crate) fn background(&self) -> RwLockReadGuard<'_, Option> { diff --git a/src/elements/icon.rs b/src/elements/icon.rs index 1023786..ef93291 100644 --- a/src/elements/icon.rs +++ b/src/elements/icon.rs @@ -126,19 +126,7 @@ impl Icon { } pub fn set_background(&self, background: impl Into) -> Result<()> { - let background = FillType::new(self.framable.clone(), background.into())?; - - if self.framable.is_framed() { - background.update_frame()?; - } - - if self.visible() { - background.enable()?; - } - - *self.background.write().unwrap() = Some(background); - - Ok(()) + super::set_background(self.visible(), &self.framable, &self.background, background) } pub fn clear_background(&self) { diff --git a/src/elements/label.rs b/src/elements/label.rs index 45ec4f8..2f2b6ac 100644 --- a/src/elements/label.rs +++ b/src/elements/label.rs @@ -124,10 +124,7 @@ impl Label { } pub fn set_background(&self, background: impl Into) -> Result<()> { - *self.background.write().unwrap() = - Some(FillType::new(self.framable.clone(), background.into())?); - - Ok(()) + super::set_background(self.visible(), &self.framable, &self.background, background) } pub fn try_from(label_info: &LabelInfo, gui_handler: &Arc) -> Result> { diff --git a/src/elements/mod.rs b/src/elements/mod.rs index a63a8c7..e68fcaa 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -2,11 +2,13 @@ use crate::{guihandler::gui::iconizable::IconBuilderType, prelude::*}; use anyhow::Result; use std::sync::{ atomic::{AtomicU32, Ordering::SeqCst}, - Arc, Mutex, + Arc, Mutex, RwLock, }; use utilities::prelude::*; use vulkan_rs::prelude::*; +use self::fill_type::FillType; + pub mod button; pub mod fill_type; pub mod grid; @@ -322,3 +324,35 @@ impl Drop for IconizableWrapper { } } } + +pub(crate) fn set_background( + visible: bool, + framable: &Arc, + current: &RwLock>, + new: impl Into, +) -> Result<()> { + let mut current_background = current.write().unwrap(); + + let fill_info = new.into(); + + let update = match &*current_background { + Some(bg) => *bg != fill_info, + None => true, + }; + + if update { + let fill_type = FillType::new(framable.clone(), fill_info)?; + + if framable.is_framed() { + fill_type.update_frame()?; + } + + if visible { + fill_type.enable()?; + } + + *current_background = Some(fill_type); + } + + Ok(()) +} diff --git a/src/elements/textfield.rs b/src/elements/textfield.rs index b106222..9225624 100644 --- a/src/elements/textfield.rs +++ b/src/elements/textfield.rs @@ -4,7 +4,7 @@ use utilities::prelude::*; use std::sync::{ atomic::{AtomicBool, Ordering::SeqCst}, - Arc, Mutex, + Arc, RwLock, }; use super::fill_type::{FillType, InnerFillType}; @@ -51,7 +51,7 @@ impl TextFieldBuilder { pub fn build(self, gui_handler: Arc) -> Result> { let framable = Framable::new(gui_handler.clone(), false)?; - let background = Mutex::new( + let background = RwLock::new( self.background .clone() .map(|info| FillType::new(framable.clone(), info)) @@ -101,7 +101,7 @@ pub struct TextField { text_changed_executable: Arc>>, - background: Mutex>, + background: RwLock>, visible: AtomicBool, } @@ -141,7 +141,7 @@ impl TextField { } fn update_color_change(self: &Arc) { - if let Some(background) = &*self.background.lock().unwrap() { + if let Some(background) = &*self.background.read().unwrap() { if let InnerFillType::Color(colorable) = &background.inner { let normal_color = colorable.color(); let focus_color = normal_color * 1.4; @@ -151,12 +151,12 @@ impl TextField { self.writeable.set_activation_changed(move |active| { if let Some(me) = weak_self.upgrade() { if active { - if let Some(background) = &*me.background.lock().unwrap() { + if let Some(background) = &*me.background.read().unwrap() { if let InnerFillType::Color(colorable) = &background.inner { colorable.set_color(focus_color)?; } } - } else if let Some(background) = &*me.background.lock().unwrap() { + } else if let Some(background) = &*me.background.read().unwrap() { if let InnerFillType::Color(colorable) = &background.inner { colorable.set_color(normal_color)?; } @@ -195,40 +195,7 @@ impl TextField { } pub fn set_background(self: &Arc, background: impl Into) -> Result<()> { - { - let mut current_background = self.background.lock().unwrap(); - let new_background: FillTypeInfo = background.into(); - - match current_background.as_mut() { - Some(current_background) => match (¤t_background.inner, &new_background) { - (InnerFillType::Image(displayable), FillTypeInfo::Image(path)) => { - displayable.set_image(path.clone())?; - } - (InnerFillType::Color(colorable), FillTypeInfo::Color(new_color)) => { - colorable.set_color(*new_color)?; - } - - _ => { - let ft = FillType::new(self.framable.clone(), new_background)?; - - if self.visible() { - ft.enable()?; - } - - *current_background = ft; - } - }, - None => { - let ft = FillType::new(self.framable.clone(), new_background)?; - - if self.visible() { - ft.enable()?; - } - - *current_background = Some(ft); - } - } - } + super::set_background(self.visible(), &self.framable, &self.background, background)?; self.update_color_change(); @@ -236,7 +203,7 @@ impl TextField { } pub fn background_color(&self) -> Option { - if let Some(background) = self.background.lock().unwrap().as_ref() { + if let Some(background) = self.background.read().unwrap().as_ref() { if let InnerFillType::Color(colorable) = &background.inner { return Some(colorable.color()); } @@ -262,7 +229,7 @@ impl TextField { self.textable.delete()?; self.writeable.delete()?; - if let Some(background) = self.background.lock().unwrap().as_ref() { + if let Some(background) = self.background.read().unwrap().as_ref() { background.disable()?; } @@ -298,7 +265,7 @@ impl Gridable for TextField { self.textable.update_text()?; - if let Some(background) = self.background.lock().unwrap().as_ref() { + if let Some(background) = self.background.read().unwrap().as_ref() { background.update_frame()?; } @@ -320,7 +287,7 @@ impl Gridable for TextField { self.textable.set_ui_layer(layer); self.writeable.set_ui_layer(layer); - if let Some(background) = self.background.lock().unwrap().as_ref() { + if let Some(background) = self.background.read().unwrap().as_ref() { background.set_ui_layer(layer); } @@ -342,7 +309,7 @@ impl Visibility for TextField { self.textable.add()?; self.writeable.add()?; - if let Some(background) = self.background.lock().unwrap().as_ref() { + if let Some(background) = self.background.read().unwrap().as_ref() { background.enable()?; } } else { diff --git a/src/guihandler/gui/displayable.rs b/src/guihandler/gui/displayable.rs index f26c4b8..b7e9aac 100644 --- a/src/guihandler/gui/displayable.rs +++ b/src/guihandler/gui/displayable.rs @@ -23,6 +23,8 @@ pub struct Displayable { buffer: Arc>, + path: AssetPath, + ui_layer: AtomicI32, left_factor: RwLock, @@ -44,7 +46,7 @@ impl Displayable { /// * `framable` is a `Arc` instance /// * `name` is the name for a png pub fn new(framable: Arc, name: AssetPath) -> Result> { - let descriptor_set = framable.gui_handler().image_descriptor(name)?; + let descriptor_set = framable.gui_handler().image_descriptor(name.clone())?; let buffer = Buffer::builder() .set_usage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT) @@ -57,6 +59,8 @@ impl Displayable { descriptor_set: RwLock::new(descriptor_set), buffer, + path: name, + ui_layer: AtomicI32::new(0), left_factor: RwLock::new(0.0), @@ -103,6 +107,10 @@ impl Displayable { .delete_displayable(self.ui_layer.load(SeqCst), self) } + pub fn path(&self) -> &AssetPath { + &self.path + } + pub fn set_ui_layer(&self, ui_layer: i32) { self.ui_layer.store(ui_layer, SeqCst); }