refactor draw stuff

This commit is contained in:
griffi-gh 2024-02-19 21:12:12 +01:00
parent 83c00e8c13
commit 99e1bc5549
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 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<RoundedCorners>,
},
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<UiDrawCommand>,
}
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);

View file

@ -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>,
}

View file

@ -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<UiEvent>,
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(),