1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#![recursion_limit = "512"]

// wasm-unknown-unknown
#[cfg(target_arch = "wasm32")]
#[path = "web_app.rs"]
pub mod sys;

#[cfg(target_arch = "wasm32")]
#[path = "web_fs.rs"]
pub mod fs;

// NOT wasm-unknown-unknown
#[cfg(not(target_arch = "wasm32"))]
extern crate glutin;

#[cfg(not(target_arch = "wasm32"))]
extern crate time;

#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
extern crate bytes;
#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
extern crate reqwest;

#[cfg(not(target_arch = "wasm32"))]
#[path = "native_app.rs"]
/// main application struct
pub mod sys;

#[cfg(all(not(target_arch = "wasm32"), feature = "http"))]
#[path = "native_fs_http.rs"]
/// filesystem api
pub mod fs;

#[cfg(all(not(target_arch = "wasm32"), not(feature = "http")))]
#[path = "native_fs.rs"]
/// filesystem api
pub mod fs;

pub use self::fs::*;
pub use self::sys::*;

/// game window configuration
pub struct AppConfig {
    /// the window title (only visible on native target)
    pub title: String,
    /// the window/canvas size in pixels
    pub size: (u32, u32),
    /// The window icon : width,height,pixel data in rgba format.
    /// winit recommends using a 32x32 image.
    /// You can use the image crate to embed the icon in your executable at build time :
    ///
    /// pub static ICON: &[u8] = include_bytes!("my_icon.png");
    /// let icon=image::load_from_memory(ICON).unwrap();
    /// app_config.icon = Some((icon.width(),icon.height(),icon.as_bytes().to_vec()))
    pub icon: Option<(u32,u32,Vec<u8>)>,
    /// sync frames with screen frequency (can only be disabled on native target)
    pub vsync: bool,
    /// start the program without actually creating a window, for test purposes
    pub headless: bool,
    /// start in full screen (native target only)
    pub fullscreen: bool,
    /// whether user can resize the window (native target only)
    pub resizable: bool,
    /// whether the mouse cursor is visible while in the window
    pub show_cursor: bool,
    /// whether clicking on the window close button exits the program or sends a CloseRequested event
    pub intercept_close_request: bool,
}

impl AppConfig {
    pub fn new<T: Into<String>>(title: T, size: (u32, u32)) -> AppConfig {
        AppConfig {
            title: title.into(),
            size,
            vsync: true,
            headless: false,
            fullscreen: false,
            resizable: true,
            show_cursor: true,
            intercept_close_request: false,
            icon : None,
        }
    }
}

/// keyboard and mouse events
pub mod events {
    use std::fmt;

    #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
    /// keyboard key scancode
    pub enum ScanCode {
        /// The '1' key over the letters.
        Key1,
        /// The '2' key over the letters.
        Key2,
        /// The '3' key over the letters.
        Key3,
        /// The '4' key over the letters.
        Key4,
        /// The '5' key over the letters.
        Key5,
        /// The '6' key over the letters.
        Key6,
        /// The '7' key over the letters.
        Key7,
        /// The '8' key over the letters.
        Key8,
        /// The '9' key over the letters.
        Key9,
        /// The '0' key over the 'O' and 'P' keys.
        Key0,

        A,
        B,
        C,
        D,
        E,
        F,
        G,
        H,
        I,
        J,
        K,
        L,
        M,
        N,
        O,
        P,
        Q,
        R,
        S,
        T,
        U,
        V,
        W,
        X,
        Y,
        Z,

        /// The Escape key, next to F1.
        Escape,

        F1,
        F2,
        F3,
        F4,
        F5,
        F6,
        F7,
        F8,
        F9,
        F10,
        F11,
        F12,
        F13,
        F14,
        F15,
        F16,
        F17,
        F18,
        F19,
        F20,
        F21,
        F22,
        F23,
        F24,

        /// Print Screen/SysRq.
        Snapshot,
        /// Scroll Lock.
        ScrollLock,
        /// Pause/Break key, next to Scroll lock.
        Pause,

        /// `Insert`, next to Backspace.
        Insert,
        Home,
        Delete,
        End,
        PageDown,
        PageUp,

        Left,
        Up,
        Right,
        Down,

        /// The Backspace key, right over Enter.
        Backspace,
        /// The Enter key.
        Enter,
        /// The space bar.
        Space,

        /// The "Compose" key on Linux.
        Compose,

        Caret,

        Numlock,
        Numpad0,
        Numpad1,
        Numpad2,
        Numpad3,
        Numpad4,
        Numpad5,
        Numpad6,
        Numpad7,
        Numpad8,
        Numpad9,
        NumpadAdd,
        NumpadDivide,
        NumpadDecimal,
        NumpadComma,
        NumpadEnter,
        NumpadEqual,
        NumpadMultiply,
        NumpadSubtract,

        Apostrophe,
        Asterisk,
        Backslash,
        CapsLock,
        Colon,
        Comma,
        Convert,
        Equal,
        Backquote,
        LAlt,
        LBracket,
        LCtrl,
        LShift,
        LWin,
        Mail,
        MediaSelect,
        MediaStop,
        Minus,
        Mute,
        Period,
        Plus,
        RAlt,
        RBracket,
        RCtrl,
        RShift,
        RWin,
        Semicolon,
        Slash,
        Tab,
        Underline,
        Copy,
        Paste,
        Cut,

        Unknown,
    }

    #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
    /// mouse button
    pub enum MouseButton {
        Left,
        Middle,
        Right,
        Other(usize),
    }

    #[derive(Debug, Clone)]
    /// data associated with a mouse button press/release event
    pub struct MouseButtonEvent {
        pub button: MouseButton,
    }

    #[derive(Clone)]
    /// data associated with a key press event
    /// Possible values for the virtual key code can be found in unrust/uni-app's `translate_scan_code`
    /// [function](https://github.com/unrust/uni-app/blob/41246b070567e3267f128fff41ededf708149d60/src/native_keycode.rs#L160).
    /// Warning, there are some slight variations from one OS to another, for example the `Command`, `F13`, `F14`, `F15` keys
    /// only exist on Mac.
    pub struct KeyDownEvent {
        /// scancode : top left letter is `ScanCode::Q` even on an azerty keyboard
        pub code: ScanCode,
        /// virtual key code : top left letter is "KeyQ" on qwerty, "KeyA" on azerty
        pub key: String,
        /// whether a shift key is pressed
        pub shift: bool,
        /// whether an alt key is pressed
        pub alt: bool,
        /// whether a control key is pressed
        pub ctrl: bool,
    }

    #[derive(Clone)]
    /// data associated with a key release event
    /// Possible values for the virtual key code can be found in unrust/uni-app's `translate_scan_code`
    /// [function](https://github.com/unrust/uni-app/blob/41246b070567e3267f128fff41ededf708149d60/src/native_keycode.rs#L160).
    /// Warning, there are some slight variations from one OS to another, for example the `Command`, `F13`, `F14`, `F15` keys
    /// only exist on Mac.
    pub struct KeyUpEvent {
        /// scancode : top left letter is `ScanCode::Q` even on an azerty keyboard
        pub code: ScanCode,
        /// virtual key code : top left letter is "KeyQ" on qwerty, "KeyA" on azerty
        pub key: String,
        /// whether a shift key is pressed
        pub shift: bool,
        /// whether an alt key is pressed
        pub alt: bool,
        /// whether a control key is pressed
        pub ctrl: bool,
    }

    impl fmt::Debug for KeyUpEvent {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{} {} {} {:?} {}",
                if self.shift { "shift" } else { "" },
                if self.alt { "alt" } else { "" },
                if self.ctrl { "ctrl" } else { "" },
                self.code,
                self.key,
            )
        }
    }

    impl fmt::Debug for KeyDownEvent {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(
                f,
                "{} {} {} {:?} {}",
                if self.shift { "shift" } else { "" },
                if self.alt { "alt" } else { "" },
                if self.ctrl { "ctrl" } else { "" },
                self.code,
                self.key,
            )
        }
    }
}

pub use events::*;

#[derive(Debug, Clone)]
/// window event types
pub enum AppEvent {
    /// mouse button press
    MouseDown(MouseButtonEvent),
    /// mouse button release
    MouseUp(MouseButtonEvent),
    /// keyboard press
    KeyDown(KeyDownEvent),
    /// keyboard release
    KeyUp(KeyUpEvent),
    /// text input events
    CharEvent(char),
    /// window resize
    Resized((u32, u32)),
    /// mouse cursor position in pixels from the window top-left
    MousePos((f64, f64)),
    /// a file has been dropped on the game window. Get it with `App.get_dropped_file`
    FileDropped(String),
    /// window close button was pressed and [`AppConfig.intercept_close_request`] is true
    CloseRequested,
}