Compare commits
5 Commits
tuple_atte
...
132113f09e
| Author | SHA1 | Date | |
|---|---|---|---|
| 132113f09e | |||
| f2cbf90d1d | |||
| 848347e6b3 | |||
| 577d62b1da | |||
| b6e43c157b |
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "gui"
|
name = "gui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
primitive::{Axis, Painter, RoundedRectData, UIRegion},
|
|
||||||
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
|
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
|
||||||
|
primitive::{Axis, Painter, RoundedRectData, UIRegion, Vec2},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
@@ -49,22 +49,20 @@ impl Widget for Span {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Span {
|
impl Span {
|
||||||
pub fn proportioned(
|
pub fn proportioned<const LEN: usize>(
|
||||||
axis: Axis,
|
axis: Axis,
|
||||||
elements: impl IntoIterator<Item = (WidgetId, impl UINum)>,
|
ratios: [impl UINum; LEN],
|
||||||
|
elements: [WidgetId; LEN],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
// TODO: update
|
let ratios = ratios.map(|r| r.to_f32());
|
||||||
let elements = elements
|
let total: f32 = ratios.iter().sum();
|
||||||
.into_iter()
|
let mut start = -1.0;
|
||||||
.map(|(w, r)| (w, r.to_f32()))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
let total: f32 = elements.iter().map(|(_, r)| r).sum();
|
|
||||||
let mut start = 0.0;
|
|
||||||
Self {
|
Self {
|
||||||
elements: elements
|
elements: elements
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.zip(ratios)
|
||||||
.map(|(e, r)| {
|
.map(|(e, r)| {
|
||||||
let end = start + r / total;
|
let end = start + (r / total) * 2.0;
|
||||||
let res = (start..end, e);
|
let res = (start..end, e);
|
||||||
start = end;
|
start = end;
|
||||||
res
|
res
|
||||||
@@ -119,26 +117,34 @@ impl<T: UINum> From<T> for Padding {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetUtil<W> {
|
pub trait WidgetUtil {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
|
||||||
|
fn center(self, size: impl Into<Vec2>) -> impl WidgetLike<Widget = Regioned>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike> WidgetUtil<W::Widget> for W {
|
impl<W: WidgetLike> WidgetUtil for W {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned> {
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned> {
|
||||||
WidgetFn(|ui| Regioned {
|
WidgetFn(|ui| Regioned {
|
||||||
region: padding.into().region(),
|
region: padding.into().region(),
|
||||||
inner: self.id(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn center(self, size: impl Into<Vec2>) -> impl WidgetLike<Widget = Regioned> {
|
||||||
|
WidgetFn(|ui| Regioned {
|
||||||
|
region: UIRegion::center(size.into()),
|
||||||
|
inner: self.add(ui).erase_type(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetArrUtil<Wa: WidgetArrLike> {
|
pub trait WidgetArrUtil<const LEN: usize> {
|
||||||
fn span(self, axis: Axis, ratios: [impl UINum; Wa::LEN]) -> impl WidgetLike<Widget = Span>;
|
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Wa: WidgetArrLike> WidgetArrUtil<Wa> for Wa {
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa {
|
||||||
fn span(self, axis: Axis, ratios: [impl UINum; Wa::LEN]) -> impl WidgetLike<Widget = Span> {
|
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
|
||||||
WidgetFn(move |ui| Span::proportioned(axis, self.ui(ui).ids().into_iter().zip(ratios)))
|
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).arr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,19 +31,23 @@ 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: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
|
||||||
WidgetRef::new(self.clone(), (self.push(w), ()))
|
WidgetRef::new([w.add(self).erase_type()])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
|
||||||
|
WidgetRef::new([self.push(w)])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {
|
||||||
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);
|
||||||
WidgetId::new(id, TypeId::of::<W>())
|
WidgetId::new(id, TypeId::of::<W>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish<WL: WidgetLike>(mut self, base: WL) -> UI {
|
pub fn finish<W: WidgetLike>(mut self, base: W) -> UI {
|
||||||
let base = base.id(&mut self).erase_type();
|
let base = base.add(&mut self).erase_type();
|
||||||
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
|
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
|
||||||
ui.base = Some(base);
|
ui.base = Some(base);
|
||||||
ui
|
ui
|
||||||
|
|||||||
@@ -3,11 +3,7 @@ use std::{
|
|||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{primitive::Painter, util::ID, UIBuilder};
|
||||||
primitive::Painter,
|
|
||||||
util::{IntoTupleList, TupleList, ID},
|
|
||||||
UIBuilder,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub trait Widget: 'static + Any {
|
pub trait Widget: 'static + Any {
|
||||||
fn draw(&self, painter: &mut Painter);
|
fn draw(&self, painter: &mut Painter);
|
||||||
@@ -19,8 +15,10 @@ impl<W: Widget> Widget for (W,) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct AnyWidget;
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq, Debug)]
|
#[derive(Eq, Hash, PartialEq, Debug)]
|
||||||
pub struct WidgetId<W = ()> {
|
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>,
|
||||||
@@ -29,11 +27,7 @@ pub struct WidgetId<W = ()> {
|
|||||||
// TODO: temp
|
// TODO: temp
|
||||||
impl<W> Clone for WidgetId<W> {
|
impl<W> Clone for WidgetId<W> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self::new(self.id.duplicate(), self.ty)
|
||||||
ty: self.ty,
|
|
||||||
id: self.id.duplicate(),
|
|
||||||
_pd: self._pd,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +39,7 @@ impl<W> WidgetId<W> {
|
|||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn erase_type(self) -> WidgetId<()> {
|
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
||||||
self.cast_type()
|
self.cast_type()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +54,7 @@ impl<W> WidgetId<W> {
|
|||||||
|
|
||||||
pub trait WidgetLike {
|
pub trait WidgetLike {
|
||||||
type Widget;
|
type Widget;
|
||||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// wouldn't be needed if negative trait bounds & disjoint impls existed
|
/// wouldn't be needed if negative trait bounds & disjoint impls existed
|
||||||
@@ -68,119 +62,98 @@ pub struct WidgetFn<F: FnOnce(&mut UIBuilder) -> W, W>(pub F);
|
|||||||
|
|
||||||
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
|
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||||
let w = (self.0)(ui);
|
let w = (self.0)(ui);
|
||||||
ui.add(w).to_id()
|
ui.add_widget(w).to_id()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget> WidgetLike for W {
|
impl<W: Widget> WidgetLike for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn id(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||||
ui.add(self).to_id()
|
ui.add_widget(self).to_id()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetId<W> {
|
impl<W> WidgetLike for WidgetId<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetArr<Ws: WidgetList> {
|
impl<W> WidgetLike for WidgetArr<1, (W,)> {
|
||||||
pub ui: UIBuilder,
|
type Widget = W;
|
||||||
pub arr: Ws::Ids,
|
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||||
}
|
let [id] = self.arr;
|
||||||
|
id.cast_type()
|
||||||
impl<Ws: WidgetList> WidgetArr<Ws> {
|
|
||||||
pub fn new(ui: UIBuilder, arr: Ws::Ids) -> Self {
|
|
||||||
Self { ui, arr }
|
|
||||||
}
|
|
||||||
pub fn ids(self) -> Vec<WidgetId> {
|
|
||||||
self.arr.all()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type WidgetRef<W> = WidgetArr<(W, ())>;
|
pub struct WidgetArr<const LEN: usize, Ws> {
|
||||||
|
pub arr: [WidgetId; LEN],
|
||||||
|
_pd: PhantomData<Ws>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
||||||
|
pub fn new(arr: [WidgetId; LEN]) -> Self {
|
||||||
|
Self {
|
||||||
|
arr,
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
|
||||||
|
|
||||||
impl<W> WidgetRef<W> {
|
impl<W> WidgetRef<W> {
|
||||||
pub fn handle(&self) -> WidgetId<W> {
|
pub fn handle(&self) -> WidgetId<W> {
|
||||||
self.arr.head().clone()
|
let [id] = &self.arr;
|
||||||
|
id.clone().cast_type()
|
||||||
}
|
}
|
||||||
pub fn to_id(self) -> WidgetId<W> {
|
pub fn to_id(self) -> WidgetId<W> {
|
||||||
self.arr.into_head()
|
let [id] = self.arr;
|
||||||
|
id.cast_type()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetRef<W> {
|
pub trait WidgetArrLike<const LEN: usize> {
|
||||||
type Widget = W;
|
type Ws;
|
||||||
fn id(self, _: &mut UIBuilder) -> WidgetId<W> {
|
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
|
||||||
self.arr.into_head()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetArrLike {
|
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
||||||
const LEN: usize;
|
|
||||||
type Ws: WidgetList;
|
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<Self::Ws>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Ws: WidgetList> WidgetArrLike for WidgetArr<Ws> {
|
|
||||||
const LEN: usize = Ws::LEN;
|
|
||||||
type Ws = Ws;
|
type Ws = Ws;
|
||||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<Ws> {
|
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike> WidgetArrLike for (W, ())
|
// I hate this language it's so bad why do I even use it
|
||||||
where
|
macro_rules! impl_widget_arr {
|
||||||
Self: TupleList,
|
($n:expr;$($T:tt)*) => {
|
||||||
{
|
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
||||||
const LEN: usize = <Self as TupleList>::LEN;
|
type Ws = ($($T::Widget,)*);
|
||||||
type Ws = (W::Widget, ());
|
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<(W::Widget, ())> {
|
#[allow(non_snake_case)]
|
||||||
WidgetArr::new(ui.clone(), (self.0.id(ui), ()))
|
let ($($T,)*) = self;
|
||||||
}
|
WidgetArr::new(
|
||||||
}
|
[$($T.add(ui).cast_type(),)*],
|
||||||
impl<W: WidgetLike, T: WidgetArrLike> WidgetArrLike for (W, T)
|
)
|
||||||
where
|
}
|
||||||
Self: TupleList,
|
}
|
||||||
{
|
};
|
||||||
const LEN: usize = <Self as TupleList>::LEN;
|
|
||||||
type Ws = (W::Widget, T::Ws);
|
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<(W::Widget, T::Ws)> {
|
|
||||||
WidgetArr::new(ui.clone(), (self.0.id(ui), self.1.ui(ui).arr))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetList: TupleList {
|
impl_widget_arr!(1;A);
|
||||||
type Ids: IdList;
|
impl_widget_arr!(2;A B);
|
||||||
}
|
impl_widget_arr!(3;A B C);
|
||||||
impl<H> WidgetList for (H, ()) {
|
impl_widget_arr!(4;A B C D);
|
||||||
type Ids = (WidgetId<H>, ());
|
impl_widget_arr!(5;A B C D E);
|
||||||
}
|
impl_widget_arr!(6;A B C D E F);
|
||||||
impl<H, T: WidgetList> WidgetList for (H, T) {
|
impl_widget_arr!(7;A B C D E F G);
|
||||||
type Ids = (WidgetId<H>, T::Ids);
|
impl_widget_arr!(8;A B C D E F G H);
|
||||||
}
|
impl_widget_arr!(9;A B C D E F G H I);
|
||||||
|
impl_widget_arr!(10;A B C D E F G H I J);
|
||||||
pub trait IdList: TupleList {
|
impl_widget_arr!(11;A B C D E F G H I J K);
|
||||||
fn iter(self) -> impl Iterator<Item = WidgetId>;
|
impl_widget_arr!(12;A B C D E F G H I J K L);
|
||||||
fn all(self) -> Vec<WidgetId>
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
self.iter().collect::<Vec<_>>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<H> IdList for (WidgetId<H>, ()) {
|
|
||||||
fn iter(self) -> impl Iterator<Item = WidgetId> {
|
|
||||||
core::iter::once(self.0.erase_type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<H, T: IdList> IdList for (WidgetId<H>, T) {
|
|
||||||
fn iter(self) -> impl Iterator<Item = WidgetId> {
|
|
||||||
core::iter::once(self.0.erase_type()).chain(self.1.iter())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,11 +3,10 @@
|
|||||||
#![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;
|
||||||
pub mod util;
|
mod util;
|
||||||
mod base;
|
mod base;
|
||||||
|
|
||||||
pub use layout::*;
|
pub use layout::*;
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ impl<T: ColorNum> Color<T> {
|
|||||||
pub const fn rgb(r: T, g: T, b: T) -> Self {
|
pub const fn rgb(r: T, g: T, b: T) -> Self {
|
||||||
Self { r, g, b, a: T::MAX }
|
Self { r, g, b, a: T::MAX }
|
||||||
}
|
}
|
||||||
|
pub fn alpha(mut self, a: T) -> Self {
|
||||||
|
self.a = a;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ColorNum {
|
pub trait ColorNum {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use crate::primitive::{point::point, Point};
|
use crate::primitive::{Vec2, vec2::point};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
||||||
pub struct UIPos {
|
pub struct UIPos {
|
||||||
pub anchor: Point,
|
pub anchor: Vec2,
|
||||||
pub offset: Point,
|
pub offset: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UIPos {
|
impl UIPos {
|
||||||
@@ -15,24 +15,32 @@ impl UIPos {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn top_left() -> Self {
|
pub const fn center() -> Self {
|
||||||
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
|
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn top_left() -> Self {
|
||||||
|
Self::anchor_offset(-1.0, -1.0, 0.0, 0.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn bottom_right() -> Self {
|
pub const fn bottom_right() -> Self {
|
||||||
Self::anchor_offset(1.0, 1.0, 0.0, 0.0)
|
Self::anchor_offset(1.0, 1.0, 0.0, 0.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn offset(mut self, offset: Vec2) -> Self {
|
||||||
|
self.offset = offset;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn within(&self, region: &UIRegion) -> UIPos {
|
pub const fn within(&self, region: &UIRegion) -> UIPos {
|
||||||
let range = region.bot_right.anchor - region.top_left.anchor;
|
let lerp = self.anchor_01();
|
||||||
let region_offset = region
|
let anchor = region.top_left.anchor.lerp(region.bot_right.anchor, lerp);
|
||||||
.top_left
|
let offset = self.offset + region.top_left.offset.lerp(region.bot_right.offset, lerp);
|
||||||
.offset
|
UIPos { anchor, offset }
|
||||||
.lerp(region.bot_right.offset, self.anchor);
|
}
|
||||||
UIPos {
|
|
||||||
anchor: region.top_left.anchor + self.anchor * range,
|
pub const fn anchor_01(&self) -> Vec2 {
|
||||||
offset: self.offset + region_offset,
|
(self.anchor + 1.0) / 2.0
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn axis_mut(&mut self, axis: Axis) -> UIPosAxisView<'_> {
|
pub fn axis_mut(&mut self, axis: Axis) -> UIPosAxisView<'_> {
|
||||||
@@ -68,6 +76,12 @@ impl UIRegion {
|
|||||||
bot_right: UIPos::bottom_right(),
|
bot_right: UIPos::bottom_right(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn center(size: Vec2) -> Self {
|
||||||
|
Self {
|
||||||
|
top_left: UIPos::center().offset(-size / 2.0),
|
||||||
|
bot_right: UIPos::center().offset(size / 2.0),
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn within(&self, parent: &Self) -> Self {
|
pub fn within(&self, parent: &Self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
top_left: self.top_left.within(parent),
|
top_left: self.top_left.within(parent),
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
mod color;
|
mod color;
|
||||||
mod def;
|
mod def;
|
||||||
mod format;
|
mod format;
|
||||||
mod point;
|
mod vec2;
|
||||||
|
|
||||||
pub use color::*;
|
pub use color::*;
|
||||||
pub use def::*;
|
pub use def::*;
|
||||||
pub use format::*;
|
pub use format::*;
|
||||||
pub use point::*;
|
pub use vec2::*;
|
||||||
|
|
||||||
use crate::{render::data::PrimitiveInstance, WidgetId, Widgets};
|
use crate::{render::data::PrimitiveInstance, WidgetId, Widgets};
|
||||||
use bytemuck::Pod;
|
use bytemuck::Pod;
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
use std::ops::*;
|
use std::ops::*;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
pub struct Point {
|
pub struct Vec2 {
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
pub y: f32,
|
pub y: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn point(x: f32, y: f32) -> Point {
|
pub const fn point(x: f32, y: f32) -> Vec2 {
|
||||||
Point::new(x, y)
|
Vec2::new(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Point {
|
impl Vec2 {
|
||||||
pub const fn new(x: f32, y: f32) -> Self {
|
pub const fn new(x: f32, y: f32) -> Self {
|
||||||
Self { x, y }
|
Self { x, y }
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ const fn lerp(x: f32, y: f32, amt: f32) -> f32 {
|
|||||||
(1.0 - amt) * x + y * amt
|
(1.0 - amt) * x + y * amt
|
||||||
}
|
}
|
||||||
|
|
||||||
impl const From<f32> for Point {
|
impl const From<f32> for Vec2 {
|
||||||
fn from(v: f32) -> Self {
|
fn from(v: f32) -> Self {
|
||||||
Self { x: v, y: v }
|
Self { x: v, y: v }
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ impl const From<f32> for Point {
|
|||||||
|
|
||||||
macro_rules! impl_op_inner {
|
macro_rules! impl_op_inner {
|
||||||
($op:ident $fn:ident $opa:ident $fna:ident) => {
|
($op:ident $fn:ident $opa:ident $fna:ident) => {
|
||||||
impl const $op for Point {
|
impl const $op for Vec2 {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn $fn(self, rhs: Self) -> Self::Output {
|
fn $fn(self, rhs: Self) -> Self::Output {
|
||||||
@@ -47,13 +47,13 @@ macro_rules! impl_op_inner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl $opa for Point {
|
impl $opa for Vec2 {
|
||||||
fn $fna(&mut self, rhs: Self) {
|
fn $fna(&mut self, rhs: Self) {
|
||||||
self.x.$fna(rhs.x);
|
self.x.$fna(rhs.x);
|
||||||
self.y.$fna(rhs.y);
|
self.y.$fna(rhs.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl const $op<f32> for Point {
|
impl const $op<f32> for Vec2 {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn $fn(self, rhs: f32) -> Self::Output {
|
fn $fn(self, rhs: f32) -> Self::Output {
|
||||||
@@ -63,7 +63,7 @@ macro_rules! impl_op_inner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl $opa<f32> for Point {
|
impl $opa<f32> for Vec2 {
|
||||||
fn $fna(&mut self, rhs: f32) {
|
fn $fna(&mut self, rhs: f32) {
|
||||||
self.x.$fna(rhs);
|
self.x.$fna(rhs);
|
||||||
self.y.$fna(rhs);
|
self.y.$fna(rhs);
|
||||||
@@ -82,3 +82,19 @@ impl_op!(Add add);
|
|||||||
impl_op!(Sub sub);
|
impl_op!(Sub sub);
|
||||||
impl_op!(Mul mul);
|
impl_op!(Mul mul);
|
||||||
impl_op!(Div div);
|
impl_op!(Div div);
|
||||||
|
|
||||||
|
impl Neg for Vec2 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn neg(mut self) -> Self::Output {
|
||||||
|
self.x = -self.x;
|
||||||
|
self.y = -self.y;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<(f32, f32)> for Vec2 {
|
||||||
|
fn from((x, y): (f32, f32)) -> Self {
|
||||||
|
Self { x, y }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,8 +42,8 @@ fn vs_main(
|
|||||||
) -> VertexOutput {
|
) -> VertexOutput {
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
|
|
||||||
let top_left = in.top_left_anchor * window.dim + in.top_left_offset;
|
let top_left = (in.top_left_anchor + 1.0) / 2.0 * window.dim + in.top_left_offset;
|
||||||
let bot_right = in.bottom_right_anchor * window.dim + in.bottom_right_offset;
|
let bot_right = (in.bottom_right_anchor + 1.0) / 2.0 * window.dim + in.bottom_right_offset;
|
||||||
let size = bot_right - top_left;
|
let size = bot_right - top_left;
|
||||||
|
|
||||||
var pos = top_left + vec2<f32>(
|
var pos = top_left + vec2<f32>(
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
use gui::util::IntoTupleList;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use app::App;
|
use app::App;
|
||||||
use gui::{primitive::Axis, RoundedRect, UIColor, WidgetArrUtil, WidgetUtil, UI};
|
use gui::{RoundedRect, UI, UIColor, WidgetArrUtil, WidgetUtil, primitive::Axis};
|
||||||
use render::Renderer;
|
use render::Renderer;
|
||||||
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
||||||
|
|
||||||
@@ -34,27 +33,24 @@ impl Client {
|
|||||||
(
|
(
|
||||||
blue,
|
blue,
|
||||||
(
|
(
|
||||||
rect.color(UIColor::RED),
|
rect.color(UIColor::RED).center((100.0, 100.0)),
|
||||||
(
|
(
|
||||||
rect.color(UIColor::ORANGE),
|
rect.color(UIColor::ORANGE),
|
||||||
rect.color(UIColor::LIME).pad(10.0),
|
rect.color(UIColor::LIME).pad(10.0),
|
||||||
)
|
)
|
||||||
.into_list()
|
.span(Axis::Y, [1, 1]),
|
||||||
.span(Axis::Y, [1, 1].into()),
|
|
||||||
rect.color(UIColor::YELLOW),
|
rect.color(UIColor::YELLOW),
|
||||||
)
|
)
|
||||||
.into_list()
|
|
||||||
.span(Axis::X, [2, 2, 1])
|
.span(Axis::X, [2, 2, 1])
|
||||||
.pad(10),
|
.pad(10),
|
||||||
)
|
)
|
||||||
.into_list()
|
|
||||||
.span(Axis::X, [1, 3]),
|
.span(Axis::X, [1, 3]),
|
||||||
rect.color(UIColor::GREEN),
|
rect.color(UIColor::GREEN),
|
||||||
)
|
)
|
||||||
.into_list()
|
|
||||||
.span(Axis::Y, [3, 1])
|
.span(Axis::Y, [3, 1])
|
||||||
.pad(10),
|
.pad(10),
|
||||||
);
|
);
|
||||||
|
// let mut ui = ui.finish((blue, rect.color(UIColor::RED)).span(Axis::X, [1, 1]));
|
||||||
ui.widgets.get_mut(&handle).unwrap().color = UIColor::MAGENTA;
|
ui.widgets.get_mut(&handle).unwrap().color = UIColor::MAGENTA;
|
||||||
renderer.update(&ui);
|
renderer.update(&ui);
|
||||||
Self { renderer }
|
Self { renderer }
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
mod id;
|
mod id;
|
||||||
mod tuple;
|
|
||||||
|
|
||||||
pub use id::*;
|
pub use id::*;
|
||||||
pub use tuple::*;
|
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
pub trait TupleList {
|
|
||||||
const LEN: usize;
|
|
||||||
type Head;
|
|
||||||
type Tail;
|
|
||||||
fn into_head(self) -> Self::Head;
|
|
||||||
fn head(&self) -> &Self::Head;
|
|
||||||
fn split(self) -> (Self::Head, Self::Tail);
|
|
||||||
}
|
|
||||||
impl TupleList for () {
|
|
||||||
const LEN: usize = 0;
|
|
||||||
type Head = ();
|
|
||||||
type Tail = ();
|
|
||||||
fn head(&self) -> &Self::Head {
|
|
||||||
&()
|
|
||||||
}
|
|
||||||
fn into_head(self) -> Self::Head {}
|
|
||||||
fn split(self) -> (Self::Head, Self::Tail) {
|
|
||||||
((), ())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T, Rest: TupleList> TupleList for (T, Rest) {
|
|
||||||
const LEN: usize = Rest::LEN + 1;
|
|
||||||
type Head = T;
|
|
||||||
type Tail = Rest;
|
|
||||||
fn head(&self) -> &Self::Head {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
fn into_head(self) -> Self::Head {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
fn split(self) -> (Self::Head, Self::Tail) {
|
|
||||||
(self.0, self.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait IntoTupleList {
|
|
||||||
type List;
|
|
||||||
fn into_list(self) -> Self::List;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoTupleList for () {
|
|
||||||
type List = ();
|
|
||||||
fn into_list(self) -> Self::List {}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_tuple {
|
|
||||||
($H:tt $($T:tt)*) => {
|
|
||||||
impl<$H, $($T,)*> IntoTupleList for ($H, $($T,)*) {
|
|
||||||
type List = ($H, <($($T,)*) as IntoTupleList>::List);
|
|
||||||
fn into_list(self) -> Self::List {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
let ($H, $($T,)*) = self;
|
|
||||||
($H, ($($T,)*).into_list())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_tuple!(A);
|
|
||||||
impl_tuple!(A B);
|
|
||||||
impl_tuple!(A B C);
|
|
||||||
impl_tuple!(A B C D);
|
|
||||||
impl_tuple!(A B C D E);
|
|
||||||
impl_tuple!(A B C D E F);
|
|
||||||
impl_tuple!(A B C D E F G);
|
|
||||||
impl_tuple!(A B C D E F G H);
|
|
||||||
impl_tuple!(A B C D E F G H I);
|
|
||||||
impl_tuple!(A B C D E F G H I J);
|
|
||||||
impl_tuple!(A B C D E F G H I J K);
|
|
||||||
impl_tuple!(A B C D E F G H I J K L);
|
|
||||||
Reference in New Issue
Block a user