diff options
author | Enrico Turri <enricoturri@seznam.cz> | 2018-06-15 17:16:55 +0300 |
---|---|---|
committer | Enrico Turri <enricoturri@seznam.cz> | 2018-06-15 17:16:55 +0300 |
commit | 53f8706805c430bc2a3dc739b336976980aea0ca (patch) | |
tree | 1b087d2a8b37e3906e773d37d5d8035e56a0bdab /xs | |
parent | 6874949556f62c56f26285fea57275ce0900a2f9 (diff) |
Rotate gizmo interaction with mouse
Diffstat (limited to 'xs')
-rw-r--r-- | xs/src/libslic3r/Point.hpp | 5 | ||||
-rw-r--r-- | xs/src/slic3r/GUI/GLGizmo.cpp | 129 | ||||
-rw-r--r-- | xs/src/slic3r/GUI/GLGizmo.hpp | 20 |
3 files changed, 105 insertions, 49 deletions
diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp index 6c9096a3d..a52cdceb6 100644 --- a/xs/src/libslic3r/Point.hpp +++ b/xs/src/libslic3r/Point.hpp @@ -238,6 +238,11 @@ inline coordf_t dot(const Pointf &v1, const Pointf &v2) { return v1.x * v2.x + v inline coordf_t dot(const Pointf &v) { return v.x * v.x + v.y * v.y; } inline double length(const Vectorf &v) { return sqrt(dot(v)); } inline double l2(const Vectorf &v) { return dot(v); } +inline Vectorf normalize(const Vectorf& v) +{ + coordf_t len = ::sqrt(sqr(v.x) + sqr(v.y)); + return (len != 0.0) ? 1.0 / len * v : Vectorf(0.0, 0.0); +} class Pointf3 : public Pointf { diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index 343df751c..706c41675 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -17,6 +17,7 @@ const float GLGizmoBase::HighlightColor[3] = { 1.0f, 0.38f, 0.0f }; GLGizmoBase::Grabber::Grabber() : center(Pointf(0.0, 0.0)) + , angle_z(0.0f) { color[0] = 1.0f; color[1] = 1.0f; @@ -25,13 +26,18 @@ GLGizmoBase::Grabber::Grabber() void GLGizmoBase::Grabber::render(bool hover) const { - float min_x = (float)center.x - HalfSize; - float max_x = (float)center.x + HalfSize; - float min_y = (float)center.y - HalfSize; - float max_y = (float)center.y + HalfSize; + float min_x = -HalfSize; + float max_x = +HalfSize; + float min_y = -HalfSize; + float max_y = +HalfSize; ::glColor3f((GLfloat)color[0], (GLfloat)color[1], (GLfloat)color[2]); + float angle_z_in_deg = angle_z * 180.0f / (float)PI; + ::glPushMatrix(); + ::glTranslatef((GLfloat)center.x, (GLfloat)center.y, 0.0f); + ::glRotatef((GLfloat)angle_z_in_deg, 0.0f, 0.0f, 1.0f); + ::glDisable(GL_CULL_FACE); ::glBegin(GL_TRIANGLES); ::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f); @@ -57,6 +63,8 @@ void GLGizmoBase::Grabber::render(bool hover) const ::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f); ::glEnd(); } + + ::glPopMatrix(); } GLGizmoBase::GLGizmoBase() @@ -137,6 +145,7 @@ void GLGizmoBase::render_grabbers() const const float GLGizmoRotate::Offset = 5.0f; const unsigned int GLGizmoRotate::CircleResolution = 64; +const unsigned int GLGizmoRotate::AngleResolution = 64; const unsigned int GLGizmoRotate::ScaleStepsCount = 60; const float GLGizmoRotate::ScaleStepRad = 2.0f * (float)PI / GLGizmoRotate::ScaleStepsCount; const unsigned int GLGizmoRotate::ScaleLongEvery = 5; @@ -147,9 +156,11 @@ const float GLGizmoRotate::GrabberOffset = 5.0f; GLGizmoRotate::GLGizmoRotate() : GLGizmoBase() - , m_angle_x(0.0f) - , m_angle_y(0.0f) +// , m_angle_x(0.0f) +// , m_angle_y(0.0f) , m_angle_z(0.0f) + , m_center(Pointf(0.0, 0.0)) + , m_radius(0.0f) { } @@ -176,7 +187,22 @@ bool GLGizmoRotate::on_init() void GLGizmoRotate::on_update(const Pointf& mouse_pos) { -// std::cout << "GLGizmoRotate::on_update() - delta (" << delta.x << ", " << delta.y << ")" << std::endl; + Vectorf orig_dir(1.0, 0.0); + Vectorf new_dir = normalize(mouse_pos - m_center); + coordf_t theta = ::acos(clamp(-1.0, 1.0, dot(new_dir, orig_dir))); + if (cross(orig_dir, new_dir) < 0.0) + theta = 2.0 * (coordf_t)PI - theta; + + if (length(m_center.vector_to(mouse_pos)) < 2.0 * (double)m_radius / 3.0) + { + coordf_t step = 2.0 * (coordf_t)PI / (coordf_t)SnapRegionsCount; + theta = step * (coordf_t)std::round(theta / step); + } + + if (theta == 2.0 * (coordf_t)PI) + theta = 0.0; + + m_angle_z = (float)theta; } void GLGizmoRotate::on_render(const BoundingBoxf3& box) const @@ -185,18 +211,20 @@ void GLGizmoRotate::on_render(const BoundingBoxf3& box) const ::glDisable(GL_DEPTH_TEST); const Pointf3& size = box.size(); - const Pointf3& center = box.center(); - - float radius = Offset + ::sqrt(sqr(0.5f * size.x) + sqr(0.5f * size.y)); + m_center = box.center(); + m_radius = Offset + ::sqrt(sqr(0.5f * size.x) + sqr(0.5f * size.y)); ::glLineWidth(2.0f); ::glColor3fv(BaseColor); - _render_circle(center, radius); - _render_scale(center, radius); - _render_snap_radii(center, radius); - _render_reference_radius(center, radius); - _render_grabber(center, radius); + _render_circle(); + _render_scale(); + _render_snap_radii(); + _render_reference_radius(); + + ::glColor3fv(HighlightColor); + _render_angle_z(); + _render_grabber(); } void GLGizmoRotate::on_render_for_picking(const BoundingBoxf3& box) const @@ -210,23 +238,23 @@ void GLGizmoRotate::on_render_for_picking(const BoundingBoxf3& box) const render_grabbers(); } -void GLGizmoRotate::_render_circle(const Pointf3& center, float radius) const +void GLGizmoRotate::_render_circle() const { ::glBegin(GL_LINE_LOOP); for (unsigned int i = 0; i < ScaleStepsCount; ++i) { float angle = (float)i * ScaleStepRad; - float x = center.x + ::cos(angle) * radius; - float y = center.y + ::sin(angle) * radius; + float x = m_center.x + ::cos(angle) * m_radius; + float y = m_center.y + ::sin(angle) * m_radius; ::glVertex3f((GLfloat)x, (GLfloat)y, 0.0f); } ::glEnd(); } -void GLGizmoRotate::_render_scale(const Pointf3& center, float radius) const +void GLGizmoRotate::_render_scale() const { - float out_radius_long = radius + ScaleLongTooth; - float out_radius_short = radius + ScaleShortTooth; + float out_radius_long = m_radius + ScaleLongTooth; + float out_radius_short = m_radius + ScaleShortTooth; ::glBegin(GL_LINES); for (unsigned int i = 0; i < ScaleStepsCount; ++i) @@ -234,55 +262,73 @@ void GLGizmoRotate::_render_scale(const Pointf3& center, float radius) const float angle = (float)i * ScaleStepRad; float cosa = ::cos(angle); float sina = ::sin(angle); - float in_x = center.x + cosa * radius; - float in_y = center.y + sina * radius; - float out_x = (i % ScaleLongEvery == 0) ? center.x + cosa * out_radius_long : center.x + cosa * out_radius_short; - float out_y = (i % ScaleLongEvery == 0) ? center.y + sina * out_radius_long : center.y + sina * out_radius_short; + float in_x = m_center.x + cosa * m_radius; + float in_y = m_center.y + sina * m_radius; + float out_x = (i % ScaleLongEvery == 0) ? m_center.x + cosa * out_radius_long : m_center.x + cosa * out_radius_short; + float out_y = (i % ScaleLongEvery == 0) ? m_center.y + sina * out_radius_long : m_center.y + sina * out_radius_short; ::glVertex3f((GLfloat)in_x, (GLfloat)in_y, 0.0f); ::glVertex3f((GLfloat)out_x, (GLfloat)out_y, 0.0f); } ::glEnd(); } -void GLGizmoRotate::_render_snap_radii(const Pointf3& center, float radius) const +void GLGizmoRotate::_render_snap_radii() const { - float step_deg = 2.0f * (float)PI / (float)SnapRegionsCount; + float step = 2.0f * (float)PI / (float)SnapRegionsCount; - float in_radius = radius / 3.0f; + float in_radius = m_radius / 3.0f; float out_radius = 2.0f * in_radius; ::glBegin(GL_LINES); for (unsigned int i = 0; i < SnapRegionsCount; ++i) { - float angle = (float)i * step_deg; + float angle = (float)i * step; float cosa = ::cos(angle); float sina = ::sin(angle); - float in_x = center.x + cosa * in_radius; - float in_y = center.y + sina * in_radius; - float out_x = center.x + cosa * out_radius; - float out_y = center.y + sina * out_radius; + float in_x = m_center.x + cosa * in_radius; + float in_y = m_center.y + sina * in_radius; + float out_x = m_center.x + cosa * out_radius; + float out_y = m_center.y + sina * out_radius; ::glVertex3f((GLfloat)in_x, (GLfloat)in_y, 0.0f); ::glVertex3f((GLfloat)out_x, (GLfloat)out_y, 0.0f); } ::glEnd(); } -void GLGizmoRotate::_render_reference_radius(const Pointf3& center, float radius) const +void GLGizmoRotate::_render_reference_radius() const { ::glBegin(GL_LINES); - ::glVertex3f((GLfloat)center.x, (GLfloat)center.y, 0.0f); - ::glVertex3f((GLfloat)center.x + radius, (GLfloat)center.y, 0.0f); + ::glVertex3f((GLfloat)m_center.x, (GLfloat)m_center.y, 0.0f); + ::glVertex3f((GLfloat)m_center.x + m_radius + GrabberOffset, (GLfloat)m_center.y, 0.0f); ::glEnd(); } -void GLGizmoRotate::_render_grabber(const Pointf3& center, float radius) const +void GLGizmoRotate::_render_angle_z() const { - float grabber_radius = radius + GrabberOffset; - m_grabbers[0].center.x = center.x + ::cos(m_angle_z) * grabber_radius; - m_grabbers[0].center.y = center.y + ::sin(m_angle_z) * grabber_radius; + float step_angle = m_angle_z / AngleResolution; + float ex_radius = m_radius + GrabberOffset; + ::glBegin(GL_LINE_STRIP); + for (unsigned int i = 0; i <= AngleResolution; ++i) + { + float angle = (float)i * step_angle; + float x = m_center.x + ::cos(angle) * ex_radius; + float y = m_center.y + ::sin(angle) * ex_radius; + ::glVertex3f((GLfloat)x, (GLfloat)y, 0.0f); + } + ::glEnd(); +} + +void GLGizmoRotate::_render_grabber() const +{ + float grabber_radius = m_radius + GrabberOffset; + m_grabbers[0].center.x = m_center.x + ::cos(m_angle_z) * grabber_radius; + m_grabbers[0].center.y = m_center.y + ::sin(m_angle_z) * grabber_radius; + m_grabbers[0].angle_z = m_angle_z; + + ::glColor3fv(BaseColor); ::glBegin(GL_LINES); - ::glVertex3f((GLfloat)center.x, (GLfloat)center.y, 0.0f); + ::glVertex3f((GLfloat)m_center.x, (GLfloat)m_center.y, 0.0f); ::glVertex3f((GLfloat)m_grabbers[0].center.x, (GLfloat)m_grabbers[0].center.y, 0.0f); ::glEnd(); @@ -330,7 +376,6 @@ void GLGizmoScale::on_update(const Pointf& mouse_pos) coordf_t orig_len = length(m_start_drag_position - center); coordf_t new_len = length(mouse_pos - center); - coordf_t ratio = (orig_len != 0.0) ? new_len / orig_len : 1.0; m_scale_x = (float)ratio; diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index bc334d26b..432a20958 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -25,6 +25,7 @@ protected: static const float HoverOffset; Pointf center; + float angle_z; float color[3]; Grabber(); @@ -82,6 +83,7 @@ class GLGizmoRotate : public GLGizmoBase { static const float Offset; static const unsigned int CircleResolution; + static const unsigned int AngleResolution; static const unsigned int ScaleStepsCount; static const float ScaleStepRad; static const unsigned int ScaleLongEvery; @@ -90,10 +92,13 @@ class GLGizmoRotate : public GLGizmoBase static const unsigned int SnapRegionsCount; static const float GrabberOffset; - float m_angle_x; - float m_angle_y; +// float m_angle_x; +// float m_angle_y; float m_angle_z; + mutable Pointf m_center; + mutable float m_radius; + public: GLGizmoRotate(); @@ -104,11 +109,12 @@ protected: virtual void on_render_for_picking(const BoundingBoxf3& box) const; private: - void _render_circle(const Pointf3& center, float radius) const; - void _render_scale(const Pointf3& center, float radius) const; - void _render_snap_radii(const Pointf3& center, float radius) const; - void _render_reference_radius(const Pointf3& center, float radius) const; - void _render_grabber(const Pointf3& center, float radius) const; + void _render_circle() const; + void _render_scale() const; + void _render_snap_radii() const; + void _render_reference_radius() const; + void _render_angle_z() const; + void _render_grabber() const; }; class GLGizmoScale : public GLGizmoBase |