Allow color to be squared

This commit is contained in:
hodasemi 2024-04-24 14:37:02 +02:00
parent ed3c470716
commit be011c467e
8 changed files with 72 additions and 52 deletions

View file

@ -179,10 +179,10 @@ impl ButtonInfo {
if let Some(background_type) = &mut button_info.normal { if let Some(background_type) = &mut button_info.normal {
match background_type { match background_type {
FillTypeInfo::Image(_, fill_type) => { FillTypeInfo::Image(_) => {}
FillTypeInfo::Color(_, fill_type) => {
*fill_type = button_info.background_fill_type; *fill_type = button_info.background_fill_type;
} }
FillTypeInfo::Color(_) => (),
FillTypeInfo::Element(_, fill_type) => { FillTypeInfo::Element(_, fill_type) => {
*fill_type = button_info.background_fill_type; *fill_type = button_info.background_fill_type;
} }
@ -191,10 +191,10 @@ impl ButtonInfo {
if let Some(background_type) = &mut button_info.selected { if let Some(background_type) = &mut button_info.selected {
match background_type { match background_type {
FillTypeInfo::Image(_, fill_type) => { FillTypeInfo::Image(_) => {}
FillTypeInfo::Color(_, fill_type) => {
*fill_type = button_info.background_fill_type; *fill_type = button_info.background_fill_type;
} }
FillTypeInfo::Color(_) => (),
FillTypeInfo::Element(_, fill_type) => { FillTypeInfo::Element(_, fill_type) => {
*fill_type = button_info.background_fill_type; *fill_type = button_info.background_fill_type;
} }

View file

@ -99,10 +99,10 @@ impl IconInfo {
if let Some(background_type) = &mut icon_info.background_type { if let Some(background_type) = &mut icon_info.background_type {
match background_type { match background_type {
FillTypeInfo::Image(_, fill_type) => { FillTypeInfo::Image(_) => {}
FillTypeInfo::Color(_, fill_type) => {
*fill_type = icon_info.background_fill_type; *fill_type = icon_info.background_fill_type;
} }
FillTypeInfo::Color(_) => (),
FillTypeInfo::Element(_, fill_type) => { FillTypeInfo::Element(_, fill_type) => {
*fill_type = icon_info.background_fill_type; *fill_type = icon_info.background_fill_type;
} }

View file

@ -93,10 +93,10 @@ impl TextFieldInfo {
if let Some(background_type) = &mut text_field_info.background_type { if let Some(background_type) = &mut text_field_info.background_type {
match background_type { match background_type {
FillTypeInfo::Image(_, fill_type) => { FillTypeInfo::Image(_) => {}
FillTypeInfo::Color(_, fill_type) => {
*fill_type = text_field_info.background_fill_type; *fill_type = text_field_info.background_fill_type;
} }
FillTypeInfo::Color(_) => (),
FillTypeInfo::Element(_, fill_type) => { FillTypeInfo::Element(_, fill_type) => {
*fill_type = text_field_info.background_fill_type; *fill_type = text_field_info.background_fill_type;
} }

View file

@ -527,10 +527,10 @@ pub fn cow_to_fill_type_info<'a>(cow: Cow<'a, [u8]>) -> FillTypeInfo {
let text = cow_to_str(cow); let text = cow_to_str(cow);
match Color::try_from(text.as_str()) { 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()) { Err(_) => match from_str(text.as_str()) {
Ok(descriptor) => FillTypeInfo::Element(descriptor, DisplayableFillType::Expand), Ok(descriptor) => FillTypeInfo::Element(descriptor, DisplayableFillType::Expand),
Err(_) => FillTypeInfo::Image(AssetPath::from(text), DisplayableFillType::Expand), Err(_) => FillTypeInfo::Image(AssetPath::from(text)),
}, },
} }
} }

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
builder::validator::buttoninfo::ButtonInfo, builder::validator::buttoninfo::ButtonInfo, guihandler::gui::iconizable::IconizablePositioning,
guihandler::gui::{displayable::DisplayableFillType, iconizable::IconizablePositioning},
prelude::*, prelude::*,
}; };
@ -113,10 +112,7 @@ impl ButtonBuilder {
framable.clone(), framable.clone(),
match self.normal { match self.normal {
Some(info) => info, Some(info) => info,
None => FillTypeInfo::from(( None => FillTypeInfo::from(gui_handler.menu_button().clone()),
gui_handler.menu_button().clone(),
DisplayableFillType::Expand,
)),
}, },
)?; )?;
@ -124,10 +120,7 @@ impl ButtonBuilder {
framable.clone(), framable.clone(),
match self.selected { match self.selected {
Some(info) => info, Some(info) => info,
None => FillTypeInfo::from(( None => FillTypeInfo::from(gui_handler.menu_button_selected().clone()),
gui_handler.menu_button_selected().clone(),
DisplayableFillType::Expand,
)),
}, },
)?; )?;

View file

@ -13,38 +13,38 @@ use std::sync::{
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum FillTypeInfo { pub enum FillTypeInfo {
Image(AssetPath, DisplayableFillType), Image(AssetPath),
Color(Color), Color(Color, DisplayableFillType),
Element(ElementDescriptor, DisplayableFillType), Element(ElementDescriptor, DisplayableFillType),
} }
impl From<Color> for FillTypeInfo { impl From<(Color, DisplayableFillType)> for FillTypeInfo {
fn from(color: Color) -> Self { fn from((color, fill_type): (Color, DisplayableFillType)) -> Self {
Self::Color(color) Self::Color(color, fill_type)
} }
} }
impl From<(String, DisplayableFillType)> for FillTypeInfo { impl From<String> for FillTypeInfo {
fn from((s, fill_type): (String, DisplayableFillType)) -> Self { fn from(s: String) -> Self {
Self::Image(AssetPath::from(s), fill_type) Self::Image(AssetPath::from(s))
} }
} }
impl From<(&str, DisplayableFillType)> for FillTypeInfo { impl From<&str> for FillTypeInfo {
fn from((s, fill_type): (&str, DisplayableFillType)) -> Self { fn from(s: &str) -> Self {
Self::Image(AssetPath::from(s), fill_type) Self::Image(AssetPath::from(s))
} }
} }
impl From<(&String, DisplayableFillType)> for FillTypeInfo { impl From<&String> for FillTypeInfo {
fn from((s, fill_type): (&String, DisplayableFillType)) -> Self { fn from(s: &String) -> Self {
Self::Image(AssetPath::from(s.as_str()), fill_type) Self::Image(AssetPath::from(s.as_str()))
} }
} }
impl From<(AssetPath, DisplayableFillType)> for FillTypeInfo { impl From<AssetPath> for FillTypeInfo {
fn from((assetpath, fill_type): (AssetPath, DisplayableFillType)) -> Self { fn from(assetpath: AssetPath) -> Self {
Self::Image(assetpath, fill_type) Self::Image(assetpath)
} }
} }
@ -59,12 +59,14 @@ impl InnerFillType {
FillTypeInfo::Element(descriptor, fill_type) => Ok(InnerFillType::Image( FillTypeInfo::Element(descriptor, fill_type) => Ok(InnerFillType::Image(
Displayable::new(framable, descriptor, fill_type)?, Displayable::new(framable, descriptor, fill_type)?,
)), )),
FillTypeInfo::Image(path, fill_type) => Ok(InnerFillType::Image(Displayable::new( FillTypeInfo::Image(path) => Ok(InnerFillType::Image(Displayable::new(
framable, path, fill_type, 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<FillTypeInfo> for FillType { impl PartialEq<FillTypeInfo> for FillType {
fn eq(&self, info: &FillTypeInfo) -> bool { fn eq(&self, info: &FillTypeInfo) -> bool {
match (&self.inner, info) { 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() { match inner_image.inner_type() {
DisplayableType::Path(path) => path == *info_image, DisplayableType::Path(path) => path == *info_image,
DisplayableType::Descriptor(_) => false, DisplayableType::Descriptor(_) => false,
@ -167,7 +169,7 @@ impl PartialEq<FillTypeInfo> for FillType {
DisplayableType::Descriptor(descriptor) => descriptor == *info_descriptor, 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 inner_color.color() == *info_color
} }

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
builder::validator::multi_line_text_field_info::MultiLineTextFieldInfo, builder::validator::multi_line_text_field_info::MultiLineTextFieldInfo,
guihandler::gui::writeable::ModifyText, prelude::*, guihandler::gui::{displayable::DisplayableFillType, writeable::ModifyText},
prelude::*,
}; };
use std::sync::{ use std::sync::{
@ -247,9 +248,11 @@ impl MultiLineTextField {
self.writeable.set_activation_changed(move |active| { self.writeable.set_activation_changed(move |active| {
if let Some(me) = weak_self.upgrade() { if let Some(me) = weak_self.upgrade() {
if active { if active {
me.grid.set_background(focus_color)?; me.grid
.set_background((focus_color, DisplayableFillType::Expand))?;
} else { } else {
me.grid.set_background(normal_color)?; me.grid
.set_background((normal_color, DisplayableFillType::Expand))?;
} }
} }

View file

@ -10,6 +10,8 @@ use cgmath::{vec4, Vector4};
use std::sync::atomic::{AtomicI32, Ordering::SeqCst}; use std::sync::atomic::{AtomicI32, Ordering::SeqCst};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use super::displayable::DisplayableFillType;
/// `Colorable` gives the ability to fill an area with a color /// `Colorable` gives the ability to fill an area with a color
pub struct Colorable { pub struct Colorable {
framable: Arc<Framable>, framable: Arc<Framable>,
@ -18,6 +20,7 @@ pub struct Colorable {
buffer: Arc<Buffer<ColorableVertex>>, buffer: Arc<Buffer<ColorableVertex>>,
color: RwLock<Color>, color: RwLock<Color>,
fill_type: DisplayableFillType,
ui_layer: AtomicI32, ui_layer: AtomicI32,
@ -29,7 +32,11 @@ pub struct Colorable {
impl Colorable { impl Colorable {
/// Factory method for `Colorable`, returns `Arc<Colorable>`. /// Factory method for `Colorable`, returns `Arc<Colorable>`.
pub fn new(framable: Arc<Framable>, color: Color) -> Result<Arc<Self>> { pub fn new(
framable: Arc<Framable>,
color: Color,
fill_type: DisplayableFillType,
) -> Result<Arc<Self>> {
let set = framable.gui_handler().color_descriptor(color)?; let set = framable.gui_handler().color_descriptor(color)?;
let buffer = Buffer::builder() let buffer = Buffer::builder()
@ -45,6 +52,7 @@ impl Colorable {
buffer, buffer,
color: RwLock::new(color), color: RwLock::new(color),
fill_type,
ui_layer: AtomicI32::new(0), ui_layer: AtomicI32::new(0),
@ -144,11 +152,25 @@ impl Colorable {
pub fn update_frame(&self) -> Result<()> { pub fn update_frame(&self) -> Result<()> {
let mut frame = self.buffer.map_complete()?; let mut frame = self.buffer.map_complete()?;
let x_start = self.framable.left() as f32; let mut x_start = self.framable.left() as f32;
let y_start = self.framable.top() as f32; let mut y_start = self.framable.top() as f32;
let width = (self.framable.right() - self.framable.left()) as f32; let mut width = (self.framable.right() - self.framable.left()) as f32;
let height = (self.framable.bottom() - self.framable.top()) 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 left = x_start + width * *self.left_factor.read().unwrap();
let right = x_start + width * *self.right_factor.read().unwrap(); let right = x_start + width * *self.right_factor.read().unwrap();