Skip to main content

grafix_toolbox/gui/primitive/
rect.rs

1use super::*;
2
3pub struct Rect {
4	pub pos: Vec2,
5	pub size: Vec2,
6	pub color: Color,
7}
8impl Rect {
9	pub fn compare(&self, crop: &Geom, r: &RectImpl) -> State {
10		let Self { pos, size, color } = *self;
11		let xyzw = State::XYZW.or_def(geom_cmp(pos, size, crop, &r.base));
12		let rgba = State::RGBA.or_def(color != r.base.color);
13		let ord = State::MISMATCH.or_def(!rgba.is_empty() && ordered(color) != r.ordered());
14		ord | xyzw | rgba
15	}
16	pub fn obj(self, crop: Geom) -> RectImpl {
17		let Self { pos, size, color } = self;
18		RectImpl { base: Base { pos, size, crop, color } }
19	}
20}
21pub struct RectImpl {
22	base: Base,
23}
24impl RectImpl {
25	pub fn batchable(&self, r: &Self) -> bool {
26		self.ordered() == r.ordered()
27	}
28}
29impl Primitive for RectImpl {
30	fn base(&self) -> &Base {
31		&self.base
32	}
33	fn write_mesh(&self, aspect: Vec2, BatchedObj { z, state, xyzw, rgba, .. }: BatchedObj) {
34		if state.contains(State::XYZW) {
35			let ((x1, y1), (x2, y2)) = mat2({
36				let (p1, p2) = self.base.bound_box();
37				(p1.mul(aspect), p2.mul(aspect))
38			});
39			let O = f16::ZERO;
40
41			xyzw[..16].copy_from_slice(&[x1, y1, z, O, x2, y1, z, O, x2, y2, z, O, x1, y2, z, O]);
42		}
43
44		if state.contains(State::RGBA) {
45			let (r, g, b, a) = vec4(self.base.color.mul(255).clmp(0, 255).round());
46
47			rgba[..16].copy_from_slice(&[r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a]);
48		}
49	}
50	fn batch_draw(&self, b: &VaoBind<u16>, (offset, num): (u16, u16)) {
51		let s = LeakyStatic!(Shader, { [vs_gui__pos_col, ps_gui__col].pipe(Shader::pure) });
52
53		let _ = s.Bind();
54		b.Draw((num, offset, gl::TRIANGLES));
55	}
56
57	fn ordered(&self) -> bool {
58		ordered(self.base().color)
59	}
60}
61
62SHADER!(
63	vs_gui__pos_col,
64	r"layout(location = 0) in vec4 Position;
65	layout(location = 1) in vec4 Color;
66	out vec4 glColor;
67
68	void main() {
69		gl_Position = vec4(Position.xyz, 1);
70		glColor = Color;
71	}"
72);
73SHADER!(
74	ps_gui__col,
75	r"in vec4 glColor;
76	layout(location = 0) out vec4 glFragColor;
77
78	void main() { glFragColor = glColor; }"
79);