gooey/interface/view/input/
mod.rs

1use std;
2use bitflags::bitflags;
3use derive_more::{From, TryInto};
4use serde::{Deserialize, Serialize};
5
6pub mod button;
7pub use self::button::Button;
8
9type ModifiersPrimitiveType = u8;
10
11/// A user or OS event from the presentation layer
12#[derive(Clone, Debug, PartialEq, From, TryInto)]
13pub enum Input {
14  Axis    (Axis),
15  Button  (Button, button::State),
16  Motion  (Motion),
17  Pointer (Pointer),
18  System  (System),
19  Text    (Text),
20  Wheel   (Wheel)
21}
22
23/// These mostly correspond to non-user-input `glutin::WindowEvent`s
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub enum System {
26  Close,
27  CursorEntered,
28  CursorLeft,
29  Destroyed,
30  Focused (bool),
31  InputMethod (/*TODO*/),
32  Occluded (bool),
33  Redraw,
34  Resized {
35    width  : u32,
36    height : u32
37  },
38  // NOTE: resume is spammed so we are not handling it for now
39  //Resumed
40}
41
42/// Corresponds to glutin `Motion` device event.
43///
44/// Glutin mouse motion will produce motion events for axis 0 and 1.
45///
46/// Note that glutin also produces `AxisMotion` window events for mouse axis
47/// motion, but the value is in window coordinates, not axis deltas.
48#[derive(Clone, Debug, Eq, PartialEq)]
49pub struct Axis {
50  pub axis  : u32,
51  pub value : i32
52}
53
54/// Change in X and Y directions.
55///
56/// Corresponds to the glutin `MouseMotion` device event. Use for FPS controls.
57#[derive(Clone, Debug, Eq, PartialEq)]
58pub struct Motion (pub i32, pub i32);
59
60#[derive(Clone, Debug, Default, PartialEq)]
61pub struct Pointer {
62  pub position_horizontal : f32,
63  pub position_vertical   : f32,
64  pub modifiers           : Modifiers
65}
66
67#[derive(Clone, Debug, Eq, PartialEq)]
68pub enum Text {
69  Char   (char),
70  String (String)
71}
72
73/// Corresponds to glutin `MouseWheel` window event.
74#[derive(Clone, Debug, Eq, PartialEq)]
75pub struct Wheel {
76  pub delta     : (i32, i32),
77  pub modifiers : Modifiers
78  // TODO: touch phase ?
79}
80
81bitflags! {
82  #[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd,
83    Deserialize, Serialize)]
84  pub struct Modifiers : ModifiersPrimitiveType {
85    const SHIFT = 0b0_0001;
86    const CTRL  = 0b0_0010;
87    const ALT   = 0b0_0100;
88    const SUPER = 0b0_1000;
89    const ANY   = 0b1_0000;
90  }
91}
92
93impl std::fmt::Display for Modifiers {
94  fn fmt (&self, f : &mut std::fmt::Formatter) -> std::fmt::Result {
95    if self.contains (Modifiers::SHIFT) {
96      write!(f, "+Shift")?
97    }
98    if self.contains (Modifiers::CTRL) {
99      write!(f, "+Ctrl")?
100    }
101    if self.contains (Modifiers::ALT) {
102      write!(f, "+Alt")?
103    }
104    if self.contains (Modifiers::SUPER) {
105      write!(f, "+Super")?
106    }
107    if self.contains (Modifiers::ANY) {
108      write!(f, "+Any")?
109    }
110    Ok(())
111  }
112}