preparation

This commit is contained in:
2025-09-09 21:53:32 -04:00
parent 15cc91d92a
commit 709a2d0e17
14 changed files with 292 additions and 220 deletions

View File

@@ -5,7 +5,10 @@ use crate::prelude::*;
pub struct Text {
pub content: String,
pub attrs: TextAttrs,
pub align: Align,
buf: TextBuffer,
cursor: Cursor,
size: Vec2,
}
impl Text {
@@ -32,16 +35,34 @@ impl Text {
content: content.into(),
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
attrs,
align: Align::Center,
cursor: Cursor::None,
size: Vec2::ZERO,
}
}
pub fn select(&mut self, pos: Vec2, size: Vec2) {
let pos = pos - self.region().top_left.to_size(size);
let Some(cursor) = self.buf.hit(pos.x, pos.y) else {
return;
};
self.cursor = Cursor::Select {
line: cursor.line,
idx: cursor.index,
};
}
pub fn region(&self) -> UiRegion {
UiRegion::from_size_align(self.size, self.align)
}
}
impl Widget for Text {
fn draw(&mut self, painter: &mut Painter) {
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
let (handle, offset) =
painter.render_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
let dims = handle.size();
let size = offset.size(&handle);
let mut region = Align::Center.pos().expand(size);
self.size = offset.size(&handle);
let mut region = self.region();
region.top_left.offset += offset.top_left;
region.bot_right.offset = region.top_left.offset + dims;
painter.draw_texture_within(&handle, region);
@@ -49,8 +70,7 @@ impl Widget for Text {
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
let (handle, offset) =
ctx.text
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
ctx.draw_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
offset.size(&handle)
}
}