idek stuff like stack
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{Painter, UINum, UiRegion, Widget, WidgetId};
|
||||
use crate::{Painter, UiNum, UiRegion, Widget, WidgetId};
|
||||
|
||||
pub struct Regioned {
|
||||
pub region: UiRegion,
|
||||
@@ -38,7 +38,7 @@ impl Padding {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: UINum> From<T> for Padding {
|
||||
impl<T: UiNum> From<T> for Padding {
|
||||
fn from(amt: T) -> Self {
|
||||
Self::uniform(amt.to_f32())
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ mod num;
|
||||
mod rect;
|
||||
mod sense;
|
||||
mod span;
|
||||
mod stack;
|
||||
mod trait_fns;
|
||||
|
||||
pub use frame::*;
|
||||
@@ -10,4 +11,5 @@ pub use num::*;
|
||||
pub use rect::*;
|
||||
pub use sense::*;
|
||||
pub use span::*;
|
||||
pub use stack::*;
|
||||
pub use trait_fns::*;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
pub trait UINum {
|
||||
pub trait UiNum {
|
||||
fn to_f32(self) -> f32;
|
||||
}
|
||||
|
||||
impl UINum for f32 {
|
||||
impl UiNum for f32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl UINum for u32 {
|
||||
impl UiNum for u32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self as f32
|
||||
}
|
||||
}
|
||||
|
||||
impl UINum for i32 {
|
||||
impl UiNum for i32 {
|
||||
fn to_f32(self) -> f32 {
|
||||
self as f32
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Painter, UiColor, Widget, primitive::RoundedRectData};
|
||||
use crate::{primitive::RoundedRectData, Painter, UiNum, UiColor, Widget};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Rect {
|
||||
@@ -21,6 +21,10 @@ impl Rect {
|
||||
self.color = color;
|
||||
self
|
||||
}
|
||||
pub fn radius(mut self, radius: impl UiNum) -> Self {
|
||||
self.radius = radius.to_f32();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Rect {
|
||||
|
||||
@@ -4,7 +4,7 @@ pub trait SenseCtx: 'static {
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool;
|
||||
}
|
||||
|
||||
pub trait Sensable<W, Ctx: SenseCtx, Tag> {
|
||||
pub trait Sensable<W, Ctx, Tag> {
|
||||
fn sense(
|
||||
self,
|
||||
sense: Sense,
|
||||
@@ -21,7 +21,7 @@ pub trait Sensable<W, Ctx: SenseCtx, Tag> {
|
||||
W: Widget<Ctx>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: SenseCtx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
||||
fn sense(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Dir, Painter, Sign, UINum, UiRegion, UIScalar, Widget, WidgetId};
|
||||
use crate::{Dir, Painter, Sign, UiNum, UiRegion, UIScalar, Widget, WidgetId};
|
||||
|
||||
pub struct Span {
|
||||
pub children: Vec<(WidgetId, SpanLen)>,
|
||||
@@ -80,19 +80,19 @@ pub enum SpanLen {
|
||||
Relative(f32),
|
||||
}
|
||||
|
||||
pub fn fixed<N: UINum>(x: N) -> SpanLen {
|
||||
pub fn fixed<N: UiNum>(x: N) -> SpanLen {
|
||||
SpanLen::Fixed(x.to_f32())
|
||||
}
|
||||
|
||||
pub fn ratio<N: UINum>(x: N) -> SpanLen {
|
||||
pub fn ratio<N: UiNum>(x: N) -> SpanLen {
|
||||
SpanLen::Ratio(x.to_f32())
|
||||
}
|
||||
|
||||
pub fn rel<N: UINum>(x: N) -> SpanLen {
|
||||
pub fn rel<N: UiNum>(x: N) -> SpanLen {
|
||||
SpanLen::Relative(x.to_f32())
|
||||
}
|
||||
|
||||
impl<N: UINum> From<N> for SpanLen {
|
||||
impl<N: UiNum> From<N> for SpanLen {
|
||||
fn from(value: N) -> Self {
|
||||
Self::Ratio(value.to_f32())
|
||||
}
|
||||
|
||||
13
src/core/stack.rs
Normal file
13
src/core/stack.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use crate::{Widget, WidgetId};
|
||||
|
||||
pub struct Stack {
|
||||
pub children: Vec<WidgetId>,
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Stack {
|
||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||
for child in &self.children {
|
||||
painter.draw(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
use super::*;
|
||||
use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
|
||||
|
||||
pub trait CoreWidget<Ctx: 'static, Tag> {
|
||||
pub trait CoreWidget<W: 'static, Ctx: 'static, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx>;
|
||||
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx>;
|
||||
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx> {
|
||||
|ui| Regioned {
|
||||
region: padding.into().region(),
|
||||
@@ -16,7 +17,14 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
|
||||
|
||||
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx> {
|
||||
|ui| Regioned {
|
||||
region: UiRegion::center(size.into()),
|
||||
region: UiRegion::center().size(size.into()),
|
||||
inner: self.add(ui).erase_type(),
|
||||
}
|
||||
}
|
||||
|
||||
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx> {
|
||||
move |ui| Regioned {
|
||||
region,
|
||||
inner: self.add(ui).erase_type(),
|
||||
}
|
||||
}
|
||||
@@ -24,10 +32,11 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
|
||||
|
||||
pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> {
|
||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx>;
|
||||
fn stack(self) -> impl WidgetFn<Stack, Ctx>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag> CoreWidgetArr<LEN, Ctx, Tag>
|
||||
for Wa
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag>
|
||||
CoreWidgetArr<LEN, Ctx, Tag> for Wa
|
||||
{
|
||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx> {
|
||||
let lengths = lengths.map(Into::into);
|
||||
@@ -36,4 +45,9 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag> Core
|
||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||
}
|
||||
}
|
||||
fn stack(self) -> impl WidgetFn<Stack, Ctx> {
|
||||
move |ui| Stack {
|
||||
children: self.ui(ui).arr.to_vec(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ impl<T: ColorNum> Color<T> {
|
||||
pub const YELLOW: Self = Self::rgb(T::MAX, T::MAX, T::MIN);
|
||||
pub const LIME: Self = Self::rgb(T::MID, T::MAX, T::MIN);
|
||||
pub const GREEN: Self = Self::rgb(T::MIN, T::MAX, T::MIN);
|
||||
pub const TURQUOISE: Self = Self::rgb(T::MIN, T::MAX, T::MID);
|
||||
pub const CYAN: Self = Self::rgb(T::MIN, T::MAX, T::MAX);
|
||||
pub const SKY: Self = Self::rgb(T::MIN, T::MID, T::MAX);
|
||||
pub const BLUE: Self = Self::rgb(T::MIN, T::MIN, T::MAX);
|
||||
pub const INDIGO: Self = Self::rgb(T::MIN, T::MID, T::MAX);
|
||||
pub const PURPLE: Self = Self::rgb(T::MID, T::MIN, T::MAX);
|
||||
pub const MAGENTA: Self = Self::rgb(T::MAX, T::MIN, T::MAX);
|
||||
|
||||
pub const fn new(r: T, g: T, b: T, a: T) -> Self {
|
||||
|
||||
@@ -40,7 +40,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, id: &WidgetId)
|
||||
pub fn draw<W>(&mut self, id: &WidgetId<W>)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
|
||||
@@ -6,12 +6,12 @@ use crate::{
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
||||
pub struct UIPos {
|
||||
pub struct UiPos {
|
||||
pub anchor: Vec2,
|
||||
pub offset: Vec2,
|
||||
}
|
||||
|
||||
impl UIPos {
|
||||
impl UiPos {
|
||||
pub const fn anchor_offset(anchor_x: f32, anchor_y: f32, offset_x: f32, offset_y: f32) -> Self {
|
||||
Self {
|
||||
anchor: vec2(anchor_x, anchor_y),
|
||||
@@ -23,6 +23,10 @@ impl UIPos {
|
||||
Self::anchor_offset(0.5, 0.5, 0.0, 0.0)
|
||||
}
|
||||
|
||||
pub const fn anchor(anchor: Vec2) -> Self {
|
||||
Self::anchor_offset(anchor.x, anchor.y, 0.0, 0.0)
|
||||
}
|
||||
|
||||
pub const fn top_left() -> Self {
|
||||
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
|
||||
}
|
||||
@@ -31,12 +35,16 @@ impl UIPos {
|
||||
Self::anchor_offset(1.0, 1.0, 0.0, 0.0)
|
||||
}
|
||||
|
||||
pub const fn offset(mut self, offset: Vec2) -> Self {
|
||||
self.offset = offset;
|
||||
pub const fn shift(&mut self, offset: Vec2) {
|
||||
self.offset += offset;
|
||||
}
|
||||
|
||||
pub const fn shifted(mut self, offset: Vec2) -> Self {
|
||||
self.shift(offset);
|
||||
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);
|
||||
@@ -44,7 +52,7 @@ impl UIPos {
|
||||
+ self
|
||||
.anchor
|
||||
.lerp(region.top_left.offset, region.bot_right.offset);
|
||||
UIPos { anchor, offset }
|
||||
UiPos { anchor, offset }
|
||||
}
|
||||
|
||||
pub fn axis_mut(&mut self, axis: Axis) -> UIScalarView<'_> {
|
||||
@@ -111,23 +119,32 @@ impl UIScalar {
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct UiRegion {
|
||||
pub top_left: UIPos,
|
||||
pub bot_right: UIPos,
|
||||
pub top_left: UiPos,
|
||||
pub bot_right: UiPos,
|
||||
}
|
||||
|
||||
impl UiRegion {
|
||||
pub const fn full() -> Self {
|
||||
Self {
|
||||
top_left: UIPos::top_left(),
|
||||
bot_right: UIPos::bottom_right(),
|
||||
top_left: UiPos::top_left(),
|
||||
bot_right: UiPos::bottom_right(),
|
||||
}
|
||||
}
|
||||
pub fn center(size: Vec2) -> Self {
|
||||
pub fn center() -> Self {
|
||||
Self::anchor(Vec2::new(0.5, 0.5))
|
||||
}
|
||||
pub fn anchor(anchor: Vec2) -> Self {
|
||||
Self {
|
||||
top_left: UIPos::center().offset(-size / 2.0),
|
||||
bot_right: UIPos::center().offset(size / 2.0),
|
||||
top_left: UiPos::anchor(anchor),
|
||||
bot_right: UiPos::anchor(anchor),
|
||||
}
|
||||
}
|
||||
pub fn size(mut self, size: impl Into<Vec2>) -> Self {
|
||||
let size = size.into();
|
||||
self.top_left = self.top_left.shifted(-size / 2.0);
|
||||
self.bot_right = self.bot_right.shifted(size / 2.0);
|
||||
self
|
||||
}
|
||||
pub fn within(&self, parent: &Self) -> Self {
|
||||
Self {
|
||||
top_left: self.top_left.within(parent),
|
||||
@@ -150,6 +167,33 @@ impl UiRegion {
|
||||
std::mem::swap(&mut self.top_left, &mut self.bot_right);
|
||||
}
|
||||
|
||||
pub fn shift(&mut self, offset: impl Into<Vec2>) {
|
||||
let offset = offset.into();
|
||||
self.top_left.shift(offset);
|
||||
self.bot_right.shift(offset);
|
||||
}
|
||||
|
||||
pub fn shifted(mut self, offset: impl Into<Vec2>) -> Self {
|
||||
println!("before {:?}", self);
|
||||
self.shift(offset);
|
||||
println!("after {:?}", self);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn top_left() -> Self {
|
||||
Self {
|
||||
top_left: UiPos::top_left(),
|
||||
bot_right: UiPos::top_left(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bottom_right() -> Self {
|
||||
Self {
|
||||
top_left: UiPos::bottom_right(),
|
||||
bot_right: UiPos::bottom_right(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_screen(&self, size: Vec2) -> ScreenRect {
|
||||
ScreenRect {
|
||||
top_left: self.top_left.anchor * size + self.top_left.offset,
|
||||
|
||||
@@ -147,7 +147,7 @@ impl<Ctx> Widgets<Ctx> {
|
||||
Self(HashMap::new())
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget<Ctx> {
|
||||
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
|
||||
self.0.get(&id.id).unwrap().as_ref()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::util::{F32Util, impl_op};
|
||||
use crate::{util::{impl_op, F32Util}, UiNum};
|
||||
use std::ops::*;
|
||||
|
||||
#[repr(C)]
|
||||
@@ -52,8 +52,11 @@ impl Neg for Vec2 {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(f32, f32)> for Vec2 {
|
||||
fn from((x, y): (f32, f32)) -> Self {
|
||||
Self { x, y }
|
||||
impl<T: UiNum, U: UiNum> From<(T, U)> for Vec2 {
|
||||
fn from((x, y): (T, U)) -> Self {
|
||||
Self {
|
||||
x: x.to_f32(),
|
||||
y: y.to_f32(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +106,21 @@ impl Client {
|
||||
)
|
||||
.span(Dir::RIGHT, [1, 1, 1]),
|
||||
);
|
||||
ui.set_base((buttons, pad_test.pad(10).id(&main)).span(Dir::DOWN, [fixed(40), ratio(1)]));
|
||||
ui.set_base(
|
||||
(
|
||||
buttons,
|
||||
(
|
||||
pad_test.pad(10).id(&main),
|
||||
Rect::new(Color::PURPLE).radius(30).region(
|
||||
UiRegion::bottom_right()
|
||||
.size((150, 150))
|
||||
.shifted((-75, -75)),
|
||||
),
|
||||
)
|
||||
.stack(),
|
||||
)
|
||||
.span(Dir::DOWN, [fixed(40), ratio(1)]),
|
||||
);
|
||||
(ui, UiIds { test })
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ macro_rules! impl_op {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl $opa for $T {
|
||||
impl const $opa for $T {
|
||||
fn $fna(&mut self, rhs: Self) {
|
||||
$(self.$field.$fna(rhs.$field);)*
|
||||
}
|
||||
@@ -49,7 +49,7 @@ macro_rules! impl_op {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl $opa<f32> for $T {
|
||||
impl const $opa<f32> for $T {
|
||||
fn $fna(&mut self, rhs: f32) {
|
||||
$(self.$field.$fna(rhs);)*
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user