فهرست منبع

fixed key handling bug, legacy key_down/key_up left in place

Former-commit-id: 349a6e2f2507076b5adf9a254631626412902d52
Alec Jacobson 10 سال پیش
والد
کامیت
093ff3be7e
5فایلهای تغییر یافته به همراه73 افزوده شده و 27 حذف شده
  1. 49 22
      include/igl/viewer/Viewer.cpp
  2. 5 1
      include/igl/viewer/Viewer.h
  3. 2 2
      include/igl/viewer/ViewerCore.cpp
  4. 7 1
      include/igl/viewer/ViewerCore.h
  5. 10 1
      include/igl/viewer/ViewerPlugin.h

+ 49 - 22
include/igl/viewer/Viewer.cpp

@@ -62,6 +62,7 @@
 #include <igl/quat_mult.h>
 #include <igl/quat_mult.h>
 #include <igl/axis_angle_to_quat.h>
 #include <igl/axis_angle_to_quat.h>
 #include <igl/trackball.h>
 #include <igl/trackball.h>
+#include <igl/two_axis_valuator_fixed_up.h>
 #include <igl/snap_to_canonical_view_quat.h>
 #include <igl/snap_to_canonical_view_quat.h>
 #include <igl/unproject.h>
 #include <igl/unproject.h>
 
 
@@ -108,6 +109,16 @@ static void glfw_error_callback(int error, const char* description)
   fputs(description, stderr);
   fputs(description, stderr);
 }
 }
 
 
+static void glfw_char_mods_callback(GLFWwindow* window, unsigned int codepoint, int modifier)
+{
+  // TODO: pass to nanogui (although it's also using physical key down/up
+  // rather than character codes...
+  if(! __viewer->ngui->charEvent(window,codepoint) )
+  {
+    __viewer->key_pressed(codepoint, modifier);
+  }
+}
+
 static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
 static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
 {
 {
   if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
   if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
@@ -150,11 +161,6 @@ static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
     __viewer->mouse_scroll(y);
     __viewer->mouse_scroll(y);
 }
 }
 
 
-static void glfw_char_callback(GLFWwindow* window, unsigned int c)
-{
-  __viewer->ngui->charEvent(window,c);
-}
-
 static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
 static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
 {
 {
   __viewer->ngui->dropEvent(window,count,filenames);
   __viewer->ngui->dropEvent(window,count,filenames);
@@ -410,18 +416,26 @@ namespace viewer
     return true;
     return true;
   }
   }
 
 
+  IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
+  {
+    if (callback_key_pressed)
+      if (callback_key_pressed(*this,unicode_key,modifiers))
+        return true;
+
+    for (unsigned int i = 0; i<plugins.size(); ++i)
+      if (plugins[i]->key_pressed(unicode_key, modifiers))
+        return true;
+    return false;
+  }
+
   IGL_INLINE bool Viewer::key_down(int key,int modifiers)
   IGL_INLINE bool Viewer::key_down(int key,int modifiers)
   {
   {
     if (callback_key_down)
     if (callback_key_down)
       if (callback_key_down(*this,key,modifiers))
       if (callback_key_down(*this,key,modifiers))
         return true;
         return true;
-
     for (unsigned int i = 0; i<plugins.size(); ++i)
     for (unsigned int i = 0; i<plugins.size(); ++i)
       if (plugins[i]->key_down(key, modifiers))
       if (plugins[i]->key_down(key, modifiers))
         return true;
         return true;
-
-    char k = key;
-
     return false;
     return false;
   }
   }
 
 
@@ -526,17 +540,30 @@ namespace viewer
     {
     {
       switch (mouse_mode)
       switch (mouse_mode)
       {
       {
-        case MouseMode::Rotation :
+        case MouseMode::Rotation:
         {
         {
-          igl::trackball(core.viewport(2),
-                         core.viewport(3),
-                         2.0f,
-                         down_rotation.data(),
-                         down_mouse_x,
-                         down_mouse_y,
-                         mouse_x,
-                         mouse_y,
-                         core.trackball_angle.data());
+          switch(core.rotation_type)
+          {
+            case ViewerCore::ROTATION_TYPE_TRACKBALL:
+              igl::trackball(core.viewport(2),
+                             core.viewport(3),
+                             2.0f,
+                             down_rotation,
+                             down_mouse_x,
+                             down_mouse_y,
+                             mouse_x,
+                             mouse_y,
+                             core.trackball_angle);
+              break;
+            case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
+              igl::two_axis_valuator_fixed_up(
+                  core.viewport(2),core.viewport(3),
+                  2.0,
+                  down_rotation,
+                  down_mouse_x, down_mouse_y, mouse_x, mouse_y,
+                  core.trackball_angle);
+              break;
+          }
           //Eigen::Vector4f snapq = core.trackball_angle;
           //Eigen::Vector4f snapq = core.trackball_angle;
 
 
           break;
           break;
@@ -673,8 +700,8 @@ namespace viewer
 
 
   IGL_INLINE void Viewer::snap_to_canonical_quaternion()
   IGL_INLINE void Viewer::snap_to_canonical_quaternion()
   {
   {
-    Eigen::Vector4f snapq = this->core.trackball_angle;
-    igl::snap_to_canonical_view_quat(snapq.data(),1.0f,this->core.trackball_angle.data());
+    Eigen::Quaternionf snapq = this->core.trackball_angle;
+    igl::snap_to_canonical_view_quat(snapq,1.0f,this->core.trackball_angle);
   }
   }
 
 
   IGL_INLINE void Viewer::open_dialog_load_mesh()
   IGL_INLINE void Viewer::open_dialog_load_mesh()
@@ -769,7 +796,7 @@ namespace viewer
     glfwSetWindowSizeCallback(window,glfw_window_size);
     glfwSetWindowSizeCallback(window,glfw_window_size);
     glfwSetMouseButtonCallback(window,glfw_mouse_press);
     glfwSetMouseButtonCallback(window,glfw_mouse_press);
     glfwSetScrollCallback(window,glfw_mouse_scroll);
     glfwSetScrollCallback(window,glfw_mouse_scroll);
-    glfwSetCharCallback(window,glfw_char_callback);
+    glfwSetCharModsCallback(window,glfw_char_mods_callback);
     glfwSetDropCallback(window,glfw_drop_callback);
     glfwSetDropCallback(window,glfw_drop_callback);
 
 
     // Handle retina displays (windows and mac)
     // Handle retina displays (windows and mac)

+ 5 - 1
include/igl/viewer/Viewer.h

@@ -61,7 +61,7 @@ namespace viewer
     IGL_INLINE void shutdown_plugins();
     IGL_INLINE void shutdown_plugins();
 
 
     // Temporary data stored when the mouse button is pressed
     // Temporary data stored when the mouse button is pressed
-    Eigen::Vector4f down_rotation;
+    Eigen::Quaternionf down_rotation;
     int current_mouse_x;
     int current_mouse_x;
     int current_mouse_y;
     int current_mouse_y;
     int down_mouse_x;
     int down_mouse_x;
@@ -88,6 +88,7 @@ namespace viewer
     IGL_INLINE bool save_mesh_to_file(const char* mesh_file_name);
     IGL_INLINE bool save_mesh_to_file(const char* mesh_file_name);
 
 
     // Callbacks
     // Callbacks
+    IGL_INLINE bool key_pressed(unsigned int unicode_key,int modifier);
     IGL_INLINE bool key_down(int key,int modifier);
     IGL_INLINE bool key_down(int key,int modifier);
     IGL_INLINE bool key_up(int key,int modifier);
     IGL_INLINE bool key_up(int key,int modifier);
 
 
@@ -121,6 +122,8 @@ namespace viewer
     std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
     std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
     std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
     std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
     std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
     std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
+    std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
+    // THESE SHOULD BE DEPRECATED:
     std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
     std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
     std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
     std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
 
 
@@ -132,6 +135,7 @@ namespace viewer
     void* callback_mouse_up_data;
     void* callback_mouse_up_data;
     void* callback_mouse_move_data;
     void* callback_mouse_move_data;
     void* callback_mouse_scroll_data;
     void* callback_mouse_scroll_data;
+    void* callback_key_pressed_data;
     void* callback_key_down_data;
     void* callback_key_down_data;
     void* callback_key_up_data;
     void* callback_key_up_data;
 
 

+ 2 - 2
include/igl/viewer/ViewerCore.cpp

@@ -184,7 +184,7 @@ IGL_INLINE void igl::viewer::ViewerCore::draw(ViewerData& data, OpenGL_state& op
 
 
     // Set model transformation
     // Set model transformation
     float mat[16];
     float mat[16];
-    igl::quat_to_mat(trackball_angle.data(), mat);
+    igl::quat_to_mat(trackball_angle.coeffs().data(), mat);
 
 
     for (unsigned i=0;i<4;++i)
     for (unsigned i=0;i<4;++i)
       for (unsigned j=0;j<4;++j)
       for (unsigned j=0;j<4;++j)
@@ -417,7 +417,7 @@ IGL_INLINE igl::viewer::ViewerCore::ViewerCore()
   lighting_factor = 1.0f; //on
   lighting_factor = 1.0f; //on
 
 
   // Default trackball
   // Default trackball
-  trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
+  trackball_angle = Eigen::Quaternionf::Identity();
 
 
   // Defalut model viewing parameters
   // Defalut model viewing parameters
   model_zoom = 1.0f;
   model_zoom = 1.0f;

+ 7 - 1
include/igl/viewer/ViewerCore.h

@@ -84,7 +84,13 @@ public:
   float lighting_factor;
   float lighting_factor;
 
 
   // Trackball angle (quaternion)
   // Trackball angle (quaternion)
-  Eigen::Vector4f trackball_angle;
+  enum RotationType
+  {
+    ROTATION_TYPE_TRACKBALL = 0,
+    ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
+    NUM_ROTATION_TYPES = 2
+  } rotation_type;
+  Eigen::Quaternionf trackball_angle;
 
 
   // Model viewing parameters
   // Model viewing parameters
   float model_zoom;
   float model_zoom;

+ 10 - 1
include/igl/viewer/ViewerPlugin.h

@@ -126,7 +126,16 @@ public:
     return false;
     return false;
   }
   }
 
 
-  // This function is called when a keyboard key is pressed
+  // This function is called when a keyboard key is pressed. Unlike key_down
+  // this will reveal the actual character being sent (not just the physical
+  // key)
+  // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
+  IGL_INLINE virtual bool key_pressed(unsigned int key, int modifiers)
+  {
+    return false;
+  }
+
+  // This function is called when a keyboard key is down
   // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
   // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
   IGL_INLINE virtual bool key_down(int key, int modifiers)
   IGL_INLINE virtual bool key_down(int key, int modifiers)
   {
   {