Compare commits
1 Commits
6251c23d37
...
tuple_expe
| Author | SHA1 | Date | |
|---|---|---|---|
| a6b36fdcca |
@@ -2,7 +2,8 @@ use std::ops::Range;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
primitive::{Axis, Painter, RoundedRectData, UIRegion},
|
primitive::{Axis, Painter, RoundedRectData, UIRegion},
|
||||||
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
|
ToId, UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetIdLikeTuple, WidgetLike,
|
||||||
|
WidgetLikeTuple,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
@@ -134,9 +135,12 @@ pub trait WidgetArrUtil<const LEN: usize> {
|
|||||||
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
|
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa {
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa
|
||||||
|
where
|
||||||
|
<Wa::Ws as WidgetLikeTuple<LEN>>::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||||
|
{
|
||||||
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
|
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
|
||||||
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).arr))
|
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).erase_types()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,10 +32,10 @@ impl From<UI> for UIBuilder {
|
|||||||
|
|
||||||
impl UIBuilder {
|
impl UIBuilder {
|
||||||
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
|
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
|
||||||
WidgetRef::new(self.clone(), [self.push(w)])
|
WidgetRef::new(self.clone(), (self.push(w),))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {
|
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
let mut ui = self.ui.borrow_mut();
|
let mut ui = self.ui.borrow_mut();
|
||||||
let id = ui.ids.next();
|
let id = ui.ids.next();
|
||||||
ui.widgets.insert(id.duplicate(), w);
|
ui.widgets.insert(id.duplicate(), w);
|
||||||
|
|||||||
@@ -3,7 +3,11 @@ use std::{
|
|||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{primitive::Painter, util::ID, UIBuilder};
|
use crate::{
|
||||||
|
primitive::Painter,
|
||||||
|
util::{impl_tuple, ID},
|
||||||
|
UIBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait Widget: 'static + Any {
|
pub trait Widget: 'static + Any {
|
||||||
fn draw(&self, painter: &mut Painter);
|
fn draw(&self, painter: &mut Painter);
|
||||||
@@ -23,7 +27,7 @@ pub struct WidgetId<W = ()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: temp
|
// TODO: temp
|
||||||
impl Clone for WidgetId {
|
impl<W: Widget> Clone for WidgetId<W> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ty: self.ty,
|
ty: self.ty,
|
||||||
@@ -55,7 +59,7 @@ impl<W> WidgetId<W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetLike {
|
pub trait WidgetLike {
|
||||||
type Widget;
|
type Widget: Widget;
|
||||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,89 +81,98 @@ impl<W: Widget> WidgetLike for W {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetId<W> {
|
impl<W: Widget> WidgetLike for WidgetId<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetArr<1, (W,)> {
|
impl<W: Widget> WidgetLike for WidgetArr<1, (W,)> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||||
let [id] = self.arr;
|
self.arr.0
|
||||||
id.cast_type()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetArr<const LEN: usize, Ws> {
|
pub struct WidgetArr<const LEN: usize, Ws: WidgetLikeTuple<LEN>> {
|
||||||
pub ui: UIBuilder,
|
pub ui: UIBuilder,
|
||||||
pub arr: [WidgetId<()>; LEN],
|
pub arr: Ws::Wrap<ToId>,
|
||||||
_pd: PhantomData<Ws>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArr<LEN, Ws>
|
||||||
pub fn new(ui: UIBuilder, arr: [WidgetId<()>; LEN]) -> Self {
|
where
|
||||||
Self {
|
Ws::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||||
ui,
|
{
|
||||||
arr,
|
pub fn new(ui: UIBuilder, arr: Ws::Wrap<ToId>) -> Self {
|
||||||
_pd: PhantomData,
|
Self { ui, arr }
|
||||||
}
|
}
|
||||||
|
pub fn erase_types(self) -> [WidgetId; LEN] {
|
||||||
|
self.arr.map::<EraseId>(&mut ())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
|
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
|
||||||
|
|
||||||
impl<W> WidgetRef<W> {
|
impl<W: WidgetLike> WidgetRef<W> {
|
||||||
pub fn handle(&self) -> WidgetId<W> {
|
pub fn handle(&self) -> WidgetId<W::Widget> {
|
||||||
let [id] = &self.arr;
|
self.arr.0.clone()
|
||||||
id.clone().cast_type()
|
|
||||||
}
|
}
|
||||||
pub fn to_id(self) -> WidgetId<W> {
|
pub fn to_id(self) -> WidgetId<W::Widget> {
|
||||||
let [id] = self.arr;
|
self.arr.0
|
||||||
id.cast_type()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetArrLike<const LEN: usize> {
|
pub trait WidgetArrLike<const LEN: usize> {
|
||||||
type Ws;
|
type Ws: WidgetLikeTuple<LEN>;
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
|
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
||||||
type Ws = Ws;
|
type Ws = Ws;
|
||||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// I hate this language it's so bad why do I even use it
|
impl_tuple!(Widget);
|
||||||
macro_rules! impl_widget_arr {
|
impl_tuple!(WidgetLike);
|
||||||
($n:expr;$($T:tt)*) => {
|
impl_tuple!(WidgetIdLike);
|
||||||
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
|
||||||
type Ws = ($($T::Widget,)*);
|
pub trait WidgetIdLike {
|
||||||
#[allow(unused_variables)]
|
fn erase_type(self) -> WidgetId;
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let ($($T,)*) = self;
|
|
||||||
WidgetArr::new(
|
|
||||||
ui.clone(),
|
|
||||||
[$($T.add(ui).cast_type(),)*],
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_widget_arr!(1;A);
|
impl<W> WidgetIdLike for WidgetId<W> {
|
||||||
impl_widget_arr!(2;A B);
|
fn erase_type(self) -> WidgetId {
|
||||||
impl_widget_arr!(3;A B C);
|
self.erase_type()
|
||||||
impl_widget_arr!(4;A B C D);
|
}
|
||||||
impl_widget_arr!(5;A B C D E);
|
}
|
||||||
impl_widget_arr!(6;A B C D E F);
|
|
||||||
impl_widget_arr!(7;A B C D E F G);
|
pub struct ToId;
|
||||||
impl_widget_arr!(8;A B C D E F G H);
|
impl WidgetLikeWrapper for ToId {
|
||||||
impl_widget_arr!(9;A B C D E F G H I);
|
type Wrap<T: WidgetLike> = WidgetId<T::Widget>;
|
||||||
impl_widget_arr!(10;A B C D E F G H I J);
|
type Ctx = UIBuilder;
|
||||||
impl_widget_arr!(11;A B C D E F G H I J K);
|
fn wrap<T: WidgetLike>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T> {
|
||||||
impl_widget_arr!(12;A B C D E F G H I J K L);
|
t.add(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EraseId;
|
||||||
|
impl WidgetIdLikeMapper for EraseId {
|
||||||
|
type Map = WidgetId<()>;
|
||||||
|
type Ctx = ();
|
||||||
|
fn map<Id: WidgetIdLike>(t: Id, _: &mut Self::Ctx) -> Self::Map {
|
||||||
|
t.erase_type()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: WidgetLikeTuple<LEN>, const LEN: usize> WidgetArrLike<LEN> for T
|
||||||
|
where
|
||||||
|
T::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
|
||||||
|
{
|
||||||
|
type Ws = T;
|
||||||
|
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, T> {
|
||||||
|
WidgetArr::new(ui.clone(), self.wrap::<ToId>(ui))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#![feature(const_trait_impl)]
|
#![feature(const_trait_impl)]
|
||||||
#![feature(const_from)]
|
#![feature(const_from)]
|
||||||
#![feature(trait_alias)]
|
#![feature(trait_alias)]
|
||||||
#![feature(generic_const_exprs)]
|
|
||||||
|
|
||||||
mod layout;
|
mod layout;
|
||||||
mod render;
|
mod render;
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ impl IDTracker {
|
|||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: use this
|
||||||
|
#[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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
mod id;
|
mod id;
|
||||||
|
mod tuple;
|
||||||
|
|
||||||
pub use id::*;
|
pub use id::*;
|
||||||
|
pub use tuple::*;
|
||||||
|
|||||||
54
src/util/tuple.rs
Normal file
54
src/util/tuple.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
macro_rules! impl_tuple {
|
||||||
|
($Tuple:ident $Bound:ident $Wrapper:ident $Mapper:ident $n:expr;$TL:tt $($T:tt)*) => {
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
impl<$($T: $Bound,)* $TL: $Bound> $Tuple<$n> for ($($T,)* $TL,) {
|
||||||
|
type Wrap<W: $Wrapper> = ($(W::Wrap<$T>,)* W::Wrap<$TL>,);
|
||||||
|
type Map<M: $Mapper> = [M::Map; $n];
|
||||||
|
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> ($(W::Wrap<$T>,)* W::Wrap<$TL>,) {
|
||||||
|
let ($($T,)* $TL,) = self;
|
||||||
|
($(W::wrap($T, ctx),)* W::wrap($TL, ctx),)
|
||||||
|
}
|
||||||
|
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; $n] {
|
||||||
|
let ($($T,)* $TL,) = self;
|
||||||
|
[$(M::map($T, ctx),)* M::map($TL, ctx)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
($Tuple:ident, $Bound:ident, $Wrapper:ident, $Mapper:ident) => {
|
||||||
|
pub trait $Wrapper {
|
||||||
|
type Wrap<T: $Bound>;
|
||||||
|
type Ctx;
|
||||||
|
fn wrap<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait $Mapper {
|
||||||
|
type Map;
|
||||||
|
type Ctx;
|
||||||
|
fn map<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Map;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait $Tuple<const LEN: usize> {
|
||||||
|
type Wrap<W: $Wrapper>;
|
||||||
|
type Map<M: $Mapper>;
|
||||||
|
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; LEN];
|
||||||
|
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> Self::Wrap<W>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 1;A);
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 2;A B);
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 3;A B C);
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 4;A B C D);
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 5;A B C D E);
|
||||||
|
impl_tuple!($Tuple $Bound $Wrapper $Mapper 6;A B C D E F);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 7;A B C D E F G);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 8;A B C D E F G H);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 9;A B C D E F G H I);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 10;A B C D E F G H I J);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 11;A B C D E F G H I J K);
|
||||||
|
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 12;A B C D E F G H I J K L);
|
||||||
|
};
|
||||||
|
($Bound:ident) => {
|
||||||
|
impl_tuple!(${concat($Bound, Tuple)}, $Bound, ${concat($Bound, Wrapper)}, ${concat($Bound, Mapper)});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub(crate) use impl_tuple;
|
||||||
Reference in New Issue
Block a user