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  virtual void do_show_regions(const NodePath &render2d,
161  const std::string &bin_name, int draw_order);
162  virtual void do_hide_regions();
163 
164  static void intersect_regions(Regions &only_a,
165  Regions &only_b,
166  Regions &both,
167  const Regions &regions_a,
168  const Regions &regions_b);
169  static bool remove_region_from(Regions &regions,
170  MouseWatcherRegion *region);
171  static bool has_region_in(const Regions &regions,
172  MouseWatcherRegion *region);
173 
174  void throw_event_pattern(const std::string &pattern,
175  const MouseWatcherRegion *region,
176  const ButtonHandle &button);
177 
178  void move();
179  void press(ButtonHandle button, bool keyrepeat);
180  void release(ButtonHandle button);
181  void keystroke(int keycode);
182  void candidate(const std::wstring &candidate, size_t highlight_start,
183  size_t highlight_end, size_t cursor_pos);
184 
185  void global_keyboard_press(const MouseWatcherParameter &param);
186  void global_keyboard_release(const MouseWatcherParameter &param);
187 
188  INLINE void within_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
189  INLINE void without_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
190  void enter_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
191  void exit_region(MouseWatcherRegion *region, const MouseWatcherParameter &param);
192 
193  void set_no_mouse();
194  void set_mouse(const LVecBase2 &xy, const LVecBase2 &pixel_xy);
195 
196 private:
197  void consider_keyboard_suppress(const MouseWatcherRegion *region);
198  void discard_excess_trail_log();
199  void update_trail_node();
200 
201  bool constrain_display_region(DisplayRegion *display_region,
202  LVecBase2 &f, LVecBase2 &p,
203  Thread *current_thread);
204 
205 private:
206  // This wants to be a set, but because you cannot export sets across dlls in
207  // windows, we will make it a vector instead
208  typedef pvector< PT(MouseWatcherGroup) > Groups;
209  Groups _groups;
210 
211  bool _has_mouse;
212  int _internal_suppress;
213  int _external_suppress;
214  LPoint2 _mouse;
215  LPoint2 _mouse_pixel;
216  BitArray _current_buttons_down;
217 
218  LVecBase4 _frame;
219 
220  PT(PointerEventList) _trail_log;
221  size_t _num_trail_recent;
222  double _trail_log_duration;
223  PT(GeomNode) _trail_node;
224 
225  Regions _current_regions;
226  PT(MouseWatcherRegion) _preferred_region;
227  PT(MouseWatcherRegion) _preferred_button_down_region;
228  bool _button_down;
229 
230  bool _enter_multiple;
231  bool _implicit_click;
232 
233  std::string _button_down_pattern;
234  std::string _button_up_pattern;
235  std::string _button_repeat_pattern;
236  std::string _enter_pattern;
237  std::string _leave_pattern;
238  std::string _within_pattern;
239  std::string _without_pattern;
240 
241  PT(PandaNode) _geometry;
242 
243  EventHandler *_eh;
244  ModifierButtons _mods;
245  DisplayRegion *_display_region;
246  DisplayRegion *_button_down_display_region;
247 
248  bool _has_inactivity_timeout;
249  double _inactivity_timeout;
250  std::string _inactivity_timeout_event;
251  double _last_activity;
252 
253  enum InactivityState {
254  IS_active,
255  IS_inactive,
256  IS_active_to_inactive,
257  IS_inactive_to_active,
258  };
259  InactivityState _inactivity_state;
260 
261  NodePath _show_regions_render2d;
262  std::string _show_regions_bin_name;
263  int _show_regions_draw_order;
264 
265 protected:
266  // Inherited from DataNode
267  virtual void do_transmit_data(DataGraphTraverser *trav,
268  const DataNodeTransmit &input,
269  DataNodeTransmit &output);
270 
271 private:
272  // inputs
273  int _pixel_xy_input;
274  int _pixel_size_input;
275  int _xy_input;
276  int _button_events_input;
277  int _pointer_events_input;
278 
279  // outputs
280  int _pixel_xy_output;
281  int _pixel_size_output;
282  int _xy_output;
283  int _button_events_output;
284 
285  PT(EventStoreVec2) _pixel_xy;
286  PT(EventStoreVec2) _xy;
287  PT(EventStoreVec2) _pixel_size;
288  PT(ButtonEventList) _button_events;
289 
290 public:
291  static TypeHandle get_class_type() {
292  return _type_handle;
293  }
294  static void init_type() {
295  DataNode::init_type();
296  register_type(_type_handle, "MouseWatcher",
297  DataNode::get_class_type());
298  }
299  virtual TypeHandle get_type() const {
300  return get_class_type();
301  }
302  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
303 
304 private:
305  static TypeHandle _type_handle;
306 };
307 
308 #include "mouseWatcher.I"
309 
310 #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:27
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:65
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:40