Browse Source

MouseWatcher::set_frame

David Rose 17 năm trước cách đây
mục cha
commit
85e5ad2e45

+ 43 - 0
panda/src/tform/mouseWatcher.I

@@ -79,6 +79,49 @@ get_mouse_y() const {
   return _mouse[1];
   return _mouse[1];
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: MouseWatcher::set_frame
+//       Access: Published
+//  Description: Sets the frame of the MouseWatcher.  See the next
+//               flavor of this method for a more verbose explanation.
+////////////////////////////////////////////////////////////////////
+INLINE void MouseWatcher::
+set_frame(float left, float right, float bottom, float top) {
+  set_frame(LVecBase4f(left, right, bottom, top));
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: MouseWatcher::set_frame
+//       Access: Published
+//  Description: Sets the frame of the MouseWatcher.  This determines
+//               the coordinate space in which the MouseWatcherRegions
+//               should be expected to live.  Normally, this is left
+//               at -1, 1, -1, 1, which is the default setting, and
+//               matches the mouse coordinate range.
+//
+//               Whatever values you specify here indicate the shape
+//               of the full screen, and the MouseWatcherRegions will
+//               be given in coordinate space matching it.  For
+//               instance, if you specify (0, 1, 0, 1), then a
+//               MouseWatcherRegion with the frame (0, 1, 0, .5) will
+//               cover the lower half of the screen.
+////////////////////////////////////////////////////////////////////
+INLINE void MouseWatcher::
+set_frame(const LVecBase4f &frame) {
+  _frame = frame;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: MouseWatcher::get_frame
+//       Access: Published
+//  Description: Returns the frame of the MouseWatcher.  See
+//               set_frame().
+////////////////////////////////////////////////////////////////////
+INLINE const LVecBase4f &MouseWatcher::
+get_frame() const {
+  return _frame;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: MouseWatcher::is_over_region
 //     Function: MouseWatcher::is_over_region
 //       Access: Published
 //       Access: Published

+ 13 - 4
panda/src/tform/mouseWatcher.cxx

@@ -69,6 +69,8 @@ MouseWatcher(const string &name) :
   _button_down = false;
   _button_down = false;
   _eh = (EventHandler *)NULL;
   _eh = (EventHandler *)NULL;
   _display_region = (DisplayRegion *)NULL;
   _display_region = (DisplayRegion *)NULL;
+
+  _frame.set(-1.0f, 1.0f, -1.0f, 1.0f);
   
   
   _inactivity_timeout = inactivity_timeout;
   _inactivity_timeout = inactivity_timeout;
   _has_inactivity_timeout = !IS_NEARLY_ZERO(_inactivity_timeout);
   _has_inactivity_timeout = !IS_NEARLY_ZERO(_inactivity_timeout);
@@ -586,6 +588,13 @@ void MouseWatcher::
 get_over_regions(MouseWatcher::Regions &regions, const LPoint2f &pos) const {
 get_over_regions(MouseWatcher::Regions &regions, const LPoint2f &pos) const {
   nassertv(_lock.debug_is_locked());
   nassertv(_lock.debug_is_locked());
 
 
+  // Scale the mouse coordinates into the frame.
+  float mx = (pos[0] + 1.0f) * 0.5f * (_frame[1] - _frame[0]) + _frame[0];
+  float my = (pos[1] + 1.0f) * 0.5f * (_frame[3] - _frame[2]) + _frame[2];
+
+  //  pos[0] = 2.0f * (mx - _frame[0]) / (_frame[1] - _frame[0]) - 1.0f;
+  //  pos[1] = 2.0f * (my - _frame[2]) / (_frame[3] - _frame[2]) - 1.0f;
+
   // Ensure the vector is empty before we begin.
   // Ensure the vector is empty before we begin.
   regions.clear();
   regions.clear();
 
 
@@ -595,8 +604,8 @@ get_over_regions(MouseWatcher::Regions &regions, const LPoint2f &pos) const {
     const LVecBase4f &frame = region->get_frame();
     const LVecBase4f &frame = region->get_frame();
 
 
     if (region->get_active() &&
     if (region->get_active() &&
-        pos[0] >= frame[0] && pos[0] <= frame[1] &&
-        pos[1] >= frame[2] && pos[1] <= frame[3]) {
+        mx >= frame[0] && mx <= frame[1] &&
+        my >= frame[2] && my <= frame[3]) {
 
 
       regions.push_back(region);
       regions.push_back(region);
     }
     }
@@ -611,8 +620,8 @@ get_over_regions(MouseWatcher::Regions &regions, const LPoint2f &pos) const {
       const LVecBase4f &frame = region->get_frame();
       const LVecBase4f &frame = region->get_frame();
       
       
       if (region->get_active() &&
       if (region->get_active() &&
-          pos[0] >= frame[0] && pos[0] <= frame[1] &&
-          pos[1] >= frame[2] && pos[1] <= frame[3]) {
+          mx >= frame[0] && mx <= frame[1] &&
+          my >= frame[2] && my <= frame[3]) {
         
         
         regions.push_back(region);
         regions.push_back(region);
       }
       }

+ 6 - 0
panda/src/tform/mouseWatcher.h

@@ -78,6 +78,10 @@ PUBLISHED:
   INLINE float get_mouse_x() const;
   INLINE float get_mouse_x() const;
   INLINE float get_mouse_y() const;
   INLINE float get_mouse_y() const;
 
 
+  INLINE void set_frame(float left, float right, float bottom, float top);
+  INLINE void set_frame(const LVecBase4f &frame);
+  INLINE const LVecBase4f &get_frame() const;
+
   INLINE bool is_over_region() const;
   INLINE bool is_over_region() const;
   INLINE bool is_over_region(float x, float y) const;
   INLINE bool is_over_region(float x, float y) const;
   INLINE bool is_over_region(const LPoint2f &pos) const;
   INLINE bool is_over_region(const LPoint2f &pos) const;
@@ -216,6 +220,8 @@ private:
   LPoint2f _mouse_pixel;
   LPoint2f _mouse_pixel;
   BitArray _current_buttons_down;
   BitArray _current_buttons_down;
 
 
+  LVecBase4f _frame;
+
   PT(PointerEventList) _trail_log;
   PT(PointerEventList) _trail_log;
   int _num_trail_recent;
   int _num_trail_recent;
   double _trail_log_duration;
   double _trail_log_duration;