Btk
event.hpp
1 #if !defined(_BTK_EVENT_HPP_)
2 #define _BTK_EVENT_HPP_
3 #include <cstdint>
4 #include <string_view>
5 #include "locals.hpp"
6 #include "defs.hpp"
7 #include "rect.hpp"
8 union SDL_Event;
9 struct SDL_MouseButtonEvent;
10 namespace Btk{
11  class Window;
12  class Widget;
17  class BTKAPI Event{
18  public:
19  enum Type:Uint32{
20  SetRect = 0,//Update widget's rect
21  KeyBoard = 1,//Key pressed or released
22 
23  //MotionEvent
24  Enter = 2,//Mouse enter Widget
25  Leave = 3,//Mouse leave Widget
26  Motion = 4,//Mouse motion in Widget
27  //MouseEvent
28  Click = 5,//Mouse Click
29 
30  DragBegin = 6,//The drag is begin
31  Drag = 7,//Is draging now
32  DragEnd = 8,//The drag is end
33 
34  TextInput = 9,//Text the Input
35 
36 
37  TakeFocus = 10,//The widget take focus
38  //accept the event to task focus
39 
40  LostFocus = 11,//<The widget lost focus
41  //Of course you can refuse it
42 
43  Enable = 12,//The widget was enabled
44  Disable = 13,//The widget was disabled
45 
46  Wheel = 14,//The mouse Wheel
47 
48  User = 1000,
49  UserMax = UINT32_MAX - 1,
50  Error = UINT32_MAX
51  };
57  static Type Register();
58  Type type() const noexcept{
59  return _type;
60  };
61  public:
62  Event(Type t):
63  _type(t),
64  _accepted(false){};
65  Event(const Event &ev):
66  _type(ev._type),
67  _accepted(false){};
68  virtual ~Event();
75  bool is_accepted() const noexcept{
76  return _accepted;
77  };
78  void accept() noexcept{
79  _accepted = true;
80  };
81  void reject() noexcept{
82  _accepted = false;
83  };
90  void set_type(Type t) noexcept{
91  _type = t;
92  };
93  private:
94  //Event type
95  Type _type;
96  bool _accepted;
97  friend class Window;
98  };
99  class ResizeEvent:public Event{
100 
101  };
106  struct BTKAPI MouseEvent:public Event{
112  MouseEvent():Event(Event::Type::Click){};
113  MouseEvent(const MouseEvent &) = default;
114  ~MouseEvent();
115 
116  bool is_pressed() const noexcept{
117  return state == Pressed;
118  };
119  bool is_released() const noexcept{
120  return state == Released;
121  };
122  bool is_up() const noexcept{
123  return state == Pressed;
124  };
125  bool is_down() const noexcept{
126  return state == Released;
127  };
128  Vec2 position() const noexcept{
129  return {x,y};
130  };
135  enum{
136  Pressed,//< Button UP
137  Released//< Button Down
138  }state;
143  struct{
144  Uint8 value;
145  bool is_right() const noexcept{
146  return value == SDL_BUTTON_RIGHT;
147  }
148  bool is_left() const noexcept{
149  return value == SDL_BUTTON_LEFT;
150  }
151  bool is_middle() const noexcept{
152  return value == SDL_BUTTON_MIDDLE;
153  }
154  }button;
159  Uint8 clicks;
164  int x;
165  int y;
166  };
171  struct BTKAPI KeyEvent:public Event{
178  KeyEvent():Event(Event::Type::KeyBoard){};
179  KeyEvent(const KeyEvent &ev) = default;
180  ~KeyEvent();
181  //Keycode and scancode
182  Scancode scancode;
183  Keycode keycode;
184  Keymode keymode;
185  enum{
186  Pressed,
187  Released
188  }state;
189  bool repeat;//is repeat
190  //Check kmode
191  bool has_kmod(Keymode mode) const noexcept{
192  return static_cast<bool>(keymode & mode);
193  }
194  };
199  struct SetRectEvent:public Event{
200  SetRectEvent(const Rect n):
201  Event(Type::SetRect),
202  rect(n){};
203  SetRectEvent(const SetRectEvent &ev) = default;
204  ~SetRectEvent();
205 
206  Vec2 position() const noexcept{
207  return {rect.x,rect.y};
208  };
209 
210  Rect rect;
211  };
216  struct BTKAPI MotionEvent:public Event{
217  MotionEvent(Event::Type type = Event::Type::Motion):
218  Event(type){};
219  MotionEvent(const MotionEvent &) = default;
220  ~MotionEvent();
221 
222  //datas
223  int x;
224  int y;
225 
226  int xrel;
227  int yrel;
228  //methods
229  Vec2 position() const noexcept{
230  return {x,y};
231  };
232  };
237  struct BTKAPI TextInputEvent:Event{
238  TextInputEvent():Event(TextInput){};
239  std::string_view text;//<Text input buffer(utf-8 encoded)
245  size_t length() const noexcept;
246  };
251  struct BTKAPI DragEvent:public Event{
261  DragEvent(Event::Type type,int x,int y,int xrel,int yrel):
262  Event(type),
263  x(x),
264  y(y),
265  xrel(xrel),
266  yrel(yrel){};
273  DragEvent(Event::Type type,const MotionEvent &motion):Event(type){
274  x = motion.x;
275  y = motion.y;
276  xrel = motion.xrel;
277  yrel = motion.yrel;
278  };
279  DragEvent(const DragEvent &) = default;
280  ~DragEvent();
281 
282  int x;
283  int y;
284  int xrel;//< Note It cannot be used at DragEnd
285  int yrel;//< Note It cannot be used at DragEnd
286  };
287  struct BTKAPI WheelEvent:public Event{
288  WheelEvent(Uint32 which,Sint64 x,Sint64 y):Event(Type::Wheel){
289  this->which = which;
290  this->x = x;
291  this->y = y;
292  }
293  WheelEvent(const WheelEvent &) = default;
294  ~WheelEvent();
295  Uint32 which;//< Which mouse
296  Sint64 x;//< Vertical scroll,postive for scroll right
297  Sint64 y;//< Horizontal scroll,postive for scroll up
298  };
299  typedef MouseEvent ClickEvent;
306  void PushEvent(Event *event,Window &receiver);
307  void PushEvent(Event *event,Widget &receiver);
315  bool SendEvent(Event &event,Window &receiver);
316  bool SendEvent(Event &event,Widget &receiver);
322  void DispatchEvent(const SDL_Event &ev,void *);
323 };
324 
325 
326 #endif // _BTK_EVENT_HPP_
bool is_accepted() const noexcept
is accepted
Definition: event.hpp:75
KeyEvent()
Construct a new Key Event object.
Definition: event.hpp:178
Uint8 clicks
Clicks count.
Definition: event.hpp:159
void PushEvent(Event *event, Window &receiver)
Push event to queue.
Definition: event.cpp:63
a SDL_Rect with methods
Definition: rect.hpp:10
MouseEvent()
Construct a new Mouse Event object.
Definition: event.hpp:112
Definition: widget.hpp:219
This header include many useful containers.
Definition: async.hpp:7
A Basic Window.
Definition: window.hpp:20
A Event of text input.
Definition: event.hpp:237
A event about mouse click.
Definition: event.hpp:106
void set_type(Type t) noexcept
Chaneg the event type.
Definition: event.hpp:90
A event about keyboard.
Definition: event.hpp:171
Definition: event.hpp:99
A event about mouse drag.
Definition: event.hpp:251
Definition: rect.hpp:81
bool SendEvent(Event &event, Window &receiver)
Dispatched event right now.
Definition: event.cpp:74
A event about set Widget rect.
Definition: event.hpp:199
void DispatchEvent(const SDL_Event &ev, void *)
This function was called by System to dispatch our event.
Definition: event.cpp:81
DragEvent(Event::Type type, const MotionEvent &motion)
Construct a new Drag Event object from MotionEvent.
Definition: event.hpp:273
A event about mouse motion.
Definition: event.hpp:216
A base event of all events.
Definition: event.hpp:17
Definition: event.hpp:287
int x
Mouse position.
Definition: event.hpp:164
DragEvent(Event::Type type, int x, int y, int xrel, int yrel)
Construct a new Drag Event object.
Definition: event.hpp:261