diff --git a/src/builder/validator/buttoninfo.rs b/src/builder/validator/buttoninfo.rs index 719ea60..67353e4 100644 --- a/src/builder/validator/buttoninfo.rs +++ b/src/builder/validator/buttoninfo.rs @@ -179,10 +179,10 @@ impl ButtonInfo { if let Some(background_type) = &mut button_info.normal { match background_type { - FillTypeInfo::Image(_, fill_type) => { + FillTypeInfo::Image(_) => {} + FillTypeInfo::Color(_, fill_type) => { *fill_type = button_info.background_fill_type; } - FillTypeInfo::Color(_) => (), FillTypeInfo::Element(_, fill_type) => { *fill_type = button_info.background_fill_type; } @@ -191,10 +191,10 @@ impl ButtonInfo { if let Some(background_type) = &mut button_info.selected { match background_type { - FillTypeInfo::Image(_, fill_type) => { + FillTypeInfo::Image(_) => {} + FillTypeInfo::Color(_, fill_type) => { *fill_type = button_info.background_fill_type; } - FillTypeInfo::Color(_) => (), FillTypeInfo::Element(_, fill_type) => { *fill_type = button_info.background_fill_type; } diff --git a/src/builder/validator/iconinfo.rs b/src/builder/validator/iconinfo.rs index 57415e9..3dcebd8 100644 --- a/src/builder/validator/iconinfo.rs +++ b/src/builder/validator/iconinfo.rs @@ -99,10 +99,10 @@ impl IconInfo { if let Some(background_type) = &mut icon_info.background_type { match background_type { - FillTypeInfo::Image(_, fill_type) => { + FillTypeInfo::Image(_) => {} + FillTypeInfo::Color(_, fill_type) => { *fill_type = icon_info.background_fill_type; } - FillTypeInfo::Color(_) => (), FillTypeInfo::Element(_, fill_type) => { *fill_type = icon_info.background_fill_type; } diff --git a/src/builder/validator/textfieldinfo.rs b/src/builder/validator/textfieldinfo.rs index 37b75f1..2367d19 100644 --- a/src/builder/validator/textfieldinfo.rs +++ b/src/builder/validator/textfieldinfo.rs @@ -93,10 +93,10 @@ impl TextFieldInfo { if let Some(background_type) = &mut text_field_info.background_type { match background_type { - FillTypeInfo::Image(_, fill_type) => { + FillTypeInfo::Image(_) => {} + FillTypeInfo::Color(_, fill_type) => { *fill_type = text_field_info.background_fill_type; } - FillTypeInfo::Color(_) => (), FillTypeInfo::Element(_, fill_type) => { *fill_type = text_field_info.background_fill_type; } diff --git a/src/builder/validator/validator.rs b/src/builder/validator/validator.rs index 9420cbc..fac567c 100644 --- a/src/builder/validator/validator.rs +++ b/src/builder/validator/validator.rs @@ -527,10 +527,10 @@ pub fn cow_to_fill_type_info<'a>(cow: Cow<'a, [u8]>) -> FillTypeInfo { let text = cow_to_str(cow); match Color::try_from(text.as_str()) { - Ok(color) => FillTypeInfo::Color(color), + Ok(color) => FillTypeInfo::Color(color, DisplayableFillType::Expand), Err(_) => match from_str(text.as_str()) { Ok(descriptor) => FillTypeInfo::Element(descriptor, DisplayableFillType::Expand), - Err(_) => FillTypeInfo::Image(AssetPath::from(text), DisplayableFillType::Expand), + Err(_) => FillTypeInfo::Image(AssetPath::from(text)), }, } } diff --git a/src/elements/button.rs b/src/elements/button.rs index 45f184e..72f9236 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -1,6 +1,5 @@ use crate::{ - builder::validator::buttoninfo::ButtonInfo, - guihandler::gui::{displayable::DisplayableFillType, iconizable::IconizablePositioning}, + builder::validator::buttoninfo::ButtonInfo, guihandler::gui::iconizable::IconizablePositioning, prelude::*, }; @@ -113,10 +112,7 @@ impl ButtonBuilder { framable.clone(), match self.normal { Some(info) => info, - None => FillTypeInfo::from(( - gui_handler.menu_button().clone(), - DisplayableFillType::Expand, - )), + None => FillTypeInfo::from(gui_handler.menu_button().clone()), }, )?; @@ -124,10 +120,7 @@ impl ButtonBuilder { framable.clone(), match self.selected { Some(info) => info, - None => FillTypeInfo::from(( - gui_handler.menu_button_selected().clone(), - DisplayableFillType::Expand, - )), + None => FillTypeInfo::from(gui_handler.menu_button_selected().clone()), }, )?; diff --git a/src/elements/fill_type.rs b/src/elements/fill_type.rs index f7bbc1c..5f09733 100644 --- a/src/elements/fill_type.rs +++ b/src/elements/fill_type.rs @@ -13,38 +13,38 @@ use std::sync::{ #[derive(Debug, Clone)] pub enum FillTypeInfo { - Image(AssetPath, DisplayableFillType), - Color(Color), + Image(AssetPath), + Color(Color, DisplayableFillType), Element(ElementDescriptor, DisplayableFillType), } -impl From for FillTypeInfo { - fn from(color: Color) -> Self { - Self::Color(color) +impl From<(Color, DisplayableFillType)> for FillTypeInfo { + fn from((color, fill_type): (Color, DisplayableFillType)) -> Self { + Self::Color(color, fill_type) } } -impl From<(String, DisplayableFillType)> for FillTypeInfo { - fn from((s, fill_type): (String, DisplayableFillType)) -> Self { - Self::Image(AssetPath::from(s), fill_type) +impl From for FillTypeInfo { + fn from(s: String) -> Self { + Self::Image(AssetPath::from(s)) } } -impl From<(&str, DisplayableFillType)> for FillTypeInfo { - fn from((s, fill_type): (&str, DisplayableFillType)) -> Self { - Self::Image(AssetPath::from(s), fill_type) +impl From<&str> for FillTypeInfo { + fn from(s: &str) -> Self { + Self::Image(AssetPath::from(s)) } } -impl From<(&String, DisplayableFillType)> for FillTypeInfo { - fn from((s, fill_type): (&String, DisplayableFillType)) -> Self { - Self::Image(AssetPath::from(s.as_str()), fill_type) +impl From<&String> for FillTypeInfo { + fn from(s: &String) -> Self { + Self::Image(AssetPath::from(s.as_str())) } } -impl From<(AssetPath, DisplayableFillType)> for FillTypeInfo { - fn from((assetpath, fill_type): (AssetPath, DisplayableFillType)) -> Self { - Self::Image(assetpath, fill_type) +impl From for FillTypeInfo { + fn from(assetpath: AssetPath) -> Self { + Self::Image(assetpath) } } @@ -59,12 +59,14 @@ impl InnerFillType { FillTypeInfo::Element(descriptor, fill_type) => Ok(InnerFillType::Image( Displayable::new(framable, descriptor, fill_type)?, )), - FillTypeInfo::Image(path, fill_type) => Ok(InnerFillType::Image(Displayable::new( - framable, path, fill_type, + FillTypeInfo::Image(path) => Ok(InnerFillType::Image(Displayable::new( + framable, + path, + DisplayableFillType::Expand, + )?)), + FillTypeInfo::Color(color, fill_type) => Ok(InnerFillType::Color(Colorable::new( + framable, color, fill_type, )?)), - FillTypeInfo::Color(color) => { - Ok(InnerFillType::Color(Colorable::new(framable, color)?)) - } } } @@ -155,7 +157,7 @@ impl FillType { impl PartialEq for FillType { fn eq(&self, info: &FillTypeInfo) -> bool { match (&self.inner, info) { - (InnerFillType::Image(inner_image), FillTypeInfo::Image(info_image, _)) => { + (InnerFillType::Image(inner_image), FillTypeInfo::Image(info_image)) => { match inner_image.inner_type() { DisplayableType::Path(path) => path == *info_image, DisplayableType::Descriptor(_) => false, @@ -167,7 +169,7 @@ impl PartialEq for FillType { DisplayableType::Descriptor(descriptor) => descriptor == *info_descriptor, } } - (InnerFillType::Color(inner_color), FillTypeInfo::Color(info_color)) => { + (InnerFillType::Color(inner_color), FillTypeInfo::Color(info_color, _)) => { inner_color.color() == *info_color } diff --git a/src/elements/multi_line_textfield.rs b/src/elements/multi_line_textfield.rs index 443467b..88c7f7b 100644 --- a/src/elements/multi_line_textfield.rs +++ b/src/elements/multi_line_textfield.rs @@ -1,6 +1,7 @@ use crate::{ builder::validator::multi_line_text_field_info::MultiLineTextFieldInfo, - guihandler::gui::writeable::ModifyText, prelude::*, + guihandler::gui::{displayable::DisplayableFillType, writeable::ModifyText}, + prelude::*, }; use std::sync::{ @@ -247,9 +248,11 @@ impl MultiLineTextField { self.writeable.set_activation_changed(move |active| { if let Some(me) = weak_self.upgrade() { if active { - me.grid.set_background(focus_color)?; + me.grid + .set_background((focus_color, DisplayableFillType::Expand))?; } else { - me.grid.set_background(normal_color)?; + me.grid + .set_background((normal_color, DisplayableFillType::Expand))?; } } diff --git a/src/guihandler/gui/colorable.rs b/src/guihandler/gui/colorable.rs index 3692777..505daf8 100644 --- a/src/guihandler/gui/colorable.rs +++ b/src/guihandler/gui/colorable.rs @@ -10,6 +10,8 @@ use cgmath::{vec4, Vector4}; use std::sync::atomic::{AtomicI32, Ordering::SeqCst}; use std::sync::{Arc, RwLock}; +use super::displayable::DisplayableFillType; + /// `Colorable` gives the ability to fill an area with a color pub struct Colorable { framable: Arc, @@ -18,6 +20,7 @@ pub struct Colorable { buffer: Arc>, color: RwLock, + fill_type: DisplayableFillType, ui_layer: AtomicI32, @@ -29,7 +32,11 @@ pub struct Colorable { impl Colorable { /// Factory method for `Colorable`, returns `Arc`. - pub fn new(framable: Arc, color: Color) -> Result> { + pub fn new( + framable: Arc, + color: Color, + fill_type: DisplayableFillType, + ) -> Result> { let set = framable.gui_handler().color_descriptor(color)?; let buffer = Buffer::builder() @@ -45,6 +52,7 @@ impl Colorable { buffer, color: RwLock::new(color), + fill_type, ui_layer: AtomicI32::new(0), @@ -144,11 +152,25 @@ impl Colorable { pub fn update_frame(&self) -> Result<()> { let mut frame = self.buffer.map_complete()?; - let x_start = self.framable.left() as f32; - let y_start = self.framable.top() as f32; + let mut x_start = self.framable.left() as f32; + let mut y_start = self.framable.top() as f32; - let width = (self.framable.right() - self.framable.left()) as f32; - let height = (self.framable.bottom() - self.framable.top()) as f32; + let mut width = (self.framable.right() - self.framable.left()) as f32; + let mut height = (self.framable.bottom() - self.framable.top()) as f32; + + if let DisplayableFillType::Square = self.fill_type { + if width > height { + let diff = width - height; + + x_start += diff * 0.5; + width = height; + } else if height > width { + let diff = height - width; + + y_start += diff * 0.5; + height = width; + } + } let left = x_start + width * *self.left_factor.read().unwrap(); let right = x_start + width * *self.right_factor.read().unwrap();