uni_app/lib.rs
1#![recursion_limit = "512"]
2
3// wasm-unknown-unknown
4#[cfg(target_arch = "wasm32")]
5#[path = "web_app.rs"]
6pub mod sys;
7
8#[cfg(target_arch = "wasm32")]
9#[path = "web_fs.rs"]
10pub mod fs;
11
12// NOT wasm-unknown-unknown
13#[cfg(not(target_arch = "wasm32"))]
14extern crate glutin;
15
16#[cfg(not(target_arch = "wasm32"))]
17extern crate time;
18
19#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
20extern crate bytes;
21#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
22extern crate reqwest;
23
24#[cfg(not(target_arch = "wasm32"))]
25#[path = "native_app.rs"]
26/// main application struct
27pub mod sys;
28
29#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
30#[path = "native_fs_http.rs"]
31/// filesystem api
32pub mod fs;
33
34#[cfg(all(not(target_arch = "wasm32"), not(feature = "http")))]
35#[path = "native_fs.rs"]
36/// filesystem api
37pub mod fs;
38
39pub use self::fs::*;
40pub use self::sys::*;
41
42/// game window configuration
43pub struct AppConfig {
44 /// the window title (only visible on native target)
45 pub title: String,
46 /// the window/canvas size in pixels
47 pub size: (u32, u32),
48 /// The window icon : width,height,pixel data in rgba format.
49 /// winit recommends using a 32x32 image.
50 /// You can use the image crate to embed the icon in your executable at build time :
51 ///
52 /// pub static ICON: &[u8] = include_bytes!("my_icon.png");
53 /// let icon=image::load_from_memory(ICON).unwrap();
54 /// app_config.icon = Some((icon.width(),icon.height(),icon.as_bytes().to_vec()))
55 pub icon: Option<(u32,u32,Vec<u8>)>,
56 /// sync frames with screen frequency (can only be disabled on native target)
57 pub vsync: bool,
58 /// start the program without actually creating a window, for test purposes
59 pub headless: bool,
60 /// start in full screen (native target only)
61 pub fullscreen: bool,
62 /// whether user can resize the window (native target only)
63 pub resizable: bool,
64 /// whether the mouse cursor is visible while in the window
65 pub show_cursor: bool,
66 /// whether clicking on the window close button exits the program or sends a CloseRequested event
67 pub intercept_close_request: bool,
68}
69
70impl AppConfig {
71 pub fn new<T: Into<String>>(title: T, size: (u32, u32)) -> AppConfig {
72 AppConfig {
73 title: title.into(),
74 size,
75 vsync: true,
76 headless: false,
77 fullscreen: false,
78 resizable: true,
79 show_cursor: true,
80 intercept_close_request: false,
81 icon : None,
82 }
83 }
84}
85
86/// keyboard and mouse events
87pub mod events {
88 use std::fmt;
89
90 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
91 /// keyboard key scancode
92 pub enum ScanCode {
93 /// The '1' key over the letters.
94 Key1,
95 /// The '2' key over the letters.
96 Key2,
97 /// The '3' key over the letters.
98 Key3,
99 /// The '4' key over the letters.
100 Key4,
101 /// The '5' key over the letters.
102 Key5,
103 /// The '6' key over the letters.
104 Key6,
105 /// The '7' key over the letters.
106 Key7,
107 /// The '8' key over the letters.
108 Key8,
109 /// The '9' key over the letters.
110 Key9,
111 /// The '0' key over the 'O' and 'P' keys.
112 Key0,
113
114 A,
115 B,
116 C,
117 D,
118 E,
119 F,
120 G,
121 H,
122 I,
123 J,
124 K,
125 L,
126 M,
127 N,
128 O,
129 P,
130 Q,
131 R,
132 S,
133 T,
134 U,
135 V,
136 W,
137 X,
138 Y,
139 Z,
140
141 /// The Escape key, next to F1.
142 Escape,
143
144 F1,
145 F2,
146 F3,
147 F4,
148 F5,
149 F6,
150 F7,
151 F8,
152 F9,
153 F10,
154 F11,
155 F12,
156 F13,
157 F14,
158 F15,
159 F16,
160 F17,
161 F18,
162 F19,
163 F20,
164 F21,
165 F22,
166 F23,
167 F24,
168
169 /// Print Screen/SysRq.
170 Snapshot,
171 /// Scroll Lock.
172 ScrollLock,
173 /// Pause/Break key, next to Scroll lock.
174 Pause,
175
176 /// `Insert`, next to Backspace.
177 Insert,
178 Home,
179 Delete,
180 End,
181 PageDown,
182 PageUp,
183
184 Left,
185 Up,
186 Right,
187 Down,
188
189 /// The Backspace key, right over Enter.
190 Backspace,
191 /// The Enter key.
192 Enter,
193 /// The space bar.
194 Space,
195
196 /// The "Compose" key on Linux.
197 Compose,
198
199 Caret,
200
201 Numlock,
202 Numpad0,
203 Numpad1,
204 Numpad2,
205 Numpad3,
206 Numpad4,
207 Numpad5,
208 Numpad6,
209 Numpad7,
210 Numpad8,
211 Numpad9,
212 NumpadAdd,
213 NumpadDivide,
214 NumpadDecimal,
215 NumpadComma,
216 NumpadEnter,
217 NumpadEqual,
218 NumpadMultiply,
219 NumpadSubtract,
220
221 Apostrophe,
222 Asterisk,
223 Backslash,
224 CapsLock,
225 Colon,
226 Comma,
227 Convert,
228 Equal,
229 Backquote,
230 LAlt,
231 LBracket,
232 LCtrl,
233 LShift,
234 LWin,
235 Mail,
236 MediaSelect,
237 MediaStop,
238 Minus,
239 Mute,
240 Period,
241 Plus,
242 RAlt,
243 RBracket,
244 RCtrl,
245 RShift,
246 RWin,
247 Semicolon,
248 Slash,
249 Tab,
250 Underline,
251 Copy,
252 Paste,
253 Cut,
254
255 Unknown,
256 }
257
258 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
259 /// mouse button
260 pub enum MouseButton {
261 Left,
262 Middle,
263 Right,
264 Other(usize),
265 }
266
267 #[derive(Debug, Clone)]
268 /// data associated with a mouse button press/release event
269 pub struct MouseButtonEvent {
270 pub button: MouseButton,
271 }
272
273 #[derive(Clone)]
274 /// data associated with a key press event
275 /// Possible values for the virtual key code can be found in unrust/uni-app's `translate_scan_code`
276 /// [function](https://github.com/unrust/uni-app/blob/41246b070567e3267f128fff41ededf708149d60/src/native_keycode.rs#L160).
277 /// Warning, there are some slight variations from one OS to another, for example the `Command`, `F13`, `F14`, `F15` keys
278 /// only exist on Mac.
279 pub struct KeyDownEvent {
280 /// scancode : top left letter is `ScanCode::Q` even on an azerty keyboard
281 pub code: ScanCode,
282 /// virtual key code : top left letter is "KeyQ" on qwerty, "KeyA" on azerty
283 pub key: String,
284 /// whether a shift key is pressed
285 pub shift: bool,
286 /// whether an alt key is pressed
287 pub alt: bool,
288 /// whether a control key is pressed
289 pub ctrl: bool,
290 }
291
292 #[derive(Clone)]
293 /// data associated with a key release event
294 /// Possible values for the virtual key code can be found in unrust/uni-app's `translate_scan_code`
295 /// [function](https://github.com/unrust/uni-app/blob/41246b070567e3267f128fff41ededf708149d60/src/native_keycode.rs#L160).
296 /// Warning, there are some slight variations from one OS to another, for example the `Command`, `F13`, `F14`, `F15` keys
297 /// only exist on Mac.
298 pub struct KeyUpEvent {
299 /// scancode : top left letter is `ScanCode::Q` even on an azerty keyboard
300 pub code: ScanCode,
301 /// virtual key code : top left letter is "KeyQ" on qwerty, "KeyA" on azerty
302 pub key: String,
303 /// whether a shift key is pressed
304 pub shift: bool,
305 /// whether an alt key is pressed
306 pub alt: bool,
307 /// whether a control key is pressed
308 pub ctrl: bool,
309 }
310
311 impl fmt::Debug for KeyUpEvent {
312 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
313 write!(
314 f,
315 "{} {} {} {:?} {}",
316 if self.shift { "shift" } else { "" },
317 if self.alt { "alt" } else { "" },
318 if self.ctrl { "ctrl" } else { "" },
319 self.code,
320 self.key,
321 )
322 }
323 }
324
325 impl fmt::Debug for KeyDownEvent {
326 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
327 write!(
328 f,
329 "{} {} {} {:?} {}",
330 if self.shift { "shift" } else { "" },
331 if self.alt { "alt" } else { "" },
332 if self.ctrl { "ctrl" } else { "" },
333 self.code,
334 self.key,
335 )
336 }
337 }
338}
339
340pub use events::*;
341
342#[derive(Debug, Clone)]
343/// window event types
344pub enum AppEvent {
345 /// mouse button press
346 MouseDown(MouseButtonEvent),
347 /// mouse button release
348 MouseUp(MouseButtonEvent),
349 /// keyboard press
350 KeyDown(KeyDownEvent),
351 /// keyboard release
352 KeyUp(KeyUpEvent),
353 /// text input events
354 CharEvent(char),
355 /// window resize
356 Resized((u32, u32)),
357 /// mouse cursor position in pixels from the window top-left
358 MousePos((f64, f64)),
359 /// a file has been dropped on the game window. Get it with `App.get_dropped_file`
360 FileDropped(String),
361 /// window close button was pressed and [`AppConfig.intercept_close_request`] is true
362 CloseRequested,
363}