Scale gizmo interaction with mouse

This commit is contained in:
Enrico Turri 2018-06-15 14:10:28 +02:00
parent 52a7d7ed09
commit 6874949556
5 changed files with 263 additions and 94 deletions

View file

@ -223,12 +223,8 @@ sub system_info {
my $opengl_info_txt = '';
if (defined($self->{mainframe}) && defined($self->{mainframe}->{plater}) &&
defined($self->{mainframe}->{plater}->{canvas3D})) {
#==============================================================================================================================
$opengl_info = Slic3r::GUI::_3DScene::get_gl_info(1, 1);
$opengl_info_txt = Slic3r::GUI::_3DScene::get_gl_info(0, 1);
# $opengl_info = $self->{mainframe}->{plater}->{canvas3D}->opengl_info(format => 'html');
# $opengl_info_txt = $self->{mainframe}->{plater}->{canvas3D}->opengl_info;
#==============================================================================================================================
}
my $about = Slic3r::GUI::SystemInfo->new(
parent => undef,

View file

@ -1078,6 +1078,7 @@ const float GLCanvas3D::Gizmos::OverlayGapY = 10.0f;
GLCanvas3D::Gizmos::Gizmos()
: m_enabled(false)
, m_current(Undefined)
, m_dragging(false)
{
}
@ -1196,7 +1197,10 @@ void GLCanvas3D::Gizmos::reset_all_states()
for (GizmosMap::const_iterator it = m_gizmos.begin(); it != m_gizmos.end(); ++it)
{
if (it->second != nullptr)
{
it->second->set_state(GLGizmoBase::Off);
it->second->set_hover_id(-1);
}
}
m_current = Undefined;
@ -1240,6 +1244,43 @@ bool GLCanvas3D::Gizmos::overlay_contains_mouse(const GLCanvas3D& canvas, const
return false;
}
bool GLCanvas3D::Gizmos::grabber_contains_mouse() const
{
if (!m_enabled)
return false;
GLGizmoBase* curr = _get_current();
return (curr != nullptr) ? (curr->get_hover_id() != -1) : false;
}
void GLCanvas3D::Gizmos::update(const Pointf& mouse_pos)
{
if (!m_enabled)
return;
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->update(mouse_pos);
}
bool GLCanvas3D::Gizmos::is_dragging() const
{
return m_dragging;
}
void GLCanvas3D::Gizmos::start_dragging()
{
m_dragging = true;
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->start_dragging();
}
void GLCanvas3D::Gizmos::stop_dragging()
{
m_dragging = false;
}
void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const
{
if (!m_enabled)
@ -1264,11 +1305,9 @@ void GLCanvas3D::Gizmos::render_current_gizmo_for_picking_pass(const BoundingBox
::glDisable(GL_DEPTH_TEST);
GizmosMap::const_iterator it = m_gizmos.find(m_current);
if (it == m_gizmos.end())
return;
it->second->render_for_picking(box);
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->render_for_picking(box);
}
void GLCanvas3D::Gizmos::_reset()
@ -1305,16 +1344,15 @@ void GLCanvas3D::Gizmos::_render_overlay(const GLCanvas3D& canvas) const
void GLCanvas3D::Gizmos::_render_current_gizmo(const BoundingBoxf3& box) const
{
GizmosMap::const_iterator it = m_gizmos.find(m_current);
if (it == m_gizmos.end())
return;
it->second->render(box);
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->render(box);
}
float GLCanvas3D::Gizmos::_get_total_overlay_height() const
{
float height = 0.0f;
for (GizmosMap::const_iterator it = m_gizmos.begin(); it != m_gizmos.end(); ++it)
{
height += (float)it->second->get_textures_size();
@ -1325,6 +1363,12 @@ float GLCanvas3D::Gizmos::_get_total_overlay_height() const
return height;
}
GLGizmoBase* GLCanvas3D::Gizmos::_get_current() const
{
GizmosMap::const_iterator it = m_gizmos.find(m_current);
return (it != m_gizmos.end()) ? it->second : nullptr;
}
GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context)
: m_canvas(canvas)
, m_context(context)
@ -2619,6 +2663,10 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
m_gizmos.update_on_off_state(*this, m_mouse.position);
m_dirty = true;
}
else if ((selected_object_idx != -1) && m_gizmos.grabber_contains_mouse())
{
m_gizmos.start_dragging();
}
else
{
// Select volume in this 3D canvas.
@ -2725,6 +2773,14 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
m_dirty = true;
}
else if (evt.Dragging() && m_gizmos.is_dragging())
{
m_mouse.dragging = true;
const Pointf3& cur_pos = _mouse_to_bed_3d(pos);
m_gizmos.update(Pointf(cur_pos.x, cur_pos.y));
m_dirty = true;
}
else if (evt.Dragging() && !gizmos_overlay_contains_mouse)
{
m_mouse.dragging = true;
@ -2799,7 +2855,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
_on_move(volume_idxs);
}
else if (!m_mouse.dragging && (m_hover_volume_id == -1) && !gizmos_overlay_contains_mouse && !is_layers_editing_enabled())
else if (!m_mouse.dragging && (m_hover_volume_id == -1) && !gizmos_overlay_contains_mouse && !m_gizmos.is_dragging() && !is_layers_editing_enabled())
{
// deselect and propagate event through callback
if (m_picking_enabled)
@ -2808,6 +2864,10 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
_on_select(-1);
}
}
else if (evt.LeftUp() && m_gizmos.is_dragging())
{
m_gizmos.stop_dragging();
}
m_mouse.drag.volume_idx = -1;
m_mouse.set_start_position_3D_as_invalid();
@ -3483,10 +3543,36 @@ Pointf3 GLCanvas3D::_mouse_to_3d(const Point& mouse_pos, float* z)
mouse_z = *z;
GLdouble out_x, out_y, out_z;
::gluUnProject((GLdouble)mouse_pos.x, (GLdouble)y, mouse_z, modelview_matrix, projection_matrix, viewport, &out_x, &out_y, &out_z);
::gluUnProject((GLdouble)mouse_pos.x, (GLdouble)y, (GLdouble)mouse_z, modelview_matrix, projection_matrix, viewport, &out_x, &out_y, &out_z);
return Pointf3((coordf_t)out_x, (coordf_t)out_y, (coordf_t)out_z);
}
Pointf3 GLCanvas3D::_mouse_to_bed_3d(const Point& mouse_pos)
{
if (!set_current())
return Pointf3(DBL_MAX, DBL_MAX, DBL_MAX);
GLint viewport[4];
::glGetIntegerv(GL_VIEWPORT, viewport);
_camera_tranform();
GLdouble modelview_matrix[16];
::glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
GLdouble projection_matrix[16];
::glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
GLint y = viewport[3] - (GLint)mouse_pos.y;
GLdouble x0, y0, z0;
::gluUnProject((GLdouble)mouse_pos.x, (GLdouble)y, 0.1, modelview_matrix, projection_matrix, viewport, &x0, &y0, &z0);
GLdouble x1, y1, z1;
::gluUnProject((GLdouble)mouse_pos.x, (GLdouble)y, 0.9, modelview_matrix, projection_matrix, viewport, &x1, &y1, &z1);
return Linef3(Pointf3(x0, y0, z0), Pointf3(x1, y1, z1)).intersect_plane(0.0);
}
void GLCanvas3D::_start_timer()
{
if (m_timer != nullptr)

View file

@ -340,6 +340,7 @@ public:
typedef std::map<EType, GLGizmoBase*> GizmosMap;
GizmosMap m_gizmos;
EType m_current;
bool m_dragging;
public:
Gizmos();
@ -357,6 +358,12 @@ public:
void set_hover_id(int id);
bool overlay_contains_mouse(const GLCanvas3D& canvas, const Pointf& mouse_pos) const;
bool grabber_contains_mouse() const;
void update(const Pointf& mouse_pos);
bool is_dragging() const;
void start_dragging();
void stop_dragging();
void render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const;
void render_current_gizmo_for_picking_pass(const BoundingBoxf3& box) const;
@ -368,6 +375,7 @@ public:
void _render_current_gizmo(const BoundingBoxf3& box) const;
float _get_total_overlay_height() const;
GLGizmoBase* _get_current() const;
};
private:
@ -587,6 +595,9 @@ private:
// If the Z screen space coordinate is not provided, a depth buffer value is substituted.
Pointf3 _mouse_to_3d(const Point& mouse_pos, float* z = nullptr);
// Convert the screen space coordinate to world coordinate on the bed.
Pointf3 _mouse_to_bed_3d(const Point& mouse_pos);
void _start_timer();
void _stop_timer();

View file

@ -10,10 +10,54 @@
namespace Slic3r {
namespace GUI {
const float GLGizmoBase::Grabber::HalfSize = 2.0f;
const float GLGizmoBase::Grabber::HoverOffset = 0.5f;
const float GLGizmoBase::BaseColor[3] = { 1.0f, 1.0f, 1.0f };
const float GLGizmoBase::HighlightColor[3] = { 1.0f, 0.38f, 0.0f };
const float GLGizmoBase::GrabberHalfSize = 2.0f;
const float GLGizmoBase::HoverOffset = 0.5f;
GLGizmoBase::Grabber::Grabber()
: center(Pointf(0.0, 0.0))
{
color[0] = 1.0f;
color[1] = 1.0f;
color[2] = 1.0f;
}
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;
::glColor3f((GLfloat)color[0], (GLfloat)color[1], (GLfloat)color[2]);
::glDisable(GL_CULL_FACE);
::glBegin(GL_TRIANGLES);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glEnd();
::glEnable(GL_CULL_FACE);
if (hover)
{
min_x -= HoverOffset;
max_x += HoverOffset;
min_y -= HoverOffset;
max_y += HoverOffset;
::glBegin(GL_LINE_LOOP);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glEnd();
}
}
GLGizmoBase::GLGizmoBase()
: m_state(Off)
@ -50,9 +94,27 @@ int GLGizmoBase::get_textures_size() const
return m_textures[Off].get_width();
}
int GLGizmoBase::get_hover_id() const
{
return m_hover_id;
}
void GLGizmoBase::set_hover_id(int id)
{
m_hover_id = id;
if (id < (int)m_grabbers.size())
m_hover_id = id;
}
void GLGizmoBase::start_dragging()
{
if (m_hover_id != -1)
m_start_drag_position = m_grabbers[m_hover_id].center;
}
void GLGizmoBase::update(const Pointf& mouse_pos)
{
if (m_hover_id != -1)
on_update(mouse_pos);
}
void GLGizmoBase::render(const BoundingBoxf3& box) const
@ -65,37 +127,11 @@ void GLGizmoBase::render_for_picking(const BoundingBoxf3& box) const
on_render_for_picking(box);
}
void GLGizmoBase::render_grabber(const Pointf3& center, bool hover) const
void GLGizmoBase::render_grabbers() const
{
float min_x = (float)center.x - GrabberHalfSize;
float max_x = (float)center.x + GrabberHalfSize;
float min_y = (float)center.y - GrabberHalfSize;
float max_y = (float)center.y + GrabberHalfSize;
::glDisable(GL_CULL_FACE);
::glBegin(GL_TRIANGLES);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glEnd();
::glEnable(GL_CULL_FACE);
if (hover)
for (unsigned int i = 0; i < (unsigned int)m_grabbers.size(); ++i)
{
min_x -= HoverOffset;
max_x += HoverOffset;
min_y -= HoverOffset;
max_y += HoverOffset;
::glBegin(GL_LINE_LOOP);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
::glEnd();
m_grabbers[i].render(m_hover_id == i);
}
}
@ -133,9 +169,16 @@ bool GLGizmoRotate::on_init()
if (!m_textures[On].load_from_file(filename, false))
return false;
m_grabbers.push_back(Grabber());
return true;
}
void GLGizmoRotate::on_update(const Pointf& mouse_pos)
{
// std::cout << "GLGizmoRotate::on_update() - delta (" << delta.x << ", " << delta.y << ")" << std::endl;
}
void GLGizmoRotate::on_render(const BoundingBoxf3& box) const
{
::glDisable(GL_LIGHTING);
@ -161,15 +204,10 @@ void GLGizmoRotate::on_render_for_picking(const BoundingBoxf3& box) const
::glDisable(GL_LIGHTING);
::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)) + GrabberOffset;
float x = center.x + ::cos(m_angle_z) * radius;
float y = center.y + ::sin(m_angle_z) * radius;
::glColor3f(1.0f, 1.0f, 254.0f / 255.0f);
render_grabber(Pointf3((coordf_t)x, (coordf_t)y, 0.0), false);
m_grabbers[0].color[0] = 1.0f;
m_grabbers[0].color[1] = 1.0f;
m_grabbers[0].color[2] = 254.0f / 255.0f;
render_grabbers();
}
void GLGizmoRotate::_render_circle(const Pointf3& center, float radius) const
@ -240,16 +278,16 @@ void GLGizmoRotate::_render_reference_radius(const Pointf3& center, float radius
void GLGizmoRotate::_render_grabber(const Pointf3& center, float radius) const
{
float grabber_radius = radius + GrabberOffset;
float x = center.x + ::cos(m_angle_z) * grabber_radius;
float y = center.y + ::sin(m_angle_z) * grabber_radius;
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;
::glBegin(GL_LINES);
::glVertex3f((GLfloat)center.x, (GLfloat)center.y, 0.0f);
::glVertex3f((GLfloat)x, (GLfloat)y, 0.0f);
::glVertex3f((GLfloat)m_grabbers[0].center.x, (GLfloat)m_grabbers[0].center.y, 0.0f);
::glEnd();
::glColor3fv(HighlightColor);
render_grabber(Pointf3((coordf_t)x, (coordf_t)y, 0.0), (m_hover_id != -1));
::memcpy((void*)m_grabbers[0].color, (const void*)HighlightColor, 4 * sizeof(float));
render_grabbers();
}
const float GLGizmoScale::Offset = 5.0f;
@ -278,9 +316,28 @@ bool GLGizmoScale::on_init()
if (!m_textures[On].load_from_file(filename, false))
return false;
for (unsigned int i = 0; i < 4; ++i)
{
m_grabbers.push_back(Grabber());
}
return true;
}
void GLGizmoScale::on_update(const Pointf& mouse_pos)
{
Pointf center(0.5 * (m_grabbers[1].center.x + m_grabbers[0].center.x), 0.5 * (m_grabbers[3].center.y + m_grabbers[0].center.y));
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;
m_scale_y = (float)ratio;
m_scale_z = (float)ratio;
}
void GLGizmoScale::on_render(const BoundingBoxf3& box) const
{
::glDisable(GL_LIGHTING);
@ -295,22 +352,31 @@ void GLGizmoScale::on_render(const BoundingBoxf3& box) const
coordf_t min_y = center.y - half_scaled_size.y - (coordf_t)Offset;
coordf_t max_y = center.y + half_scaled_size.y + (coordf_t)Offset;
m_grabbers[0].center.x = min_x;
m_grabbers[0].center.y = min_y;
m_grabbers[1].center.x = max_x;
m_grabbers[1].center.y = min_y;
m_grabbers[2].center.x = max_x;
m_grabbers[2].center.y = max_y;
m_grabbers[3].center.x = min_x;
m_grabbers[3].center.y = max_y;
::glLineWidth(2.0f);
::glColor3fv(BaseColor);
// draw outline
::glBegin(GL_LINE_LOOP);
::glVertex3f((GLfloat)min_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)min_y, 0.0f);
::glVertex3f((GLfloat)max_x, (GLfloat)max_y, 0.0f);
::glVertex3f((GLfloat)min_x, (GLfloat)max_y, 0.0f);
for (unsigned int i = 0; i < 4; ++i)
{
::glVertex3f((GLfloat)m_grabbers[i].center.x, (GLfloat)m_grabbers[i].center.y, 0.0f);
}
::glEnd();
// draw grabbers
::glColor3fv(HighlightColor);
render_grabber(Pointf3(min_x, min_y, 0.0), (m_hover_id == 0));
render_grabber(Pointf3(max_x, min_y, 0.0), (m_hover_id == 1));
render_grabber(Pointf3(max_x, max_y, 0.0), (m_hover_id == 2));
render_grabber(Pointf3(min_x, max_y, 0.0), (m_hover_id == 3));
for (unsigned int i = 0; i < 4; ++i)
{
::memcpy((void*)m_grabbers[i].color, (const void*)HighlightColor, 4 * sizeof(float));
}
render_grabbers();
}
void GLGizmoScale::on_render_for_picking(const BoundingBoxf3& box) const
@ -320,24 +386,13 @@ void GLGizmoScale::on_render_for_picking(const BoundingBoxf3& box) const
::glDisable(GL_LIGHTING);
::glDisable(GL_DEPTH_TEST);
const Pointf3& size = box.size();
const Pointf3& center = box.center();
Pointf half_scaled_size = 0.5 * Pointf((coordf_t)m_scale_x * size.x, (coordf_t)m_scale_y * size.y);
coordf_t min_x = center.x - half_scaled_size.x - (coordf_t)Offset;
coordf_t max_x = center.x + half_scaled_size.x + (coordf_t)Offset;
coordf_t min_y = center.y - half_scaled_size.y - (coordf_t)Offset;
coordf_t max_y = center.y + half_scaled_size.y + (coordf_t)Offset;
// draw grabbers
::glColor3f(1.0f, 1.0f, 254.0f * INV_255);
render_grabber(Pointf3(min_x, min_y, 0.0), false);
::glColor3f(1.0f, 1.0f, 253.0f * INV_255);
render_grabber(Pointf3(max_x, min_y, 0.0), false);
::glColor3f(1.0f, 1.0f, 252.0f * INV_255);
render_grabber(Pointf3(max_x, max_y, 0.0), false);
::glColor3f(1.0f, 1.0f, 251.0f * INV_255);
render_grabber(Pointf3(min_x, max_y, 0.0), false);
for (unsigned int i = 0; i < 4; ++i)
{
m_grabbers[i].color[0] = 1.0f;
m_grabbers[i].color[1] = 1.0f;
m_grabbers[i].color[2] = (254.0f - (float)i) * INV_255;
}
render_grabbers();
}
} // namespace GUI

View file

@ -2,6 +2,9 @@
#define slic3r_GLGizmo_hpp_
#include "../../slic3r/GUI/GLTexture.hpp"
#include "../../libslic3r/Point.hpp"
#include <vector>
namespace Slic3r {
@ -15,8 +18,18 @@ class GLGizmoBase
protected:
static const float BaseColor[3];
static const float HighlightColor[3];
static const float GrabberHalfSize;
static const float HoverOffset;
struct Grabber
{
static const float HalfSize;
static const float HoverOffset;
Pointf center;
float color[3];
Grabber();
void render(bool hover) const;
};
public:
enum EState
@ -29,10 +42,11 @@ public:
protected:
EState m_state;
// textures are assumed to be square and all with the same size in pixels
// no internal check is done
// textures are assumed to be square and all with the same size in pixels, no internal check is done
GLTexture m_textures[Num_States];
int m_hover_id;
mutable std::vector<Grabber> m_grabbers;
Pointf m_start_drag_position;
public:
GLGizmoBase();
@ -46,17 +60,22 @@ public:
unsigned int get_textures_id() const;
int get_textures_size() const;
int get_hover_id() const;
void set_hover_id(int id);
void start_dragging();
void update(const Pointf& mouse_pos);
void render(const BoundingBoxf3& box) const;
void render_for_picking(const BoundingBoxf3& box) const;
protected:
virtual bool on_init() = 0;
virtual void on_update(const Pointf& mouse_pos) = 0;
virtual void on_render(const BoundingBoxf3& box) const = 0;
virtual void on_render_for_picking(const BoundingBoxf3& box) const = 0;
void render_grabber(const Pointf3& center, bool hover) const;
void render_grabbers() const;
};
class GLGizmoRotate : public GLGizmoBase
@ -80,6 +99,7 @@ public:
protected:
virtual bool on_init();
virtual void on_update(const Pointf& mouse_pos);
virtual void on_render(const BoundingBoxf3& box) const;
virtual void on_render_for_picking(const BoundingBoxf3& box) const;
@ -104,6 +124,7 @@ public:
protected:
virtual bool on_init();
virtual void on_update(const Pointf& mouse_pos);
virtual void on_render(const BoundingBoxf3& box) const;
virtual void on_render_for_picking(const BoundingBoxf3& box) const;
};