gramods
Loading...
Searching...
No Matches
TouchState.hh
1
2#ifndef GRAMODS_TOUCH_TOUCHSTATE
3#define GRAMODS_TOUCH_TOUCHSTATE
4
5#include <gmTouch/config.hh>
6
7#include <gmMisc/EFFOAW.hh>
8
9#include <Eigen/Eigen>
10
11#include <vector>
12#include <chrono>
13#include <map>
14#include <typeindex>
15#include <typeinfo>
16#include <unordered_map>
17#include <unordered_set>
18
19#include <assert.h>
20
21BEGIN_NAMESPACE_GMTOUCH;
22
66
67public:
68
70 TouchState();
71
74
76 typedef std::chrono::steady_clock clock;
77
83 typedef int64_t TouchPointId;
84
89
94 struct State {
95 static const unsigned char NONE = 0;
96
98 static const unsigned char DRAG = 0x01 << 0;
99
101 static const unsigned char HOLD = 0x01 << 1;
102
107 static const unsigned char MULTI = 0x01 << 2;
108
111 static const unsigned char TOUCH_DOWN = 0x01 << 3;
112
114 static const unsigned char RELEASE = 0x01 << 4;
115
118 static const unsigned char CLICK = 0x01 << 5;
119 };
120
124 struct TouchPoint {
125
127 TouchPoint(TouchPointId id, float x, float y);
128
130 TouchPoint();
131
133 float x;
134
136 float y;
137
139 float vx;
140
142 float vy;
143
145 float ex;
146
148 float ey;
149
151 float sx;
152
154 float sy;
155
158
164 unsigned char state;
165
170 unsigned int clicks;
171 };
172
176 typedef std::vector<TouchPoint> TouchPoints;
177
181 struct TouchLine {
182
184 Eigen::Vector3f x;
185
187 Eigen::Vector3f v;
188
191
197 unsigned char state;
198
203 unsigned int clicks;
204 };
205
209 typedef std::vector<TouchLine> TouchLines;
210
215
219 bool empty() const { return current_state.empty(); }
220
227 int getTouchPoints(TouchPoints &current) const;
228
238 int getTouchPoints(TouchPoints &current, TouchPoints &previous) const;
239
252 int getTouchPoints(void *ass, TouchPoints &current) const;
253
270 int getTouchPoints(void *ass, TouchPoints &current, TouchPoints &previous) const;
271
279 int getTouchPoints(std::map<void*, TouchPoints> &current) const;
280
290 int getTouchPoints(std::map<void*, TouchPoints> &current,
291 std::map<void*, TouchPoints> &previous) const;
292
300 bool setAssociation(TouchPointId id, void* pt);
301
310 bool unsetAssociation(TouchPointId id, void* pt);
311
323 bool getAssociation(TouchPointId id, void** pt) const;
324
326
331
349 void setCurrentProjection(Eigen::Matrix4f WPV_inv);
350
359
360 virtual ~CameraAdaptor() {}
361
365 virtual void init(int width, int height);
366
370 virtual void done();
371
372 protected:
373
389 void setCurrentProjection(Eigen::Matrix4f WPV_inv);
390
391 private:
392
393 TouchState *owner;
394 friend TouchState;
395 };
396 friend CameraAdaptor;
397
403 template<class T>
404 T & getCameraAdaptor();
405
414 bool getTouchLines(TouchLines &current) const;
415
425 bool getTouchLines(TouchLines &current, TouchLines &previous) const;
426
436 bool getTouchLines(void *ass, TouchLines &current) const;
437
448 bool getTouchLines(void *ass, TouchLines &current, TouchLines &previous) const;
449
458 bool getTouchLines(std::map<void*, TouchLines> &current) const;
459
470 bool getTouchLines(std::map<void*, TouchLines> &current,
471 std::map<void*, TouchLines> &previous) const;
472
474
479
483 float getMouseWheel() const;
484
488 bool getMouseDown() const;
489
493 void getMousePoint(int &x, int &y) const;
494
499 bool getMouseLine(Eigen::Vector3f &x,
500 Eigen::Vector3f &v) const;
501
503
508
515 void setSmoothing(float r);
516
521 float getSmoothing();
522
528 void setRemoveMouseUponTouch(bool on);
529
534 bool getRemoveMouseUponTouch();
535
547 void setMoveMagnitude(float dist);
548
553 float getMoveMagnitude();
554
559 void setHoldTime(clock::duration time);
560
565 clock::duration getHoldTime();
566
571 void setClickTime(clock::duration time);
572
577 clock::duration getClickTime();
578
584 void setMultiClickTime(clock::duration time);
585
590 clock::duration getMultiClickTime();
591
593
598
607
608 virtual ~EventAdaptor() {}
609
613 virtual void init(int, int);
614
618 virtual void done();
619
620 protected:
621
629 void addTouchState(TouchPointId id, float x, float y);
630
634 void removeTouchState(TouchPointId id, float x, float y);
635
640 void addMouseState(float x, float y, bool mouse_down);
641
645 void addMouseWheel(float s);
646
647 private:
648
649 TouchState *owner;
650 friend TouchState;
651 };
652 friend EventAdaptor;
653
659 template<class T>
660 T & getEventAdaptor();
661
669 void eventsInit(int width, int height);
670
675 void eventsDone();
676
678
679private:
680
681 struct HistoryState {
682 TouchPoint point;
683 clock::time_point time;
684 };
685
686 gmMisc::EFFOAW velocityEstimator;
687
695 void addTouchState(TouchPointId id, float x, float y);
696
700 void removeTouchState(TouchPointId id, float x, float y);
701
706 void addMouseState(float x, float y, bool mouse_down);
707
711 void addMouseWheel(float s);
712
716 void addState(TouchPointId id, float x, float y);
717
718 clock::time_point start_time = clock::time_point::min();
719 float noise_level;
720 float velocity_reestimation_rate;
721
722 float smoothing;
723 float move_magnitude;
724 clock::duration hold_time;
725 clock::duration click_time;
726 clock::duration multi_time;
727
728 bool remove_mouse_upon_touch = true;
729 bool use_mouse = true;
730
731 std::map<TouchPointId, TouchPoint> current_state;
732 std::map<TouchPointId, TouchPoint> previous_state;
733
734 std::map<TouchPointId, void*> last_association;
735 std::map<TouchPointId, void*> current_association;
736
737 std::map<TouchPointId, HistoryState> history;
738
746 void check_multi(TouchPoint &new_pt);
747
755 void check_drag(HistoryState hist, TouchPoint &new_pt);
756
764 void check_hold(HistoryState hist, TouchPoint &new_pt);
765
773 void check_click(HistoryState hist, TouchPoint &new_pt);
774
779 void clearReleasedStates();
780
781 int state = 0;
782
787 TouchLine touchPointToTouchLine(TouchPoint pt, Eigen::Matrix4f WPV_inv) const;
788
789 Eigen::Matrix4f current_WPV_inv;
790 bool current_WPV_inv_valid = false;
791
792 Eigen::Matrix4f previous_WPV_inv;
793 bool previous_WPV_inv_valid = false;
794
795 int current_height = -1;
796 int previous_height = -1;
797 int current_width = -1;
798 int previous_width = -1;
799
800 float mouse_wheel = 0.f;
801 bool mouse_down = false;
802 int mouse_point_x = 0;
803 int mouse_point_y = 0;
804
805 std::unordered_map<std::type_index, EventAdaptor*> event_adaptors;
806 std::unordered_map<std::type_index, CameraAdaptor*> camera_adaptors;
807};
808
809
810template<class T>
811T & TouchState::getEventAdaptor() {
812 if (event_adaptors.find(typeid(T)) == event_adaptors.end()) {
813 event_adaptors[typeid(T)] = new T;
814 event_adaptors[typeid(T)]->owner = this;
815 }
816 assert(dynamic_cast<T*>(event_adaptors[typeid(T)]));
817 return *dynamic_cast<T*>(event_adaptors[typeid(T)]);
818}
819
820template<class T>
821T & TouchState::getCameraAdaptor() {
822 if (camera_adaptors.find(typeid(T)) == camera_adaptors.end()) {
823 camera_adaptors[typeid(T)] = new T;
824 camera_adaptors[typeid(T)]->owner = this;
825 }
826 assert(dynamic_cast<T*>(camera_adaptors[typeid(T)]));
827 return *dynamic_cast<T*>(camera_adaptors[typeid(T)]);
828}
829
830END_NAMESPACE_GMTOUCH;
831
832#endif
This is an end-fitting first-order adaptive window estimator of velocity from samples allowing jitter...
Definition EFFOAW.hh:34
The TouchState class represent the internal states during touch interaction.
Definition TouchState.hh:65
std::chrono::steady_clock clock
The clock used in this class.
Definition TouchState.hh:76
std::vector< TouchLine > TouchLines
List of touch lines.
Definition TouchState.hh:209
int64_t TouchPointId
The internal type of a touch point id.
Definition TouchState.hh:83
static const TouchPointId MOUSE_STATE_ID
The touch point id used for mouse pointer simulating touch.
Definition TouchState.hh:88
bool empty() const
Checks whether the TouchState is empty of touch points.
Definition TouchState.hh:219
std::vector< TouchPoint > TouchPoints
List of touch points.
Definition TouchState.hh:176
Base type for camera adaptors providing means to input camera data from different scenegraph or graph...
Definition TouchState.hh:358
Base type for event adaptors providing means to input events from different platforms and window libr...
Definition TouchState.hh:606
Touch states that can be bitwise combined (except NONE).
Definition TouchState.hh:94
A 3D line representation of a touch point.
Definition TouchState.hh:181
TouchPointId id
internal id of the touch point.
Definition TouchState.hh:190
unsigned int clicks
The number of times this position was clicked in succession, for example two (2) for a double click.
Definition TouchState.hh:203
unsigned char state
State of the touch point expressed as a bitwise combination.
Definition TouchState.hh:197
Eigen::Vector3f x
The origin of the 3D line.
Definition TouchState.hh:184
Eigen::Vector3f v
The direction of the 3D line.
Definition TouchState.hh:187
A single (potentially smoothed) touch point.
Definition TouchState.hh:124
float y
y position in (sub) pixels from top edge.
Definition TouchState.hh:136
float vy
y velocity in pixels per second from top edge.
Definition TouchState.hh:142
unsigned char state
State of the touch point expressed as a bitwise combination.
Definition TouchState.hh:164
float ey
y position estimated by the recent motion.
Definition TouchState.hh:148
TouchPointId id
internal id of the touch point.
Definition TouchState.hh:157
float sx
smoothed version of the x position.
Definition TouchState.hh:151
float ex
x position estimated by the recent motion.
Definition TouchState.hh:145
float x
x position in (sub) pixels from left edge.
Definition TouchState.hh:133
float vx
x velocity in pixels per second from left edge.
Definition TouchState.hh:139
float sy
smoothed version of the y position.
Definition TouchState.hh:154
unsigned int clicks
The number of times this position was clicked in succession, for example two (2) for a double click.
Definition TouchState.hh:170