3DScene mouse wheel event moved to c++

This commit is contained in:
Enrico Turri 2018-05-28 15:23:01 +02:00
parent aacdcd4add
commit db260a669c
9 changed files with 148 additions and 116 deletions

View file

@ -1802,16 +1802,16 @@ void _3DScene::select_volume(wxGLCanvas* canvas, unsigned int id)
s_canvas_mgr.select_volume(canvas, id);
}
DynamicPrintConfig* _3DScene::get_config(wxGLCanvas* canvas)
{
return s_canvas_mgr.get_config(canvas);
}
void _3DScene::set_config(wxGLCanvas* canvas, DynamicPrintConfig* config)
{
s_canvas_mgr.set_config(canvas, config);
}
void _3DScene::set_print(wxGLCanvas* canvas, Print* print)
{
s_canvas_mgr.set_print(canvas, print);
}
void _3DScene::set_bed_shape(wxGLCanvas* canvas, const Pointfs& shape)
{
return s_canvas_mgr.set_bed_shape(canvas, shape);

View file

@ -559,8 +559,8 @@ public:
static void deselect_volumes(wxGLCanvas* canvas);
static void select_volume(wxGLCanvas* canvas, unsigned int id);
static DynamicPrintConfig* get_config(wxGLCanvas* canvas);
static void set_config(wxGLCanvas* canvas, DynamicPrintConfig* config);
static void set_print(wxGLCanvas* canvas, Print* print);
static void set_bed_shape(wxGLCanvas* canvas, const Pointfs& shape);
static void set_auto_bed_shape(wxGLCanvas* canvas);

View file

@ -1041,6 +1041,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, wxGLContext* context)
, m_context(context)
, m_volumes(nullptr)
, m_config(nullptr)
, m_print(nullptr)
, m_dirty(true)
, m_apply_zoom_to_volumes_filter(false)
, m_hover_volume_id(-1)
@ -1242,16 +1243,16 @@ void GLCanvas3D::select_volume(unsigned int id)
}
}
DynamicPrintConfig* GLCanvas3D::get_config()
{
return m_config;
}
void GLCanvas3D::set_config(DynamicPrintConfig* config)
{
m_config = config;
}
void GLCanvas3D::set_print(Print* print)
{
m_print = print;
}
void GLCanvas3D::set_bed_shape(const Pointfs& shape)
{
m_bed.set_shape(shape);
@ -1946,15 +1947,10 @@ void GLCanvas3D::on_size(wxSizeEvent& evt)
void GLCanvas3D::on_idle(wxIdleEvent& evt)
{
if (!is_dirty() || !is_shown_on_screen())
if (!is_dirty())
return;
if (m_canvas != nullptr)
{
const Size& cnv_size = get_canvas_size();
resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
m_canvas->Refresh();
}
_refresh_if_shown_on_screen();
}
void GLCanvas3D::on_char(wxKeyEvent& evt)
@ -1992,6 +1988,51 @@ void GLCanvas3D::on_char(wxKeyEvent& evt)
}
}
void GLCanvas3D::on_mouse_wheel(wxMouseEvent& evt)
{
// Ignore the wheel events if the middle button is pressed.
if (evt.MiddleIsDown())
return;
// Performs layers editing updates, if enabled
if (is_layers_editing_enabled() && (m_print != nullptr))
{
int object_idx_selected = get_layers_editing_first_selected_object_id((unsigned int)m_print->objects.size());
if (object_idx_selected != -1)
{
// A volume is selected. Test, whether hovering over a layer thickness bar.
if (bar_rect_contains((float)evt.GetX(), (float)evt.GetY()))
{
// Adjust the width of the selection.
set_layers_editing_band_width(std::max(std::min(get_layers_editing_band_width() * (1.0f + 0.1f * (float)evt.GetWheelRotation() / (float)evt.GetWheelDelta()), 10.0f), 1.5f));
if (m_canvas != nullptr)
m_canvas->Refresh();
return;
}
}
}
// Calculate the zoom delta and apply it to the current zoom factor
float zoom = (float)evt.GetWheelRotation() / (float)evt.GetWheelDelta();
zoom = std::max(std::min(zoom, 4.0f), -4.0f) / 10.0f;
zoom = get_camera_zoom() / (1.0f - zoom);
// Don't allow to zoom too far outside the scene.
float zoom_min = _get_zoom_to_bounding_box_factor(max_bounding_box());
if (zoom_min > 0.0f)
{
zoom_min *= 0.4f;
if (zoom < zoom_min)
zoom = zoom_min;
}
set_camera_zoom(zoom);
m_on_viewport_changed_callback.call();
_refresh_if_shown_on_screen();
}
Size GLCanvas3D::get_canvas_size() const
{
int w = 0;
@ -2024,13 +2065,7 @@ void GLCanvas3D::_zoom_to_bounding_box(const BoundingBoxf3& bbox)
m_on_viewport_changed_callback.call();
if (is_shown_on_screen())
{
const Size& cnv_size = get_canvas_size();
resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
if (m_canvas != nullptr)
m_canvas->Refresh();
}
_refresh_if_shown_on_screen();
}
}
@ -2126,5 +2161,16 @@ void GLCanvas3D::_deregister_callbacks()
m_on_mark_volumes_for_layer_height_callback.deregister_callback();
}
void GLCanvas3D::_refresh_if_shown_on_screen()
{
if (is_shown_on_screen())
{
const Size& cnv_size = get_canvas_size();
resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height());
if (m_canvas != nullptr)
m_canvas->Refresh();
}
}
} // namespace GUI
} // namespace Slic3r

View file

@ -10,6 +10,7 @@ class wxGLContext;
class wxSizeEvent;
class wxIdleEvent;
class wxKeyEvent;
class wxMouseEvent;
namespace Slic3r {
@ -302,6 +303,7 @@ private:
GLVolumeCollection* m_volumes;
DynamicPrintConfig* m_config;
Print* m_print;
bool m_dirty;
bool m_apply_zoom_to_volumes_filter;
@ -336,8 +338,8 @@ public:
void deselect_volumes();
void select_volume(unsigned int id);
DynamicPrintConfig* get_config();
void set_config(DynamicPrintConfig* config);
void set_print(Print* print);
// Set the bed shape to a single closed 2D polygon(array of two element arrays),
// triangulate the bed and store the triangles into m_bed.m_triangles,
@ -450,6 +452,7 @@ public:
void on_size(wxSizeEvent& evt);
void on_idle(wxIdleEvent& evt);
void on_char(wxKeyEvent& evt);
void on_mouse_wheel(wxMouseEvent& evt);
Size get_canvas_size() const;
Point get_local_mouse_position() const;
@ -459,6 +462,8 @@ private:
float _get_zoom_to_bounding_box_factor(const BoundingBoxf3& bbox) const;
void _deregister_callbacks();
void _refresh_if_shown_on_screen();
};
} // namespace GUI

View file

@ -75,6 +75,7 @@ bool GLCanvas3DManager::add(wxGLCanvas* canvas, wxGLContext* context)
canvas->Bind(wxEVT_SIZE, [canvas3D](wxSizeEvent& evt) { canvas3D->on_size(evt); });
canvas->Bind(wxEVT_IDLE, [canvas3D](wxIdleEvent& evt) { canvas3D->on_idle(evt); });
canvas->Bind(wxEVT_CHAR, [canvas3D](wxKeyEvent& evt) { canvas3D->on_char(evt); });
canvas->Bind(wxEVT_MOUSEWHEEL, [canvas3D](wxMouseEvent& evt) { canvas3D->on_mouse_wheel(evt); });
m_canvases.insert(CanvasesMap::value_type(canvas, canvas3D));
@ -203,12 +204,6 @@ void GLCanvas3DManager::select_volume(wxGLCanvas* canvas, unsigned int id)
it->second->select_volume(id);
}
DynamicPrintConfig* GLCanvas3DManager::get_config(wxGLCanvas* canvas)
{
CanvasesMap::const_iterator it = _get_canvas(canvas);
return (it != m_canvases.end()) ? it->second->get_config() : nullptr;
}
void GLCanvas3DManager::set_config(wxGLCanvas* canvas, DynamicPrintConfig* config)
{
CanvasesMap::iterator it = _get_canvas(canvas);
@ -216,6 +211,13 @@ void GLCanvas3DManager::set_config(wxGLCanvas* canvas, DynamicPrintConfig* confi
it->second->set_config(config);
}
void GLCanvas3DManager::set_print(wxGLCanvas* canvas, Print* print)
{
CanvasesMap::iterator it = _get_canvas(canvas);
if (it != m_canvases.end())
it->second->set_print(print);
}
void GLCanvas3DManager::set_bed_shape(wxGLCanvas* canvas, const Pointfs& shape)
{
CanvasesMap::iterator it = _get_canvas(canvas);

View file

@ -59,8 +59,8 @@ public:
void deselect_volumes(wxGLCanvas* canvas);
void select_volume(wxGLCanvas* canvas, unsigned int id);
DynamicPrintConfig* get_config(wxGLCanvas* canvas);
void set_config(wxGLCanvas* canvas, DynamicPrintConfig* config);
void set_print(wxGLCanvas* canvas, Print* print);
void set_bed_shape(wxGLCanvas* canvas, const Pointfs& shape);
void set_auto_bed_shape(wxGLCanvas* canvas);