refactor draw stuff

This commit is contained in:
griffi-gh 2024-02-19 21:12:12 +01:00
parent 52d8b9866f
commit e3c4be7fdc
3 changed files with 42 additions and 18 deletions

View file

@ -7,6 +7,10 @@ use std::borrow::Cow;
use fontdue::layout::{Layout, CoordinateSystem, TextStyle}; use fontdue::layout::{Layout, CoordinateSystem, TextStyle};
use glam::{Vec2, Vec4, vec2}; 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)] #[derive(Clone, Debug, PartialEq)]
pub enum UiDrawCommand { pub enum UiDrawCommand {
///Filled, colored rectangle ///Filled, colored rectangle
@ -20,6 +24,14 @@ pub enum UiDrawCommand {
///Rounded corners ///Rounded corners
rounded_corners: Option<RoundedCorners>, rounded_corners: Option<RoundedCorners>,
}, },
Circle {
///Position in pixels
position: Vec2,
///Radius in pixels
radius: f32,
///Color (RGBA)
color: Vec4,
},
Text { Text {
///Position in pixels ///Position in pixels
position: Vec2, 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)] #[derive(Default)]
pub struct UiDrawCommands { pub struct UiDrawCommandList {
pub commands: Vec<UiDrawCommand>, pub commands: Vec<UiDrawCommand>,
} }
impl UiDrawCommands { impl UiDrawCommandList {
pub fn add(&mut self, command: UiDrawCommand) { pub fn add(&mut self, command: UiDrawCommand) {
self.commands.push(command); self.commands.push(command);
} }
@ -109,13 +131,13 @@ impl CallSwapper {
} }
impl UiDrawPlan { 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 swapper = CallSwapper::new();
let mut prev_command = None; let mut prev_command: Option<&UiDrawCommand> = None;
for command in &draw_commands.commands { for command in &draw_commands.commands {
let do_swap = if let Some(prev_command) = prev_command { 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 { } else {
false false
}; };
@ -125,11 +147,10 @@ impl UiDrawPlan {
} }
if do_swap || prev_command.is_none() { if do_swap || prev_command.is_none() {
match command { swapper.current_mut().bind_texture = match command {
UiDrawCommand::Rectangle { .. } => (), UiDrawCommand::Rectangle { .. } |
UiDrawCommand::Text { .. } => { UiDrawCommand::Circle { .. } => None,
swapper.current_mut().bind_texture = Some(BindTexture::FontTexture); 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 } => { UiDrawCommand::Text { position, size, color, text, font } => {
//XXX: should we be doing this every time? //XXX: should we be doing this every time?
let mut layout = Layout::new(CoordinateSystem::PositiveYDown); let mut layout = Layout::new(CoordinateSystem::PositiveYDown);

View file

@ -1,6 +1,6 @@
use std::any::Any; use std::any::Any;
use crate::{ use crate::{
draw::UiDrawCommands, draw::UiDrawCommandList,
measure::Response, measure::Response,
state::StateRepo, state::StateRepo,
text::TextMeasure, text::TextMeasure,
@ -29,7 +29,7 @@ pub struct ProcessContext<'a> {
pub measure: &'a Response, pub measure: &'a Response,
pub state: &'a mut StateRepo, pub state: &'a mut StateRepo,
pub layout: &'a LayoutInfo, pub layout: &'a LayoutInfo,
pub draw: &'a mut UiDrawCommands, pub draw: &'a mut UiDrawCommandList,
pub text_measure: TextMeasure<'a>, pub text_measure: TextMeasure<'a>,
} }

View file

@ -19,7 +19,7 @@ pub mod interaction;
use element::{MeasureContext, ProcessContext, UiElement}; use element::{MeasureContext, ProcessContext, UiElement};
use event::UiEvent; use event::UiEvent;
use state::StateRepo; use state::StateRepo;
use draw::{UiDrawCommands, UiDrawPlan}; use draw::{UiDrawCommandList, UiDrawPlan};
use text::{TextRenderer, FontTextureInfo, FontHandle}; use text::{TextRenderer, FontTextureInfo, FontHandle};
use glam::Vec2; use glam::Vec2;
@ -36,8 +36,8 @@ pub struct UiInstance {
//mouse_position: Vec2, //mouse_position: Vec2,
stateful_state: StateRepo, stateful_state: StateRepo,
//event_queue: VecDeque<UiEvent>, //event_queue: VecDeque<UiEvent>,
prev_draw_commands: UiDrawCommands, prev_draw_commands: UiDrawCommandList,
draw_commands: UiDrawCommands, draw_commands: UiDrawCommandList,
draw_plan: UiDrawPlan, draw_plan: UiDrawPlan,
draw_plan_modified: bool, draw_plan_modified: bool,
text_renderer: TextRenderer, text_renderer: TextRenderer,
@ -51,8 +51,8 @@ impl UiInstance {
stateful_state: StateRepo::default(), stateful_state: StateRepo::default(),
//event_queue: VecDeque::new(), //event_queue: VecDeque::new(),
// root_elements: Vec::new(), // root_elements: Vec::new(),
prev_draw_commands: UiDrawCommands::default(), prev_draw_commands: UiDrawCommandList::default(),
draw_commands: UiDrawCommands::default(), draw_commands: UiDrawCommandList::default(),
draw_plan: UiDrawPlan::default(), draw_plan: UiDrawPlan::default(),
draw_plan_modified: false, draw_plan_modified: false,
// ftm: FontTextureManager::default(), // ftm: FontTextureManager::default(),