From d2598f8a202810bbf20d9ff58cb2fb3d8a307928 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 24 Mar 2024 01:13:20 +0100 Subject: [PATCH] move impls into separate module --- hui/src/frame.rs | 28 +-------- hui/src/frame/impls.rs | 125 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 26 deletions(-) create mode 100644 hui/src/frame/impls.rs diff --git a/hui/src/frame.rs b/hui/src/frame.rs index a89ebc7..c01936a 100644 --- a/hui/src/frame.rs +++ b/hui/src/frame.rs @@ -1,37 +1,13 @@ use glam::Vec2; -use crate::{draw::{UiDrawCommand, UiDrawCommandList}, rect::FillColor}; +use crate::draw::UiDrawCommandList; pub mod point; mod rect; pub mod stack; +mod impls; pub use rect::FrameRect; pub trait Frame { fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2); } - -impl Frame for FillColor { - fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { - draw.add(UiDrawCommand::Rectangle { - position, - size: parent_size, - color: self.corners(), - texture: None, - rounded_corners: None, - }) - } -} - -// impl + Clone> Frame for T { -// fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { -// let color: FillColor = self.clone().into(); -// draw.add(UiDrawCommand::Rectangle { -// position, -// size: parent_size, -// color: color.corners(), -// texture: None, -// rounded_corners: None, -// }) -// } -// } diff --git a/hui/src/frame/impls.rs b/hui/src/frame/impls.rs new file mode 100644 index 0000000..d5db6cd --- /dev/null +++ b/hui/src/frame/impls.rs @@ -0,0 +1,125 @@ +use glam::{Vec2, Vec3, Vec4}; +use super::Frame; +use crate::{ + color, + draw::{ImageHandle, UiDrawCommand, UiDrawCommandList}, + rect::{Corners, FillColor}, +}; + +impl Frame for ImageHandle { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + draw.add(UiDrawCommand::Rectangle { + position, + size: parent_size, + color: color::WHITE.into(), + texture: Some(*self), + rounded_corners: None, + }) + } +} + +impl Frame for FillColor { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + draw.add(UiDrawCommand::Rectangle { + position, + size: parent_size, + color: self.corners(), + texture: None, + rounded_corners: None, + }) + } +} + +// impl for various types resembling colors + +// Corners (RGBA): + +impl Frame for Corners { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for (Vec4, Vec4, Vec4, Vec4) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for ((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32)) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for [[f32; 4]; 4] { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +// Corners (RGB): + +impl Frame for Corners { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for (Vec3, Vec3, Vec3, Vec3) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for ((f32, f32, f32), (f32, f32, f32), (f32, f32, f32), (f32, f32, f32)) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for [[f32; 3]; 4] { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +// RGBA: + +impl Frame for Vec4 { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for (f32, f32, f32, f32) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for [f32; 4] { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +// RGB: + +impl Frame for Vec3 { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for (f32, f32, f32) { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +} + +impl Frame for [f32; 3] { + fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) { + FillColor::from(*self).draw(draw, position, parent_size) + } +}