sized widgets!

This commit is contained in:
2025-08-28 01:35:43 -04:00
parent d4d0b3b580
commit 834182ffe8
14 changed files with 313 additions and 106 deletions

View File

@@ -1,9 +1,7 @@
use image::GenericImageView;
use crate::{
layout::{
Active, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiRegion, Vec2, WidgetId,
WidgetInstance, Widgets,
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
Textures, UiRegion, Vec2, WidgetId, WidgetInstance, Widgets,
},
render::{Primitive, PrimitiveHandle, Primitives},
util::Id,
@@ -33,7 +31,7 @@ pub struct Painter<'a> {
impl<'a> Painter<'a> {
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
nodes: &'a Widgets,
widgets: &'a Widgets,
primitives: &'a mut Primitives,
sensors_map: &'a SensorMap,
active: &'a mut Active,
@@ -42,7 +40,7 @@ impl<'a> Painter<'a> {
screen_size: Vec2,
) -> Self {
Self {
widgets: nodes,
widgets,
active,
sensors_map,
primitives,
@@ -59,9 +57,14 @@ impl<'a> Painter<'a> {
}
}
/// Writes a primitive to be rendered
pub fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) -> PrimitiveHandle<P> {
self.primitives.write(data, region)
}
/// Writes a primitive to be rendered
pub fn write<P: Primitive>(&mut self, data: P) -> PrimitiveHandle<P> {
self.primitives.write(data, self.state.region)
self.write_at(data, self.state.region)
}
/// Draws a widget within this widget's region.
@@ -81,9 +84,6 @@ impl<'a> Painter<'a> {
}
fn draw_raw_at(&mut self, id: &Id, region: UiRegion) {
if self.active.widgets.contains_key(id) {
panic!("widget drawn twice!");
}
self.state.children.push(id.duplicate());
// &mut self is passed to avoid copying all of the "static" pointers in self
@@ -100,7 +100,7 @@ impl<'a> Painter<'a> {
// draw widgets
let start_i = self.primitives.cur_pos();
self.widgets.get_dyn(id).draw(self);
self.widgets.get_dyn_dynamic(id).draw(self);
let end_i = self.primitives.cur_pos();
// restore state
@@ -122,7 +122,7 @@ impl<'a> Painter<'a> {
pub fn draw_texture(&mut self, handle: &TextureHandle) {
self.state.textures.push(handle.clone());
self.write(handle.inner);
self.write(handle.primitive());
}
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
@@ -132,11 +132,14 @@ impl<'a> Painter<'a> {
self.state.region = old;
}
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 region = self.state.region.center().expand(dims);
self.draw_texture_at(&handle, region);
/// returns (handle, offset from top left)
pub fn render_text(
&mut self,
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
) -> (TextureHandle, TextOffset) {
self.text.draw(buffer, content, attrs, self.textures)
}
pub fn region(&self) -> UiRegion {
@@ -147,6 +150,14 @@ impl<'a> Painter<'a> {
self.state.region.in_size(self.screen_size)
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
self.widgets.get_dyn_dynamic(&id.id).size(SizeCtx {
size: self.region().in_size(self.screen_size),
text: self.text,
textures: self.textures,
})
}
pub(crate) fn redraw(&mut self, id: &Id) {
if !self.active.widgets.contains_key(id) {
return;