TAG TECHNOLOGY

This commit is contained in:
2025-08-14 12:21:26 -04:00
parent 4d68fa476d
commit e41970287d
19 changed files with 267 additions and 165 deletions

View File

@@ -1,37 +0,0 @@
use super::*;
use crate::{UIRegion, Ui, Vec2, WidgetArrLike, WidgetFn, WidgetFnRet, WidgetLike};
pub trait BaseWidget {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned);
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned);
}
impl<W: WidgetLike> BaseWidget for W {
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned) {
WidgetFn(|ui| Regioned {
region: padding.into().region(),
inner: self.add(ui).erase_type(),
})
}
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned) {
WidgetFn(|ui| Regioned {
region: UIRegion::center(size.into()),
inner: self.add(ui).erase_type(),
})
}
}
pub trait BaseWidgetArr<const LEN: usize> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span);
}
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> BaseWidgetArr<LEN> for Wa {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span) {
let lengths = lengths.map(Into::into);
WidgetFn(move |ui| Span {
dir,
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
})
}
}

View File

@@ -1,12 +1,12 @@
use crate::{Painter, UINum, UIRegion, Widget, WidgetId}; use crate::{Painter, UINum, UiRegion, Widget, WidgetId};
pub struct Regioned { pub struct Regioned {
pub region: UIRegion, pub region: UiRegion,
pub inner: WidgetId, pub inner: WidgetId,
} }
impl Widget for Regioned { impl<Ctx: 'static> Widget<Ctx> for Regioned {
fn draw(&self, painter: &mut Painter) { fn draw(&self, painter: &mut Painter<Ctx>) {
painter.region.select(&self.region); painter.region.select(&self.region);
painter.draw(&self.inner); painter.draw(&self.inner);
} }
@@ -28,8 +28,8 @@ impl Padding {
bottom: amt, bottom: amt,
} }
} }
pub fn region(&self) -> UIRegion { pub fn region(&self) -> UiRegion {
let mut region = UIRegion::full(); let mut region = UiRegion::full();
region.top_left.offset.x += self.left; region.top_left.offset.x += self.left;
region.top_left.offset.y += self.top; region.top_left.offset.y += self.top;
region.bot_right.offset.x -= self.right; region.bot_right.offset.x -= self.right;

View File

@@ -1,11 +1,13 @@
mod frame; mod frame;
mod num; mod num;
mod rect; mod rect;
mod sense;
mod span; mod span;
mod trait_fns; mod trait_fns;
pub use frame::*; pub use frame::*;
pub use num::*; pub use num::*;
pub use rect::*; pub use rect::*;
pub use sense::*;
pub use span::*; pub use span::*;
pub use trait_fns::*; pub use trait_fns::*;

View File

@@ -23,8 +23,8 @@ impl Rect {
} }
} }
impl Widget for Rect { impl<Ctx> Widget<Ctx> for Rect {
fn draw(&self, painter: &mut Painter) { fn draw(&self, painter: &mut Painter<Ctx>) {
painter.write(RoundedRectData { painter.write(RoundedRectData {
color: self.color, color: self.color,
radius: self.radius, radius: self.radius,

35
src/core/sense.rs Normal file
View File

@@ -0,0 +1,35 @@
use std::marker::PhantomData;
use crate::{Painter, Widget, WidgetFn, WidgetId, WidgetLike};
pub struct Sensor<F: SenseFn<Ctx>, Ctx> {
inner: WidgetId,
f: F,
_pd: PhantomData<Ctx>,
}
impl<F: SenseFn<Ctx>, Ctx: 'static> Widget<Ctx> for Sensor<F, Ctx> {
fn draw(&self, painter: &mut Painter<Ctx>) {
(self.f)(painter.ctx_mut());
painter.draw(&self.inner);
}
}
pub trait SenseFn<Ctx> = Fn(&mut Ctx) + 'static;
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 {
fn sense<F: SenseFn<Ctx>>(self, f: F) -> impl WidgetFn<Sensor<F, Ctx>, Ctx> {
|ui| {
let inner = self.add(ui).erase_type();
Sensor {
inner,
f,
_pd: PhantomData,
}
}
}
}

View File

@@ -1,16 +1,16 @@
use crate::{Dir, Painter, Sign, UINum, UIRegion, UIScalar, Widget, WidgetId}; use crate::{Dir, Painter, Sign, UINum, UiRegion, UIScalar, Widget, WidgetId};
pub struct Span { pub struct Span {
pub children: Vec<(WidgetId, SpanLen)>, pub children: Vec<(WidgetId, SpanLen)>,
pub dir: Dir, pub dir: Dir,
} }
impl Widget for Span { impl<Ctx: 'static> Widget<Ctx> for Span {
fn draw(&self, painter: &mut Painter) { fn draw(&self, painter: &mut Painter<Ctx>) {
let total = self.sums(); let total = self.sums();
let mut start = UIScalar::min(); let mut start = UIScalar::min();
for (child, length) in &self.children { for (child, length) in &self.children {
let mut child_region = UIRegion::full(); let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis); let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start); axis.top_left.set(start);
match *length { match *length {

39
src/core/trait_fns.rs Normal file
View File

@@ -0,0 +1,39 @@
use super::*;
use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
pub trait CoreWidget<Ctx: 'static, Tag> {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx>;
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx>;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx> {
|ui| Regioned {
region: padding.into().region(),
inner: self.add(ui).erase_type(),
}
}
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx> {
|ui| Regioned {
region: UiRegion::center(size.into()),
inner: self.add(ui).erase_type(),
}
}
}
pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx>;
}
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag> CoreWidgetArr<LEN, Ctx, Tag>
for Wa
{
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx> {
let lengths = lengths.map(Into::into);
move |ui| Span {
dir,
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
}
}
}

View File

@@ -6,10 +6,10 @@ mod vec2;
mod widget; mod widget;
pub use color::*; pub use color::*;
pub use painter::*;
pub use region::*; pub use region::*;
pub use ui::*; pub use ui::*;
pub use vec2::*; pub use vec2::*;
pub use widget::*; pub use widget::*;
pub use painter::*;
pub type UiColor = Color<u8>; pub type UiColor = Color<u8>;

View File

@@ -1,20 +1,22 @@
use crate::{ use crate::{
UIRegion, WidgetId, Widgets, UiRegion, WidgetId, Widgets,
primitive::{PrimitiveData, PrimitiveInstance, Primitives}, primitive::{PrimitiveData, PrimitiveInstance, Primitives},
}; };
pub struct Painter<'a> { pub struct Painter<'a, Ctx> {
nodes: &'a Widgets, nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
primitives: Primitives, primitives: Primitives,
pub region: UIRegion, pub region: UiRegion,
} }
impl<'a> Painter<'a> { impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(nodes: &'a Widgets) -> Self { pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx) -> Self {
Self { Self {
nodes, nodes,
ctx,
primitives: Primitives::default(), primitives: Primitives::default(),
region: UIRegion::full(), region: UiRegion::full(),
} }
} }
pub fn write<Data: PrimitiveData>(&mut self, data: Data) { pub fn write<Data: PrimitiveData>(&mut self, data: Data) {
@@ -28,10 +30,12 @@ impl<'a> Painter<'a> {
.data .data
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data])); .extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
} }
pub fn draw(&mut self, node: &WidgetId) {
self.nodes.get_dyn(node).draw(self); 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) {
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);
@@ -41,4 +45,8 @@ impl<'a> Painter<'a> {
pub fn finish(self) -> Primitives { pub fn finish(self) -> Primitives {
self.primitives self.primitives
} }
pub fn ctx_mut(&mut self) -> &mut Ctx {
&mut self.ctx
}
} }

View File

@@ -36,7 +36,7 @@ impl UIPos {
self self
} }
pub const fn within(&self, region: &UIRegion) -> UIPos { pub const fn within(&self, region: &UiRegion) -> UIPos {
let anchor = self let anchor = self
.anchor .anchor
.lerp(region.top_left.anchor, region.bot_right.anchor); .lerp(region.top_left.anchor, region.bot_right.anchor);
@@ -110,12 +110,12 @@ impl UIScalar {
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UIRegion { pub struct UiRegion {
pub top_left: UIPos, pub top_left: UIPos,
pub bot_right: UIPos, pub bot_right: UIPos,
} }
impl UIRegion { impl UiRegion {
pub const fn full() -> Self { pub const fn full() -> Self {
Self { Self {
top_left: UIPos::top_left(), top_left: UIPos::top_left(),

View File

@@ -1,46 +1,60 @@
use crate::{ use crate::{
HashMap, Painter, Widget, WidgetId, WidgetLike, HashMap, Painter, Vec2, Widget, WidgetId, WidgetLike,
primitive::Primitives, primitive::Primitives,
util::{ID, IDTracker}, util::{IDTracker, Id},
}; };
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
ops::{Index, IndexMut}, ops::{Index, IndexMut},
}; };
#[derive(Default)] pub struct Ui<Ctx> {
pub struct Ui {
ids: IDTracker, ids: IDTracker,
base: Option<WidgetId>, base: Option<WidgetId>,
widgets: Widgets, widgets: Widgets<Ctx>,
updates: Vec<WidgetId>, updates: Vec<WidgetId>,
primitives: Primitives, primitives: Primitives,
full_redraw: bool, full_redraw: bool,
} }
#[derive(Default)] pub struct MouseState {
pub struct Widgets(HashMap<ID, Box<dyn Widget>>); pos: Vec2,
primary: ButtonState,
secondary: ButtonState,
}
impl Ui { pub enum ButtonState {
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetId<W> { JustPressed,
Pressed,
Released,
}
#[derive(Default)]
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>);
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget<Ctx>, Tag>(
&mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>,
) -> WidgetId<W> {
w.add(self) w.add(self)
} }
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> { pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
self.push(w) self.push(w)
} }
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> { pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
let id = self.ids.next(); let id = self.ids.next();
self.widgets.insert(id.duplicate(), w); self.widgets.insert(id.duplicate(), w);
WidgetId::new(id, TypeId::of::<W>()) WidgetId::new(id, TypeId::of::<W>())
} }
pub fn set<W: Widget>(&mut self, i: &WidgetId<W>, w: W) { pub fn set<W: Widget<Ctx>>(&mut self, i: &WidgetId<W>, w: W) {
self.widgets.insert(i.id.duplicate(), w); self.widgets.insert(i.id.duplicate(), w);
} }
pub fn set_base(&mut self, w: impl WidgetLike) { pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
self.base = Some(w.add(self).erase_type()); self.base = Some(w.add(self).erase_type());
self.full_redraw = true; self.full_redraw = true;
} }
@@ -49,36 +63,42 @@ impl Ui {
Self::default() Self::default()
} }
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> { pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
self.widgets.get(id) self.widgets.get(id)
} }
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> { pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.widgets.get_mut(id) self.widgets.get_mut(id)
} }
pub fn id<W: Widget>(&mut self) -> WidgetId<W> { pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
WidgetId::new(self.ids.next(), TypeId::of::<W>()) WidgetId::new(self.ids.next(), TypeId::of::<W>())
} }
pub fn redraw_all(&mut self) { pub fn redraw_all(&mut self, ctx: &mut Ctx)
let mut painter = Painter::new(&self.widgets); where
Ctx: 'static,
{
let mut painter = Painter::new(&self.widgets, ctx);
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 update(&mut self) -> Option<&Primitives> { pub fn update(&mut self, ctx: &mut Ctx) -> Option<&Primitives>
where
Ctx: 'static,
{
if self.full_redraw { if self.full_redraw {
self.redraw_all(); self.redraw_all(ctx);
self.full_redraw = false; self.full_redraw = false;
return Some(&self.primitives); return Some(&self.primitives);
} }
if self.updates.is_empty() { if self.updates.is_empty() {
return None; return None;
} }
self.redraw_all(); self.redraw_all(ctx);
self.updates.drain(..); self.updates.drain(..);
Some(&self.primitives) Some(&self.primitives)
} }
@@ -86,9 +106,11 @@ impl Ui {
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> Index<&WidgetId<W>> for Ui { impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
type Output = W; type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output { fn index(&self, id: &WidgetId<W>) -> &Self::Output {
@@ -96,36 +118,40 @@ impl<W: Widget> Index<&WidgetId<W>> for Ui {
} }
} }
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui { impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.clone().erase_type()); self.updates.push(id.clone().erase_type());
self.get_mut(id).unwrap() self.get_mut(id).unwrap()
} }
} }
impl Widgets { impl<Ctx> Widgets<Ctx> {
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget { pub fn new() -> Self {
Self(HashMap::new())
}
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget<Ctx> {
self.0.get(&id.id).unwrap().as_ref() self.0.get(&id.id).unwrap().as_ref()
} }
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> { pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
self.0.get(&id.id).unwrap().as_any().downcast_ref() self.0.get(&id.id).unwrap().as_any().downcast_ref()
} }
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> { pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.0.get_mut(&id.id).unwrap().as_any_mut().downcast_mut() self.0.get_mut(&id.id).unwrap().as_any_mut().downcast_mut()
} }
pub fn insert(&mut self, id: ID, widget: impl Widget) { pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
self.0.insert(id, Box::new(widget)); self.0.insert(id, Box::new(widget));
} }
pub fn insert_any(&mut self, id: ID, widget: Box<dyn Widget>) { pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
self.0.insert(id, widget); self.0.insert(id, widget);
} }
} }
impl dyn Widget { impl<Ctx> dyn Widget<Ctx> {
pub fn as_any(&self) -> &dyn Any { pub fn as_any(&self) -> &dyn Any {
self self
} }
@@ -134,3 +160,16 @@ impl dyn Widget {
self self
} }
} }
impl<Ctx> Default for Ui<Ctx> {
fn default() -> Self {
Self {
ids: Default::default(),
base: Default::default(),
widgets: Widgets::new(),
updates: Default::default(),
primitives: Default::default(),
full_redraw: Default::default(),
}
}
}

View File

@@ -3,10 +3,10 @@ use std::{
marker::PhantomData, marker::PhantomData,
}; };
use crate::{Painter, Ui, util::ID}; use crate::{Painter, Ui, util::Id};
pub trait Widget: 'static + Any { pub trait Widget<Ctx>: Any {
fn draw(&self, painter: &mut Painter); fn draw(&self, painter: &mut Painter<Ctx>);
} }
pub struct AnyWidget; pub struct AnyWidget;
@@ -19,7 +19,7 @@ pub struct AnyWidget;
#[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,
pub(super) id: ID, pub(super) id: Id,
_pd: PhantomData<W>, _pd: PhantomData<W>,
} }
@@ -31,7 +31,7 @@ impl<W> Clone for WidgetId<W> {
} }
impl<W> WidgetId<W> { impl<W> WidgetId<W> {
pub(super) fn new(id: ID, ty: TypeId) -> Self { pub(super) fn new(id: Id, ty: TypeId) -> Self {
Self { Self {
ty, ty,
id, id,
@@ -51,85 +51,81 @@ impl<W> WidgetId<W> {
} }
} }
pub trait WidgetLike { pub struct WidgetTag;
pub struct FnTag;
pub struct IdTag;
pub trait WidgetLike<Ctx, Tag> {
type Widget; type Widget;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>; fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
} }
/// A function that returns a widget given a UI. /// A function that returns a widget given a UI.
/// Useful for defining trait functions on widgets that create a parent widget so that the children /// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet /// don't need to be IDs yet
pub trait WFn<W> = FnOnce(&mut Ui) -> W; pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F); pub trait WidgetIdFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F);
macro_rules! WidgetFnRet { pub trait Idable<Ctx, Tag> {
($W:ident) => { type Widget: Widget<Ctx>;
WidgetFn<impl FnOnce(&mut Ui) -> $W, $W> fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
};
}
pub(crate) use WidgetFnRet;
pub trait Idable {
type Widget: Widget;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
} }
pub trait WidgetFns<W> { pub trait WidgetFns<W: Widget<Ctx>, Ctx, Tag> {
fn id(self, id: &WidgetId<W>) -> impl WidgetLike<Widget = W>; fn id(self, id: &WidgetId<W>) -> impl WidgetIdFn<W, Ctx>;
} }
impl<I: Idable> WidgetFns<I::Widget> for I { impl<I: Idable<Ctx, Tag>, Ctx, Tag> WidgetFns<I::Widget, Ctx, Tag> for I {
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetLike<Widget = I::Widget> { fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetIdFn<I::Widget, Ctx> {
WidgetIdFn(|ui| { |ui| {
self.set(ui, id); self.set(ui, id);
id.clone() id.clone()
}) }
} }
} }
impl<W: Widget> Idable for W { impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
type Widget = W; type Widget = W;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) { fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
ui.set(id, self); ui.set(id, self);
} }
} }
impl<F: for<'a> FnOnce(&'a mut Ui) -> W, W: Widget> Idable for WidgetFn<F, W> { impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
type Widget = W; type Widget = W;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) { fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
let w = self.0(ui); let w = self(ui);
ui.set(id, w); ui.set(id, w);
} }
} }
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike for WidgetFn<F, W> { impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> { fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
let w = (self.0)(ui); let w = self(ui);
ui.add(w) ui.add(w)
} }
} }
impl<W: Widget, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike for WidgetIdFn<F, W> { impl<W: Widget<Ctx>, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> { fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
(self.0)(ui) self(ui)
} }
} }
impl<W: Widget> WidgetLike for W { impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
type Widget = W; type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> { fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
ui.add_widget(self) ui.add_widget(self)
} }
} }
impl<W> WidgetLike for WidgetId<W> { impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
type Widget = W; type Widget = W;
fn add(self, _: &mut Ui) -> WidgetId<W> { fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
self self
} }
} }
@@ -148,35 +144,39 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
} }
} }
pub trait WidgetArrLike<const LEN: usize> { pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
type Ws; type Ws;
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>; fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
} }
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> { impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws; type Ws = Ws;
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> { fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
self self
} }
} }
impl<W: WidgetLike> WidgetArrLike<1> for W { impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
type Ws = (W::Widget,); type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> { fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).erase_type()]) WidgetArr::new([self.add(ui).erase_type()])
} }
} }
// I hate this language it's so bad why do I even use it // I hate this language it's so bad why do I even use it
macro_rules! impl_widget_arr { macro_rules! impl_widget_arr {
($n:expr;$($T:tt)*) => { ($n:expr;$($W:ident)*) => {
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) { impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
type Ws = ($($T::Widget,)*); };
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($T::Widget,)*)> { ($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<$($W: WidgetLike<Ctx, $Tag>,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)] #[allow(non_snake_case)]
let ($($T,)*) = self; let ($($W,)*) = self;
WidgetArr::new( WidgetArr::new(
[$($T.add(ui).cast_type(),)*], [$($W.add(ui).cast_type(),)*],
) )
} }
} }

View File

@@ -3,14 +3,16 @@
#![feature(const_trait_impl)] #![feature(const_trait_impl)]
#![feature(const_from)] #![feature(const_from)]
#![feature(trait_alias)] #![feature(trait_alias)]
#![feature(negative_impls)]
mod layout; mod layout;
mod render; mod render;
mod util; mod util;
mod base; mod core;
pub use layout::*; pub use layout::*;
pub use render::*; pub use render::*;
pub use base::*; pub use core::*;
pub type HashMap<K, V> = std::collections::HashMap<K, V>; pub type HashMap<K, V> = std::collections::HashMap<K, V>;
pub type HashSet<K> = std::collections::HashSet<K>;

View File

@@ -1,6 +1,6 @@
use wgpu::VertexAttribute; use wgpu::VertexAttribute;
use crate::UIRegion; use crate::UiRegion;
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
@@ -12,7 +12,7 @@ pub struct WindowUniform {
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct PrimitiveInstance { pub struct PrimitiveInstance {
pub region: UIRegion, pub region: UiRegion,
pub ptr: u32, pub ptr: u32,
} }

View File

@@ -1,4 +1,7 @@
use crate::{Ui, primitive::PrimitiveInstance, render::util::ArrBuf}; use crate::{
primitive::{PrimitiveInstance, Primitives},
render::util::ArrBuf,
};
use data::WindowUniform; use data::WindowUniform;
use wgpu::{ use wgpu::{
util::{BufferInitDescriptor, DeviceExt}, util::{BufferInitDescriptor, DeviceExt},
@@ -32,8 +35,13 @@ impl UIRenderNode {
} }
} }
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) { pub fn update(
if let Some(primitives) = ui.update() { &mut self,
device: &Device,
queue: &Queue,
primitives: Option<&Primitives>,
) {
if let Some(primitives) = primitives {
self.instance.update(device, queue, &primitives.instances); self.instance.update(device, queue, &primitives.instances);
self.data.update(device, queue, &primitives.data); self.data.update(device, queue, &primitives.data);
self.bind_group = Self::bind_group( self.bind_group = Self::bind_group(

View File

@@ -12,9 +12,13 @@ pub fn main() {
App::run(); App::run();
} }
struct Data {
x: u32,
}
pub struct Client { pub struct Client {
renderer: Renderer, renderer: Renderer,
ui: Ui, ui: Ui<Data>,
test: WidgetId<Span>, test: WidgetId<Span>,
} }
@@ -32,7 +36,8 @@ impl Client {
ui.set_base( ui.set_base(
( (
( (
rect.color(UiColor::BLUE), rect.color(UiColor::BLUE)
.sense(|d: &mut Data| println!("{}", d.x)),
( (
rect.color(UiColor::RED).center((100.0, 100.0)), rect.color(UiColor::RED).center((100.0, 100.0)),
( (
@@ -77,7 +82,8 @@ impl Client {
match event { match event {
WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
self.renderer.update(&mut self.ui); let primitives = self.ui.update(&mut Data { x: 39 });
self.renderer.update(primitives);
self.renderer.draw() self.renderer.draw()
} }
WindowEvent::Resized(size) => self.renderer.resize(&size), WindowEvent::Resized(size) => self.renderer.resize(&size),

View File

@@ -1,4 +1,4 @@
use gui::{UIRenderNode, Ui}; use gui::{primitive::Primitives, UIRenderNode, Ui};
use pollster::FutureExt; use pollster::FutureExt;
use std::sync::Arc; use std::sync::Arc;
use wgpu::util::StagingBelt; use wgpu::util::StagingBelt;
@@ -18,8 +18,8 @@ pub struct Renderer {
} }
impl Renderer { impl Renderer {
pub fn update(&mut self, ui: &mut Ui) { pub fn update(&mut self, primitives: Option<&Primitives>) {
self.ui_node.update(&self.device, &self.queue, ui); self.ui_node.update(&self.device, &self.queue, primitives);
} }
pub fn draw(&mut self) { pub fn draw(&mut self) {

View File

@@ -4,11 +4,11 @@
/// point to something valid, although duplicate /// point to something valid, although duplicate
/// gets around this if needed /// gets around this if needed
#[derive(Eq, Hash, PartialEq, Debug)] #[derive(Eq, Hash, PartialEq, Debug)]
pub struct ID(u64); pub struct Id(u64);
#[derive(Default)] #[derive(Default)]
pub struct IDTracker { pub struct IDTracker {
free: Vec<ID>, free: Vec<Id>,
cur: u64, cur: u64,
} }
@@ -18,22 +18,22 @@ impl IDTracker {
} }
#[allow(clippy::should_implement_trait)] #[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> ID { pub fn next(&mut self) -> Id {
if let Some(id) = self.free.pop() { if let Some(id) = self.free.pop() {
return id; return id;
} }
let id = ID(self.cur); let id = Id(self.cur);
self.cur += 1; self.cur += 1;
id id
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn free(&mut self, id: ID) { pub fn free(&mut self, id: Id) {
self.free.push(id); self.free.push(id);
} }
} }
impl ID { impl Id {
/// this must be used carefully to make sure /// this must be used carefully to make sure
/// all IDs are still valid references; /// all IDs are still valid references;
/// named weirdly to indicate this. /// named weirdly to indicate this.