snap text on shader

This commit is contained in:
2025-08-23 22:16:00 -04:00
parent 5ce6fca275
commit 50ccf7393d
10 changed files with 101 additions and 64 deletions

View File

@@ -2,8 +2,8 @@ use image::GenericImageView;
use crate::{
layout::{
ActiveSensors, SensorMap, TextData, TextureHandle, Textures, UiPos, UiRegion, Vec2,
WidgetId, Widgets,
ActiveSensors, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiPos, UiRegion,
Vec2, WidgetId, Widgets,
},
render::{Primitive, Primitives},
};
@@ -21,6 +21,7 @@ pub struct Painter<'a, Ctx: 'static> {
}
impl<'a, Ctx> Painter<'a, Ctx> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
nodes: &'a Widgets<Ctx>,
primitives: &'a mut Primitives,
@@ -94,16 +95,14 @@ impl<'a, Ctx> Painter<'a, Ctx> {
self.region = old;
}
pub fn draw_text(&mut self, content: &str) {
let handle = self.text.draw(content, self.textures);
pub fn draw_text(&mut self, content: &str, attrs: &TextAttrs) {
let handle = self.text.draw(content, attrs, 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),
};
let mut region = self.region.center().expand(dims);
// TODO: I feel like this shouldn't be needed
// what this does is makes sure the text doesn't get squeezed into one pixel less
// I'm unsure exactly why it happens or if this will ever expand it too much
region.bot_right.shift(0.01);
self.draw_texture_at(&handle, region);
}