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]
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},
};
#[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

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,7 +1,7 @@
use crate::primitive::{point::point, Point};
use crate::primitive::{Point, point::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,
@@ -16,7 +16,7 @@ impl UIPos {
}
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 {
@@ -24,15 +24,14 @@ impl UIPos {
}
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) -> Point {
(self.anchor + 1.0) / 2.0
}
pub fn axis_mut(&mut self, axis: Axis) -> UIPosAxisView<'_> {

View File

@@ -1,7 +1,7 @@
use std::ops::*;
#[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 x: f32,
pub y: f32,

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