Files
endorphin-tessera-gui/src/display_list.rs
T
2026-10-22 12:24:23 +08:00

99 lines
2.1 KiB
Rust

use crate::geometry::{Point, Rect};
use crate::text::TextStyle;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Color {
pub red: u8,
pub green: u8,
pub blue: u8,
pub alpha: u8,
}
impl Color {
pub const fn rgb(red: u8, green: u8, blue: u8) -> Self {
Self {
red,
green,
blue,
alpha: 255,
}
}
pub const fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self {
red,
green,
blue,
alpha,
}
}
}
#[derive(Clone, Debug)]
pub enum DrawCommand {
Clear(Color),
PushClip(Rect),
PopClip,
FillRoundedRect {
rect: Rect,
radius: f32,
color: Color,
},
StrokeRoundedRect {
rect: Rect,
radius: f32,
width: f32,
color: Color,
},
WrappedText {
rect: Rect,
value: String,
style: TextStyle,
color: Color,
},
Text {
origin: Point,
value: String,
style: TextStyle,
color: Color,
},
}
#[derive(Clone, Debug, Default)]
pub struct DisplayList {
commands: Vec<DrawCommand>,
}
impl DisplayList {
pub fn push(&mut self, command: DrawCommand) {
self.commands.push(command);
}
pub fn translate(&mut self, delta: Point) {
for command in &mut self.commands {
match command {
DrawCommand::WrappedText { rect, .. }
| DrawCommand::PushClip(rect)
| DrawCommand::FillRoundedRect { rect, .. }
| DrawCommand::StrokeRoundedRect { rect, .. } => {
rect.x += delta.x;
rect.y += delta.y;
}
DrawCommand::Text { origin, .. } => {
origin.x += delta.x;
origin.y += delta.y;
}
DrawCommand::Clear(_) | DrawCommand::PopClip => {}
}
}
}
pub fn commands(&self) -> &[DrawCommand] {
&self.commands
}
pub fn extend(&mut self, other: DisplayList) {
self.commands.extend(other.commands);
}
}