Auto scale of the toolbars in respect to the canvas size

This commit is contained in:
YuSanka 2020-05-12 23:07:06 +02:00
parent d928d6e47e
commit e7f3206250
9 changed files with 83 additions and 7 deletions

View File

@ -84,6 +84,9 @@ void AppConfig::set_defaults()
if (get("custom_toolbar_size").empty())
set("custom_toolbar_size", "100");
if (get("auto_toolbar_size").empty())
set("auto_toolbar_size", "100");
if (get("use_perspective_camera").empty())
set("use_perspective_camera", "1");

View File

@ -5477,6 +5477,44 @@ void GLCanvas3D::_render_selection_center() const
}
#endif // ENABLE_RENDER_SELECTION_CENTER
void GLCanvas3D::_check_and_update_toolbar_icon_scale() const
{
float scale = wxGetApp().toolbar_icon_scale();
Size cnv_size = get_canvas_size();
float size = GLToolbar::Default_Icons_Size * scale;
// Set current size for all top toolbars. It will be used for next calculations
#if ENABLE_RETINA_GL
const float sc = m_retina_helper->get_scale_factor() * scale;
m_main_toolbar.set_scale(sc);
m_undoredo_toolbar.set_scale(sc);
m_collapse_toolbar.set_scale(sc);
#else
m_main_toolbar.set_icons_size(size);
m_undoredo_toolbar.set_icons_size(size);
m_collapse_toolbar.set_icons_size(size);
#endif // ENABLE_RETINA_GL
float top_tb_width = m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + m_collapse_toolbar.get_width();
int items_cnt = m_main_toolbar.get_visible_items_cnt() + m_undoredo_toolbar.get_visible_items_cnt() + m_collapse_toolbar.get_visible_items_cnt();
float noitems_width = top_tb_width - size * items_cnt; // width of separators and borders in top toolbars
// calculate scale needed for items in all top toolbars
float new_h_scale = (cnv_size.get_width() - noitems_width) / (items_cnt * GLToolbar::Default_Icons_Size);
items_cnt = m_gizmos.get_selectable_icons_cnt() + 3; // +3 means a place for top and view toolbars and separators in gizmos toolbar
// calculate scale needed for items in the gizmos toolbar
float new_v_scale = cnv_size.get_height() / (items_cnt * GLGizmosManager::Default_Icons_Size);
// set minimum scale as a auto scale for the toolbars
float new_scale = std::min(new_h_scale, new_v_scale);
if (fabs(new_scale - scale) > EPSILON)
wxGetApp().set_auto_toolbar_icon_scale(new_scale);
}
void GLCanvas3D::_render_overlays() const
{
glsafe(::glDisable(GL_DEPTH_TEST));
@ -5489,6 +5527,8 @@ void GLCanvas3D::_render_overlays() const
double gui_scale = camera.get_gui_scale();
glsafe(::glScaled(gui_scale, gui_scale, 1.0));
_check_and_update_toolbar_icon_scale();
_render_gizmos_overlay();
_render_warning_texture();
_render_legend_texture();
@ -5496,12 +5536,12 @@ void GLCanvas3D::_render_overlays() const
// main toolbar and undoredo toolbar need to be both updated before rendering because both their sizes are needed
// to correctly place them
#if ENABLE_RETINA_GL
const float scale = m_retina_helper->get_scale_factor() * wxGetApp().toolbar_icon_scale(true);
const float scale = m_retina_helper->get_scale_factor() * wxGetApp().toolbar_icon_scale(/*true*/);
m_main_toolbar.set_scale(scale);
m_undoredo_toolbar.set_scale(scale);
m_collapse_toolbar.set_scale(scale);
#else
const float size = int(GLToolbar::Default_Icons_Size * wxGetApp().toolbar_icon_scale(true));
const float size = int(GLToolbar::Default_Icons_Size * wxGetApp().toolbar_icon_scale(/*true*/));
m_main_toolbar.set_icons_size(size);
m_undoredo_toolbar.set_icons_size(size);
m_collapse_toolbar.set_icons_size(size);
@ -5608,7 +5648,7 @@ void GLCanvas3D::_render_main_toolbar() const
float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom();
float top = 0.5f * (float)cnv_size.get_height() * inv_zoom;
float left = -0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width()) * inv_zoom;
float left = -0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + m_collapse_toolbar.get_width()) * inv_zoom;
m_main_toolbar.set_position(top, left);
m_main_toolbar.render(*this);
@ -5623,7 +5663,7 @@ void GLCanvas3D::_render_undoredo_toolbar() const
float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom();
float top = 0.5f * (float)cnv_size.get_height() * inv_zoom;
float left = (m_main_toolbar.get_width() - 0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width())) * inv_zoom;
float left = (m_main_toolbar.get_width() - 0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + m_collapse_toolbar.get_width())) * inv_zoom;
m_undoredo_toolbar.set_position(top, left);
m_undoredo_toolbar.render(*this);
}

View File

@ -754,6 +754,7 @@ private:
#if ENABLE_RENDER_SELECTION_CENTER
void _render_selection_center() const;
#endif // ENABLE_RENDER_SELECTION_CENTER
void _check_and_update_toolbar_icon_scale() const;
void _render_overlays() const;
void _render_warning_texture() const;
void _render_legend_texture() const;

View File

@ -627,6 +627,16 @@ float GLToolbar::get_main_size() const
return size * m_layout.scale;
}
int GLToolbar::get_visible_items_cnt() const
{
int cnt = 0;
for (unsigned int i = 0; i < (unsigned int)m_items.size(); ++i)
if (m_items[i]->is_visible() && !m_items[i]->is_separator())
cnt++;
return cnt;
}
void GLToolbar::do_action(GLToolbarItem::EActionType type, int item_id, GLCanvas3D& parent, bool check_hover)
{
if ((m_pressed_toggable_id == -1) || (m_pressed_toggable_id == item_id))

View File

@ -319,6 +319,7 @@ public:
void get_additional_tooltip(int item_id, std::string& text);
void set_additional_tooltip(int item_id, const std::string& text);
void set_tooltip(int item_id, const std::string& text);
int get_visible_items_cnt() const;
// returns true if any item changed its state
bool update_items_state();

View File

@ -580,17 +580,35 @@ float GUI_App::toolbar_icon_scale(const bool is_limited/* = false*/) const
const std::string& use_val = app_config->get("use_custom_toolbar_size");
const std::string& val = app_config->get("custom_toolbar_size");
const std::string& auto_val = app_config->get("auto_toolbar_size");
if (val.empty() || use_val.empty() || use_val == "0")
if (val.empty() || auto_val.empty() || use_val.empty())
return icon_sc;
int int_val = atoi(val.c_str());
int int_val = use_val == "0" ? 100 : atoi(val.c_str());
// correct value in respect to auto_toolbar_size
int_val = std::min(atoi(auto_val.c_str()), int_val);
if (is_limited && int_val < 50)
int_val = 50;
return 0.01f * int_val * icon_sc;
}
void GUI_App::set_auto_toolbar_icon_scale(float scale) const
{
#ifdef __APPLE__
const float icon_sc = 1.0f; // for Retina display will be used its own scale
#else
const float icon_sc = m_em_unit * 0.1f;
#endif // __APPLE__
int int_val = std::min(int(scale / icon_sc * 100), 100);
std::string val = std::to_string(int_val);
app_config->set("auto_toolbar_size", val);
}
void GUI_App::recreate_GUI(const wxString& msg_name)
{
mainframe->shutdown();

View File

@ -135,6 +135,7 @@ public:
const wxFont& normal_font() { return m_normal_font; }
int em_unit() const { return m_em_unit; }
float toolbar_icon_scale(const bool is_limited = false) const;
void set_auto_toolbar_icon_scale(float scale) const;
void recreate_GUI(const wxString& message);
void system_info();

View File

@ -226,6 +226,8 @@ public:
void update_after_undo_redo(const UndoRedo::Snapshot& snapshot);
int get_selectable_icons_cnt() const { return get_selectable_idxs().size(); }
private:
void render_background(float left, float top, float right, float bottom, float border) const;
void do_render_overlay() const;

View File

@ -7,7 +7,7 @@
#include <wx/menu.h>
#include <wx/progdlg.h>
#include <wx/tooltip.h>
#include <wx/glcanvas.h>
//#include <wx/glcanvas.h>
#include <wx/debug.h>
#include <boost/algorithm/string/predicate.hpp>