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,34 +1,57 @@
use std::marker::PhantomData; use crate::{
Painter, Sense, SenseFn, SenseShape, SenseTrigger, Ui, Widget, WidgetFn, WidgetId, WidgetLike,
};
use crate::{Painter, Widget, WidgetFn, WidgetId, WidgetLike}; pub struct Sensor<Ctx> {
pub struct Sensor<F: SenseFn<Ctx>, Ctx> {
inner: WidgetId, inner: WidgetId,
f: F, sense: Sense,
_pd: PhantomData<Ctx>, f: Box<dyn SenseFn<Ctx>>,
} }
impl<F: SenseFn<Ctx>, Ctx: 'static> Widget<Ctx> for Sensor<F, Ctx> { impl<Ctx: 'static> Widget<Ctx> for Sensor<Ctx> {
fn draw(&self, painter: &mut Painter<Ctx>) { fn draw(&self, painter: &mut Painter<Ctx>) {
(self.f)(painter.ctx_mut()); painter.sense(
painter.draw(&self.inner); SenseTrigger {
shape: painter.region,
sense: self.sense,
},
self.f.box_clone(),
);
painter.draw(self.inner.as_any());
} }
} }
pub trait SenseFn<Ctx> = Fn(&mut Ctx) + 'static; pub trait SenseCtx: 'static {
fn active(&mut self, trigger: &SenseTrigger) -> bool;
pub trait Sensable<Ctx: 'static, Tag> {
fn sense<F: SenseFn<Ctx>>(self, f: F) -> impl WidgetFn<Sensor<F, Ctx>, Ctx>;
} }
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> Sensable<Ctx, Tag> for W { pub trait WidgetSenseFn<W: Widget<Ctx>, Ctx> = Fn(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static;
fn sense<F: SenseFn<Ctx>>(self, f: F) -> impl WidgetFn<Sensor<F, Ctx>, Ctx> {
|ui| { pub trait Sensable<W: Widget<Ctx>, Ctx: 'static, Tag> {
let inner = self.add(ui).erase_type(); // copied here so LSP can at least get the UI and id
fn sense<F: Fn(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone>(
self,
sense: Sense,
f: F,
) -> impl WidgetFn<Sensor<Ctx>, Ctx>;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx: SenseCtx, Tag> Sensable<W::Widget, Ctx, Tag> for W
where
W::Widget: Widget<Ctx>,
{
fn sense<F: WidgetSenseFn<W::Widget, Ctx> + Clone>(
self,
sense: Sense,
f: F,
) -> impl WidgetFn<Sensor<Ctx>, Ctx> {
move |ui| {
let inner_arg = self.add(ui);
let inner = inner_arg.clone().erase_type();
Sensor { Sensor {
inner, inner,
f, sense,
_pd: PhantomData, f: Box::new(move |ui, ctx| (f)(&inner_arg, ui, ctx)),
} }
} }
} }

View File

@@ -1,20 +1,22 @@
use crate::{ use crate::{
UiRegion, WidgetId, Widgets, SenseFn, SenseTrigger, Sensors, UiRegion, WidgetId, Widgets,
primitive::{PrimitiveData, PrimitiveInstance, Primitives}, primitive::{PrimitiveData, PrimitiveInstance, Primitives},
}; };
pub struct Painter<'a, Ctx> { pub struct Painter<'a, Ctx> {
nodes: &'a Widgets<Ctx>, nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx, ctx: &'a mut Ctx,
sensors: &'a mut Sensors<Ctx>,
primitives: Primitives, primitives: Primitives,
pub region: UiRegion, pub region: UiRegion,
} }
impl<'a, Ctx> Painter<'a, Ctx> { 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 { Self {
nodes, nodes,
ctx, ctx,
sensors,
primitives: Primitives::default(), primitives: Primitives::default(),
region: UiRegion::full(), region: UiRegion::full(),
} }
@@ -31,17 +33,27 @@ impl<'a, Ctx> Painter<'a, Ctx> {
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data])); .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); 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; let old = self.region;
self.region.select(&region); self.region.select(&region);
self.draw(node); self.draw(node);
self.region = old; 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 { pub fn finish(self) -> Primitives {
self.primitives self.primitives
} }

View File

@@ -149,6 +149,26 @@ impl UiRegion {
self.bot_right.flip(); self.bot_right.flip();
std::mem::swap(&mut self.top_left, &mut self.bot_right); 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> { pub struct UIRegionAxisView<'a> {

View File

@@ -1,5 +1,5 @@
use crate::{ use crate::{
HashMap, Painter, Widget, WidgetId, WidgetLike, HashMap, Painter, SenseCtx, UiRegion, Widget, WidgetId, WidgetLike,
primitive::Primitives, primitive::Primitives,
util::{IDTracker, Id}, util::{IDTracker, Id},
}; };
@@ -13,6 +13,7 @@ pub struct Ui<Ctx> {
base: Option<WidgetId>, base: Option<WidgetId>,
widgets: Widgets<Ctx>, widgets: Widgets<Ctx>,
updates: Vec<WidgetId>, updates: Vec<WidgetId>,
sensors: Sensors<Ctx>,
primitives: Primitives, primitives: Primitives,
full_redraw: bool, full_redraw: bool,
} }
@@ -20,6 +21,27 @@ pub struct Ui<Ctx> {
#[derive(Default)] #[derive(Default)]
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>); 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> { impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, Tag>( pub fn add<W: Widget<Ctx>, Tag>(
&mut self, &mut self,
@@ -67,13 +89,27 @@ impl<Ctx> Ui<Ctx> {
where where
Ctx: 'static, 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 { if let Some(base) = &self.base {
painter.draw(base); painter.draw(base);
} }
self.primitives = painter.finish(); 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> pub fn update(&mut self, ctx: &mut Ctx) -> Option<&Primitives>
where where
Ctx: 'static, Ctx: 'static,
@@ -94,8 +130,6 @@ impl<Ctx> Ui<Ctx> {
pub fn needs_redraw(&self) -> bool { pub fn needs_redraw(&self) -> bool {
self.full_redraw || !self.updates.is_empty() 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> { impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
@@ -157,7 +191,8 @@ impl<Ctx> Default for Ui<Ctx> {
widgets: Widgets::new(), widgets: Widgets::new(),
updates: Default::default(), updates: Default::default(),
primitives: 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; /// 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. /// Instead, add generic bounds on methods that take an ID if they need specific data.
#[repr(C)]
#[derive(Eq, Hash, PartialEq, Debug)] #[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = AnyWidget> { pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId, pub(super) ty: TypeId,
@@ -38,10 +39,16 @@ impl<W> WidgetId<W> {
_pd: PhantomData, _pd: PhantomData,
} }
} }
pub fn erase_type(self) -> WidgetId<AnyWidget> { pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type() 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> { fn cast_type<W2>(self) -> WidgetId<W2> {
WidgetId { WidgetId {
ty: self.ty, ty: self.ty,

View File

@@ -1,3 +1,4 @@
use gui::Ui;
use winit::{ use winit::{
application::ApplicationHandler, application::ApplicationHandler,
event::WindowEvent, event::WindowEvent,
@@ -9,7 +10,7 @@ use super::Client;
#[derive(Default)] #[derive(Default)]
pub struct App { pub struct App {
client: Option<Client>, client: Option<(Client, Ui<Client>)>,
} }
impl App { impl App {
@@ -25,12 +26,14 @@ impl ApplicationHandler for App {
let window = event_loop let window = event_loop
.create_window(Window::default_attributes()) .create_window(Window::default_attributes())
.unwrap(); .unwrap();
let client = Client::new(window.into()); let (ui, ids) = Client::create_ui();
self.client = Some(client); let client = Client::new(window.into(), ids);
self.client = Some((client, ui));
} }
} }
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
self.client.as_mut().unwrap().event(event, event_loop); let (client, ui) = self.client.as_mut().unwrap();
client.event(event, event_loop, ui);
} }
} }

58
src/testing/input.rs Normal file
View File

@@ -0,0 +1,58 @@
use gui::{SenseCtx, SenseTrigger, Vec2};
use winit::event::WindowEvent;
use crate::testing::Client;
#[derive(Default)]
pub struct Input {
size: Vec2,
mouse_pos: Vec2,
mouse_pressed: bool,
mouse_just_pressed: bool,
}
impl Input {
pub fn event(&mut self, event: &WindowEvent) {
self.mouse_just_pressed = false;
match event {
WindowEvent::Resized(size) => {
self.size = Vec2::new(size.width as f32, size.height as f32)
}
WindowEvent::CursorMoved { position, .. } => {
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32)
}
WindowEvent::MouseInput { state, button, .. } => match button {
winit::event::MouseButton::Left => {
if state.is_pressed() {
if !self.mouse_pressed {
self.mouse_just_pressed = true;
} else {
self.mouse_just_pressed = false;
}
self.mouse_pressed = true;
} else {
self.mouse_pressed = false;
}
}
_ => (),
},
_ => (),
}
}
fn active(&mut self, trigger: &SenseTrigger) -> bool {
let region = trigger.shape.to_screen(self.size);
if !region.contains(self.mouse_pos) {
return false;
}
match trigger.sense {
gui::Sense::Click => self.mouse_just_pressed,
gui::Sense::Hover => true,
}
}
}
impl SenseCtx for Client {
fn active(&mut self, trigger: &SenseTrigger) -> bool {
self.input.active(trigger)
}
}

View File

@@ -5,39 +5,44 @@ use gui::*;
use render::Renderer; use render::Renderer;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window}; use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
use crate::testing::input::Input;
mod app; mod app;
mod input;
mod render; mod render;
pub fn main() { pub fn main() {
App::run(); App::run();
} }
struct Data {
x: u32,
}
pub struct Client { pub struct Client {
renderer: Renderer, renderer: Renderer,
ui: Ui<Data>, input: Input,
ui: UiIds,
}
pub struct UiIds {
test: WidgetId<Span>, test: WidgetId<Span>,
} }
impl Client { impl Client {
pub fn new(window: Arc<Window>) -> Self { pub fn create_ui() -> (Ui<Self>, UiIds) {
let renderer = Renderer::new(window); let mut ui = Ui::new();
let test = ui.id();
let rect = Rect { let rect = Rect {
color: UiColor::WHITE, color: UiColor::WHITE,
radius: 20.0, radius: 20.0,
thickness: 0.0, thickness: 0.0,
inner_radius: 0.0, inner_radius: 0.0,
}; };
let mut ui = Ui::new();
let test = ui.id();
ui.set_base( ui.set_base(
( (
( (
rect.color(UiColor::BLUE) rect.color(UiColor::BLUE)
.sense(|d: &mut Data| println!("{}", d.x)), .sense(Sense::Click, |id, ui, client| {
println!("hello!");
ui[id].color.a -= 1;
}),
( (
rect.color(UiColor::RED).center((100.0, 100.0)), rect.color(UiColor::RED).center((100.0, 100.0)),
( (
@@ -75,26 +80,33 @@ impl Client {
.span(Dir::DOWN, [3, 1, 1]) .span(Dir::DOWN, [3, 1, 1])
.pad(10), .pad(10),
); );
Self { renderer, ui, test } (ui, UiIds { test })
} }
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) { pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
let renderer = Renderer::new(window);
Self {
renderer,
ui,
input: Input::default(),
}
}
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
self.input.event(&event);
ui.run_sensors(self);
match event { match event {
WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
let primitives = self.ui.update(&mut Data { x: 39 }); let primitives = ui.update(self);
self.renderer.update(primitives); self.renderer.update(primitives);
self.renderer.draw() self.renderer.draw()
} }
WindowEvent::Resized(size) => self.renderer.resize(&size), WindowEvent::Resized(size) => self.renderer.resize(&size),
WindowEvent::KeyboardInput { event, .. } => {
if event.state.is_pressed() {
let child = self.ui.add(Rect::new(Color::YELLOW)).erase_type();
self.ui[&self.test].children.push((child, fixed(20.0)));
self.renderer.window().request_redraw();
}
}
_ => (), _ => (),
} }
if ui.needs_redraw() {
self.renderer.window().request_redraw();
}
} }
} }