Add anchor data

This commit is contained in:
Carter Anderson 2020-01-11 17:33:44 -08:00
parent 9c3c92f39a
commit 7f0f2d0515
4 changed files with 47 additions and 4 deletions

View File

@ -89,6 +89,7 @@ fn setup(world: &mut World) {
vec![(Rect {
position: math::vec2(75.0, 75.0),
dimensions: math::vec2(100.0, 100.0),
anchors: Anchors::new(0.5, 0.5, 0.5, 0.5),
color: math::vec4(0.0, 1.0, 0.0, 1.0),
},)],
);
@ -98,6 +99,7 @@ fn setup(world: &mut World) {
vec![(Rect {
position: math::vec2(50.0, 50.0),
dimensions: math::vec2(100.0, 100.0),
anchors: Anchors::new(0.5, 0.5, 0.5, 0.5),
color: math::vec4(1.0, 0.0, 0.0, 1.0),
},)],
);
@ -107,6 +109,7 @@ fn setup(world: &mut World) {
vec![(Rect {
position: math::vec2(100.0, 100.0),
dimensions: math::vec2(100.0, 100.0),
anchors: Anchors::new(0.5, 0.5, 0.5, 0.5),
color: math::vec4(0.0, 0.0, 1.0, 1.0),
},)],
);

View File

@ -13,6 +13,7 @@ use zerocopy::{AsBytes, FromBytes};
pub struct RectData {
pub position: [f32; 2],
pub dimensions: [f32; 2],
pub anchors: [f32; 4],
pub color: [f32; 4],
pub z_index: f32,
}
@ -54,6 +55,7 @@ impl UiPipeline {
position: rect.position.into(),
dimensions: rect.dimensions.into(),
color: rect.color.into(),
anchors: [rect.anchors.top, rect.anchors.bottom, rect.anchors.left, rect.anchors.right],
z_index: z,
});
@ -152,10 +154,15 @@ impl Pipeline for UiPipeline {
shader_location: 4,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float,
format: wgpu::VertexFormat::Float4,
offset: 8 * 4,
shader_location: 5,
},
wgpu::VertexAttributeDescriptor {
format: wgpu::VertexFormat::Float,
offset: 12 * 4,
shader_location: 6,
},
],
};

View File

@ -7,8 +7,9 @@ layout(location = 1) in vec4 a_Normal;
// instanced attributes (RectData)
layout (location = 2) in vec2 a_RectPosition;
layout (location = 3) in vec2 a_RectDimensions;
layout (location = 4) in vec4 a_RectColor;
layout (location = 5) in float a_RectZIndex;
layout (location = 4) in vec4 a_RectAnchors;
layout (location = 5) in vec4 a_RectColor;
layout (location = 6) in float a_RectZIndex;
layout(location = 0) out vec4 v_Color;

View File

@ -1,16 +1,48 @@
use crate::math::{Vec2, Vec4};
pub struct Anchors {
pub top: f32,
pub bottom: f32,
pub left: f32,
pub right: f32,
}
impl Anchors {
pub fn new(top: f32, bottom: f32, left: f32, right: f32) -> Self {
Anchors {
top,
bottom,
left,
right
}
}
}
impl Default for Anchors {
fn default() -> Self {
Anchors {
top: 0.5,
bottom: 0.5,
left: 0.5,
right: 0.5
}
}
}
pub struct Rect {
pub position: Vec2,
pub dimensions: Vec2,
pub anchors: Anchors,
pub color: Vec4,
}
impl Rect {
pub fn new(position: Vec2, dimensions: Vec2, color: Vec4) -> Self {
pub fn new(position: Vec2, dimensions: Vec2, anchors: Anchors, color: Vec4) -> Self {
Rect {
position,
dimensions,
anchors,
color,
}
}