spans now good (other than direction) + refactor
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
use crate::primitive::UIRegion;
|
||||
use wgpu::VertexAttribute;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct PrimitiveInstance {
|
||||
pub region: UIRegion,
|
||||
pub ptr: u32,
|
||||
}
|
||||
use crate::UIRegion;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
||||
@@ -15,6 +9,13 @@ pub struct WindowUniform {
|
||||
pub height: f32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct PrimitiveInstance {
|
||||
pub region: UIRegion,
|
||||
pub ptr: u32,
|
||||
}
|
||||
|
||||
impl PrimitiveInstance {
|
||||
const ATTRIBS: [VertexAttribute; 5] = wgpu::vertex_attr_array![
|
||||
0 => Float32x2,
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use crate::{
|
||||
render::{data::PrimitiveInstance, util::ArrBuf},
|
||||
UI,
|
||||
};
|
||||
use crate::{UI, primitive::PrimitiveInstance, render::util::ArrBuf};
|
||||
use data::WindowUniform;
|
||||
use wgpu::{
|
||||
util::{BufferInitDescriptor, DeviceExt},
|
||||
|
||||
27
src/render/primitive.rs
Normal file
27
src/render/primitive.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use crate::Color;
|
||||
|
||||
pub use super::data::PrimitiveInstance;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Primitives {
|
||||
pub instances: Vec<PrimitiveInstance>,
|
||||
pub data: Vec<u32>,
|
||||
}
|
||||
|
||||
/// NOTE: Self must have at least u32 alignment
|
||||
pub trait PrimitiveData: bytemuck::Pod {
|
||||
const DISCRIM: u32;
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct RoundedRectData {
|
||||
pub color: Color<u8>,
|
||||
pub radius: f32,
|
||||
pub thickness: f32,
|
||||
pub inner_radius: f32,
|
||||
}
|
||||
|
||||
impl PrimitiveData for RoundedRectData {
|
||||
const DISCRIM: u32 = 0;
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
#![allow(clippy::multiple_bound_locations)]
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, bytemuck::Zeroable)]
|
||||
pub struct Color<T: ColorNum> {
|
||||
r: T,
|
||||
g: T,
|
||||
b: T,
|
||||
a: T,
|
||||
}
|
||||
|
||||
impl<T: ColorNum> Color<T> {
|
||||
pub const BLACK: Self = Self::rgb(T::MIN, T::MIN, T::MIN);
|
||||
pub const WHITE: Self = Self::rgb(T::MAX, T::MAX, T::MAX);
|
||||
|
||||
pub const RED: Self = Self::rgb(T::MAX, T::MIN, T::MIN);
|
||||
pub const ORANGE: Self = Self::rgb(T::MAX, T::MID, T::MIN);
|
||||
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 CYAN: Self = Self::rgb(T::MIN, T::MAX, T::MAX);
|
||||
pub const BLUE: Self = Self::rgb(T::MIN, T::MIN, T::MAX);
|
||||
pub const MAGENTA: Self = Self::rgb(T::MAX, T::MIN, T::MAX);
|
||||
}
|
||||
|
||||
impl<T: ColorNum> Color<T> {
|
||||
pub const fn new(r: T, g: T, b: T, a: T) -> Self {
|
||||
Self { r, g, b, a }
|
||||
}
|
||||
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 {
|
||||
const MIN: Self;
|
||||
const MID: Self;
|
||||
const MAX: Self;
|
||||
}
|
||||
|
||||
impl ColorNum for u8 {
|
||||
const MIN: Self = u8::MIN;
|
||||
const MID: Self = u8::MAX / 2;
|
||||
const MAX: Self = u8::MAX;
|
||||
}
|
||||
|
||||
unsafe impl bytemuck::Pod for Color<u8> {}
|
||||
@@ -1,14 +0,0 @@
|
||||
use crate::primitive::{Color, PrimitiveData};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct RoundedRectData {
|
||||
pub color: Color<u8>,
|
||||
pub radius: f32,
|
||||
pub thickness: f32,
|
||||
pub inner_radius: f32,
|
||||
}
|
||||
|
||||
impl PrimitiveData for RoundedRectData {
|
||||
const DISCRIM: u32 = 0;
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
use crate::primitive::{Vec2, vec2::point};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
|
||||
pub struct UIPos {
|
||||
pub anchor: Vec2,
|
||||
pub offset: Vec2,
|
||||
}
|
||||
|
||||
impl UIPos {
|
||||
pub const fn anchor_offset(anchor_x: f32, anchor_y: f32, offset_x: f32, offset_y: f32) -> Self {
|
||||
Self {
|
||||
anchor: point(anchor_x, anchor_y),
|
||||
offset: point(offset_x, offset_y),
|
||||
}
|
||||
}
|
||||
|
||||
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 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<'_> {
|
||||
match axis {
|
||||
Axis::X => UIPosAxisView {
|
||||
anchor: &mut self.anchor.x,
|
||||
offset: &mut self.offset.x,
|
||||
},
|
||||
Axis::Y => UIPosAxisView {
|
||||
anchor: &mut self.anchor.y,
|
||||
offset: &mut self.offset.y,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UIPosAxisView<'a> {
|
||||
pub anchor: &'a mut f32,
|
||||
pub offset: &'a mut f32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct UIRegion {
|
||||
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(),
|
||||
}
|
||||
}
|
||||
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),
|
||||
bot_right: self.bot_right.within(parent),
|
||||
}
|
||||
}
|
||||
pub fn select(&mut self, inner: &Self) {
|
||||
*self = inner.within(self);
|
||||
}
|
||||
pub fn axis_mut(&mut self, axis: Axis) -> UIRegionAxisView<'_> {
|
||||
UIRegionAxisView {
|
||||
top_left: self.top_left.axis_mut(axis),
|
||||
bot_right: self.bot_right.axis_mut(axis),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UIRegionAxisView<'a> {
|
||||
pub top_left: UIPosAxisView<'a>,
|
||||
pub bot_right: UIPosAxisView<'a>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum Axis {
|
||||
X,
|
||||
Y,
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
mod color;
|
||||
mod def;
|
||||
mod format;
|
||||
mod vec2;
|
||||
|
||||
pub use color::*;
|
||||
pub use def::*;
|
||||
pub use format::*;
|
||||
pub use vec2::*;
|
||||
|
||||
use crate::{render::data::PrimitiveInstance, WidgetId, Widgets};
|
||||
use bytemuck::Pod;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Primitives {
|
||||
pub instances: Vec<PrimitiveInstance>,
|
||||
pub data: Vec<u32>,
|
||||
}
|
||||
|
||||
pub struct Painter<'a> {
|
||||
nodes: &'a Widgets,
|
||||
primitives: Primitives,
|
||||
pub region: UIRegion,
|
||||
}
|
||||
|
||||
/// NOTE: Self must have at least u32 alignment
|
||||
pub trait PrimitiveData: Pod {
|
||||
const DISCRIM: u32;
|
||||
}
|
||||
|
||||
impl<'a> Painter<'a> {
|
||||
pub fn new(nodes: &'a Widgets) -> Self {
|
||||
Self {
|
||||
nodes,
|
||||
primitives: Primitives::default(),
|
||||
region: UIRegion::full(),
|
||||
}
|
||||
}
|
||||
pub fn write<Data: PrimitiveData>(&mut self, data: Data) {
|
||||
let ptr = self.primitives.data.len() as u32;
|
||||
let region = self.region;
|
||||
self.primitives
|
||||
.instances
|
||||
.push(PrimitiveInstance { region, ptr });
|
||||
self.primitives.data.push(Data::DISCRIM);
|
||||
self.primitives
|
||||
.data
|
||||
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
|
||||
}
|
||||
pub fn draw(&mut self, node: &WidgetId) {
|
||||
self.nodes.get(node).draw(self);
|
||||
}
|
||||
pub fn draw_within(&mut self, node: &WidgetId, region: UIRegion) {
|
||||
let old = self.region;
|
||||
self.region.select(®ion);
|
||||
self.draw(node);
|
||||
self.region = old;
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Primitives {
|
||||
self.primitives
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
use std::ops::*;
|
||||
|
||||
#[repr(C)]
|
||||
#[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) -> Vec2 {
|
||||
Vec2::new(x, y)
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
pub const fn new(x: f32, y: f32) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
pub const fn lerp(self, to: Self, amt: impl const Into<Self>) -> Self {
|
||||
let amt = amt.into();
|
||||
Self {
|
||||
x: lerp(self.x, to.x, amt.x),
|
||||
y: lerp(self.y, to.y, amt.y),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fn lerp(x: f32, y: f32, amt: f32) -> f32 {
|
||||
(1.0 - amt) * x + y * amt
|
||||
}
|
||||
|
||||
impl const From<f32> for Vec2 {
|
||||
fn from(v: f32) -> Self {
|
||||
Self { x: v, y: v }
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_op_inner {
|
||||
($op:ident $fn:ident $opa:ident $fna:ident) => {
|
||||
impl const $op for Vec2 {
|
||||
type Output = Self;
|
||||
|
||||
fn $fn(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
x: self.x.$fn(rhs.x),
|
||||
y: self.y.$fn(rhs.y),
|
||||
}
|
||||
}
|
||||
}
|
||||
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 Vec2 {
|
||||
type Output = Self;
|
||||
|
||||
fn $fn(self, rhs: f32) -> Self::Output {
|
||||
Self {
|
||||
x: self.x.$fn(rhs),
|
||||
y: self.y.$fn(rhs),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl $opa<f32> for Vec2 {
|
||||
fn $fna(&mut self, rhs: f32) {
|
||||
self.x.$fna(rhs);
|
||||
self.y.$fna(rhs);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_op {
|
||||
($op:ident $fn:ident) => {
|
||||
impl_op_inner!($op $fn ${concat($op,Assign)} ${concat($fn,_assign)});
|
||||
};
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
}
|
||||
@@ -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>(
|
||||
|
||||
Reference in New Issue
Block a user