From 99e1bc554922d3691079e40974485245cd961717 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 19 Feb 2024 21:12:12 +0100 Subject: [PATCH] refactor draw stuff --- hui/src/draw.rs | 46 +++++++++++++++++++++++++++++++++++----------- hui/src/element.rs | 4 ++-- hui/src/lib.rs | 10 +++++----- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/hui/src/draw.rs b/hui/src/draw.rs index bea316a..4c47461 100644 --- a/hui/src/draw.rs +++ b/hui/src/draw.rs @@ -7,6 +7,10 @@ use std::borrow::Cow; use fontdue::layout::{Layout, CoordinateSystem, TextStyle}; use glam::{Vec2, Vec4, vec2}; +/// Available draw commands +/// - Rectangle: Filled, colored rectangle, with optional rounded corners +/// - Circle: Simple filled, colored circle +/// - Text: Draw text using the specified font, size, color, and position #[derive(Clone, Debug, PartialEq)] pub enum UiDrawCommand { ///Filled, colored rectangle @@ -20,6 +24,14 @@ pub enum UiDrawCommand { ///Rounded corners rounded_corners: Option, }, + Circle { + ///Position in pixels + position: Vec2, + ///Radius in pixels + radius: f32, + ///Color (RGBA) + color: Vec4, + }, Text { ///Position in pixels position: Vec2, @@ -34,12 +46,22 @@ pub enum UiDrawCommand { }, } +impl UiDrawCommand { + fn texture_eq_index(&self) -> u64 { + match self { + UiDrawCommand::Rectangle { .. } | + UiDrawCommand::Circle { .. } => u64::MAX - 1, + UiDrawCommand::Text { .. } => u64::MAX - 2, + } + } +} + #[derive(Default)] -pub struct UiDrawCommands { +pub struct UiDrawCommandList { pub commands: Vec, } -impl UiDrawCommands { +impl UiDrawCommandList { pub fn add(&mut self, command: UiDrawCommand) { self.commands.push(command); } @@ -109,13 +131,13 @@ impl CallSwapper { } impl UiDrawPlan { - pub fn build(draw_commands: &UiDrawCommands, tr: &mut TextRenderer) -> Self { + pub fn build(draw_commands: &UiDrawCommandList, tr: &mut TextRenderer) -> Self { let mut swapper = CallSwapper::new(); - let mut prev_command = None; + let mut prev_command: Option<&UiDrawCommand> = None; for command in &draw_commands.commands { - let do_swap = if let Some(prev_command) = prev_command { - std::mem::discriminant(prev_command) != std::mem::discriminant(command) + //std::mem::discriminant(prev_command) != std::mem::discriminant(command) + prev_command.texture_eq_index() != command.texture_eq_index() } else { false }; @@ -125,11 +147,10 @@ impl UiDrawPlan { } if do_swap || prev_command.is_none() { - match command { - UiDrawCommand::Rectangle { .. } => (), - UiDrawCommand::Text { .. } => { - swapper.current_mut().bind_texture = Some(BindTexture::FontTexture); - } + swapper.current_mut().bind_texture = match command { + UiDrawCommand::Rectangle { .. } | + UiDrawCommand::Circle { .. } => None, + UiDrawCommand::Text { .. } => Some(BindTexture::FontTexture), } } @@ -246,6 +267,9 @@ impl UiDrawPlan { ]); } }, + UiDrawCommand::Circle { .. } => { + todo!("circle draw command not implemented yet") + }, UiDrawCommand::Text { position, size, color, text, font } => { //XXX: should we be doing this every time? let mut layout = Layout::new(CoordinateSystem::PositiveYDown); diff --git a/hui/src/element.rs b/hui/src/element.rs index 71a692f..ae76851 100644 --- a/hui/src/element.rs +++ b/hui/src/element.rs @@ -1,6 +1,6 @@ use std::any::Any; use crate::{ - draw::UiDrawCommands, + draw::UiDrawCommandList, measure::Response, state::StateRepo, text::TextMeasure, @@ -29,7 +29,7 @@ pub struct ProcessContext<'a> { pub measure: &'a Response, pub state: &'a mut StateRepo, pub layout: &'a LayoutInfo, - pub draw: &'a mut UiDrawCommands, + pub draw: &'a mut UiDrawCommandList, pub text_measure: TextMeasure<'a>, } diff --git a/hui/src/lib.rs b/hui/src/lib.rs index db697e7..fdcaf8a 100644 --- a/hui/src/lib.rs +++ b/hui/src/lib.rs @@ -19,7 +19,7 @@ pub mod interaction; use element::{MeasureContext, ProcessContext, UiElement}; use event::UiEvent; use state::StateRepo; -use draw::{UiDrawCommands, UiDrawPlan}; +use draw::{UiDrawCommandList, UiDrawPlan}; use text::{TextRenderer, FontTextureInfo, FontHandle}; use glam::Vec2; @@ -36,8 +36,8 @@ pub struct UiInstance { //mouse_position: Vec2, stateful_state: StateRepo, //event_queue: VecDeque, - prev_draw_commands: UiDrawCommands, - draw_commands: UiDrawCommands, + prev_draw_commands: UiDrawCommandList, + draw_commands: UiDrawCommandList, draw_plan: UiDrawPlan, draw_plan_modified: bool, text_renderer: TextRenderer, @@ -51,8 +51,8 @@ impl UiInstance { stateful_state: StateRepo::default(), //event_queue: VecDeque::new(), // root_elements: Vec::new(), - prev_draw_commands: UiDrawCommands::default(), - draw_commands: UiDrawCommands::default(), + prev_draw_commands: UiDrawCommandList::default(), + draw_commands: UiDrawCommandList::default(), draw_plan: UiDrawPlan::default(), draw_plan_modified: false, // ftm: FontTextureManager::default(),