new rendering engine

This commit is contained in:
2026-06-19 02:55:33 +02:00
parent 0a90d6a5d7
commit 81a4167382
53 changed files with 1668 additions and 378 deletions

View File

@@ -1,4 +1,4 @@
use client_domain::{BoundingBox, DisplayPort};
use client_domain::{BoundingBox, Color, DisplayPort, FontSize};
use embedded_graphics::{
mono_font::{ascii::FONT_6X10, ascii::FONT_10X20, MonoTextStyle},
pixelcolor::Rgb565,
@@ -6,7 +6,6 @@ use embedded_graphics::{
primitives::{PrimitiveStyle, Rectangle},
text::Text,
};
use embedded_text::{TextBox, style::TextBoxStyleBuilder};
#[derive(Debug)]
pub enum DisplayError {
@@ -26,10 +25,8 @@ pub struct Esp32DisplayAdapter {
}
trait ErasedDisplay {
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), DisplayError>;
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), DisplayError>;
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), DisplayError>;
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), DisplayError>;
fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError>;
fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError>;
fn flush(&mut self) -> Result<(), DisplayError>;
}
@@ -38,55 +35,37 @@ where
D: DrawTarget<Color = Rgb565>,
D::Error: std::fmt::Debug,
{
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), DisplayError> {
fn draw_text_span(&mut self, text: &str, x: u16, y: u16, color: Rgb565, font: FontSize) -> Result<(), DisplayError> {
let (style, y_offset) = match font {
FontSize::Small => (MonoTextStyle::new(&FONT_6X10, color), 10),
FontSize::Large => (MonoTextStyle::new(&FONT_10X20, color), 20),
};
Text::new(text, Point::new(x as i32, y as i32 + y_offset), style)
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn fill_rect(&mut self, bounds: BoundingBox, color: Rgb565) -> Result<(), DisplayError> {
Rectangle::new(
Point::new(bounds.x as i32, bounds.y as i32),
Size::new(bounds.width as u32, bounds.height as u32),
)
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.into_styled(PrimitiveStyle::with_fill(color))
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn draw_text(&mut self, text: &str, _x: u16, _y: u16, bounds: BoundingBox) -> Result<(), DisplayError> {
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
let textbox_style = TextBoxStyleBuilder::new().build();
let rect = Rectangle::new(
Point::new(bounds.x as i32, bounds.y as i32),
Size::new(bounds.width as u32, bounds.height as u32),
);
TextBox::with_textbox_style(text, rect, style, textbox_style)
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), DisplayError> {
let style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
let icon_char = match icon {
"sunny" | "clear" => "*",
"cloud_rain" | "rain" => "~",
"cloud" | "cloudy" => "=",
"dollar" | "money" => "$",
"music" | "note" => "#",
_ => "?",
};
Text::new(icon_char, Point::new(x as i32, y as i32 + 20), style)
.draw(self)
.map_err(|e| DisplayError::Draw(format!("{e:?}")))?;
Ok(())
}
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), DisplayError> {
self.clear_region(bounds)
}
fn flush(&mut self) -> Result<(), DisplayError> {
Ok(())
}
}
fn to_rgb565(c: Color) -> Rgb565 {
Rgb565::new(c.0 >> 3, c.1 >> 2, c.2 >> 3)
}
impl Esp32DisplayAdapter {
pub fn new<D>(display: D) -> Self
where
@@ -102,20 +81,19 @@ impl Esp32DisplayAdapter {
impl DisplayPort for Esp32DisplayAdapter {
type Error = DisplayError;
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
self.inner.clear_region(bounds)
fn draw_text_span(
&mut self,
text: &str,
x: u16,
y: u16,
color: Color,
font: FontSize,
) -> Result<(), Self::Error> {
self.inner.draw_text_span(text, x, y, to_rgb565(color), font)
}
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error> {
self.inner.draw_text(text, x, y, bounds)
}
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), Self::Error> {
self.inner.draw_icon(icon, x, y)
}
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
self.inner.fill_background(bounds)
fn fill_rect(&mut self, bounds: BoundingBox, color: Color) -> Result<(), Self::Error> {
self.inner.fill_rect(bounds, to_rgb565(color))
}
fn flush(&mut self) -> Result<(), Self::Error> {