Panda3D
mouseWatcher.h
1 // Filename: mouseWatcher.h
2 // Created by: drose (12Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef MOUSEWATCHER_H
16 #define MOUSEWATCHER_H
17 
18 #include "pandabase.h"
19 
20 #include "mouseWatcherRegion.h"
21 #include "mouseWatcherGroup.h"
22 #include "dataNode.h"
23 #include "luse.h"
24 #include "pointerTo.h"
25 #include "eventHandler.h"
26 #include "modifierButtons.h"
27 #include "buttonHandle.h"
28 #include "buttonEventList.h"
29 #include "pointerEvent.h"
30 #include "pointerEventList.h"
31 #include "linmath_events.h"
32 #include "bitArray.h"
33 #include "clockObject.h"
34 #include "pvector.h"
35 #include "displayRegion.h"
36 
38 class DisplayRegion;
39 
40 ////////////////////////////////////////////////////////////////////
41 // Class : MouseWatcher
42 // Description : This TFormer maintains a list of rectangular regions
43 // on the screen that are considered special mouse
44 // regions; typically these will be click buttons. When
45 // the mouse passes in or out of one of these regions,
46 // or when a button is clicked while the mouse is in one
47 // of these regions, an event is thrown.
48 //
49 // Mouse events may also be suppressed from the rest of
50 // the datagraph in these special regions.
51 //
52 // This class can also implement a software mouse
53 // pointer by automatically generating a transform to
54 // apply to a piece of geometry placed under the 2-d
55 // scene graph. It will move the geometry around
56 // according to the mouse's known position.
57 //
58 // Finally, this class can keep a record of the mouse
59 // trail. This is useful if you want to know, not just
60 // where the mouse is, but the exact sequence of movements
61 // it took to get there. This information is mainly useful
62 // for gesture-recognition code. To use trail logging,
63 // you need to enable the generation of pointer events
64 // in the GraphicsWindowInputDevice and set the trail
65 // log duration in the MouseWatcher. Otherwise, the
66 // trail log will be empty.
67 ////////////////////////////////////////////////////////////////////
68 class EXPCL_PANDA_TFORM MouseWatcher : public DataNode, public MouseWatcherBase {
69 PUBLISHED:
70  MouseWatcher(const string &name = "");
71  ~MouseWatcher();
72 
73  bool remove_region(MouseWatcherRegion *region);
74 
75  INLINE bool has_mouse() const;
76  INLINE bool is_mouse_open() const;
77  INLINE const LPoint2 &get_mouse() const;
78  INLINE PN_stdfloat get_mouse_x() const;
79  INLINE PN_stdfloat get_mouse_y() const;
80 
81  INLINE void set_frame(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top);
82  INLINE void set_frame(const LVecBase4 &frame);
83  INLINE const LVecBase4 &get_frame() const;
84 
85  INLINE bool is_over_region() const;
86  INLINE bool is_over_region(PN_stdfloat x, PN_stdfloat y) const;
87  INLINE bool is_over_region(const LPoint2 &pos) const;
88 
89  INLINE MouseWatcherRegion *get_over_region() const;
90  INLINE MouseWatcherRegion *get_over_region(PN_stdfloat x, PN_stdfloat y) const;
91  MouseWatcherRegion *get_over_region(const LPoint2 &pos) const;
92 
93  INLINE bool is_button_down(ButtonHandle button) const;
94 
95  INLINE void set_button_down_pattern(const string &pattern);
96  INLINE const string &get_button_down_pattern() const;
97 
98  INLINE void set_button_up_pattern(const string &pattern);
99  INLINE const string &get_button_up_pattern() const;
100 
101  INLINE void set_button_repeat_pattern(const string &pattern);
102  INLINE const string &get_button_repeat_pattern() const;
103 
104  INLINE void set_enter_pattern(const string &pattern);
105  INLINE const string &get_enter_pattern() const;
106 
107  INLINE void set_leave_pattern(const string &pattern);
108  INLINE const string &get_leave_pattern() const;
109 
110  INLINE void set_within_pattern(const string &pattern);
111  INLINE const string &get_within_pattern() const;
112 
113  INLINE void set_without_pattern(const string &pattern);
114  INLINE const string &get_without_pattern() const;
115 
116  INLINE void set_geometry(PandaNode *node);
117  INLINE bool has_geometry() const;
118  INLINE PandaNode *get_geometry() const;
119  INLINE void clear_geometry();
120 
121  INLINE void set_extra_handler(EventHandler *eh);
122  INLINE EventHandler *get_extra_handler() const;
123 
124  INLINE void set_modifier_buttons(const ModifierButtons &mods);
125  INLINE ModifierButtons get_modifier_buttons() const;
126 
127  INLINE void set_display_region(DisplayRegion *dr);
128  INLINE void clear_display_region();
129  INLINE DisplayRegion *get_display_region() const;
130  INLINE bool has_display_region() const;
131 
132  bool add_group(MouseWatcherGroup *group);
133  bool remove_group(MouseWatcherGroup *group);
134  bool replace_group(MouseWatcherGroup *old_group, MouseWatcherGroup *new_group);
135  int get_num_groups() const;
136  MouseWatcherGroup *get_group(int n) const;
137  MAKE_SEQ(get_groups, get_num_groups, get_group);
138 
139  INLINE void set_inactivity_timeout(double timeout);
140  INLINE bool has_inactivity_timeout() const;
141  INLINE double get_inactivity_timeout() const;
142  INLINE void clear_inactivity_timeout();
143 
144  INLINE void set_inactivity_timeout_event(const string &event);
145  INLINE const string &get_inactivity_timeout_event() const;
146 
147  INLINE CPT(PointerEventList) get_trail_log() const;
148  INLINE int num_trail_recent() const;
149  void set_trail_log_duration(double duration);
150  PT(GeomNode) get_trail_node();
151  void clear_trail_node();
152  INLINE void clear_trail_log();
153 
154  void note_activity();
155 
156 public:
157  virtual void output(ostream &out) const;
158  virtual void write(ostream &out, int indent_level = 0) const;
159 
160 protected:
161  void get_over_regions(Regions &regions, const LPoint2 &pos) const;
162  static MouseWatcherRegion *get_preferred_region(const Regions &regions);
163 
164  void set_current_regions(Regions &regions);
165  void clear_current_regions();
166 
167 #ifndef NDEBUG
168  virtual void do_show_regions(const NodePath &render2d,
169  const string &bin_name, int draw_order);
170  virtual void do_hide_regions();
171 #endif // NDEBUG
172 
173  static void intersect_regions(Regions &only_a,
174  Regions &only_b,
175  Regions &both,
176  const Regions &regions_a,
177  const Regions &regions_b);
178  static bool remove_region_from(Regions &regions,
179  MouseWatcherRegion *region);
180  static bool has_region_in(const Regions &regions,
181  MouseWatcherRegion *region);
182 
183  void throw_event_pattern(const string &pattern,
184  const MouseWatcherRegion *region,
185  const ButtonHandle &button);
186 
187  void move();
188  void press(ButtonHandle button, bool keyrepeat);
189  void release(ButtonHandle button);
190  void keystroke(int keycode);
191  void candidate(const wstring &candidate, size_t highlight_start,
192  size_t highlight_end, size_t cursor_pos);
193 
194  void global_keyboard_press(const MouseWatcherParameter &param);
195  void global_keyboard_release(const MouseWatcherParameter &param);
196 
197  INLINE void within_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
198  INLINE void without_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
199  void enter_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
200  void exit_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
201 
202  void set_no_mouse();
203  void set_mouse(const LVecBase2 &xy, const LVecBase2 &pixel_xy);
204 
205 private:
206  void consider_keyboard_suppress(const MouseWatcherRegion *region);
207  void discard_excess_trail_log();
208  void update_trail_node();
209 
210  bool constrain_display_region(DisplayRegion *display_region,
211  LVecBase2 &f, LVecBase2 &p,
212  Thread *current_thread);
213 
214 private:
215  // This wants to be a set, but because you cannot export sets across
216  // dlls in windows, we will make it a vector instead
218  Groups _groups;
219 
220  bool _has_mouse;
221  int _internal_suppress;
222  int _external_suppress;
223  LPoint2 _mouse;
224  LPoint2 _mouse_pixel;
225  BitArray _current_buttons_down;
226 
227  LVecBase4 _frame;
228 
229  PT(PointerEventList) _trail_log;
230  int _num_trail_recent;
231  double _trail_log_duration;
232  PT(GeomNode) _trail_node;
233 
234  Regions _current_regions;
235  PT(MouseWatcherRegion) _preferred_region;
236  PT(MouseWatcherRegion) _preferred_button_down_region;
237  bool _button_down;
238 
239  bool _enter_multiple;
240  bool _implicit_click;
241 
242  string _button_down_pattern;
243  string _button_up_pattern;
244  string _button_repeat_pattern;
245  string _enter_pattern;
246  string _leave_pattern;
247  string _within_pattern;
248  string _without_pattern;
249 
250  PT(PandaNode) _geometry;
251 
252  EventHandler *_eh;
253  ModifierButtons _mods;
254  DisplayRegion *_display_region;
255  DisplayRegion *_button_down_display_region;
256 
257  bool _has_inactivity_timeout;
258  double _inactivity_timeout;
259  string _inactivity_timeout_event;
260  double _last_activity;
261 
262  enum InactivityState {
263  IS_active,
264  IS_inactive,
265  IS_active_to_inactive,
266  IS_inactive_to_active,
267  };
268  InactivityState _inactivity_state;
269 
270 #ifndef NDEBUG
271  NodePath _show_regions_render2d;
272  string _show_regions_bin_name;
273  int _show_regions_draw_order;
274 #endif
275 
276 protected:
277  // Inherited from DataNode
278  virtual void do_transmit_data(DataGraphTraverser *trav,
279  const DataNodeTransmit &input,
280  DataNodeTransmit &output);
281 
282 private:
283  // inputs
284  int _pixel_xy_input;
285  int _pixel_size_input;
286  int _xy_input;
287  int _button_events_input;
288  int _pointer_events_input;
289 
290  // outputs
291  int _pixel_xy_output;
292  int _pixel_size_output;
293  int _xy_output;
294  int _button_events_output;
295 
296  PT(EventStoreVec2) _pixel_xy;
297  PT(EventStoreVec2) _xy;
298  PT(EventStoreVec2) _pixel_size;
299  PT(ButtonEventList) _button_events;
300 
301 public:
302  static TypeHandle get_class_type() {
303  return _type_handle;
304  }
305  static void init_type() {
306  DataNode::init_type();
307  register_type(_type_handle, "MouseWatcher",
308  DataNode::get_class_type());
309  }
310  virtual TypeHandle get_type() const {
311  return get_class_type();
312  }
313  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
314 
315 private:
316  static TypeHandle _type_handle;
317 };
318 
319 #include "mouseWatcher.I"
320 
321 #endif
This TFormer maintains a list of rectangular regions on the screen that are considered special mouse ...
Definition: mouseWatcher.h:68
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
The fundamental type of node for the data graph.
Definition: dataNode.h:64
This represents a collection of MouseWatcherRegions that may be managed as a group.
This class monitors the state of a number of individual buttons and tracks whether each button is kno...
A class to monitor events from the C++ side of things.
Definition: eventHandler.h:41
bool remove_region(MouseWatcherRegion *region)
Removes the indicated region from the group.
Records a set of pointer events that happened recently.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:109
Records a set of button events that happened recently.
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
A dynamic array with an unlimited number of bits.
Definition: bitArray.h:42
This is the class that defines a rectangular region on the screen for the MouseWatcher.
This represents a collection of MouseWatcherRegions that may be managed as a group.
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A thread; that is, a lightweight process.
Definition: thread.h:51
A rectangular subregion within a window for rendering into.
Definition: displayRegion.h:61
This is a two-component point in space.
Definition: lpoint2.h:92
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is sent along as a parameter to most events generated for a region to indicate the mouse and but...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37
Encapsulates the data generated from (or sent into) any particular DataNode.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...