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