shura 0.6.0

A fast cross-plattform 2D component-based game framework
Documentation
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::Camera;
use crate::Vector;
#[cfg(feature = "gamepad")]
use gilrs::*;
use instant::{Duration, Instant};
use rustc_hash::FxHashMap;
use winit::event::*;

#[cfg(all(feature = "log", feature = "gamepad"))]
use crate::log::info;

pub use winit::event::ModifiersState as Modifier;
pub use winit::event::MouseButton;
pub use winit::event::VirtualKeyCode as Key;

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
/// Indicates if the screen is touched anywhere.
pub struct ScreenTouch;

#[cfg(feature = "gamepad")]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
/// Button on a GamePad
pub struct GamepadButton {
    pub gamepad: GamepadId,
    pub button: Button,
}

#[cfg(feature = "gamepad")]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum GamepadStick {
    Left,
    Right,
}

#[cfg(feature = "gamepad")]
impl GamepadButton {
    pub fn new(gamepad: GamepadId, button: Button) -> Self {
        Self { gamepad, button }
    }
}

#[cfg(feature = "gamepad")]
impl From<GamepadButton> for InputTrigger {
    fn from(k: GamepadButton) -> Self {
        Self::GamepadButton(k)
    }
}

impl From<Key> for InputTrigger {
    fn from(k: Key) -> Self {
        Self::Key(k)
    }
}

impl From<MouseButton> for InputTrigger {
    fn from(m: MouseButton) -> Self {
        Self::MouseButton(m)
    }
}

impl From<ScreenTouch> for InputTrigger {
    fn from(t: ScreenTouch) -> Self {
        Self::ScreenTouch(t)
    }
}

#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
/// Trigger of a key-like event
pub enum InputTrigger {
    Key(Key),
    MouseButton(MouseButton),
    ScreenTouch(ScreenTouch),
    #[cfg(feature = "gamepad")]
    GamepadButton(GamepadButton),
}

/// Event of a [InputTrigger] that holds the trigger and the time of the event.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct InputEvent {
    trigger: InputTrigger,
    pressed: bool,
    #[cfg_attr(feature = "serde", serde(skip))]
    #[cfg_attr(feature = "serde", serde(default = "Instant::now"))]
    start: Instant,
    frames: u32,
    pressure: f32,
}

impl InputEvent {
    pub fn new(trigger: InputTrigger, pressure: f32) -> Self {
        Self {
            trigger,
            pressed: true,
            start: Instant::now(),
            frames: 1,
            pressure,
        }
    }

    // Value between 0 and 1 of how much a analog key is pressed
    pub fn pressure(&self) -> f32 {
        self.pressure
    }

    pub fn update(&mut self) {
        self.pressed = false;
        self.frames += 1;
    }

    pub fn frames(&self) -> u32 {
        self.frames
    }

    pub fn held_time(&self) -> Duration {
        self.start.elapsed()
    }

    pub fn is_pressed(&self) -> bool {
        self.pressed
    }

    pub fn trigger(&self) -> InputTrigger {
        self.trigger
    }
}

/// Manage input from touch devices, keyboards, mice and gamepads.
pub struct Input {
    cursor_raw: Vector<u32>,
    touches: FxHashMap<u64, Vector<u32>>,
    events: FxHashMap<InputTrigger, InputEvent>,
    modifiers: Modifier,
    wheel_delta: f32,
    window_size: Vector<f32>,
    #[cfg(feature = "gamepad")]
    game_pad_manager: Gilrs,
    #[cfg(feature = "gamepad")]
    active_gamepad: Option<GamepadId>,
    #[cfg(feature = "gamepad")]
    dead_zone: f32,
}

impl Input {
    #[cfg(feature = "gamepad")]
    pub const DEFAULT_DEAD_ZONE: f32 = 0.1;

    pub(crate) fn new(window_size: Vector<u32>) -> Self {
        Self {
            cursor_raw: Vector::new(0, 0),
            touches: Default::default(),
            events: Default::default(),
            modifiers: Default::default(),
            wheel_delta: 0.0,
            window_size: window_size.cast(),
            #[cfg(feature = "gamepad")]
            game_pad_manager: match Gilrs::new() {
                Ok(ok) => ok,
                Err(err) => match err {
                    Error::NotImplemented(gilrs) => gilrs,
                    Error::InvalidAxisToBtn => panic!("Gamepad Error: Invalid Axis To Button!"),
                    Error::Other(err) => panic!("Gamepad Error: {}", err),
                },
            },
            #[cfg(feature = "gamepad")]
            active_gamepad: None,
            #[cfg(feature = "gamepad")]
            dead_zone: Self::DEFAULT_DEAD_ZONE,
        }
    }

    pub(crate) fn resize(&mut self, window_size: Vector<u32>) {
        self.window_size = window_size.cast()
    }

    pub(crate) fn on_event(&mut self, event: &WindowEvent) {
        match event {
            WindowEvent::CursorMoved { position, .. } => {
                self.cursor_raw = Vector::new(position.x as u32, position.y as u32);
            }
            WindowEvent::Touch(touch) => {
                let pos = Vector::new(touch.location.x as u32, touch.location.y as u32);
                self.cursor_raw = pos;
                match touch.phase {
                    TouchPhase::Started => {
                        let trigger = ScreenTouch.into();
                        self.touches.insert(touch.id, pos);
                        self.events.insert(trigger, InputEvent::new(trigger, 1.0));
                    }
                    TouchPhase::Ended | TouchPhase::Cancelled => {
                        let trigger = ScreenTouch.into();
                        self.touches.remove(&touch.id);
                        self.events.remove(&trigger);
                    }
                    TouchPhase::Moved => {
                        if let Some(touch) = self.touches.get_mut(&touch.id) {
                            *touch = pos;
                        }
                    }
                }
            }
            WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
                Some(key) => {
                    let trigger = key.into();
                    match input.state {
                        ElementState::Pressed => {
                            if !self.events.contains_key(&trigger) {
                                self.events.insert(trigger, InputEvent::new(trigger, 1.0));
                            }
                        }
                        ElementState::Released => {
                            self.events.remove(&trigger);
                        }
                    }
                }
                None => {}
            },
            WindowEvent::MouseInput { state, button, .. } => {
                let trigger = (*button).into();
                match state {
                    ElementState::Pressed => {
                        self.events.insert(trigger, InputEvent::new(trigger, 1.0));
                    }
                    ElementState::Released => {
                        self.events.remove(&trigger);
                    }
                }
            }
            WindowEvent::MouseWheel { delta, .. } => match delta {
                MouseScrollDelta::LineDelta(_x, y) => {
                    self.wheel_delta = *y;
                    // if *y > 0.0 {
                    //     let trigger =
                    //     self.events.insert(trigger, InputEvent::new(trigger));
                    // } else if *y < 0.0 {
                    //     let trigger = key.into();
                    //     self.events.insert(trigger, InputEvent::new(trigger));
                    // }
                }
                MouseScrollDelta::PixelDelta(delta) => {
                    self.wheel_delta = if delta.y > 0.0 { 1.0 } else { -1.0 };
                }
            },
            WindowEvent::ModifiersChanged(state) => {
                self.modifiers = *state;
            }
            _ => {}
        }
    }

    pub(crate) fn update(&mut self) {
        self.wheel_delta = 0.0;

        for trigger in self.events.values_mut() {
            trigger.update();
        }
    }

    pub fn are_pressed(&self, trigger: &[impl Into<InputTrigger> + Copy]) -> bool {
        trigger
            .iter()
            .all(|trigger| match self.events.get(&(*trigger).into()) {
                Some(i) => return i.is_pressed(),
                None => false,
            })
    }

    pub fn are_held(&self, trigger: &[impl Into<InputTrigger> + Copy]) -> bool {
        trigger
            .iter()
            .all(|trigger| self.events.contains_key(&(*trigger).into()))
    }

    pub fn any_pressed(&self, trigger: &[impl Into<InputTrigger> + Copy]) -> bool {
        trigger
            .iter()
            .any(|trigger| match self.events.get(&(*trigger).into()) {
                Some(i) => return i.is_pressed(),
                None => false,
            })
    }

    pub fn any_held(&self, trigger: &[impl Into<InputTrigger> + Copy]) -> bool {
        trigger
            .iter()
            .any(|trigger| self.events.contains_key(&(*trigger).into()))
    }

    pub fn events(&self) -> impl Iterator<Item = (&InputTrigger, &InputEvent)> {
        self.events.iter()
    }

    pub fn event(&self, trigger: impl Into<InputTrigger>) -> Option<&InputEvent> {
        self.events.get(&trigger.into())
    }

    pub fn is_pressed(&self, trigger: impl Into<InputTrigger>) -> bool {
        match self.events.get(&trigger.into()) {
            Some(i) => return i.is_pressed(),
            None => false,
        }
    }

    pub fn is_held(&self, trigger: impl Into<InputTrigger>) -> bool {
        return self.events.contains_key(&trigger.into());
    }

    pub fn held_time(&self, trigger: impl Into<InputTrigger>) -> f32 {
        match self.events.get(&trigger.into()) {
            Some(i) => return i.held_time().as_secs_f32(),
            None => 0.0,
        }
    }

    pub fn held_time_duration(&self, trigger: impl Into<InputTrigger>) -> Option<Duration> {
        match self.events.get(&trigger.into()) {
            Some(i) => return Some(i.held_time()),
            None => None,
        }
    }

    #[cfg(feature = "gamepad")]
    // Syncs the gamepad inputs to the inputs of the gamepad. This is automatically done once every update cycle.
    pub fn sync_gamepad(&mut self) {
        while let Some(event) = self.game_pad_manager.next_event() {
            let gamepad = event.id;
            self.active_gamepad = Some(gamepad);
            match event.event {
                EventType::ButtonPressed(button, _) => {
                    let trigger = GamepadButton { gamepad, button };
                    self.events
                        .insert(trigger.into(), InputEvent::new(trigger.into(), 1.0));
                }
                EventType::ButtonChanged(button, pressure, _) => {
                    let trigger = GamepadButton { gamepad, button };
                    if pressure == 0.0 {
                        self.events.remove(&trigger.into());
                    } else {
                        self.events
                            .insert(trigger.into(), InputEvent::new(trigger.into(), pressure));
                    }
                }
                EventType::ButtonReleased(button, _) => {
                    let trigger = GamepadButton { gamepad, button };
                    self.events.remove(&trigger.into());
                }
                EventType::Disconnected => {
                    #[cfg(feature = "log")]
                    {
                        info!("Dropped gamepad: {}", gamepad);
                    }
                    self.events.retain(|trigger, _| match trigger {
                        InputTrigger::GamepadButton(c) => c.gamepad != gamepad,
                        _ => true,
                    })
                }
                // TODO: Maybe support this
                EventType::ButtonRepeated(_, _) => {}
                EventType::Dropped => {}
                EventType::Connected => {
                    #[cfg(feature = "log")]
                    {
                        let gamepad_ref = self.gamepad(gamepad).unwrap();
                        info!(
                            "Connected gamepad: {} with power {:?} and id {}",
                            gamepad_ref.name(),
                            gamepad_ref.power_info(),
                            gamepad
                        );
                    }
                }
                EventType::AxisChanged(_, _, _) => {}
            }
        }
        if let Some(active) = self.active_gamepad {
            let exists = self.game_pad_manager.gamepad(active).is_connected();
            if !exists {
                self.active_gamepad = None;
            }
        }
    }

    #[cfg(feature = "gamepad")]
    /// Returns a vector between [-1.0, -1.0] and [1.0, 1.0]
    pub fn gamepad_stick_deadzone(
        &self,
        gamepad_id: GamepadId,
        stick: GamepadStick,
        dead_zone: f32,
    ) -> Vector<f32> {
        fn axis_values(
            gamepad: &Gamepad,
            deadzone: f32,
            x_axis: Axis,
            y_axis: Axis,
        ) -> Vector<f32> {
            fn axis(gamepad: &Gamepad, x_axis: Axis) -> f32 {
                let value = gamepad.axis_data(x_axis).map(|a| a.value()).unwrap_or(0.0);
                return value;
            }
            let value = Vector::new(axis(gamepad, x_axis), axis(gamepad, y_axis));
            if value.magnitude() >= deadzone {
                return value;
            }
            return Vector::default();
        }
        if let Some(gamepad) = self.gamepad(gamepad_id) {
            match stick {
                GamepadStick::Left => {
                    return axis_values(&gamepad, dead_zone, Axis::LeftStickX, Axis::LeftStickY);
                }
                GamepadStick::Right => {
                    return axis_values(&gamepad, dead_zone, Axis::RightStickX, Axis::RightStickY);
                }
            }
        }
        return Vector::default();
    }

    #[cfg(feature = "gamepad")]
    /// Returns a vector between [-1.0, -1.0] and [1.0, 1.0]
    pub fn gamepad_stick(&self, gamepad_id: GamepadId, stick: GamepadStick) -> Vector<f32> {
        return Self::gamepad_stick_deadzone(&self, gamepad_id, stick, self.dead_zone);
    }

    #[cfg(feature = "gamepad")]
    pub fn dead_zone(&self) -> f32 {
        return self.dead_zone;
    }

    #[cfg(feature = "gamepad")]
    pub fn set_dead_zone(&mut self, val: f32) {
        self.dead_zone = val;
    }

    #[cfg(feature = "gamepad")]
    pub fn active_gamepad(&self) -> Option<GamepadId> {
        return self.active_gamepad;
    }

    #[cfg(feature = "gamepad")]
    pub fn first_gamepad(&self) -> Option<(GamepadId, Gamepad)> {
        return self.game_pad_manager.gamepads().next();
    }

    #[cfg(feature = "gamepad")]
    pub fn gamepads(&self) -> ConnectedGamepadsIterator {
        return self.game_pad_manager.gamepads();
    }

    #[cfg(feature = "gamepad")]
    pub fn gamepad(&self, gamepad_id: GamepadId) -> Option<Gamepad> {
        return self.game_pad_manager.connected_gamepad(gamepad_id);
    }

    pub const fn modifiers(&self) -> Modifier {
        self.modifiers
    }

    pub const fn wheel_delta(&self) -> f32 {
        self.wheel_delta
    }

    pub const fn cursor_raw(&self) -> &Vector<u32> {
        &self.cursor_raw
    }

    pub fn touches_raw(&self) -> impl Iterator<Item = (&u64, &Vector<u32>)> {
        self.touches.iter()
    }

    pub fn compute_cursor(&self, cursor: Vector<u32>, camera: &Camera) -> Vector<f32> {
        let fov = camera.fov() * 2.0;
        let camera_translation = camera.translation();
        let cursor: Vector<f32> = Vector::new(cursor.x as f32, cursor.y as f32);
        camera_translation
            + Vector::new(
                cursor.x / self.window_size.x * fov.x - fov.x / 2.0,
                cursor.y / self.window_size.y * -fov.y + fov.y / 2.0,
            )
    }

    pub fn cursor(&self, camera: &Camera) -> Vector<f32> {
        self.compute_cursor(self.cursor_raw, camera)
    }

    pub fn touches(&self, camera: &Camera) -> Vec<(u64, Vector<f32>)> {
        let mut touches = vec![];
        for (id, raw) in &self.touches {
            touches.push((*id, self.compute_cursor(*raw, camera)));
        }
        return touches;
    }

    #[cfg(feature = "gamepad")]
    pub fn set_gamepad_mapping(
        &mut self,
        gamepad_id: GamepadId,
        mapping: &Mapping,
        name: Option<&str>,
    ) -> Result<String, MappingError> {
        return self
            .game_pad_manager
            .set_mapping(gamepad_id.into(), mapping, name);
    }

    #[cfg(feature = "gamepad")]
    pub fn set_gamepad_mapping_strict(
        &mut self,
        gamepad_id: GamepadId,
        mapping: &Mapping,
        name: Option<&str>,
    ) -> Result<String, MappingError> {
        return self
            .game_pad_manager
            .set_mapping_strict(gamepad_id.into(), mapping, name);
    }
}