Partial refactoring of 3d gizmos

This commit is contained in:
Enrico Turri 2018-09-10 10:01:49 +02:00
parent df320523b1
commit 5e8bd47988
4 changed files with 91 additions and 138 deletions

View File

@ -1320,16 +1320,6 @@ void GLCanvas3D::Gizmos::update(const Linef3& mouse_ray)
curr->update(mouse_ray);
}
void GLCanvas3D::Gizmos::refresh()
{
if (!m_enabled)
return;
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->refresh();
}
GLCanvas3D::Gizmos::EType GLCanvas3D::Gizmos::get_current_type() const
{
return m_current;
@ -1349,12 +1339,12 @@ bool GLCanvas3D::Gizmos::is_dragging() const
return m_dragging;
}
void GLCanvas3D::Gizmos::start_dragging()
void GLCanvas3D::Gizmos::start_dragging(const BoundingBoxf3& box)
{
m_dragging = true;
GLGizmoBase* curr = _get_current();
if (curr != nullptr)
curr->start_dragging();
curr->start_dragging(box);
}
void GLCanvas3D::Gizmos::stop_dragging()
@ -2487,8 +2477,6 @@ void GLCanvas3D::reload_scene(bool force)
if (!m_objects_selections.empty())
update_gizmos_data();
m_gizmos.refresh();
if (m_config->has("nozzle_diameter"))
{
// Should the wipe tower be visualized ?
@ -3019,7 +3007,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
else if ((selected_object_idx != -1) && m_gizmos.grabber_contains_mouse())
{
update_gizmos_data();
m_gizmos.start_dragging();
m_gizmos.start_dragging(_selected_volumes_bounding_box());
m_mouse.drag.gizmo_volume_idx = _get_first_selected_volume_id(selected_object_idx);
if (m_gizmos.get_current_type() == Gizmos::Flatten) {
@ -3062,7 +3050,6 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
}
update_gizmos_data();
m_gizmos.refresh();
m_dirty = true;
}
}
@ -3158,7 +3145,6 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
update_position_values(volume->get_offset());
m_mouse.drag.start_position_3D = cur_pos;
m_gizmos.refresh();
m_dirty = true;
}
@ -3226,9 +3212,6 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
m_on_update_geometry_info_callback.call(size(0), size(1), size(2), m_gizmos.get_scale());
}
if ((m_gizmos.get_current_type() != Gizmos::Rotate) && (volumes.size() > 1))
m_gizmos.refresh();
m_dirty = true;
}
else if (evt.Dragging() && !gizmos_overlay_contains_mouse)

View File

@ -367,14 +367,13 @@ public:
bool overlay_contains_mouse(const GLCanvas3D& canvas, const Vec2d& mouse_pos) const;
bool grabber_contains_mouse() const;
void update(const Linef3& mouse_ray);
void refresh();
EType get_current_type() const;
bool is_running() const;
bool is_dragging() const;
void start_dragging();
void start_dragging(const BoundingBoxf3& box);
void stop_dragging();
float get_scale() const;

View File

@ -129,6 +129,7 @@ GLGizmoBase::GLGizmoBase(GLCanvas3D& parent)
, m_group_id(-1)
, m_state(Off)
, m_hover_id(-1)
, m_dragging(false)
{
::memcpy((void*)m_base_color, (const void*)DEFAULT_BASE_COLOR, 3 * sizeof(float));
::memcpy((void*)m_drag_color, (const void*)DEFAULT_DRAG_COLOR, 3 * sizeof(float));
@ -150,18 +151,21 @@ void GLGizmoBase::set_highlight_color(const float* color)
::memcpy((void*)m_highlight_color, (const void*)color, 3 * sizeof(float));
}
void GLGizmoBase::start_dragging()
void GLGizmoBase::start_dragging(const BoundingBoxf3& box)
{
m_dragging = true;
for (int i = 0; i < (int)m_grabbers.size(); ++i)
{
m_grabbers[i].dragging = (m_hover_id == i);
}
on_start_dragging();
on_start_dragging(box);
}
void GLGizmoBase::stop_dragging()
{
m_dragging = false;
set_tooltip("");
for (int i = 0; i < (int)m_grabbers.size(); ++i)
@ -235,7 +239,6 @@ GLGizmoRotate::GLGizmoRotate(GLCanvas3D& parent, GLGizmoRotate::Axis axis)
, m_angle(0.0)
, m_center(0.0, 0.0, 0.0)
, m_radius(0.0f)
, m_keep_initial_values(false)
{
}
@ -253,6 +256,12 @@ bool GLGizmoRotate::on_init()
return true;
}
void GLGizmoRotate::on_start_dragging(const BoundingBoxf3& box)
{
m_center = box.center();
m_radius = Offset + box.radius();
}
void GLGizmoRotate::on_update(const Linef3& mouse_ray)
{
Vec2d mouse_pos = to_2d(mouse_position_in_local_plane(mouse_ray));
@ -294,18 +303,16 @@ void GLGizmoRotate::on_update(const Linef3& mouse_ray)
void GLGizmoRotate::on_render(const BoundingBoxf3& box) const
{
if (m_grabbers[0].dragging)
if (m_dragging)
set_tooltip(format(m_angle * 180.0f / (float)PI, 4));
::glEnable(GL_DEPTH_TEST);
if (!m_keep_initial_values)
else
{
m_center = box.center();
m_radius = Offset + box.radius();
m_keep_initial_values = true;
}
::glEnable(GL_DEPTH_TEST);
::glPushMatrix();
transform_to_local();
@ -509,23 +516,29 @@ Vec3d GLGizmoRotate::mouse_position_in_local_plane(const Linef3& mouse_ray) cons
GLGizmoRotate3D::GLGizmoRotate3D(GLCanvas3D& parent)
: GLGizmoBase(parent)
, m_x(parent, GLGizmoRotate::X)
, m_y(parent, GLGizmoRotate::Y)
, m_z(parent, GLGizmoRotate::Z)
{
m_x.set_group_id(0);
m_y.set_group_id(1);
m_z.set_group_id(2);
m_gizmos.emplace_back(parent, GLGizmoRotate::X);
m_gizmos.emplace_back(parent, GLGizmoRotate::Y);
m_gizmos.emplace_back(parent, GLGizmoRotate::Z);
for (unsigned int i = 0; i < 3; ++i)
{
m_gizmos[i].set_group_id(i);
}
}
bool GLGizmoRotate3D::on_init()
{
if (!m_x.init() || !m_y.init() || !m_z.init())
return false;
for (GLGizmoRotate& g : m_gizmos)
{
if (!g.init())
return false;
}
m_x.set_highlight_color(AXES_COLOR[0]);
m_y.set_highlight_color(AXES_COLOR[1]);
m_z.set_highlight_color(AXES_COLOR[2]);
for (unsigned int i = 0; i < 3; ++i)
{
m_gizmos[i].set_highlight_color(AXES_COLOR[i]);
}
std::string path = resources_dir() + "/icons/overlay/";
@ -544,68 +557,28 @@ bool GLGizmoRotate3D::on_init()
return true;
}
void GLGizmoRotate3D::on_start_dragging()
void GLGizmoRotate3D::on_start_dragging(const BoundingBoxf3& box)
{
switch (m_hover_id)
{
case 0:
{
m_x.start_dragging();
break;
}
case 1:
{
m_y.start_dragging();
break;
}
case 2:
{
m_z.start_dragging();
break;
}
default:
{
break;
}
}
if ((0 <= m_hover_id) && (m_hover_id < 3))
m_gizmos[m_hover_id].start_dragging(box);
}
void GLGizmoRotate3D::on_stop_dragging()
{
switch (m_hover_id)
{
case 0:
{
m_x.stop_dragging();
break;
}
case 1:
{
m_y.stop_dragging();
break;
}
case 2:
{
m_z.stop_dragging();
break;
}
default:
{
break;
}
}
if ((0 <= m_hover_id) && (m_hover_id < 3))
m_gizmos[m_hover_id].stop_dragging();
}
void GLGizmoRotate3D::on_render(const BoundingBoxf3& box) const
{
if ((m_hover_id == -1) || (m_hover_id == 0))
m_x.render(box);
m_gizmos[X].render(box);
if ((m_hover_id == -1) || (m_hover_id == 1))
m_y.render(box);
m_gizmos[Y].render(box);
if ((m_hover_id == -1) || (m_hover_id == 2))
m_z.render(box);
m_gizmos[Z].render(box);
}
const float GLGizmoScale3D::Offset = 5.0f;
@ -652,13 +625,13 @@ bool GLGizmoScale3D::on_init()
return true;
}
void GLGizmoScale3D::on_start_dragging()
void GLGizmoScale3D::on_start_dragging(const BoundingBoxf3& box)
{
if (m_hover_id != -1)
{
m_starting_drag_position = m_grabbers[m_hover_id].center;
m_show_starting_box = true;
m_starting_box = m_box;
m_starting_box = box;
}
}
@ -989,7 +962,8 @@ double GLGizmoScale3D::calc_ratio(unsigned int preferred_plane_id, const Linef3&
GLGizmoFlatten::GLGizmoFlatten(GLCanvas3D& parent)
: GLGizmoBase(parent)
, m_normal(0.0, 0.0, 0.0)
, m_normal(Vec3d::Zero())
, m_center(Vec3d::Zero())
{
}
@ -1012,10 +986,13 @@ bool GLGizmoFlatten::on_init()
return true;
}
void GLGizmoFlatten::on_start_dragging()
void GLGizmoFlatten::on_start_dragging(const BoundingBoxf3& box)
{
if (m_hover_id != -1)
{
m_normal = m_planes[m_hover_id].normal;
m_center = box.center();
}
}
void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const
@ -1023,10 +1000,9 @@ void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const
// the dragged_offset is a vector measuring where was the object moved
// with the gizmo being on. This is reset in set_flattening_data and
// does not work correctly when there are multiple copies.
if (!m_center) // this is the first bounding box that we see
m_center.reset(new Vec3d(box.center()));
Vec3d dragged_offset = box.center() - *m_center;
Vec3d dragged_offset(Vec3d::Zero());
if (m_dragging)
dragged_offset = box.center() - m_center;
::glEnable(GL_BLEND);
::glEnable(GL_DEPTH_TEST);
@ -1073,7 +1049,6 @@ void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const
void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object)
{
m_center.release(); // object is not being dragged (this would not be called otherwise) - we must forget about the bounding box position...
m_model_object = model_object;
// ...and save the updated positions of the object instances:

View File

@ -57,6 +57,7 @@ protected:
// 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;
bool m_dragging;
float m_base_color[3];
float m_drag_color[3];
float m_highlight_color[3];
@ -82,10 +83,11 @@ public:
void set_highlight_color(const float* color);
void start_dragging();
void start_dragging(const BoundingBoxf3& box);
void stop_dragging();
bool is_dragging() const { return m_dragging; }
void update(const Linef3& mouse_ray);
void refresh() { on_refresh(); }
void render(const BoundingBoxf3& box) const { on_render(box); }
void render_for_picking(const BoundingBoxf3& box) const { on_render_for_picking(box); }
@ -94,10 +96,9 @@ protected:
virtual bool on_init() = 0;
virtual void on_set_state() {}
virtual void on_set_hover_id() {}
virtual void on_start_dragging() {}
virtual void on_start_dragging(const BoundingBoxf3& box) {}
virtual void on_stop_dragging() {}
virtual void on_update(const Linef3& mouse_ray) = 0;
virtual void on_refresh() {}
virtual void on_render(const BoundingBoxf3& box) const = 0;
virtual void on_render_for_picking(const BoundingBoxf3& box) const = 0;
@ -136,7 +137,6 @@ private:
mutable Vec3d m_center;
mutable float m_radius;
mutable bool m_keep_initial_values;
public:
GLGizmoRotate(GLCanvas3D& parent, Axis axis);
@ -146,9 +146,8 @@ public:
protected:
virtual bool on_init();
virtual void on_set_state() { m_keep_initial_values = (m_state == On) ? false : true; }
virtual void on_start_dragging(const BoundingBoxf3& box);
virtual void on_update(const Linef3& mouse_ray);
virtual void on_refresh() { m_keep_initial_values = false; }
virtual void on_render(const BoundingBoxf3& box) const;
virtual void on_render_for_picking(const BoundingBoxf3& box) const;
@ -167,56 +166,52 @@ private:
class GLGizmoRotate3D : public GLGizmoBase
{
GLGizmoRotate m_x;
GLGizmoRotate m_y;
GLGizmoRotate m_z;
std::vector<GLGizmoRotate> m_gizmos;
public:
explicit GLGizmoRotate3D(GLCanvas3D& parent);
double get_angle_x() const { return m_x.get_angle(); }
void set_angle_x(double angle) { m_x.set_angle(angle); }
double get_angle_x() const { return m_gizmos[X].get_angle(); }
void set_angle_x(double angle) { m_gizmos[X].set_angle(angle); }
double get_angle_y() const { return m_y.get_angle(); }
void set_angle_y(double angle) { m_y.set_angle(angle); }
double get_angle_y() const { return m_gizmos[Y].get_angle(); }
void set_angle_y(double angle) { m_gizmos[Y].set_angle(angle); }
double get_angle_z() const { return m_z.get_angle(); }
void set_angle_z(double angle) { m_z.set_angle(angle); }
double get_angle_z() const { return m_gizmos[Z].get_angle(); }
void set_angle_z(double angle) { m_gizmos[Z].set_angle(angle); }
protected:
virtual bool on_init();
virtual void on_set_state()
{
m_x.set_state(m_state);
m_y.set_state(m_state);
m_z.set_state(m_state);
for (GLGizmoRotate& g : m_gizmos)
{
g.set_state(m_state);
}
}
virtual void on_set_hover_id()
{
m_x.set_hover_id(m_hover_id == 0 ? 0 : -1);
m_y.set_hover_id(m_hover_id == 1 ? 0 : -1);
m_z.set_hover_id(m_hover_id == 2 ? 0 : -1);
for (unsigned int i = 0; i < 3; ++i)
{
m_gizmos[i].set_hover_id((m_hover_id == i) ? 0 : -1);
}
}
virtual void on_start_dragging();
virtual void on_start_dragging(const BoundingBoxf3& box);
virtual void on_stop_dragging();
virtual void on_update(const Linef3& mouse_ray)
{
m_x.update(mouse_ray);
m_y.update(mouse_ray);
m_z.update(mouse_ray);
}
virtual void on_refresh()
{
m_x.refresh();
m_y.refresh();
m_z.refresh();
for (GLGizmoRotate& g : m_gizmos)
{
g.update(mouse_ray);
}
}
virtual void on_render(const BoundingBoxf3& box) const;
virtual void on_render_for_picking(const BoundingBoxf3& box) const
{
m_x.render_for_picking(box);
m_y.render_for_picking(box);
m_z.render_for_picking(box);
for (const GLGizmoRotate& g : m_gizmos)
{
g.render_for_picking(box);
}
}
};
@ -249,7 +244,7 @@ public:
protected:
virtual bool on_init();
virtual void on_start_dragging();
virtual void on_start_dragging(const BoundingBoxf3& box);
virtual void on_stop_dragging() { m_show_starting_box = false; }
virtual void on_update(const Linef3& mouse_ray);
virtual void on_render(const BoundingBoxf3& box) const;
@ -291,7 +286,7 @@ private:
std::vector<PlaneData> m_planes;
std::vector<Vec2d> m_instances_positions;
mutable std::unique_ptr<Vec3d> m_center = nullptr;
Vec3d m_center;
const ModelObject* m_model_object = nullptr;
void update_planes();
@ -305,11 +300,12 @@ public:
protected:
virtual bool on_init();
virtual void on_start_dragging();
virtual void on_start_dragging(const BoundingBoxf3& box);
virtual void on_update(const Linef3& mouse_ray) {}
virtual void on_render(const BoundingBoxf3& box) const;
virtual void on_render_for_picking(const BoundingBoxf3& box) const;
virtual void on_set_state() {
virtual void on_set_state()
{
if (m_state == On && is_plane_update_necessary())
update_planes();
}