00001 // Filename: mouseWatcher.I 00002 // Created by: drose (12Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: MouseWatcher::has_mouse 00022 // Access: Published 00023 // Description: Returns true if the mouse is anywhere within the 00024 // window, false otherwise. Also see is_mouse_open(). 00025 //////////////////////////////////////////////////////////////////// 00026 INLINE bool MouseWatcher:: 00027 has_mouse() const { 00028 return _has_mouse; 00029 } 00030 00031 //////////////////////////////////////////////////////////////////// 00032 // Function: MouseWatcher::is_mouse_open 00033 // Access: Published 00034 // Description: Returns true if the mouse is within the window and 00035 // not over some particular MouseWatcherRegion that is 00036 // marked to suppress mouse events; that is, that the 00037 // mouse is in open space within the window. 00038 //////////////////////////////////////////////////////////////////// 00039 INLINE bool MouseWatcher:: 00040 is_mouse_open() const { 00041 return _has_mouse && (_suppress_flags & MouseWatcherRegion::SF_mouse_position) == 0; 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: MouseWatcher::get_mouse 00046 // Access: Published 00047 // Description: It is only valid to call this if has_mouse() returns 00048 // true. If so, this returns the current position of 00049 // the mouse within the window. 00050 //////////////////////////////////////////////////////////////////// 00051 INLINE const LPoint2f &MouseWatcher:: 00052 get_mouse() const { 00053 #ifndef NDEBUG 00054 static LPoint2f bogus_mouse(0.0f, 0.0f); 00055 nassertr(_has_mouse, bogus_mouse); 00056 #endif 00057 return _mouse; 00058 } 00059 00060 //////////////////////////////////////////////////////////////////// 00061 // Function: MouseWatcher::get_mouse_x 00062 // Access: Published 00063 // Description: It is only valid to call this if has_mouse() returns 00064 // true. If so, this returns the current X position of 00065 // the mouse within the window. 00066 //////////////////////////////////////////////////////////////////// 00067 INLINE float MouseWatcher:: 00068 get_mouse_x() const { 00069 nassertr(_has_mouse, 0.0f); 00070 return _mouse[0]; 00071 } 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Function: MouseWatcher::get_mouse_y 00075 // Access: Published 00076 // Description: It is only valid to call this if has_mouse() returns 00077 // true. If so, this returns the current Y position of 00078 // the mouse within the window. 00079 //////////////////////////////////////////////////////////////////// 00080 INLINE float MouseWatcher:: 00081 get_mouse_y() const { 00082 nassertr(_has_mouse, 0.0f); 00083 return _mouse[1]; 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: MouseWatcher::is_over_region 00088 // Access: Published 00089 // Description: Returns true if the mouse is over any rectangular 00090 // region, false otherwise. 00091 //////////////////////////////////////////////////////////////////// 00092 INLINE bool MouseWatcher:: 00093 is_over_region() const { 00094 return get_over_region() != (MouseWatcherRegion *)NULL; 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: MouseWatcher::is_over_region 00099 // Access: Published 00100 // Description: Returns true if the mouse is over any rectangular 00101 // region, false otherwise. 00102 //////////////////////////////////////////////////////////////////// 00103 INLINE bool MouseWatcher:: 00104 is_over_region(float x, float y) const { 00105 return get_over_region(x, y) != (MouseWatcherRegion *)NULL; 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: MouseWatcher::is_over_region 00110 // Access: Published 00111 // Description: Returns true if the mouse is over any rectangular 00112 // region, false otherwise. 00113 //////////////////////////////////////////////////////////////////// 00114 INLINE bool MouseWatcher:: 00115 is_over_region(const LPoint2f &pos) const { 00116 return get_over_region(pos) != (MouseWatcherRegion *)NULL; 00117 } 00118 00119 //////////////////////////////////////////////////////////////////// 00120 // Function: MouseWatcher::get_over_region 00121 // Access: Published 00122 // Description: Returns the smallest region the mouse is currently 00123 // over, or NULL if it is over no region. 00124 //////////////////////////////////////////////////////////////////// 00125 INLINE MouseWatcherRegion *MouseWatcher:: 00126 get_over_region() const { 00127 return _preferred_region; 00128 } 00129 00130 //////////////////////////////////////////////////////////////////// 00131 // Function: MouseWatcher::get_over_region 00132 // Access: Published 00133 // Description: Returns the smallest region the indicated point is 00134 // over, or NULL if it is over no region. 00135 //////////////////////////////////////////////////////////////////// 00136 INLINE MouseWatcherRegion *MouseWatcher:: 00137 get_over_region(float x, float y) const { 00138 return get_over_region(LPoint2f(x, y)); 00139 } 00140 00141 //////////////////////////////////////////////////////////////////// 00142 // Function: MouseWatcher::set_button_down_pattern 00143 // Access: Published 00144 // Description: Sets the pattern string that indicates how the event 00145 // names are generated when a button is depressed. This 00146 // is a string that may contain any of the following: 00147 // 00148 // %r - the name of the region the mouse is over 00149 // %b - the name of the button pressed. 00150 // 00151 // The event name will be based on the in_pattern 00152 // string specified here, with all occurrences of the 00153 // above strings replaced with the corresponding values. 00154 //////////////////////////////////////////////////////////////////// 00155 INLINE void MouseWatcher:: 00156 set_button_down_pattern(const string &pattern) { 00157 _button_down_pattern = pattern; 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: MouseWatcher::get_button_down_pattern 00162 // Access: Published 00163 // Description: Returns the string that indicates how event names are 00164 // generated when a button is depressed. See 00165 // set_button_down_pattern(). 00166 //////////////////////////////////////////////////////////////////// 00167 INLINE const string &MouseWatcher:: 00168 get_button_down_pattern() const { 00169 return _button_down_pattern; 00170 } 00171 00172 //////////////////////////////////////////////////////////////////// 00173 // Function: MouseWatcher::set_button_up_pattern 00174 // Access: Published 00175 // Description: Sets the pattern string that indicates how the event 00176 // names are generated when a button is released. See 00177 // set_button_down_pattern(). 00178 //////////////////////////////////////////////////////////////////// 00179 INLINE void MouseWatcher:: 00180 set_button_up_pattern(const string &pattern) { 00181 _button_up_pattern = pattern; 00182 } 00183 00184 //////////////////////////////////////////////////////////////////// 00185 // Function: MouseWatcher::get_button_up_pattern 00186 // Access: Published 00187 // Description: Returns the string that indicates how event names are 00188 // generated when a button is released. See 00189 // set_button_down_pattern(). 00190 //////////////////////////////////////////////////////////////////// 00191 INLINE const string &MouseWatcher:: 00192 get_button_up_pattern() const { 00193 return _button_up_pattern; 00194 } 00195 00196 //////////////////////////////////////////////////////////////////// 00197 // Function: MouseWatcher::set_enter_pattern 00198 // Access: Published 00199 // Description: Sets the pattern string that indicates how the event 00200 // names are generated when the mouse enters a region. 00201 // This is different from within_pattern, in that a 00202 // mouse is only "entered" in the topmost region at a 00203 // given time, while it might be "within" multiple 00204 // nested regions. 00205 //////////////////////////////////////////////////////////////////// 00206 INLINE void MouseWatcher:: 00207 set_enter_pattern(const string &pattern) { 00208 _enter_pattern = pattern; 00209 } 00210 00211 //////////////////////////////////////////////////////////////////// 00212 // Function: MouseWatcher::get_enter_pattern 00213 // Access: Published 00214 // Description: Returns the string that indicates how event names are 00215 // generated when the mouse enters a region. This is 00216 // different from within_pattern, in that a mouse is 00217 // only "entered" in the topmost region at a given time, 00218 // while it might be "within" multiple nested regions. 00219 //////////////////////////////////////////////////////////////////// 00220 INLINE const string &MouseWatcher:: 00221 get_enter_pattern() const { 00222 return _enter_pattern; 00223 } 00224 00225 //////////////////////////////////////////////////////////////////// 00226 // Function: MouseWatcher::set_leave_pattern 00227 // Access: Published 00228 // Description: Sets the pattern string that indicates how the event 00229 // names are generated when the mouse leaves a region. 00230 // This is different from without_pattern, in that a 00231 // mouse is only "entered" in the topmost region at a 00232 // given time, while it might be "within" multiple 00233 // nested regions. 00234 //////////////////////////////////////////////////////////////////// 00235 INLINE void MouseWatcher:: 00236 set_leave_pattern(const string &pattern) { 00237 _leave_pattern = pattern; 00238 } 00239 00240 //////////////////////////////////////////////////////////////////// 00241 // Function: MouseWatcher::get_leave_pattern 00242 // Access: Published 00243 // Description: Returns the string that indicates how event names are 00244 // generated when the mouse leaves a region. This is 00245 // different from without_pattern, in that a mouse is 00246 // only "entered" in the topmost region at a given time, 00247 // while it might be "within" multiple nested regions. 00248 //////////////////////////////////////////////////////////////////// 00249 INLINE const string &MouseWatcher:: 00250 get_leave_pattern() const { 00251 return _leave_pattern; 00252 } 00253 00254 //////////////////////////////////////////////////////////////////// 00255 // Function: MouseWatcher::set_within_pattern 00256 // Access: Published 00257 // Description: Sets the pattern string that indicates how the event 00258 // names are generated when the mouse wanders over a 00259 // region. This is different from enter_pattern, in 00260 // that a mouse is only "entered" in the topmost region 00261 // at a given time, while it might be "within" multiple 00262 // nested regions. 00263 //////////////////////////////////////////////////////////////////// 00264 INLINE void MouseWatcher:: 00265 set_within_pattern(const string &pattern) { 00266 _within_pattern = pattern; 00267 } 00268 00269 //////////////////////////////////////////////////////////////////// 00270 // Function: MouseWatcher::get_within_pattern 00271 // Access: Published 00272 // Description: Returns the string that indicates how event names are 00273 // generated when the mouse wanders over a region. This 00274 // is different from enter_pattern, in that a mouse is 00275 // only "entered" in the topmost region at a given time, 00276 // while it might be "within" multiple nested regions. 00277 //////////////////////////////////////////////////////////////////// 00278 INLINE const string &MouseWatcher:: 00279 get_within_pattern() const { 00280 return _within_pattern; 00281 } 00282 00283 //////////////////////////////////////////////////////////////////// 00284 // Function: MouseWatcher::set_without_pattern 00285 // Access: Published 00286 // Description: Sets the pattern string that indicates how the event 00287 // names are generated when the mouse wanders out of a 00288 // region. This is different from leave_pattern, in 00289 // that a mouse is only "entered" in the topmost region 00290 // at a given time, while it might be "within" multiple 00291 // nested regions. 00292 //////////////////////////////////////////////////////////////////// 00293 INLINE void MouseWatcher:: 00294 set_without_pattern(const string &pattern) { 00295 _without_pattern = pattern; 00296 } 00297 00298 //////////////////////////////////////////////////////////////////// 00299 // Function: MouseWatcher::get_without_pattern 00300 // Access: Published 00301 // Description: Returns the string that indicates how event names are 00302 // generated when the mouse wanders out of a region. 00303 // This is different from leave_pattern, in that a mouse 00304 // is only "entered" in the topmost region at a given 00305 // time, while it might be "within" multiple nested 00306 // regions. 00307 //////////////////////////////////////////////////////////////////// 00308 INLINE const string &MouseWatcher:: 00309 get_without_pattern() const { 00310 return _without_pattern; 00311 } 00312 00313 //////////////////////////////////////////////////////////////////// 00314 // Function: MouseWatcher::set_geometry 00315 // Access: Published 00316 // Description: Sets the node that will be transformed each frame by 00317 // the mouse's coordinates. It will also be hidden when 00318 // the mouse goes outside the window. This can be used 00319 // to implement a software mouse pointer for when a 00320 // hardware (or system) mouse pointer is unavailable. 00321 //////////////////////////////////////////////////////////////////// 00322 INLINE void MouseWatcher:: 00323 set_geometry(PandaNode *node) { 00324 _geometry = node; 00325 } 00326 00327 //////////////////////////////////////////////////////////////////// 00328 // Function: MouseWatcher::has_geometry 00329 // Access: Published 00330 // Description: Returns true if a software mouse pointer has been 00331 // setup via set_geometry(), or false otherwise. See 00332 // set_geometry(). 00333 //////////////////////////////////////////////////////////////////// 00334 INLINE bool MouseWatcher:: 00335 has_geometry() const { 00336 return !_geometry.is_null(); 00337 } 00338 00339 //////////////////////////////////////////////////////////////////// 00340 // Function: MouseWatcher::get_geometry 00341 // Access: Published 00342 // Description: Returns the node that has been set as the software 00343 // mouse pointer, or NULL if no node has been set. See 00344 // has_geometry() and set_geometry(). 00345 //////////////////////////////////////////////////////////////////// 00346 INLINE PandaNode *MouseWatcher:: 00347 get_geometry() const { 00348 return _geometry; 00349 } 00350 00351 //////////////////////////////////////////////////////////////////// 00352 // Function: MouseWatcher::clear_geometry 00353 // Access: Published 00354 // Description: Stops the use of the software cursor set up via 00355 // set_geometry(). 00356 //////////////////////////////////////////////////////////////////// 00357 INLINE void MouseWatcher:: 00358 clear_geometry() { 00359 _geometry.clear(); 00360 } 00361 00362 //////////////////////////////////////////////////////////////////// 00363 // Function: MouseWatcher::set_extra_handler 00364 // Access: Published 00365 // Description: As an optimization for the C++ Gui, an extra handler 00366 // can be registered with a mouseWatcher so that events 00367 // can be dealt with much sooner. 00368 //////////////////////////////////////////////////////////////////// 00369 INLINE void MouseWatcher:: 00370 set_extra_handler(EventHandler *eh) { 00371 _eh = eh; 00372 } 00373 00374 //////////////////////////////////////////////////////////////////// 00375 // Function: MouseWatcher::get_extra_handler 00376 // Access: Published 00377 // Description: As an optimization for the C++ Gui, an extra handler 00378 // can be registered with a mouseWatcher so that events 00379 // can be dealt with much sooner. 00380 //////////////////////////////////////////////////////////////////// 00381 INLINE EventHandler *MouseWatcher:: 00382 get_extra_handler(void) const { 00383 return _eh; 00384 } 00385 00386 //////////////////////////////////////////////////////////////////// 00387 // Function: MouseWatcher::set_modifier_buttons 00388 // Access: Public 00389 // Description: Sets the buttons that should be monitored as modifier 00390 // buttons for generating events to the 00391 // MouseWatcherRegions. 00392 //////////////////////////////////////////////////////////////////// 00393 INLINE void MouseWatcher:: 00394 set_modifier_buttons(const ModifierButtons &mods) { 00395 _mods = mods; 00396 } 00397 00398 //////////////////////////////////////////////////////////////////// 00399 // Function: MouseWatcher::get_modifier_buttons 00400 // Access: Published 00401 // Description: Returns the set of buttons that are being monitored 00402 // as modifier buttons, as well as their current state. 00403 //////////////////////////////////////////////////////////////////// 00404 INLINE ModifierButtons MouseWatcher:: 00405 get_modifier_buttons() const { 00406 return _mods; 00407 } 00408 00409 //////////////////////////////////////////////////////////////////// 00410 // Function: MouseWatcher::within_region 00411 // Access: Protected 00412 // Description: Called internally to indicate the mouse pointer has 00413 // moved within the indicated region's boundaries. 00414 //////////////////////////////////////////////////////////////////// 00415 INLINE void MouseWatcher:: 00416 within_region(MouseWatcherRegion *region, const MouseWatcherParameter ¶m) { 00417 region->within(param); 00418 throw_event_pattern(_within_pattern, region, ButtonHandle::none()); 00419 if (_enter_multiple) { 00420 enter_region(region, param); 00421 } 00422 } 00423 00424 //////////////////////////////////////////////////////////////////// 00425 // Function: MouseWatcher::without_region 00426 // Access: Protected 00427 // Description: Called internally to indicate the mouse pointer has 00428 // moved outside of the indicated region's boundaries. 00429 //////////////////////////////////////////////////////////////////// 00430 INLINE void MouseWatcher:: 00431 without_region(MouseWatcherRegion *region, const MouseWatcherParameter ¶m) { 00432 if (_enter_multiple) { 00433 exit_region(region, param); 00434 } 00435 region->without(param); 00436 throw_event_pattern(_without_pattern, region, ButtonHandle::none()); 00437 }