1 Commits

13 changed files with 178 additions and 145 deletions

View File

@@ -1,7 +1,7 @@
[package]
name = "gui"
version = "0.1.0"
edition = "2024"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -1,8 +1,9 @@
use std::ops::Range;
use crate::{
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
primitive::{Axis, Painter, RoundedRectData, UIRegion, Vec2},
primitive::{Axis, Painter, RoundedRectData, UIRegion},
ToId, UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetIdLikeTuple, WidgetLike,
WidgetLikeTuple,
};
#[derive(Clone, Copy)]
@@ -56,13 +57,13 @@ impl Span {
) -> Self {
let ratios = ratios.map(|r| r.to_f32());
let total: f32 = ratios.iter().sum();
let mut start = -1.0;
let mut start = 0.0;
Self {
elements: elements
.into_iter()
.zip(ratios)
.map(|(e, r)| {
let end = start + (r / total) * 2.0;
let end = start + r / total;
let res = (start..end, e);
start = end;
res
@@ -119,7 +120,6 @@ impl<T: UINum> From<T> for Padding {
pub trait WidgetUtil {
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 for W {
@@ -129,22 +129,18 @@ impl<W: WidgetLike> WidgetUtil for W {
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<const LEN: usize> {
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> {
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).arr))
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).erase_types()))
}
}

View File

@@ -31,15 +31,11 @@ impl From<UI> for UIBuilder {
}
impl UIBuilder {
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
WidgetRef::new([w.add(self).erase_type()])
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new(self.clone(), (self.push(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 {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let mut ui = self.ui.borrow_mut();
let id = ui.ids.next();
ui.widgets.insert(id.duplicate(), w);

View File

@@ -3,7 +3,11 @@ use std::{
marker::PhantomData,
};
use crate::{primitive::Painter, util::ID, UIBuilder};
use crate::{
primitive::Painter,
util::{impl_tuple, ID},
UIBuilder,
};
pub trait Widget: 'static + Any {
fn draw(&self, painter: &mut Painter);
@@ -15,19 +19,21 @@ impl<W: Widget> Widget for (W,) {
}
}
pub struct AnyWidget;
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = AnyWidget> {
pub struct WidgetId<W = ()> {
pub(super) ty: TypeId,
pub(super) id: ID,
_pd: PhantomData<W>,
}
// TODO: temp
impl<W> Clone for WidgetId<W> {
impl<W: Widget> Clone for WidgetId<W> {
fn clone(&self) -> Self {
Self::new(self.id.duplicate(), self.ty)
Self {
ty: self.ty,
id: self.id.duplicate(),
_pd: self._pd,
}
}
}
@@ -39,7 +45,7 @@ impl<W> WidgetId<W> {
_pd: PhantomData,
}
}
pub fn erase_type(self) -> WidgetId<AnyWidget> {
pub fn erase_type(self) -> WidgetId<()> {
self.cast_type()
}
@@ -53,7 +59,7 @@ impl<W> WidgetId<W> {
}
pub trait WidgetLike {
type Widget;
type Widget: Widget;
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
}
@@ -64,96 +70,109 @@ impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
let w = (self.0)(ui);
ui.add_widget(w).to_id()
ui.add(w).to_id()
}
}
impl<W: Widget> WidgetLike for W {
type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
ui.add_widget(self).to_id()
ui.add(self).to_id()
}
}
impl<W> WidgetLike for WidgetId<W> {
impl<W: Widget> WidgetLike for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
self
}
}
impl<W> WidgetLike for WidgetArr<1, (W,)> {
impl<W: Widget> WidgetLike for WidgetArr<1, (W,)> {
type Widget = W;
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
let [id] = self.arr;
id.cast_type()
self.arr.0
}
}
pub struct WidgetArr<const LEN: usize, Ws> {
pub arr: [WidgetId; LEN],
_pd: PhantomData<Ws>,
pub struct WidgetArr<const LEN: usize, Ws: WidgetLikeTuple<LEN>> {
pub ui: UIBuilder,
pub arr: Ws::Wrap<ToId>,
}
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
pub fn new(arr: [WidgetId; LEN]) -> Self {
Self {
arr,
_pd: PhantomData,
}
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArr<LEN, Ws>
where
Ws::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
{
pub fn new(ui: UIBuilder, arr: Ws::Wrap<ToId>) -> Self {
Self { ui, arr }
}
pub fn erase_types(self) -> [WidgetId; LEN] {
self.arr.map::<EraseId>(&mut ())
}
}
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
impl<W> WidgetRef<W> {
pub fn handle(&self) -> WidgetId<W> {
let [id] = &self.arr;
id.clone().cast_type()
impl<W: WidgetLike> WidgetRef<W> {
pub fn handle(&self) -> WidgetId<W::Widget> {
self.arr.0.clone()
}
pub fn to_id(self) -> WidgetId<W> {
let [id] = self.arr;
id.cast_type()
pub fn to_id(self) -> WidgetId<W::Widget> {
self.arr.0
}
}
pub trait WidgetArrLike<const LEN: usize> {
type Ws;
type Ws: WidgetLikeTuple<LEN>;
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;
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
self
}
}
// 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 UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
#[allow(non_snake_case)]
let ($($T,)*) = self;
WidgetArr::new(
[$($T.add(ui).cast_type(),)*],
)
}
}
};
impl_tuple!(Widget);
impl_tuple!(WidgetLike);
impl_tuple!(WidgetIdLike);
pub trait WidgetIdLike {
fn erase_type(self) -> WidgetId;
}
impl_widget_arr!(1;A);
impl_widget_arr!(2;A B);
impl_widget_arr!(3;A B C);
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);
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);
impl_widget_arr!(11;A B C D E F G H I J K);
impl_widget_arr!(12;A B C D E F G H I J K L);
impl<W> WidgetIdLike for WidgetId<W> {
fn erase_type(self) -> WidgetId {
self.erase_type()
}
}
pub struct ToId;
impl WidgetLikeWrapper for ToId {
type Wrap<T: WidgetLike> = WidgetId<T::Widget>;
type Ctx = UIBuilder;
fn wrap<T: WidgetLike>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T> {
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))
}
}

View File

@@ -30,10 +30,6 @@ impl<T: ColorNum> Color<T> {
pub const fn rgb(r: T, g: T, b: T) -> Self {
Self { r, g, b, a: T::MAX }
}
pub fn alpha(mut self, a: T) -> Self {
self.a = a;
self
}
}
pub trait ColorNum {

View File

@@ -1,10 +1,10 @@
use crate::primitive::{Vec2, vec2::point};
use crate::primitive::{point::point, Point};
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct UIPos {
pub anchor: Vec2,
pub offset: Vec2,
pub anchor: Point,
pub offset: Point,
}
impl UIPos {
@@ -15,32 +15,24 @@ impl UIPos {
}
}
pub const fn center() -> Self {
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)
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
}
pub const fn bottom_right() -> Self {
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 {
let lerp = self.anchor_01();
let anchor = region.top_left.anchor.lerp(region.bot_right.anchor, lerp);
let offset = self.offset + region.top_left.offset.lerp(region.bot_right.offset, lerp);
UIPos { anchor, offset }
}
pub const fn anchor_01(&self) -> Vec2 {
(self.anchor + 1.0) / 2.0
let range = region.bot_right.anchor - region.top_left.anchor;
let region_offset = region
.top_left
.offset
.lerp(region.bot_right.offset, self.anchor);
UIPos {
anchor: region.top_left.anchor + self.anchor * range,
offset: self.offset + region_offset,
}
}
pub fn axis_mut(&mut self, axis: Axis) -> UIPosAxisView<'_> {
@@ -76,12 +68,6 @@ impl UIRegion {
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 {
Self {
top_left: self.top_left.within(parent),

View File

@@ -1,12 +1,12 @@
mod color;
mod def;
mod format;
mod vec2;
mod point;
pub use color::*;
pub use def::*;
pub use format::*;
pub use vec2::*;
pub use point::*;
use crate::{render::data::PrimitiveInstance, WidgetId, Widgets};
use bytemuck::Pod;

View File

@@ -1,17 +1,17 @@
use std::ops::*;
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vec2 {
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Point {
pub x: f32,
pub y: f32,
}
pub const fn point(x: f32, y: f32) -> Vec2 {
Vec2::new(x, y)
pub const fn point(x: f32, y: f32) -> Point {
Point::new(x, y)
}
impl Vec2 {
impl Point {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
@@ -29,7 +29,7 @@ const fn lerp(x: f32, y: f32, amt: f32) -> f32 {
(1.0 - amt) * x + y * amt
}
impl const From<f32> for Vec2 {
impl const From<f32> for Point {
fn from(v: f32) -> Self {
Self { x: v, y: v }
}
@@ -37,7 +37,7 @@ impl const From<f32> for Vec2 {
macro_rules! impl_op_inner {
($op:ident $fn:ident $opa:ident $fna:ident) => {
impl const $op for Vec2 {
impl const $op for Point {
type Output = Self;
fn $fn(self, rhs: Self) -> Self::Output {
@@ -47,13 +47,13 @@ macro_rules! impl_op_inner {
}
}
}
impl $opa for Vec2 {
impl $opa for Point {
fn $fna(&mut self, rhs: Self) {
self.x.$fna(rhs.x);
self.y.$fna(rhs.y);
}
}
impl const $op<f32> for Vec2 {
impl const $op<f32> for Point {
type Output = Self;
fn $fn(self, rhs: f32) -> Self::Output {
@@ -63,7 +63,7 @@ macro_rules! impl_op_inner {
}
}
}
impl $opa<f32> for Vec2 {
impl $opa<f32> for Point {
fn $fna(&mut self, rhs: f32) {
self.x.$fna(rhs);
self.y.$fna(rhs);
@@ -82,19 +82,3 @@ impl_op!(Add add);
impl_op!(Sub sub);
impl_op!(Mul mul);
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 }
}
}

View File

@@ -42,8 +42,8 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
let top_left = (in.top_left_anchor + 1.0) / 2.0 * window.dim + in.top_left_offset;
let bot_right = (in.bottom_right_anchor + 1.0) / 2.0 * window.dim + in.bottom_right_offset;
let top_left = in.top_left_anchor * window.dim + in.top_left_offset;
let bot_right = in.bottom_right_anchor * window.dim + in.bottom_right_offset;
let size = bot_right - top_left;
var pos = top_left + vec2<f32>(

View File

@@ -1,7 +1,7 @@
use std::sync::Arc;
use app::App;
use gui::{RoundedRect, UI, UIColor, WidgetArrUtil, WidgetUtil, primitive::Axis};
use gui::{primitive::Axis, RoundedRect, UIColor, WidgetArrUtil, WidgetUtil, UI};
use render::Renderer;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
@@ -33,7 +33,7 @@ impl Client {
(
blue,
(
rect.color(UIColor::RED).center((100.0, 100.0)),
rect.color(UIColor::RED),
(
rect.color(UIColor::ORANGE),
rect.color(UIColor::LIME).pad(10.0),
@@ -50,7 +50,6 @@ impl Client {
.span(Axis::Y, [3, 1])
.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;
renderer.update(&ui);
Self { renderer }

View File

@@ -27,6 +27,7 @@ impl IDTracker {
id
}
// TODO: use this
#[allow(dead_code)]
pub fn free(&mut self, id: ID) {
self.free.push(id);

View File

@@ -1,3 +1,5 @@
mod id;
mod tuple;
pub use id::*;
pub use tuple::*;

54
src/util/tuple.rs Normal file
View 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;