lxgui
input_dispatcher.hpp
1 #ifndef LXGUI_INPUT_DISPATCHER_HPP
2 #define LXGUI_INPUT_DISPATCHER_HPP
3 
4 #include "lxgui/gui_vector2.hpp"
5 #include "lxgui/input_keys.hpp"
6 #include "lxgui/input_signals.hpp"
7 #include "lxgui/lxgui.hpp"
8 #include "lxgui/utils_signal.hpp"
9 
10 #include <array>
11 #include <chrono>
12 #include <string>
13 #include <vector>
14 
15 namespace lxgui::input {
16 
17 class source;
18 
46 class dispatcher : public signals {
47 public:
52  explicit dispatcher(source& src);
53 
54  // Non-copiable, non-movable
55  dispatcher(const dispatcher&) = delete;
56  dispatcher(dispatcher&&) = delete;
57  dispatcher& operator=(const dispatcher&) = delete;
59 
64  bool any_key_is_down() const;
65 
71  bool key_is_down(key key_id) const;
72 
78  double get_key_down_duration(key key_id) const;
79 
84  bool alt_is_pressed() const;
85 
90  bool shift_is_pressed() const;
91 
96  bool ctrl_is_pressed() const;
97 
103  bool mouse_is_down(mouse_button button_id) const;
104 
110  double get_mouse_down_duration(mouse_button key_id) const;
111 
117 
122  float get_mouse_wheel() const;
123 
128  void set_doubleclick_time(double double_click_time);
129 
134  double get_doubleclick_time() const;
135 
142  void set_interface_scaling_factor(float scaling_factor);
143 
148  float get_interface_scaling_factor() const;
149 
154  const source& get_source() const;
155 
160  source& get_source();
161 
162 private:
163  std::vector<utils::scoped_connection> connections_;
164 
165  using timer = std::chrono::high_resolution_clock;
166  using time_point = timer::time_point;
167 
168  std::array<time_point, key_number> key_pressed_time_ = {};
169  std::array<time_point, mouse_button_number> mouse_pressed_time_ = {};
170 
171  float scaling_factor_ = 1.0f;
172 
173  double double_click_time_ = 0.25;
174 
175  bool is_mouse_dragged_ = false;
176  mouse_button mouse_drag_button_ = mouse_button::left;
177 
178  source& source_;
179 };
180 
181 } // namespace lxgui::input
182 
183 #endif
Handles inputs (keyboard and mouse)
double get_mouse_down_duration(mouse_button key_id) const
Returns elapsed time since the mouse button has been pressed.
bool alt_is_pressed() const
Checks if Alt is being pressed.
double get_key_down_duration(key key_id) const
Returns elapsed time since the key has been pressed.
dispatcher(const dispatcher &)=delete
bool key_is_down(key key_id) const
Checks if a key is being pressed.
bool any_key_is_down() const
Checks if any key is being pressed.
float get_mouse_wheel() const
Returns the accumulated rolling amount of the mouse wheel.
bool shift_is_pressed() const
Checks if Shift is being pressed.
void set_doubleclick_time(double double_click_time)
Sets the double click maximum time.
const source & get_source() const
Returns the input source.
void set_interface_scaling_factor(float scaling_factor)
Sets the scaling factor applied to the interface.
float get_interface_scaling_factor() const
Return the current interface scaling factor.
double get_doubleclick_time() const
Returns the double click maximum time.
bool ctrl_is_pressed() const
Checks if Control (Ctrl) is being pressed.
gui::vector2f get_mouse_position() const
Returns the position of the mouse in pixels.
dispatcher(source &src)
Initializes this dispatcher with a chosen input source.
bool mouse_is_down(mouse_button button_id) const
Checks if a mouse button is being pressed.
dispatcher & operator=(const dispatcher &)=delete
dispatcher(dispatcher &&)=delete
dispatcher & operator=(dispatcher &&)=delete
Stores signals for input events.
The base class for input source implementation.