guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gevent.hpp
1 //
2 // Events
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef GuitEvent_hpp
9 #define GuitEvent_hpp
10 #include <gtypes.hpp>
11 namespace guit {
12 
13 class GTrigger;
14 class GMouseEvent;
15 class GKeyEvent;
16 class GTouchEvent;
17 class GGestureEvent;
18 
21 enum struct GEventType : uint32_t {
22  NoEvent = 0,
23  Action = 1<<0,
24  Done = 1<<1,
25  Click = 1<<2,
26  Change = 1<<3,
27  Changing = 1<<4,
28  MouseButton = 1<<5,
29  MouseDrag = 1<<6,
30  MouseMove = 1<<7,
31  MouseCross = 1<<8,
32  Wheel = 1<<9,
33  Key = 1<<10,
34  Caret = 1<<11,
35  Touch = 1<<12,
36  Gesture = 1<<13,
37  Focus = 1<<14,
38  Enable = 1<<15,
39  Show = 1<<16,
40  Move = 1<<17,
41  Paint = 1<<18,
42  Resize = 1<<19,
43  Init = 1<<20,
44  AddRemove = 1<<21,
45  CutPaste = 1<<22,
46  DragDrop = 1<<23,
47  User = 1<<30
48 };
49 inline GEventType operator|(GEventType a, GEventType b) {
50  return GEventType(uint32_t(a) | uint32_t(b));
51 }
52 inline GEventType operator&(GEventType a, GEventType b) {
53  return GEventType(uint32_t(a) & uint32_t(b));
54 }
55 inline bool operator==(GEventType a, int b) {return uint32_t(a) == uint32_t(b);}
56 inline bool operator!=(GEventType a, int b) {return uint32_t(a) != uint32_t(b);}
58 
59 
64 enum struct GEventState : uint32_t {
65  NoState = 0,
66  Button1 = 1<<1,
67  Button2 = 1<<2,
68  Button3 = 1<<3,
69  Button4 = 1<<4,
70  Button5 = 1<<5,
71  Ctrl = 1<<20,
72  Shift = 1<<21,
73  Alt = 1<<22,
74  AltGr = 1<<23,
75  Meta = 1<<24,
76  Caps = 1<<25,
77  Num = 1<<26
78 };
79 inline GEventState operator|(GEventState a, GEventState b) {
80  return GEventState(uint32_t(a) | uint32_t(b));
81 }
82 inline GEventState operator&(GEventState a, GEventState b) {
83  return GEventState(uint32_t(a) & uint32_t(b));
84 }
85 inline bool operator==(GEventState a, int b) {return uint32_t(a) == uint32_t(b);}
86 inline bool operator!=(GEventState a, int b) {return uint32_t(a) != uint32_t(b);}
88 
89 
93 class GEvent {
94 public:
95 
98  static const GKeyCode
99  EscapeKey, TabKey, BackspaceKey, SpaceKey, ReturnKey, EnterKey, DeleteKey,
100  LeftKey, RightKey, UpKey, DownKey, HomeKey, EndKey,
101  CtrlKey, ShiftKey, AltKey, AltGrKey, MetaKey, CapsKey, NumKey;
102 
103 
104  GEvent(GTrigger const& trigger, GTime time);
105  virtual ~GEvent();
106 
110  virtual GMouseEvent& asMouseEvent();
111  virtual GKeyEvent& asKeyEvent();
112  virtual GTouchEvent& asTouchEvent();
113  virtual GGestureEvent& asGestureEvent();
115 
119  virtual GMouseEvent* toMouseEvent() {return nullptr;}
120  virtual GKeyEvent* toKeyEvent() {return nullptr;}
121  virtual GTouchEvent* toTouchEvent() {return nullptr;}
122  virtual GGestureEvent* toGestureEvent() {return nullptr;}
124 
136  Gadget& from() const;
137  GBox& fromBox() const;
138  GProp& fromProp() const;
140 
150  Gadget* chosen() const;
151 
153  GWindow* window() const;
154 
156  GTrigger const& trigger() const {return *trigger_;}
157 
159  GEventType eventMask() const;
160 
163  int channel() const {return channel_;}
164 
167  GTime time() const {return time_;}
168 
170  void postFire(std::function<void(GEvent&)> const& todo_after);
171 
176  GObject* tag() const {return tag_;}
177  void setTag(GObject* tag) {tag_ = tag;}
179 
181  int matches(GTrigger const& trigger, int channel) const;
182 
188  static void setMouseButtonHandler(int channel,std::function<bool(class GMouseEvent&)>);
189  static void setMouseMotionHandler(int channel, std::function<bool(class GMouseEvent&)>);
190  static void setWheelHandler(int channel, std::function<bool(class GMouseEvent&)>);
191  static void setKeyHandler(int channel, std::function<bool(class GKeyEvent&)>);
193 
196  static GString keyToName(GKeyCode key);
197  static GKeyCode nameToKey(GString const& key_name);
199 
203  static GEventState buttonToState(int mouse_button);
204 
206  static GEventState modifierToState(GKeyCode modifier_key);
207 
211  static GEventState modifiersToStates(GString const& modifiers);
212 
217  bool hasGuard() const {return hasguard_;}
218  bool guardDone() const {return guarddone_;}
219 
220  GTrigger const* trigger_{};
221  bool hasguard_{}, guarddone_{}, reshapeinter_{};
222  int channel_{};
223  GWindow* window_{};
224  Gadget* gadget_{};
225  GObject *aux_{}, *tag_{};
226  GTime time_{};
227  std::function<void(GEvent&)> postfire_{};
229 };
230 
231 
234 class GMouseEvent : public GEvent {
235 public:
236  GMouseEvent(GTrigger& trigger, GTime t) : GEvent(trigger, t) {}
237  GMouseEvent(GTrigger& trigger, GMouseEvent const&);
238 
239  GMouseEvent& asMouseEvent() override {return *this;}
240  GMouseEvent* toMouseEvent() override {return this;}
241 
244  GPoint pos() const;
245  float x() const {return pos().x;}
246  float y() const {return pos().y;}
248 
251  const GPoint& wpos() const {return wpos_;}
252  float wx() const {return wpos_.x;}
253  float wy() const {return wpos_.y;}
255 
258  float dx() const {return dx_;}
259  float dy() const {return dy_;}
261 
268  int button() const {return button_;}
269 
272  int clickCount() const {return clickcount_;}
273 
276  GEventState state() const {return state_;}
277 
280  bool shiftPressed() const {return (state_ & GEventState::Shift) != 0;}
281  bool ctrlPressed() const {return (state_ & GEventState::Ctrl) != 0;}
282  bool metaPressed() const {return (state_ & GEventState::Meta) != 0;}
284 
290  void redirectEvent(Gadget& to_gadget);
291 
294  int button_{}, clickcount_{};
295  GEventState state_{};
296  GPoint wpos_;
297  float dx_{}, dy_{};
299 };
300 
301 
304 class GKeyEvent : public GEvent {
305 public:
306  GKeyEvent(GTrigger const& trigger, GTime t) : GEvent(trigger, t) {}
307 
308  GKeyEvent& asKeyEvent() override {return *this;}
309  GKeyEvent* toKeyEvent() override {return this;}
310 
311  bool shiftMod() const {return (state_ & GEventState::Shift) != 0;}
312  bool ctrlMod() const {return (state_ & GEventState::Ctrl) != 0;}
313  bool metaMod() const {return (state_ & GEventState::Meta) != 0;}
314 
316  GEventState state() const {return state_;}
317 
320  GKeyCode key() const {return key_;}
321 
325  GString const& text() const {return text_;}
326 
329  GEventState state_{};
330  GKeyCode key_{};
331  GString text_;
333 };
334 
335 
339 class GTouchEvent : public GEvent {
340 public:
341  enum Type {Down, Up, Motion};
342 
343  GTouchEvent(GTrigger const& trigger, GTime t) : GEvent(trigger, t) {}
344 
345  GTouchEvent& asTouchEvent() override {return *this;}
346  GTouchEvent* toTouchEvent() override {return this;}
347 
348  Type type_{};
349  int64_t touchID_{}, fingerID_{};
350  GPoint pos_; // normalized in [0.,1.]
351  GPoint dist_; // normalized in [0.,1.]
352  float pressure_{}; // normalized in [0.,1.]
353 };
354 
355 
359 class GGestureEvent : public GEvent {
360 public:
361  GGestureEvent(GTrigger const& trigger, GTime t) : GEvent(trigger, t) {}
362 
363  GGestureEvent& asGestureEvent() override {return *this;}
364  GGestureEvent* toGestureEvent() override {return this;}
365 
366  int64_t touchID_{};
367  int numfingers_{};
368  GPoint pos_; // normalized in [0.,1.]
369  float dist_{}, theta_{};
370 };
371 
372 }
373 
374 #endif