Browse Source

tform: Separate Trackball into a base class and MouseTrackball. TouchTrackball comes next.

Donny Lawrence 6 years ago
parent
commit
b802ae6d09

+ 1 - 1
panda/src/framework/windowFramework.cxx

@@ -441,7 +441,7 @@ setup_trackball() {
     NodePath mouse = get_mouse();
     NodePath mouse = get_mouse();
     NodePath camera = get_camera_group();
     NodePath camera = get_camera_group();
 
 
-    _trackball = new Trackball("trackball");
+    _trackball = new MouseTrackball("trackball");
     _trackball->set_pos(LVector3::forward() * 50.0);
     _trackball->set_pos(LVector3::forward() * 50.0);
     mouse.attach_new_node(_trackball);
     mouse.attach_new_node(_trackball);
 
 

+ 1 - 1
panda/src/framework/windowFramework.h

@@ -20,7 +20,7 @@
 #include "graphicsOutput.h"
 #include "graphicsOutput.h"
 #include "graphicsWindow.h"
 #include "graphicsWindow.h"
 #include "animControlCollection.h"
 #include "animControlCollection.h"
-#include "trackball.h"
+#include "mouseTrackball.h"
 #include "filename.h"
 #include "filename.h"
 #include "frameRateMeter.h"
 #include "frameRateMeter.h"
 #include "sceneGraphAnalyzerMeter.h"
 #include "sceneGraphAnalyzerMeter.h"

+ 2 - 0
panda/src/tform/CMakeLists.txt

@@ -4,6 +4,7 @@ set(P3TFORM_HEADERS
   driveInterface.I driveInterface.h
   driveInterface.I driveInterface.h
   mouseInterfaceNode.I mouseInterfaceNode.h
   mouseInterfaceNode.I mouseInterfaceNode.h
   mouseSubregion.I mouseSubregion.h
   mouseSubregion.I mouseSubregion.h
+  mouseTrackball.h
   mouseWatcher.h mouseWatcher.I
   mouseWatcher.h mouseWatcher.I
   mouseWatcherBase.h mouseWatcherBase.I
   mouseWatcherBase.h mouseWatcherBase.I
   mouseWatcherGroup.h
   mouseWatcherGroup.h
@@ -19,6 +20,7 @@ set(P3TFORM_SOURCES
   driveInterface.cxx
   driveInterface.cxx
   mouseInterfaceNode.cxx
   mouseInterfaceNode.cxx
   mouseSubregion.cxx
   mouseSubregion.cxx
+  mouseTrackball.cxx
   mouseWatcher.cxx
   mouseWatcher.cxx
   mouseWatcherBase.cxx
   mouseWatcherBase.cxx
   mouseWatcherGroup.cxx
   mouseWatcherGroup.cxx

+ 1 - 0
panda/src/tform/config_tform.cxx

@@ -70,6 +70,7 @@ ConfigureFn(config_tform) {
   ButtonThrower::init_type();
   ButtonThrower::init_type();
   MouseInterfaceNode::init_type();
   MouseInterfaceNode::init_type();
   MouseSubregion::init_type();
   MouseSubregion::init_type();
+  MouseTrackball::init_type();
   MouseWatcher::init_type();
   MouseWatcher::init_type();
   MouseWatcherBase::init_type();
   MouseWatcherBase::init_type();
   MouseWatcherGroup::init_type();
   MouseWatcherGroup::init_type();

+ 172 - 0
panda/src/tform/mouseTrackball.cxx

@@ -0,0 +1,172 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file mouseTrackball.cxx
+ * @author D. Lawrence
+ * @date 2019-08-17
+ */
+
+#include "mouseTrackball.h"
+
+TypeHandle MouseTrackball::_type_handle;
+
+// These are used internally.
+#define B1_MASK 0x01
+#define B2_MASK 0x02
+#define B3_MASK 0x04
+
+MouseTrackball::
+MouseTrackball(const std::string &name) :
+  Trackball(name)
+{
+  _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
+
+  _last_button = 0;
+  _lastx = _lasty = 0.5f;
+
+  // We want to track the state of these buttons.
+  watch_button(MouseButton::two());
+  watch_button(MouseButton::three());
+
+  if (trackball_use_alt_keys) {
+    // In OSX mode, we need to use the command and option key in conjunction
+    // with the (one) mouse button.
+    watch_button(KeyboardButton::control());
+    watch_button(KeyboardButton::meta());
+    watch_button(KeyboardButton::alt());
+  }
+}
+
+/**
+ * Applies the operation indicated by the user's mouse motion to the current
+ * state.  Returns the matrix indicating the new state.
+ */
+void MouseTrackball::
+apply(double x, double y, int button) {
+  if (button && !_rel_to.is_empty()) {
+    // If we have a rel_to node, we must first adjust our rotation and
+    // translation to be in those local coordinates.
+    reextract();
+  }
+
+  if (button == B1_MASK && _control_mode != CM_default) {
+    // We have a control mode set; this may change the meaning of button 1.
+    // Remap button to match the current control mode setting.
+    switch (_control_mode) {
+    case CM_truck:
+      button = B1_MASK;
+      break;
+
+    case CM_pan:
+      button = B2_MASK;
+      break;
+
+    case CM_dolly:
+      button = B3_MASK;
+      break;
+
+    case CM_roll:
+      button = B2_MASK | B3_MASK;
+      break;
+
+    case CM_default:
+      // Not possible due to above logic.
+      nassertv(false);
+    }
+  }
+
+  if (button == B1_MASK) {
+    // Button 1: translate in plane parallel to screen.
+
+    _translation +=
+      x * _fwdscale * LVector3::right(_cs) +
+      y * _fwdscale * LVector3::down(_cs);
+
+  } else if (button == (B2_MASK | B3_MASK)) {
+    // Buttons 2 + 3: rotate about the vector perpendicular to the screen.
+
+    _rotation *=
+      LMatrix4::rotate_mat_normaxis((x - y) * _rotscale,
+                            LVector3::forward(_cs), _cs);
+
+  } else if ((button == B2_MASK) || (button == (B1_MASK | B3_MASK))) {
+    // Button 2, or buttons 1 + 3: rotate about the right and up vectors.  (We
+    // alternately define this as buttons 1 + 3, to support two-button mice.)
+
+    _rotation *=
+      LMatrix4::rotate_mat_normaxis(x * _rotscale, LVector3::up(_cs), _cs) *
+      LMatrix4::rotate_mat_normaxis(y * _rotscale, LVector3::right(_cs), _cs);
+
+  } else if ((button == B3_MASK) || (button == (B1_MASK | B2_MASK))) {
+    // Button 3, or buttons 1 + 2: dolly in and out along the forward vector.
+    // (We alternately define this as buttons 1 + 2, to support two-button
+    // mice.)
+    _translation -= y * _fwdscale * LVector3::forward(_cs);
+  }
+
+  if (button) {
+    recompute();
+  }
+}
+
+void MouseTrackball::
+do_transmit_data(DataGraphTraverser *trav, const DataNodeTransmit &input,
+                 DataNodeTransmit &output) {
+    // First, update our modifier buttons.
+  bool required_buttons_match;
+  check_button_events(input, required_buttons_match);
+
+  // Now, check for mouse motion.
+  if (required_buttons_match && input.has_data(_pixel_xy_input)) {
+    const EventStoreVec2 *pixel_xy;
+    DCAST_INTO_V(pixel_xy, input.get_data(_pixel_xy_input).get_ptr());
+    const LVecBase2 &p = pixel_xy->get_value();
+    PN_stdfloat this_x = p[0];
+    PN_stdfloat this_y = p[1];
+    int this_button = 0;
+
+    if (is_down(MouseButton::one())) {
+      if (is_down(KeyboardButton::alt())) {
+        // B1 + alt (option) = B2.
+        this_button |= B2_MASK;
+        if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
+          this_button |= B3_MASK;
+        }
+
+      } else if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
+        // B1 + meta (command) = B3.
+        this_button |= B3_MASK;
+
+      } else {
+        // Without a special key, B1 is B1.
+        this_button |= B1_MASK;
+      }
+    }
+    if (is_down(MouseButton::two())) {
+      this_button |= B2_MASK;
+    }
+    if (is_down(MouseButton::three())) {
+      this_button |= B3_MASK;
+    }
+
+    PN_stdfloat x = this_x - _lastx;
+    PN_stdfloat y = this_y - _lasty;
+
+    if (this_button == _last_button) {
+      apply(x, y, this_button);
+    }
+
+    _last_button = this_button;
+    _lastx = this_x;
+    _lasty = this_y;
+  } else {
+    _last_button = 0;
+  }
+
+  Trackball::do_transmit_data(trav, input, output);
+}

+ 56 - 0
panda/src/tform/mouseTrackball.h

@@ -0,0 +1,56 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file mouseTrackball.h
+ * @author D. Lawrence
+ * @date 2019-08-17
+ */
+
+#ifndef MOUSE_TRACKBALL_H
+#define MOUSE_TRACKBALL_H
+
+#include "trackball.h"
+
+class EXPCL_PANDA_TFORM MouseTrackball : public Trackball {
+PUBLISHED:
+  explicit MouseTrackball(const std::string &name);
+
+private:
+  int _last_button;
+  PN_stdfloat _lastx, _lasty;
+
+  // inputs
+  int _pixel_xy_input;
+
+  void apply(double x, double y, int button);
+
+protected:
+  // Inherited from DataNode
+  void do_transmit_data(DataGraphTraverser *trav,
+                        const DataNodeTransmit &input,
+                        DataNodeTransmit &output) override;
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    Trackball::init_type();
+    register_type(_type_handle, "MouseTrackball",
+                  Trackball::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#endif

+ 2 - 155
panda/src/tform/trackball.cxx

@@ -25,11 +25,6 @@
 
 
 TypeHandle Trackball::_type_handle;
 TypeHandle Trackball::_type_handle;
 
 
-// These are used internally.
-#define B1_MASK 0x01
-#define B2_MASK 0x02
-#define B3_MASK 0x04
-
 /**
 /**
  *
  *
  */
  */
@@ -37,8 +32,6 @@ Trackball::
 Trackball(const std::string &name) :
 Trackball(const std::string &name) :
   MouseInterfaceNode(name)
   MouseInterfaceNode(name)
 {
 {
-  _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
-
   _transform_output = define_output("transform", TransformState::get_class_type());
   _transform_output = define_output("transform", TransformState::get_class_type());
 
 
   _transform = TransformState::make_identity();
   _transform = TransformState::make_identity();
@@ -46,9 +39,6 @@ Trackball(const std::string &name) :
   _rotscale = 0.3;
   _rotscale = 0.3;
   _fwdscale = 0.3;
   _fwdscale = 0.3;
 
 
-  _last_button = 0;
-  _lastx = _lasty = 0.5f;
-
   _rotation = LMatrix4::ident_mat();
   _rotation = LMatrix4::ident_mat();
   _translation.set(0.0f, 0.0f, 0.0f);
   _translation.set(0.0f, 0.0f, 0.0f);
   _mat = LMatrix4::ident_mat();
   _mat = LMatrix4::ident_mat();
@@ -57,25 +47,7 @@ Trackball(const std::string &name) :
   _cs = get_default_coordinate_system();
   _cs = get_default_coordinate_system();
   _control_mode = CM_default;
   _control_mode = CM_default;
 
 
-  // We want to track the state of these buttons.
   watch_button(MouseButton::one());
   watch_button(MouseButton::one());
-  watch_button(MouseButton::two());
-  watch_button(MouseButton::three());
-
-  if (trackball_use_alt_keys) {
-    // In OSX mode, we need to use the command and option key in conjunction
-    // with the (one) mouse button.
-    watch_button(KeyboardButton::control());
-    watch_button(KeyboardButton::meta());
-    watch_button(KeyboardButton::alt());
-  }
-}
-
-/**
- *
- */
-Trackball::
-~Trackball() {
 }
 }
 
 
 /**
 /**
@@ -398,80 +370,6 @@ get_trans_mat() const {
   return _mat;
   return _mat;
 }
 }
 
 
-
-/**
- * Applies the operation indicated by the user's mouse motion to the current
- * state.  Returns the matrix indicating the new state.
- */
-void Trackball::
-apply(double x, double y, int button) {
-  if (button && !_rel_to.is_empty()) {
-    // If we have a rel_to node, we must first adjust our rotation and
-    // translation to be in those local coordinates.
-    reextract();
-  }
-
-  if (button == B1_MASK && _control_mode != CM_default) {
-    // We have a control mode set; this may change the meaning of button 1.
-    // Remap button to match the current control mode setting.
-    switch (_control_mode) {
-    case CM_truck:
-      button = B1_MASK;
-      break;
-
-    case CM_pan:
-      button = B2_MASK;
-      break;
-
-    case CM_dolly:
-      button = B3_MASK;
-      break;
-
-    case CM_roll:
-      button = B2_MASK | B3_MASK;
-      break;
-
-    case CM_default:
-      // Not possible due to above logic.
-      nassertv(false);
-    }
-  }
-
-  if (button == B1_MASK) {
-    // Button 1: translate in plane parallel to screen.
-
-    _translation +=
-      x * _fwdscale * LVector3::right(_cs) +
-      y * _fwdscale * LVector3::down(_cs);
-
-  } else if (button == (B2_MASK | B3_MASK)) {
-    // Buttons 2 + 3: rotate about the vector perpendicular to the screen.
-
-    _rotation *=
-      LMatrix4::rotate_mat_normaxis((x - y) * _rotscale,
-                            LVector3::forward(_cs), _cs);
-
-  } else if ((button == B2_MASK) || (button == (B1_MASK | B3_MASK))) {
-    // Button 2, or buttons 1 + 3: rotate about the right and up vectors.  (We
-    // alternately define this as buttons 1 + 3, to support two-button mice.)
-
-    _rotation *=
-      LMatrix4::rotate_mat_normaxis(x * _rotscale, LVector3::up(_cs), _cs) *
-      LMatrix4::rotate_mat_normaxis(y * _rotscale, LVector3::right(_cs), _cs);
-
-  } else if ((button == B3_MASK) || (button == (B1_MASK | B2_MASK))) {
-    // Button 3, or buttons 1 + 2: dolly in and out along the forward vector.
-    // (We alternately define this as buttons 1 + 2, to support two-button
-    // mice.)
-    _translation -= y * _fwdscale * LVector3::forward(_cs);
-  }
-
-  if (button) {
-    recompute();
-  }
-}
-
-
 /**
 /**
  * Given a correctly computed _orig matrix, rederive the translation and
  * Given a correctly computed _orig matrix, rederive the translation and
  * rotation elements.
  * rotation elements.
@@ -519,60 +417,9 @@ recompute() {
  * index numbers returned by the define_output() calls.
  * index numbers returned by the define_output() calls.
  */
  */
 void Trackball::
 void Trackball::
-do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input,
+do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &,
                  DataNodeTransmit &output) {
                  DataNodeTransmit &output) {
-  // First, update our modifier buttons.
-  bool required_buttons_match;
-  check_button_events(input, required_buttons_match);
-
-  // Now, check for mouse motion.
-  if (required_buttons_match && input.has_data(_pixel_xy_input)) {
-    const EventStoreVec2 *pixel_xy;
-    DCAST_INTO_V(pixel_xy, input.get_data(_pixel_xy_input).get_ptr());
-    const LVecBase2 &p = pixel_xy->get_value();
-    PN_stdfloat this_x = p[0];
-    PN_stdfloat this_y = p[1];
-    int this_button = 0;
-
-    if (is_down(MouseButton::one())) {
-      if (is_down(KeyboardButton::alt())) {
-        // B1 + alt (option) = B2.
-        this_button |= B2_MASK;
-        if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
-          this_button |= B3_MASK;
-        }
-
-      } else if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
-        // B1 + meta (command) = B3.
-        this_button |= B3_MASK;
-
-      } else {
-        // Without a special key, B1 is B1.
-        this_button |= B1_MASK;
-      }
-    }
-    if (is_down(MouseButton::two())) {
-      this_button |= B2_MASK;
-    }
-    if (is_down(MouseButton::three())) {
-      this_button |= B3_MASK;
-    }
-
-    PN_stdfloat x = this_x - _lastx;
-    PN_stdfloat y = this_y - _lasty;
-
-    if (this_button == _last_button) {
-      apply(x, y, this_button);
-    }
-
-    _last_button = this_button;
-    _lastx = this_x;
-    _lasty = this_y;
-  } else {
-    _last_button = 0;
-  }
-
-  // Now send our matrix down the pipe.
+  // Send our matrix down the pipe.
   _transform = TransformState::make_mat(_mat);
   _transform = TransformState::make_mat(_mat);
   output.set_data(_transform_output, EventParameter(_transform));
   output.set_data(_transform_output, EventParameter(_transform));
 }
 }

+ 4 - 14
panda/src/tform/trackball.h

@@ -35,7 +35,6 @@
 class EXPCL_PANDA_TFORM Trackball : public MouseInterfaceNode {
 class EXPCL_PANDA_TFORM Trackball : public MouseInterfaceNode {
 PUBLISHED:
 PUBLISHED:
   explicit Trackball(const std::string &name);
   explicit Trackball(const std::string &name);
-  ~Trackball();
 
 
   void reset();
   void reset();
 
 
@@ -101,15 +100,10 @@ PUBLISHED:
   const LMatrix4 &get_trans_mat() const;
   const LMatrix4 &get_trans_mat() const;
 
 
 
 
-private:
-  void apply(double x, double y, int button);
-
+protected:
   void reextract();
   void reextract();
   void recompute();
   void recompute();
 
 
-  int _last_button;
-  PN_stdfloat _lastx, _lasty;
-
   PN_stdfloat _rotscale;
   PN_stdfloat _rotscale;
   PN_stdfloat _fwdscale;
   PN_stdfloat _fwdscale;
 
 
@@ -121,16 +115,12 @@ private:
   CoordinateSystem _cs;
   CoordinateSystem _cs;
   ControlMode _control_mode;
   ControlMode _control_mode;
 
 
-protected:
   // Inherited from DataNode
   // Inherited from DataNode
-  virtual void do_transmit_data(DataGraphTraverser *trav,
-                                const DataNodeTransmit &input,
-                                DataNodeTransmit &output);
+  void do_transmit_data(DataGraphTraverser *trav,
+                        const DataNodeTransmit &input,
+                        DataNodeTransmit &output) override;
 
 
 private:
 private:
-  // inputs
-  int _pixel_xy_input;
-
   // outputs
   // outputs
   int _transform_output;
   int _transform_output;