Start ui element at runtime creator

This commit is contained in:
hodasemi 2024-03-24 19:19:08 +01:00
parent d131ad36bc
commit e0def0d91a
3 changed files with 113 additions and 0 deletions

111
src/element_creator/mod.rs Normal file
View file

@ -0,0 +1,111 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use anyhow::Result;
use utilities::color::Color;
use vulkan_rs::prelude::*;
#[derive(Debug, Clone, Copy)]
pub enum ElementBorderThickness {
Pixel(u32),
Ratio(f32),
}
impl From<u32> for ElementBorderThickness {
fn from(value: u32) -> Self {
Self::Pixel(value)
}
}
impl From<f32> for ElementBorderThickness {
fn from(value: f32) -> Self {
Self::Ratio(value)
}
}
#[derive(Debug, Clone, Copy)]
pub struct ElementDescriptor {
pub background_color: Color,
pub border_color: Color,
pub border_thickness: ElementBorderThickness,
}
impl ElementDescriptor {
pub fn new(
background_color: impl Into<Color>,
border_color: impl Into<Color>,
border_thickness: impl Into<ElementBorderThickness>,
) -> Self {
Self {
background_color: background_color.into(),
border_color: border_color.into(),
border_thickness: border_thickness.into(),
}
}
}
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
struct ElementDefinition {
width: u32,
height: u32,
background_color: Color,
border_color: Color,
border_thickness: u32,
}
pub struct ElementCreator {
device: Arc<Device>,
queue: Arc<Mutex<Queue>>,
elements: HashMap<ElementDefinition, Arc<Image>>,
}
impl ElementCreator {
pub fn new(device: Arc<Device>, queue: Arc<Mutex<Queue>>) -> Self {
Self {
device,
queue,
elements: HashMap::new(),
}
}
pub fn get(
&mut self,
width: u32,
height: u32,
descriptor: ElementDescriptor,
) -> Result<Arc<Image>> {
let definition = ElementDefinition {
width,
height,
background_color: descriptor.background_color,
border_color: descriptor.border_color,
border_thickness: match descriptor.border_thickness {
ElementBorderThickness::Pixel(pixel) => pixel,
ElementBorderThickness::Ratio(ratio) => {
(width.min(height) as f32 * ratio).round() as u32
}
},
};
match self.elements.get(&definition) {
Some(element) => Ok(element.clone()),
None => {
let element = self.create_element(definition)?;
self.elements.insert(definition, element.clone());
Ok(element)
}
}
}
fn create_element(&self, definition: ElementDefinition) -> Result<Arc<Image>> {
todo!()
}
}

View file

@ -9,6 +9,7 @@ pub mod tab_control;
mod context_interface;
mod controller_button;
mod element_creator;
mod guidirection;
mod mousebutton;
pub mod prelude;

View file

@ -1,6 +1,7 @@
pub use super::builder::{builder::GuiBuilder, snippet::GuiSnippet};
pub use super::context_interface::{ContextInterface, TargetMode, Unfold};
pub use super::controller_button::ControllerButton;
pub use super::element_creator::*;
pub use super::elements::prelude::*;
pub use super::guidirection::GuiDirection;
pub use super::guihandler::prelude::*;