devela/ui/back/miniquad/
service.rs1use crate::{Box, ToString};
10use crate::{UiCap, UiCapImage, UiCapInput, UiCapWindow, UiService, is};
11#[cfg(doc)]
12use ::miniquad::FilterMode;
13use ::miniquad::{EventHandler, conf::Conf};
14
15pub trait MiniquadEventHandlerExt: EventHandler {
21 fn init(self) -> Self;
23
24 fn interpolation(&self) -> bool;
26 fn set_interpolation(&mut self, interpolate: bool);
28
29 fn maintain_aspect_ratio(&self) -> bool;
31 fn set_maintain_aspect_ratio(&mut self, maintain: bool);
33}
34
35impl<T: MiniquadEventHandlerExt + 'static> UiService for MiniquadService<T> {
36 fn capabilities(&self) -> UiCap {
37 let image = Some(UiCapImage { rgb: true, ..Default::default() });
38 let input = Some(UiCapInput { keyboard: true, mouse: true, ..Default::default() });
39 let window = Some(UiCapWindow { multi: false });
40 UiCap { image, input, window, ..Default::default() }
41 }
42 fn version(&self) -> (u32, u32, u32) {
43 (0, 4, 7)
44 }
45}
46
47#[derive(Debug)]
49pub struct MiniquadService<T: MiniquadEventHandlerExt + 'static> {
50 handler: Option<T>,
51 conf: Conf,
52
53 width: u32,
55 height: u32,
56}
57
58impl<T: MiniquadEventHandlerExt + 'static> Default for MiniquadService<T> {
59 fn default() -> Self {
61 Self {
62 handler: None,
63 conf: Conf::default(),
64 width: 0,
66 height: 0,
67 }
68 }
69}
70
71impl<T: MiniquadEventHandlerExt + 'static> MiniquadService<T> {
73 pub fn with(handler: T) -> Self {
75 Self { handler: Some(handler), ..Default::default() }
76 }
77
78 pub fn init(self) {
85 let handler = self.handler.expect("Event handler is required");
86 ::miniquad::start(self.conf, move || Box::new(handler.init()));
87 }
88
89 pub fn handler(mut self, handler: T) -> Self {
93 self.handler = Some(handler);
94 self
95 }
96
97 pub fn conf(mut self, conf: Conf) -> Self {
99 self.conf = conf;
100 self
101 }
102
103 pub fn blocking_event_loop(mut self, blocking: bool) -> Self {
107 self.conf.platform.blocking_event_loop = blocking;
108 self
109 }
110
111 pub fn title(mut self, title: impl ToString) -> Self {
115 self.conf.window_title = title.to_string();
116 self
117 }
118
119 pub fn window_size(mut self, width: u32, height: u32) -> Self {
123 assert![width <= i32::MAX as u32];
124 assert![height <= i32::MAX as u32];
125 self.conf.window_width = width as i32;
126 self.conf.window_height = height as i32;
127 self
128 }
129
130 pub fn window_resizable(mut self, resizable: bool) -> Self {
132 self.conf.window_resizable = resizable;
133 self
134 }
135
136 pub fn fullscreen(mut self, fullscreen: bool) -> Self {
138 self.conf.fullscreen = fullscreen;
139 self
140 }
141
142 pub fn buffer_size(mut self, width: u32, height: u32) -> Self {
146 assert![width <= i32::MAX as u32];
147 assert![height <= i32::MAX as u32];
148 self.width = width;
149 self.height = height;
150 self
151 }
152 pub fn interpolation(mut self, linear: bool) -> Self {
156 is![let Some(h) = self.handler.as_mut(); h.set_interpolation(linear)];
157 self
158 }
159
160 pub fn maintain_aspect_ratio(mut self, maintain: bool) -> Self {
164 is![let Some(h) = self.handler.as_mut(); h.set_maintain_aspect_ratio(maintain)];
165 self
166 }
167}
168
169impl<T: MiniquadEventHandlerExt + 'static> MiniquadService<T> {
171 pub fn get_interpolation(self) -> bool {
173 self.handler.as_ref().map(|h| h.interpolation()).unwrap_or_default()
174 }
175 pub fn set_interpolation(&mut self, interpolate: bool) {
177 is![let Some(h) = self.handler.as_mut(); h.set_interpolation(interpolate)];
178 }
179
180 pub fn get_maintain_aspect_ratio(self) -> bool {
182 self.handler.as_ref().map(|h| h.maintain_aspect_ratio()).unwrap_or_default()
183 }
184 pub fn set_maintain_aspect_ratio(&mut self, maintain: bool) {
186 is![let Some(h) = self.handler.as_mut(); h.set_maintain_aspect_ratio(maintain)];
187 }
188}