input_default.h
1 #ifndef INPUT_DEFAULT_H
2 #define INPUT_DEFAULT_H
3 
4 #include "os/input.h"
5 
6 class InputDefault : public Input {
7 
8  OBJ_TYPE( InputDefault, Input );
9  _THREAD_SAFE_CLASS_
10 
11  int mouse_button_mask;
12  Set<int> keys_pressed;
13  Set<int> joy_buttons_pressed;
14  Map<int,float> _joy_axis;
15  Map<StringName,int> custom_action_press;
16  Vector3 accelerometer;
17  Vector2 mouse_pos;
18  MainLoop *main_loop;
19 
20  bool emulate_touch;
21  struct SpeedTrack {
22 
23  uint64_t last_tick;
24  Vector2 speed;
25  Vector2 accum;
26  float accum_t;
27  float min_ref_frame;
28  float max_ref_frame;
29 
30  void update(const Vector2& p_delta_p);
31  void reset();
32  SpeedTrack();
33  };
34 
35  struct Joystick {
36  StringName name;
37  StringName uid;
38  bool last_buttons[JOY_BUTTON_MAX + 19]; //apparently SDL specifies 35 possible buttons on android
39  float last_axis[JOY_AXIS_MAX];
40  float filter;
41  int last_hat;
42  int mapping;
43  int hat_current;
44 
45  Joystick() {
46 
47  for (int i = 0; i < JOY_AXIS_MAX; i++) {
48 
49  last_axis[i] = 0.0f;
50 
51  }
52  for (int i = 0; i < JOY_BUTTON_MAX + 19; i++) {
53 
54  last_buttons[i] = false;
55  }
56  last_hat = HAT_MASK_CENTER;
57  filter = 0.01f;
58  mapping = -1;
59  }
60  };
61 
62  SpeedTrack mouse_speed_track;
63  Map<int, Joystick> joy_names;
64  int fallback_mapping;
65  RES custom_cursor;
66 public:
67  enum HatMask {
68  HAT_MASK_CENTER = 0,
69  HAT_MASK_UP = 1,
70  HAT_MASK_RIGHT = 2,
71  HAT_MASK_DOWN = 4,
72  HAT_MASK_LEFT = 8,
73  };
74 
75  enum HatDir {
76  HAT_UP,
77  HAT_RIGHT,
78  HAT_DOWN,
79  HAT_LEFT,
80  HAT_MAX,
81  };
82  struct JoyAxis {
83  int min;
84  float value;
85  };
86 
87 private:
88 
89  enum JoyType {
90  TYPE_BUTTON,
91  TYPE_AXIS,
92  TYPE_HAT,
93  TYPE_MAX,
94  };
95 
96  struct JoyEvent {
97  int type;
98  int index;
99  int value;
100  };
101 
102  struct JoyDeviceMapping {
103 
104  String uid;
105  String name;
106  Map<int,JoyEvent> buttons;
107  Map<int,JoyEvent> axis;
108  JoyEvent hat[HAT_MAX];
109  };
110 
111  JoyEvent hat_map_default[HAT_MAX];
112 
114 
115  JoyEvent _find_to_event(String p_to);
116  uint32_t _button_event(uint32_t p_last_id, int p_device, int p_index, bool p_pressed);
117  uint32_t _axis_event(uint32_t p_last_id, int p_device, int p_axis, float p_value);
118  float _handle_deadzone(int p_device, int p_axis, float p_value);
119 
120 public:
121 
122 
123 
124  virtual bool is_key_pressed(int p_scancode);
125  virtual bool is_mouse_button_pressed(int p_button);
126  virtual bool is_joy_button_pressed(int p_device, int p_button);
127  virtual bool is_action_pressed(const StringName& p_action);
128 
129  virtual float get_joy_axis(int p_device,int p_axis);
130  String get_joy_name(int p_idx);
131  void joy_connection_changed(int p_idx, bool p_connected, String p_name, String p_guid = "");
132  void parse_joystick_mapping(String p_mapping, bool p_update_existing);
133 
134  virtual Vector3 get_accelerometer();
135 
136  virtual Point2 get_mouse_pos() const;
137  virtual Point2 get_mouse_speed() const;
138  virtual int get_mouse_button_mask() const;
139 
140  virtual void warp_mouse_pos(const Vector2& p_to);
141 
142 
143  void parse_input_event(const InputEvent& p_event);
144  void set_accelerometer(const Vector3& p_accel);
145  void set_joy_axis(int p_device,int p_axis,float p_value);
146 
147  void set_main_loop(MainLoop *main_loop);
148  void set_mouse_pos(const Point2& p_posf);
149 
150  void action_press(const StringName& p_action);
151  void action_release(const StringName& p_action);
152 
153  void iteration(float p_step);
154 
155  void set_emulate_touch(bool p_emulate);
156  virtual bool is_emulating_touchscreen() const;
157 
158  virtual void set_custom_mouse_cursor(const RES& p_cursor,const Vector2& p_hotspot=Vector2());
159  virtual void set_mouse_in_window(bool p_in_window);
160 
161  void parse_mapping(String p_mapping);
162  uint32_t joy_button(uint32_t p_last_id, int p_device, int p_button, bool p_pressed);
163  uint32_t joy_axis(uint32_t p_last_id, int p_device, int p_axis, const JoyAxis& p_value);
164  uint32_t joy_hat(uint32_t p_last_id, int p_device, int p_val);
165 
166  virtual void add_joy_mapping(String p_mapping, bool p_update_existing=false);
167  virtual void remove_joy_mapping(String p_guid);
168  virtual bool is_joy_known(int p_device);
169  virtual String get_joy_guid(int p_device) const;
170 
171  bool is_joy_mapped(int p_device);
172  String get_joy_guid_remapped(int p_device) const;
173  void set_fallback_mapping(String p_guid);
174  InputDefault();
175 };
176 
177 #endif // INPUT_DEFAULT_H
Definition: main_loop.h:38
Definition: string_db.h:48
Definition: vector3.h:38
Definition: input_event.h:263
Definition: input.h:36
Definition: input_default.h:6
Definition: math_2d.h:65
Definition: ustring.h:64
Definition: input_default.h:82