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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Oliver Berzs
// https://github.com/oberzs/duku

//! Optional feature `window` module for simple window creation.

#![cfg(feature = "window")]

use std::collections::HashSet;
use std::time::Duration;
use std::time::Instant;
use window_dep::dpi::PhysicalPosition;
use window_dep::dpi::PhysicalSize;
use window_dep::event::DeviceEvent;
use window_dep::event::ElementState;
use window_dep::event::Event as WinitEvent;
use window_dep::event::MouseScrollDelta;
use window_dep::event::WindowEvent;
use window_dep::event_loop::ControlFlow;
use window_dep::event_loop::EventLoop;
use window_dep::window::Window as WinitWindow;
use window_dep::window::WindowBuilder as WinitWindowBuilder;

pub use window_dep::event::MouseButton;
pub use window_dep::event::VirtualKeyCode as Key;
pub use window_dep::window::CursorIcon as Cursor;

use crate::duku::Duku;
use crate::duku::DukuBuilder;
use crate::error::Result;
use crate::math::Vec2;
use crate::math::Vec3;
use crate::renderer::Camera;
use crate::surface::WindowHandle;

/// OS window wrapper around `winit`.
pub struct Window {
    window: WinitWindow,
    event_loop: EventLoop<()>,
}

/// OS window event handler.
pub struct Events {
    window: WinitWindow,
    events: Vec<Event>,

    keys_pressed: HashSet<Key>,
    keys_released: HashSet<Key>,
    keys_typed: HashSet<Key>,
    buttons_pressed: HashSet<MouseButton>,
    buttons_released: HashSet<MouseButton>,
    buttons_clicked: HashSet<MouseButton>,
    typed_char: Option<char>,

    mouse_position: Vec2,
    mouse_delta: Vec2,
    mouse_grab: bool,
    scroll_delta: Vec2,
}

/// OS window event.
#[derive(Debug, Copy, Clone)]
pub enum Event {
    /// window resize event
    Resize(Vec2),
}

/// Simple orbit camera controller.
#[derive(Debug, Copy, Clone)]
pub struct Orbit {
    /// center point to rotate around
    pivot: Vec3,
    /// camera move speed
    move_speed: f32,
}

/// OS window builder.
#[derive(Debug, Clone)]
pub struct WindowBuilder {
    duku: DukuBuilder,
    title: String,
    resizable: bool,
    width: u32,
    height: u32,
}

impl Duku {
    /// Create Duku with a basic window
    pub fn windowed(width: u32, height: u32) -> Result<(Duku, Window)> {
        Self::builder().build_window(width, height).build()
    }
}

impl DukuBuilder {
    /// Create OS window builder
    pub fn build_window(self, width: u32, height: u32) -> WindowBuilder {
        WindowBuilder {
            duku: self,
            title: "".to_string(),
            resizable: false,
            width,
            height,
        }
    }
}

impl Window {
    pub(crate) fn new(title: &str, width: u32, height: u32, resizable: bool) -> Self {
        let event_loop = EventLoop::new();
        let window = WinitWindowBuilder::new()
            .with_inner_size(PhysicalSize::new(width, height))
            .with_title(title)
            .with_resizable(resizable)
            .build(&event_loop)
            .expect("bad window");

        Self { window, event_loop }
    }

    #[cfg(target_os = "windows")]
    pub(crate) fn handle(&self) -> WindowHandle {
        use window_dep::platform::windows::WindowExtWindows;

        WindowHandle {
            hwnd: self.window.hwnd(),
        }
    }

    #[cfg(target_os = "linux")]
    pub(crate) fn handle(&self) -> WindowHandle {
        use window_dep::platform::unix::WindowExtUnix;

        WindowHandle {
            xlib_window: self.window.xlib_window().expect("Wayland not supported"),
            xlib_display: self.window.xlib_display().expect("Wayland not supported"),
        }
    }

    #[cfg(target_os = "macos")]
    pub(crate) fn handle(&self) -> WindowHandle {
        unimplemented!()
    }

    /// Start window's main loop for polling events
    pub fn while_open<F>(self, mut main_fn: F)
    where
        F: FnMut(&mut Events) + 'static,
    {
        let Self { window, event_loop } = self;
        let mut events = Events {
            events: vec![],
            keys_pressed: HashSet::new(),
            keys_released: HashSet::new(),
            keys_typed: HashSet::new(),
            buttons_pressed: HashSet::new(),
            buttons_released: HashSet::new(),
            buttons_clicked: HashSet::new(),
            mouse_position: Vec2::default(),
            mouse_delta: Vec2::default(),
            mouse_grab: false,
            scroll_delta: Vec2::default(),
            typed_char: None,
            window,
        };

        let mut last_resize = None;

        event_loop.run(move |event, _, control_flow| match event {
            WinitEvent::WindowEvent { event, window_id } if window_id == events.window.id() => {
                match event {
                    // close event
                    WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,

                    // resize event
                    WindowEvent::Resized(size) => {
                        if size.width != 0 && size.height != 0 {
                            last_resize = Some(Instant::now());
                        }
                    }

                    // mouse position event
                    WindowEvent::CursorMoved { position, .. } => {
                        events.mouse_position = Vec2::new(position.x as f32, position.y as f32);
                    }

                    // keyboard key event
                    WindowEvent::KeyboardInput { input, .. } => {
                        if let Some(key) = input.virtual_keycode {
                            match input.state {
                                ElementState::Pressed => {
                                    events.keys_typed.insert(key);
                                    events.keys_pressed.insert(key);
                                    events.keys_released.remove(&key);
                                }
                                ElementState::Released => {
                                    events.keys_released.insert(key);
                                    events.keys_pressed.remove(&key);
                                    events.keys_typed.remove(&key);
                                }
                            }
                        }
                    }

                    // mouse button event
                    WindowEvent::MouseInput { state, button, .. } => match state {
                        ElementState::Pressed => {
                            events.buttons_clicked.insert(button);
                            events.buttons_pressed.insert(button);
                            events.buttons_released.remove(&button);
                        }
                        ElementState::Released => {
                            events.buttons_released.insert(button);
                            events.buttons_pressed.remove(&button);
                            events.buttons_clicked.remove(&button);
                        }
                    },

                    // text input event
                    WindowEvent::ReceivedCharacter(c) => {
                        if !c.is_ascii_control() {
                            events.typed_char = Some(c);
                        }
                    }

                    // mouse scroll event
                    WindowEvent::MouseWheel { delta, .. } => {
                        if let MouseScrollDelta::LineDelta(x, y) = delta {
                            events.scroll_delta = Vec2::new(x as f32, y as f32);
                        }
                    }

                    _ => (),
                }
            }

            // mouse delta event
            WinitEvent::DeviceEvent { event, .. } => {
                if let DeviceEvent::MouseMotion { delta } = event {
                    let (x, y) = delta;
                    events.mouse_delta = Vec2::new(x as f32, y as f32);
                }
            }

            // draw event
            WinitEvent::MainEventsCleared => {
                // check resize timing
                if let Some(last) = last_resize {
                    if Instant::now().duration_since(last) >= Duration::from_millis(100) {
                        let size = events.size();
                        events.events.push(Event::Resize(size));
                        last_resize = None;

                        info!("resized window to {}x{}", size.x as u32, size.y as u32);
                    }
                }

                let size = events.size();
                if size.x as i32 != 0 && size.y as i32 != 0 && last_resize == None {
                    main_fn(&mut events);
                }

                events.events.clear();
                events.keys_typed.clear();
                events.typed_char = None;
                events.mouse_delta = Vec2::new(0.0, 0.0);
                events.scroll_delta = Vec2::new(0.0, 0.0);
            }
            _ => (),
        });
    }
}

impl Events {
    /// Check if keyboard key is pressed
    pub fn is_key_pressed(&self, key: Key) -> bool {
        self.keys_pressed.contains(&key)
    }

    /// Check if keyboard key is released
    pub fn is_key_released(&self, key: Key) -> bool {
        self.keys_released.contains(&key)
    }

    /// Check if keyboard key has been typed
    pub fn is_key_typed(&self, key: Key) -> bool {
        self.keys_typed.contains(&key)
    }

    /// Check if mouse button is pressed
    pub fn is_button_pressed(&self, button: MouseButton) -> bool {
        self.buttons_pressed.contains(&button)
    }

    /// Check if mouse button is released
    pub fn is_button_released(&self, button: MouseButton) -> bool {
        self.buttons_released.contains(&button)
    }

    /// Check if mouse button has been clicked
    pub fn is_button_clicked(&self, button: MouseButton) -> bool {
        self.buttons_clicked.contains(&button)
    }

    /// Get mouse position
    pub const fn mouse_position(&self) -> Vec2 {
        self.mouse_position
    }

    /// Set mouse position
    pub fn set_mouse_position(&mut self, position: Vec2) {
        self.window
            .set_cursor_position(PhysicalPosition::new(position.x as i32, position.y as i32))
            .expect("cannot set cursor position");
    }

    /// Get mouse position's change since last frame
    pub const fn mouse_delta(&self) -> Vec2 {
        self.mouse_delta
    }

    /// Get scroll wheel's change since last frame
    pub const fn scroll_delta(&self) -> Vec2 {
        self.scroll_delta
    }

    /// Get if mouse is contained in window
    pub const fn mouse_grab(&self) -> bool {
        self.mouse_grab
    }

    /// Set if mouse is contained in window
    pub fn set_mouse_grab(&mut self, grab: bool) {
        self.window
            .set_cursor_grab(grab)
            .expect("cannot set cursor grab");
        self.mouse_grab = grab;
    }

    /// Set if cursor is hidden
    pub fn hide_cursor(&mut self, hide: bool) {
        self.window.set_cursor_visible(!hide);
    }

    /// Set cursor icon
    pub fn set_cursor(&mut self, cursor: Cursor) {
        self.window.set_cursor_icon(cursor);
    }

    /// Get window size
    pub fn size(&self) -> Vec2 {
        let size = self.window.inner_size();
        Vec2::new(size.width as f32, size.height as f32)
    }

    /// Iterate over window events
    pub fn events(&self) -> impl Iterator<Item = &Event> {
        self.events.iter()
    }

    /// Set window title
    pub fn set_title(&mut self, title: impl AsRef<str>) {
        self.window.set_title(title.as_ref());
    }

    /// Get typed character if there is one
    pub const fn typed_char(&self) -> Option<char> {
        self.typed_char
    }
}

impl Orbit {
    /// Create a orbit controller
    pub fn new(pivot: impl Into<Vec3>) -> Self {
        Self {
            pivot: pivot.into(),
            move_speed: 2.5,
        }
    }

    /// Update camera
    pub fn update(&mut self, camera: &mut Camera, events: &mut Events, delta_time: f32) {
        // control orbiting around pivot
        let angle = 5.0f32.powf(self.move_speed) * delta_time;

        // mouse rotation
        if events.is_button_pressed(MouseButton::Middle) {
            // toggle mouse grab if needed
            if !events.mouse_grab() {
                events.set_mouse_grab(true);
                events.hide_cursor(true);
            }

            let delta = events.mouse_delta();
            let speed = 50.0 * delta_time;

            camera.move_around_point(self.pivot, speed * delta.x, Vec3::up());
            camera.move_around_point(self.pivot, speed * delta.y, camera.rotation.local_right());
        } else {
            // toggle mouse grab if needed
            if events.mouse_grab() {
                events.set_mouse_grab(false);
                events.hide_cursor(false);
            }
        }

        // horizontal rotation
        if events.is_key_pressed(Key::D) {
            camera.move_around_point(self.pivot, -angle, Vec3::up());
        }
        if events.is_key_pressed(Key::A) {
            camera.move_around_point(self.pivot, angle, Vec3::up());
        }

        // vertical rotation
        if events.is_key_pressed(Key::W) {
            camera.move_around_point(self.pivot, angle, camera.rotation.local_right());
        }
        if events.is_key_pressed(Key::S) {
            camera.move_around_point(self.pivot, -angle, camera.rotation.local_right());
        }

        // zoom
        let scroll = events.scroll_delta();
        camera.move_forward(scroll.y * (self.pivot - camera.position).length() * 0.05);

        // look at pivot point
        camera.look_at(self.pivot);
    }
}

impl WindowBuilder {
    /// Make window resizable
    pub const fn resizable(mut self) -> Self {
        self.resizable = true;
        self
    }

    /// Use window title
    pub fn title(mut self, title: impl AsRef<str>) -> Self {
        self.title = title.as_ref().to_string();
        self
    }

    /// Build duku context and window
    pub fn build(self) -> Result<(Duku, Window)> {
        let window = Window::new(&self.title, self.width, self.height, self.resizable);
        let duku = self.duku.attach_window(window.handle()).build()?;

        Ok((duku, window))
    }
}