Skip to main content

grafix_toolbox/
gui.rs

1pub use {
2	elements::*,
3	primitive::prim,
4	render::{RenderLock, Renderer},
5	surface::*,
6};
7
8pub fn hex_to_rgba(c: u32) -> Color {
9	vec4(((c & 0xff000000) >> 24, (c & 0xff0000) >> 16, (c & 0xff00) >> 8, c & 0xff)).div(255)
10}
11
12pub fn pix_to_size(pixels: f32, window: &impl Frame) -> f32 {
13	pixels * window.pixel() * 2.
14}
15
16impl RenderLock<'_> {
17	pub fn clipboard() -> String {
18		let (str, _) = clip_store().to_owned();
19		str
20	}
21	pub fn set_clipboard(s: impl ToString) {
22		*clip_store() = (s.to_string(), true);
23	}
24	pub fn sync_clipboard(&self, w: &mut impl Window) {
25		let mut lock = clip_store();
26		let (str, changed) = &mut *lock;
27		if *changed {
28			w.set_clipboard(str);
29			*changed = false;
30			return;
31		}
32
33		let wstr = w.clipboard();
34		if *str != wstr {
35			*str = wstr
36		}
37	}
38}
39fn clip_store<'s>() -> MutexGuard<'s, (String, bool)> {
40	LazyStatic!((String, bool))
41}
42
43macro_rules! storage {
44	($($t: ident),+) => {
45		impl Renderer {
46			$(pub fn $t(&mut self, id: u32) -> &mut $t {
47				self.storage.get_mut().$t(id)
48			})+
49		}
50		impl<'l> RenderLock<'l> {
51			$(pub fn $t<'r: 'l>(&mut self, id: u32) -> Lock::$t<'r, 'l, '_> {
52				let s = unsafe { &mut *self.r.storage.as_ptr() };
53				s.$t(id).lock(self)
54			})+
55		}
56		#[derive(Default, Debug)]
57		struct ElementStorage {
58			$($t: HashMap<u32, $t>,)+
59		}
60		impl ElementStorage {
61			$(fn $t(&mut self, id: u32) -> &mut $t {
62				self.$t.entry(id).or_insert_with(|| Def())
63			})+
64		}
65	}
66}
67storage!(Button, HyperText, Label, Layout, LineEdit, Selector, Slider, SliderNum, TextEdit);
68
69#[macro_use]
70mod cache;
71
72mod batch;
73mod elements;
74mod primitive;
75mod render;
76mod surface;
77
78type Geom = (Vec2, Vec2);
79type Color = Vec4;
80type TexCoord = Vec4;
81type LogicId = usize;
82
83struct LogicStorage<'s> {
84	id: LogicId,
85	bound: LogicBound,
86	func: Box<dyn 's + EventReaction>,
87}
88trait_alias!(pub EventReaction, FnMut(&Event, bool, Vec2) -> EventReply); // TODO trait alias all
89
90enum LogicBound {
91	Crop(Geom),
92	Obj(u32),
93}
94
95use crate::{lazy::*, lib::*, math::*};
96use GL::{atlas::VTex2d, event::*, font::Font, window::*, *};
97use {Event::*, EventReply::*, batch::*, cache::*, primitive::*};