3 Commits

Author SHA1 Message Date
132113f09e center w size 2025-08-10 20:40:14 -04:00
f2cbf90d1d center anchors on 0 0 2025-08-10 20:29:16 -04:00
848347e6b3 clean up 2025-08-10 19:10:26 -04:00
12 changed files with 94 additions and 53 deletions

View File

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

View File

@@ -1,8 +1,8 @@
use std::ops::Range;
use crate::{
primitive::{Axis, Painter, RoundedRectData, UIRegion},
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
primitive::{Axis, Painter, RoundedRectData, UIRegion, Vec2},
};
#[derive(Clone, Copy)]
@@ -56,13 +56,13 @@ impl Span {
) -> Self {
let ratios = ratios.map(|r| r.to_f32());
let total: f32 = ratios.iter().sum();
let mut start = 0.0;
let mut start = -1.0;
Self {
elements: elements
.into_iter()
.zip(ratios)
.map(|(e, r)| {
let end = start + r / total;
let end = start + (r / total) * 2.0;
let res = (start..end, e);
start = end;
res
@@ -119,6 +119,7 @@ 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 {
@@ -128,6 +129,13 @@ 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> {

View File

@@ -31,8 +31,12 @@ impl From<UI> for UIBuilder {
}
impl UIBuilder {
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new(self.clone(), [self.push(w)])
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
WidgetRef::new([w.add(self).erase_type()])
}
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 {

View File

@@ -15,21 +15,19 @@ impl<W: Widget> Widget for (W,) {
}
}
pub struct AnyWidget;
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = ()> {
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: ID,
_pd: PhantomData<W>,
}
// TODO: temp
impl Clone for WidgetId {
impl<W> Clone for WidgetId<W> {
fn clone(&self) -> Self {
Self {
ty: self.ty,
id: self.id.duplicate(),
_pd: self._pd,
}
Self::new(self.id.duplicate(), self.ty)
}
}
@@ -41,7 +39,7 @@ impl<W> WidgetId<W> {
_pd: PhantomData,
}
}
pub fn erase_type(self) -> WidgetId<()> {
pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type()
}
@@ -66,14 +64,14 @@ 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(w).to_id()
ui.add_widget(w).to_id()
}
}
impl<W: Widget> WidgetLike for W {
type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
ui.add(self).to_id()
ui.add_widget(self).to_id()
}
}
@@ -93,15 +91,13 @@ impl<W> WidgetLike for WidgetArr<1, (W,)> {
}
pub struct WidgetArr<const LEN: usize, Ws> {
pub ui: UIBuilder,
pub arr: [WidgetId<()>; LEN],
pub arr: [WidgetId; LEN],
_pd: PhantomData<Ws>,
}
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
pub fn new(ui: UIBuilder, arr: [WidgetId<()>; LEN]) -> Self {
pub fn new(arr: [WidgetId; LEN]) -> Self {
Self {
ui,
arr,
_pd: PhantomData,
}
@@ -138,12 +134,10 @@ macro_rules! impl_widget_arr {
($n:expr;$($T:tt)*) => {
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
type Ws = ($($T::Widget,)*);
#[allow(unused_variables)]
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(),)*],
)
}

View File

@@ -3,7 +3,6 @@
#![feature(const_trait_impl)]
#![feature(const_from)]
#![feature(trait_alias)]
#![feature(generic_const_exprs)]
mod layout;
mod render;

View File

@@ -30,6 +30,10 @@ 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::{point::point, Point};
use crate::primitive::{Vec2, vec2::point};
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct UIPos {
pub anchor: Point,
pub offset: Point,
pub anchor: Vec2,
pub offset: Vec2,
}
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)
}
pub const fn top_left() -> Self {
Self::anchor_offset(-1.0, -1.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 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,
}
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
}
pub fn axis_mut(&mut self, axis: Axis) -> UIPosAxisView<'_> {
@@ -68,6 +76,12 @@ 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 point;
mod vec2;
pub use color::*;
pub use def::*;
pub use format::*;
pub use point::*;
pub use vec2::*;
use crate::{render::data::PrimitiveInstance, WidgetId, Widgets};
use bytemuck::Pod;

View File

@@ -1,17 +1,17 @@
use std::ops::*;
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Point {
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
pub const fn point(x: f32, y: f32) -> Point {
Point::new(x, y)
pub const fn point(x: f32, y: f32) -> Vec2 {
Vec2::new(x, y)
}
impl Point {
impl Vec2 {
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 Point {
impl const From<f32> for Vec2 {
fn from(v: f32) -> Self {
Self { x: v, y: v }
}
@@ -37,7 +37,7 @@ impl const From<f32> for Point {
macro_rules! impl_op_inner {
($op:ident $fn:ident $opa:ident $fna:ident) => {
impl const $op for Point {
impl const $op for Vec2 {
type Output = Self;
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) {
self.x.$fna(rhs.x);
self.y.$fna(rhs.y);
}
}
impl const $op<f32> for Point {
impl const $op<f32> for Vec2 {
type Output = Self;
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) {
self.x.$fna(rhs);
self.y.$fna(rhs);
@@ -82,3 +82,19 @@ 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 * window.dim + in.top_left_offset;
let bot_right = in.bottom_right_anchor * window.dim + in.bottom_right_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 + 1.0) / 2.0 * 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::{primitive::Axis, RoundedRect, UIColor, WidgetArrUtil, WidgetUtil, UI};
use gui::{RoundedRect, UI, UIColor, WidgetArrUtil, WidgetUtil, primitive::Axis};
use render::Renderer;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
@@ -33,7 +33,7 @@ impl Client {
(
blue,
(
rect.color(UIColor::RED),
rect.color(UIColor::RED).center((100.0, 100.0)),
(
rect.color(UIColor::ORANGE),
rect.color(UIColor::LIME).pad(10.0),
@@ -50,6 +50,7 @@ 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
}
#[allow(dead_code)]
pub fn free(&mut self, id: ID) {
self.free.push(id);
}