center anchors on 0 0

This commit is contained in:
2025-08-10 20:29:16 -04:00
parent 848347e6b3
commit f2cbf90d1d
7 changed files with 24 additions and 20 deletions

View File

@@ -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

View File

@@ -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},
}; };
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@@ -56,13 +56,13 @@ impl Span {
) -> Self { ) -> Self {
let ratios = ratios.map(|r| r.to_f32()); let ratios = ratios.map(|r| r.to_f32());
let total: f32 = ratios.iter().sum(); let total: f32 = ratios.iter().sum();
let mut start = 0.0; let mut start = -1.0;
Self { Self {
elements: elements elements: elements
.into_iter() .into_iter()
.zip(ratios) .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

View File

@@ -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 {

View File

@@ -1,7 +1,7 @@
use crate::primitive::{point::point, Point}; use crate::primitive::{Point, point::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: Point,
pub offset: Point, pub offset: Point,
@@ -16,7 +16,7 @@ impl UIPos {
} }
pub const fn top_left() -> Self { pub const fn top_left() -> Self {
Self::anchor_offset(0.0, 0.0, 0.0, 0.0) Self::anchor_offset(-1.0, -1.0, 0.0, 0.0)
} }
pub const fn bottom_right() -> Self { pub const fn bottom_right() -> Self {
@@ -24,15 +24,14 @@ impl UIPos {
} }
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) -> Point {
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<'_> {

View File

@@ -1,7 +1,7 @@
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 Point {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,

View File

@@ -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>(

View File

@@ -1,7 +1,7 @@
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};
@@ -50,6 +50,7 @@ impl Client {
.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 }