guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gmactouch.hpp
1 //
2 // Multitouch detection and Cursor management
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifdef __APPLE__
9 
10 #ifndef GuitTouch_h
11 #define GuitTouch_h
12 #include <gcursor.hpp>
13 namespace guit {
14 
15 class GTouch {
16 public:
17  enum Phase {
18  NotTracking = 0,
19  Start = 1,
20  Hover = 2,
21  BeginTouch = 3,
22  Touching = 4,
23  EndTouch = 5,
24  Linger = 6,
25  OutOfRange = 7
26  };
27 
28  int32_t frame; // current frame
29  double time; // event timestamp
30  int32_t touchid; // unique for life of touch per device
31  int32_t phase; // phase (distance from touchpad)
32  int32_t fingerid;
33  int32_t handid;
34  guit::GPoint pos;
35  guit::GPoint speed;
36  float area; // touch area (pseudo-pressure), between 0,0 and 1,0
37  int32_t aa;
38  float angle; // angle of the touch ellipse
39  float majoraxis; // major axis of the touch ellipse
40  float minoraxis; // minor axis of the touch ellipse
41  guit::GPoint abspos;
42  guit::GPoint absspeed;
43  int32_t bb;
44  int32_t cc;
45  float density;
46 };
47 
48 struct GTouchDevice;
49 
50 class GTouchCursor {
51 public:
52  bool isNative() const {return cursor_->nativecursor_;}
53  void touchDown(GTouch const&);
54  void touchUp(GTouch const&);
55  void touchMove(GTouch const&);
56  void mouseDown();
57  void mouseUp();
58  void sendButtonEvent(bool press);
59  void sendMoveEvent();
60 
61 private:
62  friend class GTouchManager;
63  friend class GTouchPad;
64  int32_t touchid_{};
65  GPoint mouse_, lastmouse_, locmouse_;
66  GPoint lastpos_;
67  double lasttime_{};
68  GCursor* cursor_{};
69 };
70 
71 
72 class GTouchPad {
73 public:
74  GTouchCursor& createCursor2(GColor& cursor_color);
75  GTouchCursor* cursor1() {return curs1_;}
76  GTouchCursor* cursor2() {return curs2_;}
77 
78 protected:
79  friend class GTouchManager;
80  GTouchPad(GTouchDevice*, bool nativecursor);
81  void touchCB(GTouch const* touches, int touchCount, double timestamp);
82  GTouchDevice *device_{};
83  GTouchCursor *curs1_{}, *curs2_{};
84 };
85 
86 
87 class GTouchManager {
88 public:
90  static GTouchManager& instance();
91 
94  size_t init(GWindow& window);
95  void start();
96  void stop();
97 
98  float mouseGain() const {return mousegain_;}
99  void setMouseGain(float val) {mousegain_ = val;}
100 
101  double timeResolution() const {return timeres_;}
102  void setTimeResolution(double val) {timeres_ = val;}
103 
104  //bool captureMouse() const {return capturemouse_;}
105  //void setCaptureMouse(bool val) {capturemouse_ = val;}
106 
107  //float spaceResolution() const {return spaceres_;}
108  //void setSpaceResolution(float val) {spaceres_ = val;}
109 
110  GTouchPad* getPad(unsigned int channel);
111  size_t padCount() const {return pads_.size();}
112 
113 protected:
114  friend class GTouchPad;
115  friend class GTouchCursor;
116 
117  GTouchManager();
118  GTouchManager(GTouchManager const&) = delete;
119  GTouchManager(GTouchManager const&&) = delete;
120  GTouchManager& operator=(GTouchManager const&) = delete;
121  GTouchManager& operator=(GTouchManager const&&) = delete;
122 
123  GTouchCursor* addCursor(GColor&);
124  static void focusInWindowCB(GEvent&);
125  static void focusOutWindowCB(GEvent&);
126  static bool mouseButtonCB(GMouseEvent&);
127  static bool mouseMotionCB(GMouseEvent&);
128  static bool wheelCB(GMouseEvent&);
129  static int touchCB(GTouchDevice* d, const GTouch* touches, int touchcount,
130  double timestamp, int frame);
131  GDim screensize_;
132  bool running_{};
133  float mousegain_{};
134  double timeres_{};
135  std::vector<GTouchPad*> pads_;
136  std::vector<GTouchCursor*> cursors_;
137  GTouchCursor* nativecurs_{};
138  GWindow* win_{};
139 };
140 
141 }
142 #endif
143 
144 #endif