g4tools  5.4.0
event
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_event
5 #define tools_sg_event
6 
7 #include "../scast"
8 #include "../S_STRING"
9 #include "../typedefs"
10 
11 #ifdef TOOLS_MEM
12 #include "../mem"
13 #endif
14 
15 #define TOOLS_SG_EVENT_ID_CAST
16 
17 namespace tools {
18 namespace sg {
19 
20 class event {
21 public:
22 #ifdef TOOLS_SG_EVENT_ID_CAST
23  static cid id_class() {return 0;}
24  virtual void* cast(cid) const = 0;
25 #else
26  virtual void* cast(const std::string&) const = 0;
27 #endif
28  virtual event* copy() const = 0;
29 public:
30 #ifdef TOOLS_MEM
32 #endif
33 public:
34  event(){
35 #ifdef TOOLS_MEM
36  mem::increment(s_class().c_str());
37 #endif
38  }
39  virtual ~event(){
40 #ifdef TOOLS_MEM
41  mem::decrement(s_class().c_str());
42 #endif
43  }
44 public:
45  event(const event&){
46 #ifdef TOOLS_MEM
47  mem::increment(s_class().c_str());
48 #endif
49  }
50  event& operator=(const event&){return *this;}
51 };
52 
53 class size_event : public event {
54  typedef event parent;
55 public:
56 #ifdef TOOLS_SG_EVENT_ID_CAST
57  static cid id_class() {return parent::id_class()+1;}
58  virtual void* cast(cid a_class) const {
59  if(void* p = cmp_cast<size_event>(this,a_class)) return p;
60  return 0;
61  }
62 #else
64  virtual void* cast(const std::string& a_class) const {
65  if(void* p = cmp_cast<size_event>(this,a_class)) {return p;}
66  return 0;
67  }
68 #endif
69  virtual event* copy() const {return new size_event(*this);}
70 public:
71  size_event(unsigned int a_ow,unsigned int a_oh,
72  unsigned int a_w,unsigned int a_h)
73  :m_ow(a_ow)
74  ,m_oh(a_oh)
75  ,m_w(a_w)
76  ,m_h(a_h)
77  {}
78  virtual ~size_event(){}
79 public:
80  size_event(const size_event& a_from)
81  :event(a_from)
82  ,m_ow(a_from.m_ow)
83  ,m_oh(a_from.m_oh)
84  ,m_w(a_from.m_w)
85  ,m_h(a_from.m_h)
86  {}
87  size_event& operator=(const size_event& a_from){
88  event::operator=(a_from);
89  m_ow = a_from.m_ow;
90  m_oh = a_from.m_oh;
91  m_w = a_from.m_w;
92  m_h = a_from.m_h;
93  return *this;
94  }
95 public:
96  unsigned int old_width() const {return m_ow;}
97  unsigned int old_height() const {return m_oh;}
98  unsigned int width() const {return m_w;}
99  unsigned int height() const {return m_h;}
100 protected:
101  unsigned int m_ow;
102  unsigned int m_oh;
103  unsigned int m_w;
104  unsigned int m_h;
105 };
106 
107 class mouse_down_event : public event {
108  typedef event parent;
109 public:
110 #ifdef TOOLS_SG_EVENT_ID_CAST
111  static cid id_class() {return parent::id_class()+2;}
112  virtual void* cast(cid a_class) const {
113  if(void* p = cmp_cast<mouse_down_event>(this,a_class)) return p;
114  return 0;
115  }
116 #else
118  virtual void* cast(const std::string& a_class) const {
119  if(void* p = cmp_cast<mouse_down_event>(this,a_class)) {return p;}
120  return 0;
121  }
122 #endif
123  virtual event* copy() const {return new mouse_down_event(*this);}
124 public:
125  mouse_down_event(int a_x,int a_y) //signed because of wall.
126  :m_x(a_x)
127  ,m_y(a_y)
128  {}
129  virtual ~mouse_down_event(){}
130 public:
132  :event(a_from)
133  ,m_x(a_from.m_x)
134  ,m_y(a_from.m_y)
135  {}
137  event::operator=(a_from);
138  m_x = a_from.m_x;
139  m_y = a_from.m_y;
140  return *this;
141  }
142 public:
143  int x() const {return m_x;}
144  int y() const {return m_y;}
145 protected:
146  int m_x;
147  int m_y;
148 };
149 
150 class mouse_up_event : public event {
151  typedef event parent;
152 public:
153 #ifdef TOOLS_SG_EVENT_ID_CAST
154  static cid id_class() {return parent::id_class()+3;}
155  virtual void* cast(cid a_class) const {
156  if(void* p = cmp_cast<mouse_up_event>(this,a_class)) return p;
157  return 0;
158  }
159 #else
161  virtual void* cast(const std::string& a_class) const {
162  if(void* p = cmp_cast<mouse_up_event>(this,a_class)) {return p;}
163  return 0;
164  }
165 #endif
166  virtual event* copy() const {return new mouse_up_event(*this);}
167 public:
168  mouse_up_event(int a_x,int a_y) //signed because of wall.
169  :m_x(a_x)
170  ,m_y(a_y)
171  {}
172  virtual ~mouse_up_event(){}
173 public:
175  :event(a_from)
176  ,m_x(a_from.m_x)
177  ,m_y(a_from.m_y)
178  {}
180  event::operator=(a_from);
181  m_x = a_from.m_x;
182  m_y = a_from.m_y;
183  return *this;
184  }
185 public:
186  int x() const {return m_x;}
187  int y() const {return m_y;}
188 protected:
189  int m_x;
190  int m_y;
191 };
192 
193 class mouse_move_event : public event {
194  typedef event parent;
195 public:
196 #ifdef TOOLS_SG_EVENT_ID_CAST
197  static cid id_class() {return parent::id_class()+4;}
198  virtual void* cast(cid a_class) const {
199  if(void* p = cmp_cast<mouse_move_event>(this,a_class)) return p;
200  return 0;
201  }
202 #else
204  virtual void* cast(const std::string& a_class) const {
205  if(void* p = cmp_cast<mouse_move_event>(this,a_class)) {return p;}
206  return 0;
207  }
208 #endif
209  virtual event* copy() const {return new mouse_move_event(*this);}
210 public:
211  mouse_move_event(int a_x,int a_y, //signed because of wall.
212  int a_ox,int a_oy,
213  bool a_touch) //for sliders.
214  :m_x(a_x)
215  ,m_y(a_y)
216  ,m_ox(a_ox)
217  ,m_oy(a_oy)
218  ,m_touch(a_touch)
219  {}
220  virtual ~mouse_move_event(){}
221 public:
223  :event(a_from)
224  ,m_x(a_from.m_x)
225  ,m_y(a_from.m_y)
226  ,m_ox(a_from.m_ox)
227  ,m_oy(a_from.m_oy)
228  ,m_touch(a_from.m_touch)
229  {}
231  event::operator=(a_from);
232  m_x = a_from.m_x;
233  m_y = a_from.m_y;
234 
235  m_ox = a_from.m_ox;
236  m_oy = a_from.m_oy;
237 
238  m_touch = a_from.m_touch;
239  return *this;
240  }
241 public:
242  int x() const {return m_x;}
243  int y() const {return m_y;}
244  int ox() const {return m_ox;}
245  int oy() const {return m_oy;}
246  bool is_touch() const {return m_touch;}
247 protected:
248  int m_x;
249  int m_y;
250  int m_ox;
251  int m_oy; //+ = up.
252  // etc :
253  bool m_touch;
254 };
255 
256 class anim_event : public event {
257  typedef event parent;
258 public:
259 #ifdef TOOLS_SG_EVENT_ID_CAST
260  static cid id_class() {return parent::id_class()+5;}
261  virtual void* cast(cid a_class) const {
262  if(void* p = cmp_cast<anim_event>(this,a_class)) return p;
263  return 0;
264  }
265 #else
267  virtual void* cast(const std::string& a_class) const {
268  if(void* p = cmp_cast<anim_event>(this,a_class)) {return p;}
269  return 0;
270  }
271 #endif
272  virtual event* copy() const {return new anim_event(*this);}
273 public:
274  typedef uint64 num_t;
275  anim_event(num_t a_secs,num_t a_micro_secs)
276  :m_secs(a_secs),m_micro_secs(a_micro_secs)
277  ,m_some_found(false)
278  {}
279  virtual ~anim_event(){}
280 public:
281  anim_event(const anim_event& a_from)
282  :event(a_from)
283  ,m_secs(a_from.m_secs)
284  ,m_micro_secs(a_from.m_micro_secs)
285  ,m_some_found(a_from.m_some_found)
286  {}
287  anim_event& operator=(const anim_event& a_from){
288  event::operator=(a_from);
289  m_secs = a_from.m_secs;
290  m_micro_secs = a_from.m_micro_secs;
291  m_some_found = a_from.m_some_found;
292  return *this;
293  }
294 public:
295 /*
296  void set_time(num_t a_secs,num_t a_micro_secs) {
297  m_secs = a_secs;
298  m_micro_secs = a_micro_secs;
299  }
300 */
301  num_t seconds() const {return m_secs;}
302  num_t micro_seconds() const {return m_micro_secs;}
303 
304  void set_some_found(bool a_v) {m_some_found = a_v;}
305  bool some_found() const {return m_some_found;}
306 protected:
310 };
311 
312 class key_down_event : public event {
313  typedef event parent;
314 public:
315 #ifdef TOOLS_SG_EVENT_ID_CAST
316  static cid id_class() {return parent::id_class()+6;}
317  virtual void* cast(cid a_class) const {
318  if(void* p = cmp_cast<key_down_event>(this,a_class)) return p;
319  return 0;
320  }
321 #else
323  virtual void* cast(const std::string& a_class) const {
324  if(void* p = cmp_cast<key_down_event>(this,a_class)) {return p;}
325  return 0;
326  }
327 #endif
328  virtual event* copy() const {return new key_down_event(*this);}
329 public:
331  :m_key(a_key)
332  {}
333  virtual ~key_down_event(){}
334 public:
336  :event(a_from)
337  ,m_key(a_from.m_key)
338  {}
340  event::operator=(a_from);
341  m_key = a_from.m_key;
342  return *this;
343  }
344 public:
345  key_code key() const {return m_key;}
346 protected:
348 };
349 
350 class key_up_event : public event {
351  typedef event parent;
352 public:
353 #ifdef TOOLS_SG_EVENT_ID_CAST
354  static cid id_class() {return parent::id_class()+7;}
355  virtual void* cast(cid a_class) const {
356  if(void* p = cmp_cast<key_up_event>(this,a_class)) return p;
357  return 0;
358  }
359 #else
361  virtual void* cast(const std::string& a_class) const {
362  if(void* p = cmp_cast<key_up_event>(this,a_class)) {return p;}
363  return 0;
364  }
365 #endif
366  virtual event* copy() const {return new key_up_event(*this);}
367 public:
369  :m_key(a_key)
370  {}
371  virtual ~key_up_event(){}
372 public:
373  key_up_event(const key_up_event& a_from)
374  :event(a_from)
375  ,m_key(a_from.m_key)
376  {}
378  event::operator=(a_from);
379  m_key = a_from.m_key;
380  return *this;
381  }
382 public:
383  key_code key() const {return m_key;}
384 protected:
386 };
387 
388 class wheel_rotate_event : public event {
389  typedef event parent;
390 public:
391 #ifdef TOOLS_SG_EVENT_ID_CAST
392  static cid id_class() {return parent::id_class()+8;}
393  virtual void* cast(cid a_class) const {
394  if(void* p = cmp_cast<wheel_rotate_event>(this,a_class)) return p;
395  return 0;
396  }
397 #else
399  virtual void* cast(const std::string& a_class) const {
400  if(void* p = cmp_cast<wheel_rotate_event>(this,a_class)) {return p;}
401  return 0;
402  }
403 #endif
404  virtual event* copy() const {return new wheel_rotate_event(*this);}
405 public:
406  wheel_rotate_event(int a_angle)
407  :m_angle(a_angle)
408  {}
410 public:
412  :event(a_from)
413  ,m_angle(a_from.m_angle)
414  {}
416  event::operator=(a_from);
417  m_angle = a_from.m_angle;
418  return *this;
419  }
420 public:
421  int angle() const {return m_angle;}
422 protected:
423  int m_angle;
424 };
425 
426 #ifdef TOOLS_SG_EVENT_ID_CAST
427 template <class FROM,class TO> inline TO* event_cast(FROM& a_o) {return id_cast<FROM,TO>(a_o);}
428 template <class FROM,class TO> inline const TO* event_cast(const FROM& a_o) {return id_cast<FROM,TO>(a_o);}
429 #else
430 template <class FROM,class TO> inline TO* event_cast(FROM& a_o) {return safe_cast<FROM,TO>(a_o);}
431 template <class FROM,class TO> inline const TO* event_cast(const FROM& a_o) {return safe_cast<FROM,TO>(a_o);}
432 #endif
433 
434 inline bool get_event_xy(const sg::event& a_event,int& a_x,int& a_y) {
435  typedef sg::event evt_t;
436  typedef sg::mouse_up_event up_evt_t;
437  typedef sg::mouse_down_event dn_evt_t;
438  if(const dn_evt_t* devt = event_cast<evt_t,dn_evt_t>(a_event)) {
439  a_x = devt->x();
440  a_y = devt->y();
441  return true;
442 
443  } else if(const up_evt_t* uevt = event_cast<evt_t,up_evt_t>(a_event)) {
444  a_x = uevt->x();
445  a_y = uevt->y();
446  return true;
447 
448  }
449  a_x = 0;
450  a_y = 0;
451  return false;
452 }
453 
454 }}
455 
456 #endif
tools::sg::size_event::operator=
size_event & operator=(const size_event &a_from)
Definition: event:87
tools::sg::anim_event::anim_event
anim_event(num_t a_secs, num_t a_micro_secs)
Definition: event:275
tools::sg::mouse_down_event::mouse_down_event
mouse_down_event(const mouse_down_event &a_from)
Definition: event:131
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::sg::mouse_move_event::oy
int oy() const
Definition: event:245
tools::sg::key_down_event::operator=
key_down_event & operator=(const key_down_event &a_from)
Definition: event:339
tools::sg::mouse_up_event::y
int y() const
Definition: event:187
tools::sg::anim_event::cast
virtual void * cast(cid a_class) const
Definition: event:261
tools::sg::key_up_event::key_up_event
key_up_event(const key_up_event &a_from)
Definition: event:373
tools::sg::wheel_rotate_event::angle
int angle() const
Definition: event:421
tools::sg::mouse_move_event::operator=
mouse_move_event & operator=(const mouse_move_event &a_from)
Definition: event:230
tools::sg::key_down_event::key_down_event
key_down_event(key_code a_key)
Definition: event:330
tools::sg::mouse_up_event::m_x
int m_x
Definition: event:189
tools::sg::anim_event::m_secs
num_t m_secs
Definition: event:307
tools::sg::key_down_event::key_down_event
key_down_event(const key_down_event &a_from)
Definition: event:335
tools::sg::key_up_event::key_up_event
key_up_event(key_code a_key)
Definition: event:368
tools::sg::key_up_event::~key_up_event
virtual ~key_up_event()
Definition: event:371
tools::sg::mouse_up_event::x
int x() const
Definition: event:186
tools::sg::anim_event::some_found
bool some_found() const
Definition: event:305
tools::sg::anim_event::num_t
uint64 num_t
Definition: event:274
tools::sg::get_event_xy
bool get_event_xy(const sg::event &a_event, int &a_x, int &a_y)
Definition: event:434
tools::sg::key_up_event::cast
virtual void * cast(cid a_class) const
Definition: event:355
tools::sg::anim_event::set_some_found
void set_some_found(bool a_v)
Definition: event:304
tools::sg::key_up_event::m_key
key_code m_key
Definition: event:385
tools::sg::wheel_rotate_event::~wheel_rotate_event
virtual ~wheel_rotate_event()
Definition: event:409
tools::key_code
unsigned int key_code
Definition: typedefs:131
tools::sg::size_event
Definition: event:53
tools::sg::size_event::id_class
static cid id_class()
Definition: event:57
tools::sg::mouse_down_event::id_class
static cid id_class()
Definition: event:111
tools::sg::size_event::m_h
unsigned int m_h
Definition: event:104
tools::sg::wheel_rotate_event::wheel_rotate_event
wheel_rotate_event(const wheel_rotate_event &a_from)
Definition: event:411
tools::sg::size_event::~size_event
virtual ~size_event()
Definition: event:78
tools::sg::mouse_up_event::m_y
int m_y
Definition: event:190
tools::sg::key_up_event::copy
virtual event * copy() const
Definition: event:366
tools::sg::anim_event::copy
virtual event * copy() const
Definition: event:272
tools::sg::mouse_move_event::cast
virtual void * cast(cid a_class) const
Definition: event:198
tools::sg::size_event::cast
virtual void * cast(cid a_class) const
Definition: event:58
tools::sg::key_up_event
Definition: event:350
tools::sg::wheel_rotate_event::wheel_rotate_event
wheel_rotate_event(int a_angle)
Definition: event:406
tools::sg::key_up_event::operator=
key_up_event & operator=(const key_up_event &a_from)
Definition: event:377
tools::sg::anim_event
Definition: event:256
tools::sg::wheel_rotate_event::operator=
wheel_rotate_event & operator=(const wheel_rotate_event &a_from)
Definition: event:415
tools::sg::key_down_event::id_class
static cid id_class()
Definition: event:316
tools::sg::size_event::m_ow
unsigned int m_ow
Definition: event:101
tools::sg::mouse_down_event::y
int y() const
Definition: event:144
TOOLS_SCLASS
#define TOOLS_SCLASS(a_name)
Definition: S_STRING:41
tools::sg::size_event::old_width
unsigned int old_width() const
Definition: event:96
tools::sg::anim_event::~anim_event
virtual ~anim_event()
Definition: event:279
tools::sg::mouse_move_event::~mouse_move_event
virtual ~mouse_move_event()
Definition: event:220
tools::sg::event::copy
virtual event * copy() const =0
tools::sg::mouse_move_event::is_touch
bool is_touch() const
Definition: event:246
tools::sg::mouse_move_event::m_oy
int m_oy
Definition: event:251
tools::sg::mouse_down_event::m_x
int m_x
Definition: event:146
tools::sg::mouse_up_event::operator=
mouse_up_event & operator=(const mouse_up_event &a_from)
Definition: event:179
tools::sg::size_event::m_w
unsigned int m_w
Definition: event:103
tools::sg::mouse_move_event::ox
int ox() const
Definition: event:244
tools::sg::size_event::old_height
unsigned int old_height() const
Definition: event:97
tools::sg::key_down_event
Definition: event:312
tools::sg::event
Definition: event:20
tools::sg::mouse_down_event::operator=
mouse_down_event & operator=(const mouse_down_event &a_from)
Definition: event:136
tools::sg::mouse_up_event
Definition: event:150
tools::sg::mouse_move_event::x
int x() const
Definition: event:242
tools::sg::mouse_down_event::x
int x() const
Definition: event:143
tools::sg::mouse_up_event::mouse_up_event
mouse_up_event(const mouse_up_event &a_from)
Definition: event:174
tools::sg::mouse_move_event::mouse_move_event
mouse_move_event(int a_x, int a_y, int a_ox, int a_oy, bool a_touch)
Definition: event:211
tools::sg::key_up_event::id_class
static cid id_class()
Definition: event:354
tools::sg::anim_event::m_some_found
bool m_some_found
Definition: event:309
tools::sg::key_down_event::copy
virtual event * copy() const
Definition: event:328
tools::sg::mouse_move_event::copy
virtual event * copy() const
Definition: event:209
tools::sg::mouse_move_event::m_touch
bool m_touch
Definition: event:253
tools::sg::mouse_up_event::copy
virtual event * copy() const
Definition: event:166
tools::sg::mouse_down_event::m_y
int m_y
Definition: event:147
tools::sg::size_event::copy
virtual event * copy() const
Definition: event:69
tools::sg::anim_event::seconds
num_t seconds() const
Definition: event:301
tools::sg::mouse_up_event::~mouse_up_event
virtual ~mouse_up_event()
Definition: event:172
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::wheel_rotate_event::copy
virtual event * copy() const
Definition: event:404
tools::sg::size_event::size_event
size_event(const size_event &a_from)
Definition: event:80
tools::sg::event::operator=
event & operator=(const event &)
Definition: event:50
tools::sg::event::cast
virtual void * cast(cid) const =0
tools::sg::mouse_down_event
Definition: event:107
tools::sg::mouse_move_event::y
int y() const
Definition: event:243
tools::sg::event::~event
virtual ~event()
Definition: event:39
tools::sg::event::event
event()
Definition: event:34
tools::sg::event_cast
TO * event_cast(FROM &a_o)
Definition: event:427
tools::sg::mouse_move_event::id_class
static cid id_class()
Definition: event:197
tools::sg::size_event::width
unsigned int width() const
Definition: event:98
tools::sg::wheel_rotate_event::id_class
static cid id_class()
Definition: event:392
tools::sg::mouse_down_event::mouse_down_event
mouse_down_event(int a_x, int a_y)
Definition: event:125
tools::sg::size_event::size_event
size_event(unsigned int a_ow, unsigned int a_oh, unsigned int a_w, unsigned int a_h)
Definition: event:71
tools::sg::mouse_move_event::m_y
int m_y
Definition: event:249
tools::sg::mouse_up_event::id_class
static cid id_class()
Definition: event:154
tools::sg::anim_event::m_micro_secs
num_t m_micro_secs
Definition: event:308
tools::sg::key_down_event::m_key
key_code m_key
Definition: event:347
tools::sg::key_down_event::key
key_code key() const
Definition: event:345
tools::sg::key_down_event::cast
virtual void * cast(cid a_class) const
Definition: event:317
tools::sg::mouse_move_event
Definition: event:193
tools::sg::key_up_event::key
key_code key() const
Definition: event:383
tools::sg::event::event
event(const event &)
Definition: event:45
tools::sg::anim_event::micro_seconds
num_t micro_seconds() const
Definition: event:302
tools::sg::anim_event::anim_event
anim_event(const anim_event &a_from)
Definition: event:281
tools::sg::mouse_move_event::mouse_move_event
mouse_move_event(const mouse_move_event &a_from)
Definition: event:222
tools::sg::wheel_rotate_event::cast
virtual void * cast(cid a_class) const
Definition: event:393
tools::sg::size_event::height
unsigned int height() const
Definition: event:99
tools::sg::anim_event::id_class
static cid id_class()
Definition: event:260
tools::sg::event::id_class
static cid id_class()
Definition: event:23
tools::sg::size_event::m_oh
unsigned int m_oh
Definition: event:102
tools::sg::mouse_move_event::m_ox
int m_ox
Definition: event:250
tools::sg::mouse_up_event::mouse_up_event
mouse_up_event(int a_x, int a_y)
Definition: event:168
tools::sg::mouse_move_event::m_x
int m_x
Definition: event:248
tools::sg::wheel_rotate_event
Definition: event:388
tools::sg::anim_event::operator=
anim_event & operator=(const anim_event &a_from)
Definition: event:287
tools::sg::mouse_down_event::~mouse_down_event
virtual ~mouse_down_event()
Definition: event:129
tools::sg::mouse_up_event::cast
virtual void * cast(cid a_class) const
Definition: event:155
tools::sg::key_down_event::~key_down_event
virtual ~key_down_event()
Definition: event:333
tools::sg::mouse_down_event::copy
virtual event * copy() const
Definition: event:123
tools::cid
unsigned short cid
Definition: cid:9
tools::sg::wheel_rotate_event::m_angle
int m_angle
Definition: event:423
tools::sg::mouse_down_event::cast
virtual void * cast(cid a_class) const
Definition: event:112