initial text impl

This commit is contained in:
2025-08-23 21:15:39 -04:00
parent abcbc267b5
commit 5ce6fca275
33 changed files with 530 additions and 117 deletions

View File

@@ -1,5 +1,10 @@
use image::GenericImageView;
use crate::{
ActiveSensors, SensorMap, TextureHandle, UiRegion, WidgetId, Widgets,
layout::{
ActiveSensors, SensorMap, TextData, TextureHandle, Textures, UiPos, UiRegion, Vec2,
WidgetId, Widgets,
},
render::{Primitive, Primitives},
};
@@ -9,16 +14,22 @@ pub struct Painter<'a, Ctx: 'static> {
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
primitives: &'a mut Primitives,
textures: &'a mut Textures,
text: &'a mut TextData,
region: UiRegion,
screen_size: Vec2,
}
impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(
pub(crate) fn new(
nodes: &'a Widgets<Ctx>,
primitives: &'a mut Primitives,
ctx: &'a mut Ctx,
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
text: &'a mut TextData,
textures: &'a mut Textures,
screen_size: Vec2,
) -> Self {
Self {
nodes,
@@ -26,7 +37,10 @@ impl<'a, Ctx> Painter<'a, Ctx> {
active_sensors,
sensors_map,
primitives,
text,
region: UiRegion::full(),
textures,
screen_size,
}
}
@@ -69,9 +83,30 @@ impl<'a, Ctx> Painter<'a, Ctx> {
}
pub fn draw_texture(&mut self, handle: &TextureHandle) {
self.primitives.drawn_textures.push(handle.clone());
self.write(handle.inner);
}
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
let old = self.region;
self.region = region;
self.draw_texture(handle);
self.region = old;
}
pub fn draw_text(&mut self, content: &str) {
let handle = self.text.draw(content, self.textures);
let dims: Vec2 = self.textures[&handle].dimensions().into();
let center = self.region.center().snap(self.screen_size);
let top_left = center - (dims / 2.0).floor();
let bot_right = center + (dims / 2.0).ceil();
let region = UiRegion {
top_left: UiPos::offset(top_left),
bot_right: UiPos::offset(bot_right),
};
self.draw_texture_at(&handle, region);
}
pub fn region(&self) -> UiRegion {
self.region
}