TAG TECHNOLOGY
This commit is contained in:
@@ -6,10 +6,10 @@ mod vec2;
|
||||
mod widget;
|
||||
|
||||
pub use color::*;
|
||||
pub use painter::*;
|
||||
pub use region::*;
|
||||
pub use ui::*;
|
||||
pub use vec2::*;
|
||||
pub use widget::*;
|
||||
pub use painter::*;
|
||||
|
||||
pub type UiColor = Color<u8>;
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
use crate::{
|
||||
UIRegion, WidgetId, Widgets,
|
||||
UiRegion, WidgetId, Widgets,
|
||||
primitive::{PrimitiveData, PrimitiveInstance, Primitives},
|
||||
};
|
||||
|
||||
pub struct Painter<'a> {
|
||||
nodes: &'a Widgets,
|
||||
pub struct Painter<'a, Ctx> {
|
||||
nodes: &'a Widgets<Ctx>,
|
||||
ctx: &'a mut Ctx,
|
||||
primitives: Primitives,
|
||||
pub region: UIRegion,
|
||||
pub region: UiRegion,
|
||||
}
|
||||
|
||||
impl<'a> Painter<'a> {
|
||||
pub fn new(nodes: &'a Widgets) -> Self {
|
||||
impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx) -> Self {
|
||||
Self {
|
||||
nodes,
|
||||
ctx,
|
||||
primitives: Primitives::default(),
|
||||
region: UIRegion::full(),
|
||||
region: UiRegion::full(),
|
||||
}
|
||||
}
|
||||
pub fn write<Data: PrimitiveData>(&mut self, data: Data) {
|
||||
@@ -28,10 +30,12 @@ impl<'a> Painter<'a> {
|
||||
.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;
|
||||
self.region.select(®ion);
|
||||
self.draw(node);
|
||||
@@ -41,4 +45,8 @@ impl<'a> Painter<'a> {
|
||||
pub fn finish(self) -> Primitives {
|
||||
self.primitives
|
||||
}
|
||||
|
||||
pub fn ctx_mut(&mut self) -> &mut Ctx {
|
||||
&mut self.ctx
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ impl UIPos {
|
||||
self
|
||||
}
|
||||
|
||||
pub const fn within(&self, region: &UIRegion) -> UIPos {
|
||||
pub const fn within(&self, region: &UiRegion) -> UIPos {
|
||||
let anchor = self
|
||||
.anchor
|
||||
.lerp(region.top_left.anchor, region.bot_right.anchor);
|
||||
@@ -110,12 +110,12 @@ impl UIScalar {
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct UIRegion {
|
||||
pub struct UiRegion {
|
||||
pub top_left: UIPos,
|
||||
pub bot_right: UIPos,
|
||||
}
|
||||
|
||||
impl UIRegion {
|
||||
impl UiRegion {
|
||||
pub const fn full() -> Self {
|
||||
Self {
|
||||
top_left: UIPos::top_left(),
|
||||
|
||||
@@ -1,46 +1,60 @@
|
||||
use crate::{
|
||||
HashMap, Painter, Widget, WidgetId, WidgetLike,
|
||||
HashMap, Painter, Vec2, Widget, WidgetId, WidgetLike,
|
||||
primitive::Primitives,
|
||||
util::{ID, IDTracker},
|
||||
util::{IDTracker, Id},
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
ops::{Index, IndexMut},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Ui {
|
||||
pub struct Ui<Ctx> {
|
||||
ids: IDTracker,
|
||||
base: Option<WidgetId>,
|
||||
widgets: Widgets,
|
||||
widgets: Widgets<Ctx>,
|
||||
updates: Vec<WidgetId>,
|
||||
primitives: Primitives,
|
||||
full_redraw: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets(HashMap<ID, Box<dyn Widget>>);
|
||||
pub struct MouseState {
|
||||
pos: Vec2,
|
||||
primary: ButtonState,
|
||||
secondary: ButtonState,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetId<W> {
|
||||
pub enum ButtonState {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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();
|
||||
self.widgets.insert(id.duplicate(), 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);
|
||||
}
|
||||
|
||||
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.full_redraw = true;
|
||||
}
|
||||
@@ -49,36 +63,42 @@ impl Ui {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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>())
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self) {
|
||||
let mut painter = Painter::new(&self.widgets);
|
||||
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
let mut painter = Painter::new(&self.widgets, ctx);
|
||||
if let Some(base) = &self.base {
|
||||
painter.draw(base);
|
||||
}
|
||||
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 {
|
||||
self.redraw_all();
|
||||
self.redraw_all(ctx);
|
||||
self.full_redraw = false;
|
||||
return Some(&self.primitives);
|
||||
}
|
||||
if self.updates.is_empty() {
|
||||
return None;
|
||||
}
|
||||
self.redraw_all();
|
||||
self.redraw_all(ctx);
|
||||
self.updates.drain(..);
|
||||
Some(&self.primitives)
|
||||
}
|
||||
@@ -86,9 +106,11 @@ impl Ui {
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
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;
|
||||
|
||||
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 {
|
||||
self.updates.push(id.clone().erase_type());
|
||||
self.get_mut(id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget {
|
||||
impl<Ctx> Widgets<Ctx> {
|
||||
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()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Widget {
|
||||
impl<Ctx> dyn Widget<Ctx> {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
@@ -134,3 +160,16 @@ impl dyn Widget {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ use std::{
|
||||
marker::PhantomData,
|
||||
};
|
||||
|
||||
use crate::{Painter, Ui, util::ID};
|
||||
use crate::{Painter, Ui, util::Id};
|
||||
|
||||
pub trait Widget: 'static + Any {
|
||||
fn draw(&self, painter: &mut Painter);
|
||||
pub trait Widget<Ctx>: Any {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>);
|
||||
}
|
||||
|
||||
pub struct AnyWidget;
|
||||
@@ -19,7 +19,7 @@ pub struct AnyWidget;
|
||||
#[derive(Eq, Hash, PartialEq, Debug)]
|
||||
pub struct WidgetId<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
pub(super) id: ID,
|
||||
pub(super) id: Id,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ impl<W> Clone for 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 {
|
||||
ty,
|
||||
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;
|
||||
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.
|
||||
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
||||
/// don't need to be IDs yet
|
||||
pub trait WFn<W> = FnOnce(&mut Ui) -> W;
|
||||
pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F);
|
||||
pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F);
|
||||
pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
|
||||
pub trait WidgetIdFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
||||
|
||||
macro_rules! WidgetFnRet {
|
||||
($W:ident) => {
|
||||
WidgetFn<impl FnOnce(&mut Ui) -> $W, $W>
|
||||
};
|
||||
}
|
||||
pub(crate) use WidgetFnRet;
|
||||
|
||||
pub trait Idable {
|
||||
type Widget: Widget;
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||
pub trait Idable<Ctx, Tag> {
|
||||
type Widget: Widget<Ctx>;
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
||||
}
|
||||
|
||||
pub trait WidgetFns<W> {
|
||||
fn id(self, id: &WidgetId<W>) -> impl WidgetLike<Widget = W>;
|
||||
pub trait WidgetFns<W: Widget<Ctx>, Ctx, Tag> {
|
||||
fn id(self, id: &WidgetId<W>) -> impl WidgetIdFn<W, Ctx>;
|
||||
}
|
||||
|
||||
impl<I: Idable> WidgetFns<I::Widget> for I {
|
||||
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetLike<Widget = I::Widget> {
|
||||
WidgetIdFn(|ui| {
|
||||
impl<I: Idable<Ctx, Tag>, Ctx, Tag> WidgetFns<I::Widget, Ctx, Tag> for I {
|
||||
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetIdFn<I::Widget, Ctx> {
|
||||
|ui| {
|
||||
self.set(ui, id);
|
||||
id.clone()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> Idable for W {
|
||||
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for 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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||
let w = self.0(ui);
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||
let w = self(ui);
|
||||
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;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
let w = (self.0)(ui);
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
let w = self(ui);
|
||||
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;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
(self.0)(ui)
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> WidgetLike for W {
|
||||
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for 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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetLike for WidgetId<W> {
|
||||
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
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;
|
||||
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;
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: WidgetLike> WidgetArrLike<1> for W {
|
||||
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
|
||||
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()])
|
||||
}
|
||||
}
|
||||
|
||||
// I hate this language it's so bad why do I even use it
|
||||
macro_rules! impl_widget_arr {
|
||||
($n:expr;$($T:tt)*) => {
|
||||
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
||||
type Ws = ($($T::Widget,)*);
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($T::Widget,)*)> {
|
||||
($n:expr;$($W:ident)*) => {
|
||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||
};
|
||||
($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)]
|
||||
let ($($T,)*) = self;
|
||||
let ($($W,)*) = self;
|
||||
WidgetArr::new(
|
||||
[$($T.add(ui).cast_type(),)*],
|
||||
[$($W.add(ui).cast_type(),)*],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user