2023-12-01 14:48:17 -06:00
|
|
|
use crate::{IfModified, text::TextRenderer};
|
2023-11-30 17:45:56 -06:00
|
|
|
|
2023-11-25 18:47:48 -06:00
|
|
|
use std::borrow::Cow;
|
2023-11-23 10:14:02 -06:00
|
|
|
use glam::{Vec2, Vec4, vec2};
|
2023-11-21 07:08:22 -06:00
|
|
|
|
2023-11-25 18:47:48 -06:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-11-22 04:56:46 -06:00
|
|
|
pub enum UiDrawCommand {
|
2023-11-21 07:08:22 -06:00
|
|
|
///Filled, colored rectangle
|
|
|
|
Rectangle {
|
|
|
|
///Position in pixels
|
|
|
|
position: Vec2,
|
|
|
|
///Size in pixels
|
|
|
|
size: Vec2,
|
|
|
|
///Color (RGBA)
|
|
|
|
color: Vec4,
|
2023-11-25 18:47:48 -06:00
|
|
|
},
|
|
|
|
Text {
|
|
|
|
///Position in pixels
|
|
|
|
position: Vec2,
|
|
|
|
///Font size
|
|
|
|
size: u8,
|
|
|
|
///Color (RGBA)
|
|
|
|
color: Vec4,
|
|
|
|
///Text to draw
|
|
|
|
text: Cow<'static, str>,
|
|
|
|
},
|
2023-11-21 07:08:22 -06:00
|
|
|
}
|
|
|
|
|
2023-11-22 04:56:46 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UiDrawCommands {
|
|
|
|
pub commands: Vec<UiDrawCommand>,
|
2023-11-21 07:08:22 -06:00
|
|
|
}
|
|
|
|
|
2023-11-22 04:56:46 -06:00
|
|
|
// impl UiDrawCommands {
|
|
|
|
// pub fn compare(&self, other: &Self) -> bool {
|
|
|
|
// // if self.commands.len() != other.commands.len() { return false }
|
|
|
|
// // self.commands.iter().zip(other.commands.iter()).all(|(a, b)| a == b)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-11-30 17:54:32 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2023-11-25 18:47:48 -06:00
|
|
|
pub enum BindTexture {
|
|
|
|
FontTexture,
|
|
|
|
//UserDefined(usize),
|
|
|
|
}
|
|
|
|
|
2023-11-22 04:56:46 -06:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
pub struct UiVertex {
|
|
|
|
pub position: Vec2,
|
|
|
|
pub color: Vec4,
|
2023-11-23 10:14:02 -06:00
|
|
|
pub uv: Vec2,
|
2023-11-22 04:56:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2023-11-22 13:26:44 -06:00
|
|
|
pub struct UiDrawCall {
|
2023-11-22 04:56:46 -06:00
|
|
|
pub vertices: Vec<UiVertex>,
|
|
|
|
pub indices: Vec<u32>,
|
2023-12-01 14:48:17 -06:00
|
|
|
pub bind_texture: Option<BindTexture>,
|
2023-11-22 04:56:46 -06:00
|
|
|
}
|
2023-11-21 07:08:22 -06:00
|
|
|
|
2023-11-22 13:26:44 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct UiDrawPlan {
|
|
|
|
pub calls: Vec<UiDrawCall>
|
|
|
|
}
|
|
|
|
|
2023-11-22 04:56:46 -06:00
|
|
|
impl UiDrawPlan {
|
2023-12-01 14:48:17 -06:00
|
|
|
pub fn build(calls: &UiDrawCommands, tr: &mut TextRenderer) -> Self {
|
2023-11-22 13:26:44 -06:00
|
|
|
let mut call = UiDrawCall::default();
|
|
|
|
for command in &calls.commands {
|
|
|
|
match command {
|
2023-11-22 04:56:46 -06:00
|
|
|
UiDrawCommand::Rectangle { position, size, color } => {
|
2023-11-22 13:26:44 -06:00
|
|
|
let idx = call.vertices.len() as u32;
|
|
|
|
call.indices.extend([idx, idx + 1, idx + 2, idx, idx + 2, idx + 3]);
|
|
|
|
call.vertices.extend([
|
2023-11-22 04:56:46 -06:00
|
|
|
UiVertex {
|
|
|
|
position: *position,
|
|
|
|
color: *color,
|
2023-11-23 10:14:02 -06:00
|
|
|
uv: vec2(0.0, 0.0),
|
2023-11-22 04:56:46 -06:00
|
|
|
},
|
|
|
|
UiVertex {
|
|
|
|
position: *position + Vec2::new(size.x, 0.0),
|
|
|
|
color: *color,
|
2023-11-23 10:14:02 -06:00
|
|
|
uv: vec2(1.0, 0.0),
|
2023-11-22 04:56:46 -06:00
|
|
|
},
|
|
|
|
UiVertex {
|
|
|
|
position: *position + *size,
|
|
|
|
color: *color,
|
2023-11-23 10:14:02 -06:00
|
|
|
uv: vec2(1.0, 1.0),
|
2023-11-22 04:56:46 -06:00
|
|
|
},
|
|
|
|
UiVertex {
|
|
|
|
position: *position + Vec2::new(0.0, size.y),
|
|
|
|
color: *color,
|
2023-11-23 10:14:02 -06:00
|
|
|
uv: vec2(0.0, 1.0),
|
2023-11-22 04:56:46 -06:00
|
|
|
},
|
|
|
|
]);
|
2023-11-25 18:47:48 -06:00
|
|
|
},
|
|
|
|
UiDrawCommand::Text { position, size, color, text } => {
|
|
|
|
todo!()
|
2023-11-22 04:56:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-22 13:26:44 -06:00
|
|
|
Self {
|
|
|
|
calls: vec![call]
|
|
|
|
}
|
2023-11-22 04:56:46 -06:00
|
|
|
}
|
2023-11-21 07:08:22 -06:00
|
|
|
}
|
2023-11-30 17:45:56 -06:00
|
|
|
|
|
|
|
impl IfModified<UiDrawPlan> for (bool, &UiDrawPlan) {
|
|
|
|
fn if_modified(&self) -> Option<&UiDrawPlan> {
|
|
|
|
match self.0 {
|
|
|
|
true => Some(self.1),
|
|
|
|
false => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|