Skip to main content

grafix_toolbox/gui/elements/
layout.rs

1use super::*;
2
3#[derive(Default, Debug)]
4pub struct Layout {
5	click: Vec2,
6	pub layout: Surf,
7}
8impl Layout {
9	pub fn draw<'s: 'l, 'l>(&'s mut self, r: &mut RenderLock<'l>, t: &'l Theme, content: impl FnOnce(&mut RenderLock<'l>, Surf)) {
10		let (id, s, TOP_PAD, PIP_PAD) = (ref_UUID(self), self, 0.05, 0.04);
11
12		s.layout.clamp_to_screen(r);
13		let layout = s.layout.h_sub(TOP_PAD).y(PIP_PAD).size_sub(Vec2(PIP_PAD));
14		let Self { click, layout: Surf { pos, size: s_size } } = s;
15
16		let s_pos = Cell::from_mut(pos);
17		let Surf { pos, size } = layout.y_self(1).h(TOP_PAD);
18		r.draw_with_logic(
19			Rect { pos, size, color: t.highlight },
20			move |e, focused, mouse_pos| {
21				match e {
22					OfferFocus => (),
23					MouseButton { m, .. } if m.released() => return DropFocus,
24					MouseButton { .. } if focused => *click = mouse_pos.sub(s_pos.bind()),
25					MouseMove { at, .. } if focused => s_pos.mutate(|p| *p = at.sub(*click)),
26					_ => return Reject,
27				}
28				Accept
29			},
30			id,
31		);
32
33		let Surf { pos, size } = layout.x_self(1).y(-PIP_PAD).size(Vec2(PIP_PAD));
34		r.draw_with_logic(
35			Rect { pos, size, color: t.highlight },
36			move |e, focused, _| {
37				match e {
38					OfferFocus => (),
39					MouseButton { m, .. } if m.released() => return DropFocus,
40					MouseMove { at, .. } if focused => {
41						*s_size = at.sub(s_pos.bind().sum((s_size.x(), 0))).mul((1, -1)).sum(*s_size).sub(PIP_PAD * 0.5).fmax(TOP_PAD);
42						s_pos.mutate(|(_, y)| *y = at.y() + PIP_PAD * 0.5);
43					}
44					_ => return Reject,
45				}
46				Accept
47			},
48			id + 1,
49		);
50
51		let Surf { pos, size } = layout;
52		r.draw(Rect { pos, size, color: t.bg });
53
54		let _c = r.clip(layout);
55		content(r, layout);
56	}
57}
58
59impl<'s: 'l, 'l> Lock::Layout<'s, 'l, '_> {
60	pub fn draw(self, c: impl FnOnce(&mut RenderLock<'l>, Surf)) {
61		let Self { s, r, t } = self;
62		s.draw(r, t, c)
63	}
64}