Skip to content
Permalink
Browse files

moved handling of modifiers keys and mouse button states to Window cl…

…ass. Made some of its members private for cleaner code
  • Loading branch information...
mbotsch committed Mar 20, 2019
1 parent 4534251 commit 10d93fcf939869ea4732dff19d671831e78a5264
@@ -275,7 +275,7 @@ void MeshProcessingViewer::mouse(int button, int action, int mods)
{
if (action == GLFW_PRESS &&
button == GLFW_MOUSE_BUTTON_MIDDLE &&
modifiers_ == GLFW_MOD_SHIFT)
shift_pressed())
{
double x, y;
cursor_pos(x, y);
@@ -72,7 +72,7 @@ void Viewer::process_imgui()
void Viewer::draw(const std::string& draw_mode)
{
// normal mesh draw
glViewport(0, 0, width_, height_);
glViewport(0, 0, width(), height());
mesh_.draw(projection_matrix_, modelview_matrix_, draw_mode);

// draw uv layout
@@ -81,8 +81,8 @@ void Viewer::draw(const std::string& draw_mode)
glClear(GL_DEPTH_BUFFER_BIT);

// setup viewport
GLint size = std::min(width_, height_) / 4;
glViewport(width_ - size - 1, height_ - size - 1, size, size);
GLint size = std::min(width(), height()) / 4;
glViewport(width() - size - 1, height() - size - 1, size, size);

// setup matrices
mat4 P = ortho_matrix(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
@@ -93,7 +93,7 @@ void Viewer::draw(const std::string& draw_mode)
}

// reset viewport
glViewport(0, 0, width_, height_);
glViewport(0, 0, width(), height());
}

//=============================================================================
@@ -20,10 +20,6 @@ TrackballViewer::TrackballViewer(const char* title, int width, int height,
bool showgui)
: Window(title, width, height, showgui)
{
// init mouse buttons
for (bool& i : button_down_)
i = false;

// define basic draw modes
add_draw_mode("Wireframe");
add_draw_mode("Solid Flat");
@@ -127,9 +123,6 @@ void TrackballViewer::keyboard(int key, int code, int action, int mods)

void TrackballViewer::resize(int width, int height)
{
width_ = width;
height_ = height;

glViewport(0, 0, width, height);
}

@@ -152,7 +145,7 @@ void TrackballViewer::display()

// update projection matrix
projection_matrix_ =
perspective_matrix(fovy_, (float)width_ / (float)height_, near_, far_);
perspective_matrix(fovy_, (float)width() / (float)height(), near_, far_);

// draw the scene in current draw mode
if (draw_mode_ < draw_mode_names_.size())
@@ -163,19 +156,15 @@ void TrackballViewer::display()

//-----------------------------------------------------------------------------

void TrackballViewer::mouse(int button, int action, int mods)
void TrackballViewer::mouse(int /*button*/, int action, int /*mods*/)
{
// record current modifier keys
modifiers_ = mods;

// mouse press
if (action == GLFW_PRESS)
{
last_point_ok_ = map_to_sphere(last_point_2d_, last_point_3d_);
button_down_[button] = true;

// set rotation center
if (modifiers_ == GLFW_MOD_CONTROL)
if (ctrl_pressed())
{
double x, y;
cursor_pos(x, y);
@@ -187,7 +176,6 @@ void TrackballViewer::mouse(int button, int action, int mods)
else
{
last_point_ok_ = false;
button_down_[button] = false;
}
}

@@ -207,23 +195,19 @@ void TrackballViewer::scroll(double /*xoffset*/, double yoffset)
void TrackballViewer::motion(double xpos, double ypos)
{
// zoom
if ((button_down_[GLFW_MOUSE_BUTTON_RIGHT]) ||
(button_down_[GLFW_MOUSE_BUTTON_LEFT] &&
(modifiers_ == GLFW_MOD_SHIFT)))
if (right_mouse_pressed() || (left_mouse_pressed() && shift_pressed()))
{
zoom(xpos, ypos);
}

// translation
else if (button_down_[GLFW_MOUSE_BUTTON_MIDDLE] ||
(button_down_[GLFW_MOUSE_BUTTON_LEFT] &&
(modifiers_ == GLFW_MOD_ALT)))
else if (middle_mouse_pressed() || (left_mouse_pressed() && alt_pressed()))
{
translation(xpos, ypos);
}

// rotation
else if (button_down_[GLFW_MOUSE_BUTTON_LEFT])
else if (left_mouse_pressed())
{
rotation(xpos, ypos);
}
@@ -273,6 +257,13 @@ void TrackballViewer::view_all()

//-----------------------------------------------------------------------------

bool TrackballViewer::pick(vec3& result)
{
double x, y;
cursor_pos(x,y);
return pick(x, y, result);
}

bool TrackballViewer::pick(int x, int y, vec3& result)
{
#ifndef __EMSCRIPTEN__ // WebGL cannot read depth buffer
@@ -282,8 +273,8 @@ bool TrackballViewer::pick(int x, int y, vec3& result)
glGetIntegerv(GL_VIEWPORT, viewport);

// take into accout highDPI scaling
x *= scaling_;
y *= scaling_;
x *= high_dpi_scaling();
y *= high_dpi_scaling();

// in OpenGL y=0 is at the 'bottom'
y = viewport[3] - y;
@@ -335,11 +326,13 @@ void TrackballViewer::fly_to(int x, int y)

bool TrackballViewer::map_to_sphere(const ivec2& point2D, vec3& result)
{
if ((point2D[0] >= 0) && (point2D[0] <= width_) && (point2D[1] >= 0) &&
(point2D[1] <= height_))
if ((point2D[0] >= 0) && (point2D[0] <= width()) && (point2D[1] >= 0) &&
(point2D[1] <= height()))
{
double x = (double)(point2D[0] - 0.5 * width_) / (double)width_;
double y = (double)(0.5 * height_ - point2D[1]) / (double)height_;
double w = width();
double h = height();
double x = (double)(point2D[0] - 0.5*w) / w;
double y = (double)(0.5*h - point2D[1]) / h;
double sinx = sin(M_PI * x * 0.5);
double siny = sin(M_PI * y * 0.5);
double sinx2siny2 = sinx * sinx + siny * siny;
@@ -392,20 +385,20 @@ void TrackballViewer::translation(int x, int y)
vec4 ec = modelview_matrix_ * mc;
float z = -(ec[2] / ec[3]);

float aspect = (float)width_ / (float)height_;
float aspect = (float)width() / (float)height();
float up = tan(fovy_ / 2.0f * M_PI / 180.f) * near_;
float right = aspect * up;

translate(vec3(2.0 * dx / width_ * right / near_ * z,
-2.0 * dy / height_ * up / near_ * z, 0.0f));
translate(vec3(2.0 * dx / width() * right / near_ * z,
-2.0 * dy / height() * up / near_ * z, 0.0f));
}

//-----------------------------------------------------------------------------

void TrackballViewer::zoom(int, int y)
{
float dy = y - last_point_2d_[1];
float h = height_;
float h = height();
translate(vec3(0.0, 0.0, radius_ * dy * 3.0 / h));
}

@@ -90,7 +90,10 @@ class TrackballViewer : public Window
//! turn a mouse event into a zoom, i.e., translation in z-direction. calls translate().
void zoom(int x, int y);

//! get 3D position Distributed under the mouse cursor
//! get 3D position under the mouse cursor
bool pick(vec3& result);

//! get 3D position of 2D position (x,y)
bool pick(int x, int y, vec3& result);

//! fly toward the position Distributed under the mouse cursor and set rotation center to it
@@ -126,8 +129,6 @@ class TrackballViewer : public Window
ivec2 last_point_2d_;
vec3 last_point_3d_;
bool last_point_ok_;
bool button_down_[7];
int modifiers_;
};

//=============================================================================
@@ -118,6 +118,12 @@ Window::Window(const char* title, int width, int height, bool showgui)
#ifndef __EMSCRIPTEN__
add_help_item("Esc/Q", "Quit application");
#endif


// init mouse button state and modifiers
for (bool& i : button_)
i = false;
ctrl_pressed_ = shift_pressed_ = alt_pressed_ = false;
}

//-----------------------------------------------------------------------------
@@ -265,6 +271,13 @@ void Window::add_help_item(std::string key, std::string description, int pos)

//-----------------------------------------------------------------------------

void Window::clear_help_items()
{
help_items_.clear();
}

//-----------------------------------------------------------------------------

void Window::show_help()
{
if (!show_help_) return;
@@ -427,6 +440,24 @@ void Window::glfw_keyboard(GLFWwindow* window, int key, int scancode,
ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);
if (!ImGui::GetIO().WantCaptureKeyboard)
{
// remember modifier status
switch(key)
{
case GLFW_KEY_LEFT_CONTROL:
case GLFW_KEY_RIGHT_CONTROL:
instance_->ctrl_pressed_ = (action != GLFW_RELEASE);
break;
case GLFW_KEY_LEFT_SHIFT:
case GLFW_KEY_RIGHT_SHIFT:
instance_->shift_pressed_ = (action != GLFW_RELEASE);
break;
case GLFW_KEY_LEFT_ALT:
case GLFW_KEY_RIGHT_ALT:
instance_->alt_pressed_ = (action != GLFW_RELEASE);
break;
}

// send event to window
instance_->keyboard(key, scancode, action, mods);
}
}
@@ -495,6 +526,7 @@ void Window::glfw_mouse(GLFWwindow* window, int button, int action, int mods)
ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods);
if (!ImGui::GetIO().WantCaptureMouse)
{
instance_->button_[button] = (action==GLFW_PRESS);
instance_->mouse(button, action, mods);
}
}
@@ -82,10 +82,9 @@ class Window
//! this function is called just before rendering
virtual void do_processing() {}

//! get position of mouse cursor
void cursor_pos(double& x, double& y) const;

protected:

//! setup ImGUI user interface
void init_imgui();

@@ -98,13 +97,44 @@ class Window
//! show or hide ImGUI
void show_imgui(bool b) { show_imgui_ = b; }

//! clear help items
void clear_help_items();

//! add key binding (or general action description)
void add_help_item(std::string key, std::string description, int position=-1);

//! show ImGUI help dialog
void show_help();

protected:
protected: //------------------------------------------ GLFW related functions

//! width of window
int width() const { return width_; }
//! height of window
int height() const { return height_; }

//! highDPI scaling
float high_dpi_scaling() const { return scaling_; }

//! get position of mouse cursor
void cursor_pos(double& x, double& y) const;

//! is left mouse button pressed down?
bool left_mouse_pressed() const { return button_[GLFW_MOUSE_BUTTON_LEFT]; }
//! is right mouse button pressed down?
bool right_mouse_pressed() const { return button_[GLFW_MOUSE_BUTTON_RIGHT]; }
//! is middle mouse button pressed down?
bool middle_mouse_pressed() const { return button_[GLFW_MOUSE_BUTTON_MIDDLE]; }

//! is CTRL modifier key pressed down?
bool ctrl_pressed() const { return ctrl_pressed_; }
//! is ALT modifier key pressed down?
bool alt_pressed() const { return alt_pressed_; }
//! is SHIFT modifier key pressed down?
bool shift_pressed() const { return shift_pressed_; }

private:

//! GLFW window pointer
GLFWwindow* window_;

@@ -114,10 +144,17 @@ class Window
//! highDPI scaling
float scaling_, pixel_ratio_;

// whether to show ImGUI menu
bool show_imgui_;
// scale ImGUI menu
float imgui_scale_;
// show ImGUI help dialog
bool show_help_;
// items for ImGUI help dialog
std::vector< std::pair<std::string, std::string> > help_items_;

// which mouse buttons and modifier keys are pressed down
bool button_[7], ctrl_pressed_, alt_pressed_, shift_pressed_;
};

//=============================================================================

0 comments on commit 10d93fc

Please sign in to comment.
You can’t perform that action at this time.