This commit is contained in:
2025-08-15 15:48:00 -04:00
parent c5aa0a02e2
commit 78ea738b8e
8 changed files with 222 additions and 52 deletions

View File

@@ -1,20 +1,22 @@
use crate::{
UiRegion, WidgetId, Widgets,
SenseFn, SenseTrigger, Sensors, UiRegion, WidgetId, Widgets,
primitive::{PrimitiveData, PrimitiveInstance, Primitives},
};
pub struct Painter<'a, Ctx> {
nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
sensors: &'a mut Sensors<Ctx>,
primitives: Primitives,
pub region: UiRegion,
}
impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx) -> Self {
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx, sensors: &'a mut Sensors<Ctx>) -> Self {
Self {
nodes,
ctx,
sensors,
primitives: Primitives::default(),
region: UiRegion::full(),
}
@@ -31,17 +33,27 @@ impl<'a, Ctx> Painter<'a, Ctx> {
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
}
pub fn draw(&mut self, id: &WidgetId) where Ctx: 'static {
pub fn draw(&mut self, id: &WidgetId)
where
Ctx: 'static,
{
self.nodes.get_dyn(id).draw(self);
}
pub fn draw_within(&mut self, node: &WidgetId, region: UiRegion) where Ctx: 'static {
pub fn draw_within(&mut self, node: &WidgetId, region: UiRegion)
where
Ctx: 'static,
{
let old = self.region;
self.region.select(&region);
self.draw(node);
self.region = old;
}
pub fn sense(&mut self, trigger: SenseTrigger, f: Box<dyn SenseFn<Ctx>>) {
self.sensors.push((trigger, f));
}
pub fn finish(self) -> Primitives {
self.primitives
}

View File

@@ -149,6 +149,26 @@ impl UiRegion {
self.bot_right.flip();
std::mem::swap(&mut self.top_left, &mut self.bot_right);
}
pub fn to_screen(&self, size: Vec2) -> ScreenRect {
ScreenRect {
top_left: self.top_left.anchor * size + self.top_left.offset,
bot_right: self.bot_right.anchor * size + self.bot_right.offset,
}
}
}
pub struct ScreenRect {
top_left: Vec2,
bot_right: Vec2,
}
impl ScreenRect {
pub fn contains(&self, pos: Vec2) -> bool {
pos.x >= self.top_left.x
&& pos.x <= self.bot_right.x
&& pos.y >= self.top_left.y
&& pos.y <= self.bot_right.y
}
}
pub struct UIRegionAxisView<'a> {

View File

@@ -1,5 +1,5 @@
use crate::{
HashMap, Painter, Widget, WidgetId, WidgetLike,
HashMap, Painter, SenseCtx, UiRegion, Widget, WidgetId, WidgetLike,
primitive::Primitives,
util::{IDTracker, Id},
};
@@ -13,6 +13,7 @@ pub struct Ui<Ctx> {
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
updates: Vec<WidgetId>,
sensors: Sensors<Ctx>,
primitives: Primitives,
full_redraw: bool,
}
@@ -20,6 +21,27 @@ pub struct Ui<Ctx> {
#[derive(Default)]
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>);
#[derive(Clone, Copy)]
pub enum Sense {
Click,
Hover,
}
pub type Sensors<Ctx> = Vec<(SenseTrigger, Box<dyn SenseFn<Ctx>>)>;
pub trait SenseFn_<Ctx> = Fn(&mut Ui<Ctx>, &mut Ctx) + 'static;
pub type SenseShape = UiRegion;
pub struct SenseTrigger {
pub shape: SenseShape,
pub sense: Sense,
}
pub trait SenseFn<Ctx>: SenseFn_<Ctx> {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>>;
}
impl<F: SenseFn_<Ctx> + Clone, Ctx> SenseFn<Ctx> for F {
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>> {
Box::new(self.clone())
}
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, Tag>(
&mut self,
@@ -67,13 +89,27 @@ impl<Ctx> Ui<Ctx> {
where
Ctx: 'static,
{
let mut painter = Painter::new(&self.widgets, ctx);
self.sensors.clear();
let mut painter = Painter::new(&self.widgets, ctx, &mut self.sensors);
if let Some(base) = &self.base {
painter.draw(base);
}
self.primitives = painter.finish();
}
pub fn run_sensors(&mut self, ctx: &mut Ctx)
where
Ctx: SenseCtx,
{
for (t, f) in self.sensors.iter().rev() {
if ctx.active(t) {
let f = f.as_ref().box_clone();
f(self, ctx);
return;
}
}
}
pub fn update(&mut self, ctx: &mut Ctx) -> Option<&Primitives>
where
Ctx: 'static,
@@ -94,8 +130,6 @@ impl<Ctx> Ui<Ctx> {
pub fn needs_redraw(&self) -> bool {
self.full_redraw || !self.updates.is_empty()
}
pub fn set_mouse_pos(&mut self) {}
}
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
@@ -157,7 +191,8 @@ impl<Ctx> Default for Ui<Ctx> {
widgets: Widgets::new(),
updates: Default::default(),
primitives: Default::default(),
full_redraw: Default::default(),
full_redraw: false,
sensors: Default::default(),
}
}
}

View File

@@ -16,6 +16,7 @@ pub struct AnyWidget;
///
/// W does not need to implement widget so that AnyWidget is valid;
/// Instead, add generic bounds on methods that take an ID if they need specific data.
#[repr(C)]
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
@@ -38,10 +39,16 @@ impl<W> WidgetId<W> {
_pd: PhantomData,
}
}
pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type()
}
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
// safety: self is repr(C) and generic only used for phantom data
unsafe { std::mem::transmute(self) }
}
fn cast_type<W2>(self) -> WidgetId<W2> {
WidgetId {
ty: self.ty,