00001 #ifndef INPUT_HPP
00002 #define INPUT_HPP
00003
00004 #include <utils.hpp>
00005 #include <utils_refptr.hpp>
00006 #include <utils_wptr.hpp>
00007 #include <string>
00008 #include <vector>
00009 #include <deque>
00010 #include <array>
00011 #include <map>
00012 #include "input_keys.hpp"
00013
00014 #define INPUT_MOUSE_BUTTON_NUMBER 3
00015
00016 namespace gui
00017 {
00018 class event;
00019 class event_receiver;
00020 class event_manager;
00021 }
00022
00023 namespace input
00024 {
00026 class handler_impl
00027 {
00028 public :
00029
00031 handler_impl();
00032
00034 virtual ~handler_impl() {}
00035
00037 virtual void update() = 0;
00038
00040
00047 virtual void toggle_mouse_grab() = 0;
00048
00050
00052 virtual std::string get_key_name(key::code mKey) const = 0;
00053
00054 struct key_state
00055 {
00056 std::array<bool, key::code::K_MAXKEY> lKeyState;
00057 } mKeyboard;
00058
00059 struct mouse_state
00060 {
00061 std::array<bool, INPUT_MOUSE_BUTTON_NUMBER> lButtonState;
00062 float fAbsX, fAbsY, fDX, fDY;
00063 float fRelX, fRelY, fRelDX, fRelDY;
00064 float fRelWheel;
00065 bool bHasDelta;
00066 } mMouse;
00067
00068 std::vector<char32_t> lChars;
00069 std::vector<char32_t> lCharsCache_;
00070 };
00071
00073
00080 class handler
00081 {
00082 public :
00083
00085 handler();
00086
00088
00090 template<class T>
00091 handler(utils::refptr<T> pImpl) : bManuallyUpdated_(false), pImpl_(pImpl) {
00092 }
00093
00095
00097 void update();
00098
00100
00103 bool is_manually_updated() const;
00104
00106
00114 void set_manually_updated(bool bManuallyUpdated);
00115
00117 const handler_impl::key_state& get_key_state() const;
00118
00120
00122 std::vector<char32_t> get_chars() const;
00123
00125
00127 std::string get_key_name(key::code mKey) const;
00128
00130 const handler_impl::mouse_state& get_mouse_state() const;
00131
00133 utils::wptr<handler_impl> get_impl();
00134
00135 private :
00136
00137 bool bManuallyUpdated_;
00138 utils::refptr<handler_impl> pImpl_;
00139 };
00140
00142 class manager
00143 {
00144 public :
00145
00147
00149 explicit manager(const handler& mHandler);
00150
00152 manager(const manager& mMgr) = delete;
00153
00155 manager& operator = (const manager& mMgr) = delete;
00156
00158 void update(float fDelta);
00159
00161
00163 void allow_input(const std::string& sGroupName);
00164
00166
00168 void block_input(const std::string& sGroupName);
00169
00171
00174 bool can_receive_input(const std::string& sGroupName) const;
00175
00177
00182 void force_input_allowed(const std::string& sGroupName, bool bForce);
00183
00185
00188 bool get_key(bool bForce = false) const;
00189
00191
00195 bool key_is_down(key::code mKey, bool bForce = false) const;
00196
00198
00202 bool key_is_down_long(key::code mKey, bool bForce = false) const;
00203
00205
00208 double get_key_press_duration(key::code mKey) const;
00209
00211
00216 bool key_is_pressed(key::code mKey, bool bForce = false) const;
00217
00219
00224 bool key_is_released(key::code mKey, bool bForce = false) const;
00225
00227
00229 std::vector<char32_t> get_chars() const;
00230
00232
00235 std::string get_key_name(key::code mKey) const;
00236
00238
00242 std::string get_key_name(key::code mKey, key::code mModifier) const;
00243
00245
00250 std::string get_key_name(key::code mKey, key::code mModifier1, key::code mModifier2) const;
00251
00253
00255 const std::deque<key::code>& get_key_release_stack() const;
00256
00258
00260 const std::deque<key::code>& get_key_press_stack() const;
00261
00263
00265 bool alt_is_pressed() const;
00266
00268
00270 bool shift_is_pressed() const;
00271
00273
00275 bool ctrl_is_pressed() const;
00276
00278
00281 bool mouse_is_down(mouse::button mID) const;
00282
00284
00287 bool mouse_is_down_long(mouse::button mID) const;
00288
00290
00293 double get_mouse_press_duration(mouse::button mKey) const;
00294
00296
00300 bool mouse_is_pressed(mouse::button mID) const;
00301
00303
00307 bool mouse_is_released(mouse::button mID) const;
00308
00310
00313 bool mouse_is_doubleclicked(mouse::button mID) const;
00314
00316
00318 bool wheel_is_rolled() const;
00319
00321
00323 bool mouse_last_dragged() const;
00324
00326
00329 mouse::state get_mouse_state(mouse::button mID) const;
00330
00332
00334 float get_mouse_x() const;
00335
00337
00339 float get_mouse_y() const;
00340
00342
00346 float get_mouse_rel_x() const;
00347
00349
00353 float get_mouse_rel_y() const;
00354
00356
00360 float get_mouse_raw_dx() const;
00361
00363
00367 float get_mouse_raw_dy() const;
00368
00370
00374 float get_mouse_dx() const;
00375
00377
00381 float get_mouse_dy() const;
00382
00384
00388 float get_mouse_rel_dx() const;
00389
00391
00395 float get_mouse_rel_dy() const;
00396
00398
00408 float get_mouse_smooth_dx() const;
00409
00411
00421 float get_mouse_smooth_dy() const;
00422
00424
00426 float get_mouse_wheel() const;
00427
00429
00432 std::string get_mouse_button_string(mouse::button mID) const;
00433
00435
00437 void set_doubleclick_time(double dDoubleClickTime);
00438
00440
00442 double get_doubleclick_time() const;
00443
00445
00450 void set_mouse_buffer_duration(double dMouseHistoryMaxLength);
00451
00453
00455 double get_mouse_buffer_duration() const;
00456
00458
00461 void set_mouse_sensibility(float fMouseSensibility);
00462
00464
00466 float get_mouse_sensibility() const;
00467
00469
00472 void set_long_press_delay(double dLongPressDelay);
00473
00475
00477 double get_long_press_delay() const;
00478
00480
00491 void set_focus(bool bFocus, gui::event_receiver* pReceiver = nullptr);
00492
00494
00499 bool is_focused() const;
00500
00502
00507 void register_event_manager(utils::wptr<gui::event_manager> pManager);
00508
00510
00513 void unregister_event_manager(utils::wptr<gui::event_manager> pManager);
00514
00516
00518 const handler& get_handler() const;
00519
00521
00523 handler& get_handler();
00524
00525 private :
00526
00527 void fire_event_(const gui::event& mEvent, bool bForce = false);
00528
00529 bool bRemoveFocus_;
00530 bool bFocus_;
00531 gui::event_receiver* pFocusReceiver_;
00532
00533 std::vector<utils::wptr<gui::event_manager>> lEventManagerList_;
00534
00535
00536 std::array<double, 256> lKeyDelay_;
00537 std::array<bool, 256> lKeyLong_;
00538 std::array<bool, 256> lKeyBuf_;
00539 std::array<bool, 256> lKeyBufOld_;
00540
00541 bool bCtrlPressed_;
00542 bool bShiftPressed_;
00543 bool bAltPressed_;
00544 bool bKey_;
00545 std::vector<char32_t> lChars_;
00546
00547 std::deque<key::code> lDownStack_;
00548 std::deque<key::code> lUpStack_;
00549
00550
00551 double dDoubleClickTime_;
00552 std::array<double, INPUT_MOUSE_BUTTON_NUMBER> lDoubleClickDelay_;
00553 std::array<double, INPUT_MOUSE_BUTTON_NUMBER> lMouseDelay_;
00554 std::array<bool, INPUT_MOUSE_BUTTON_NUMBER> lMouseLong_;
00555 std::array<bool, INPUT_MOUSE_BUTTON_NUMBER> lMouseBuf_;
00556 std::array<bool, INPUT_MOUSE_BUTTON_NUMBER> lMouseBufOld_;
00557 std::array<mouse::state, INPUT_MOUSE_BUTTON_NUMBER> lMouseState_;
00558
00559 std::map<std::string, bool> lClickGroupList_;
00560 std::map<std::string, bool> lForcedClickGroupList_;
00561
00562 float fMX_, fMY_;
00563 float fRelMX_, fRelMY_;
00564 float fDMX_, fDMY_;
00565 float fRelDMX_, fRelDMY_;
00566 float fRawDMX_, fRawDMY_;
00567 float fMouseSensibility_;
00568 double dMouseHistoryMaxLength_;
00569 double dLongPressDelay_;
00570 std::deque<std::pair<double, std::array<float,3>>> lMouseHistory_;
00571 float fSmoothDMX_, fSmoothDMY_, fSmoothMWheel_;
00572 float fMWheel_;
00573 bool bWheelRolled_;
00574 std::string sMouseButton_;
00575 bool bLastDragged_;
00576
00577 double dTime_;
00578
00579 handler mHandler_;
00580 };
00581 }
00582
00583 #endif