Merge remote-tracking branch 'origin/vb_undo_redo' into ys_undo_redo

This commit is contained in:
YuSanka 2019-07-11 16:00:28 +02:00
commit eced3dcea8
4 changed files with 127 additions and 60 deletions

View file

@ -3550,7 +3550,7 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Add...")) + " [" + GUI::shortkey_ctrl_prefix() + "I]";
item.sprite_id = 0;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_ADD)); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_ADD)); };
if (!m_toolbar.add_item(item))
return false;
@ -3560,8 +3560,8 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Delete")) + " [Del]";
item.sprite_id = 1;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_DELETE)); };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_delete(); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_DELETE)); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_delete(); };
if (!m_toolbar.add_item(item))
return false;
@ -3571,8 +3571,8 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Delete all")) + " [" + GUI::shortkey_ctrl_prefix() + "Del]";
item.sprite_id = 2;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_DELETE_ALL)); };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_delete_all(); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_DELETE_ALL)); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_delete_all(); };
if (!m_toolbar.add_item(item))
return false;
@ -3582,8 +3582,8 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Arrange")) + " [A]";
item.sprite_id = 3;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_ARRANGE)); };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_arrange(); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_ARRANGE)); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_arrange(); };
if (!m_toolbar.add_item(item))
return false;
@ -3596,8 +3596,8 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Copy")) + " [" + GUI::shortkey_ctrl_prefix() + "C]";
item.sprite_id = 4;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_COPY)); };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_copy_to_clipboard(); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_COPY)); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_copy_to_clipboard(); };
if (!m_toolbar.add_item(item))
return false;
@ -3607,8 +3607,8 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Paste")) + " [" + GUI::shortkey_ctrl_prefix() + "V]";
item.sprite_id = 5;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_PASTE)); };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_paste_from_clipboard(); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_PASTE)); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_paste_from_clipboard(); };
if (!m_toolbar.add_item(item))
return false;
@ -3621,9 +3621,10 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Add instance")) + " [+]";
item.sprite_id = 6;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_MORE)); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_MORE)); };
item.visibility_callback = []()->bool { return wxGetApp().get_mode() != comSimple; };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_increase_instances(); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_increase_instances(); };
if (!m_toolbar.add_item(item))
return false;
@ -3633,9 +3634,9 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Remove instance")) + " [-]";
item.sprite_id = 7;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_FEWER)); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_FEWER)); };
item.visibility_callback = []()->bool { return wxGetApp().get_mode() != comSimple; };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_decrease_instances(); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_decrease_instances(); };
if (!m_toolbar.add_item(item))
return false;
@ -3648,9 +3649,9 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Split to objects"));
item.sprite_id = 8;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_SPLIT_OBJECTS)); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_SPLIT_OBJECTS)); };
item.visibility_callback = GLToolbarItem::Default_Visibility_Callback;
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_split_to_objects(); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_split_to_objects(); };
if (!m_toolbar.add_item(item))
return false;
@ -3660,9 +3661,9 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Split to parts"));
item.sprite_id = 9;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_SPLIT_VOLUMES)); };
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_SPLIT_VOLUMES)); };
item.visibility_callback = []()->bool { return wxGetApp().get_mode() != comSimple; };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_split_to_volumes(); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_split_to_volumes(); };
if (!m_toolbar.add_item(item))
return false;
@ -3675,10 +3676,10 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Layers editing"));
item.sprite_id = 10;
item.is_toggable = true;
item.action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_LAYERSEDITING)); };
item.left_toggable = true;
item.left_action_callback = [this]() { if (m_canvas != nullptr) wxPostEvent(m_canvas, SimpleEvent(EVT_GLTOOLBAR_LAYERSEDITING)); };
item.visibility_callback = [this]()->bool { return m_process->current_printer_technology() == ptFFF; };
item.enabled_state_callback = []()->bool { return wxGetApp().plater()->can_layers_editing(); };
item.enabling_callback = []()->bool { return wxGetApp().plater()->can_layers_editing(); };
if (!m_toolbar.add_item(item))
return false;
@ -3691,10 +3692,13 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Undo")) + " [" + GUI::shortkey_ctrl_prefix() + "Z]";
item.sprite_id = 11;
item.action_callback = [this]() { m_imgui_undo_redo_hovered_pos = -1; };
item.left_toggable = false;
item.right_toggable = true;
item.left_action_callback = [this]() { post_event(SimpleEvent(EVT_GLCANVAS_UNDO)); };
item.right_action_callback = [this]() { m_imgui_undo_redo_hovered_pos = -1; };
item.visibility_callback = []()->bool { return true; };
item.enabled_state_callback = [this]()->bool { return wxGetApp().plater()->can_undo(); } ;
item.render_callback = [this](float left, float right, float, float) { if (m_canvas != nullptr) _render_undo_redo_stack(true, 0.5f * (left + right)); };
item.enabling_callback = [this]()->bool { return wxGetApp().plater()->can_undo(); };
item.right_render_callback = [this](float left, float right, float, float) { if (m_canvas != nullptr) _render_undo_redo_stack(true, 0.5f * (left + right)); };
if (!m_toolbar.add_item(item))
return false;
@ -3704,8 +3708,10 @@ bool GLCanvas3D::_init_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Redo")) + " [" + GUI::shortkey_ctrl_prefix() + "Y]";
item.sprite_id = 12;
item.enabled_state_callback = [this]()->bool { return wxGetApp().plater()->can_redo(); };
item.render_callback = [this](float left, float right, float, float) { if (m_canvas != nullptr) _render_undo_redo_stack(false, 0.5f * (left + right)); };
item.left_action_callback = [this]() { post_event(SimpleEvent(EVT_GLCANVAS_REDO)); };
item.right_action_callback = [this]() { m_imgui_undo_redo_hovered_pos = -1; };
item.enabling_callback = [this]()->bool { return wxGetApp().plater()->can_redo(); };
item.right_render_callback = [this](float left, float right, float, float) { if (m_canvas != nullptr) _render_undo_redo_stack(false, 0.5f * (left + right)); };
if (!m_toolbar.add_item(item))
return false;
@ -5877,12 +5883,12 @@ bool GLCanvas3D::_deactivate_undo_redo_toolbar_items()
{
if (m_toolbar.is_item_pressed("undo"))
{
m_toolbar.force_action(m_toolbar.get_item_id("undo"), *this);
m_toolbar.force_right_action(m_toolbar.get_item_id("undo"), *this);
return true;
}
else if (m_toolbar.is_item_pressed("redo"))
{
m_toolbar.force_action(m_toolbar.get_item_id("redo"), *this);
m_toolbar.force_right_action(m_toolbar.get_item_id("redo"), *this);
return true;
}

View file

@ -34,7 +34,7 @@ wxDEFINE_EVENT(EVT_GLVIEWTOOLBAR_PREVIEW, SimpleEvent);
const GLToolbarItem::ActionCallback GLToolbarItem::Default_Action_Callback = [](){};
const GLToolbarItem::VisibilityCallback GLToolbarItem::Default_Visibility_Callback = []()->bool { return true; };
const GLToolbarItem::EnabledStateCallback GLToolbarItem::Default_Enabled_State_Callback = []()->bool { return true; };
const GLToolbarItem::EnablingCallback GLToolbarItem::Default_Enabling_Callback = []()->bool { return true; };
const GLToolbarItem::RenderCallback GLToolbarItem::Default_Render_Callback = [](float, float, float, float){};
GLToolbarItem::Data::Data()
@ -44,12 +44,15 @@ GLToolbarItem::Data::Data()
#endif // ENABLE_SVG_ICONS
, tooltip("")
, sprite_id(-1)
, is_toggable(false)
, left_toggable(false)
, right_toggable(false)
, visible(true)
, action_callback(Default_Action_Callback)
, left_action_callback(Default_Action_Callback)
, right_action_callback(Default_Action_Callback)
, visibility_callback(Default_Visibility_Callback)
, enabled_state_callback(Default_Enabled_State_Callback)
, render_callback(Default_Render_Callback)
, enabling_callback(Default_Enabling_Callback)
, left_render_callback(nullptr)
, right_render_callback(nullptr)
{
}
@ -57,6 +60,7 @@ GLToolbarItem::GLToolbarItem(GLToolbarItem::EType type, const GLToolbarItem::Dat
: m_type(type)
, m_state(Normal)
, m_data(data)
, m_last_action(Undefined)
{
}
@ -72,7 +76,7 @@ bool GLToolbarItem::update_visibility()
bool GLToolbarItem::update_enabled_state()
{
bool enabled = m_data.enabled_state_callback();
bool enabled = m_data.enabling_callback();
bool ret = (is_enabled() != enabled);
if (ret)
m_state = enabled ? GLToolbarItem::Normal : GLToolbarItem::Disabled;
@ -84,8 +88,13 @@ void GLToolbarItem::render(unsigned int tex_id, float left, float right, float b
{
GLTexture::render_sub_texture(tex_id, left, right, bottom, top, get_uvs(tex_width, tex_height, icon_size));
if (is_toggable() && is_pressed())
m_data.render_callback(left, right, bottom, top);
if (is_pressed())
{
if ((m_last_action == Left) && m_data.left_toggable && (m_data.left_render_callback != nullptr))
m_data.left_render_callback(left, right, bottom, top);
else if ((m_last_action == Right) && m_data.right_toggable && (m_data.right_render_callback != nullptr))
m_data.right_render_callback(left, right, bottom, top);
}
}
GLTexture::Quad_UVs GLToolbarItem::get_uvs(unsigned int tex_width, unsigned int tex_height, unsigned int icon_size) const
@ -400,9 +409,14 @@ unsigned int GLToolbar::get_item_id(const std::string& name) const
return -1;
}
void GLToolbar::force_action(unsigned int item_id, GLCanvas3D& parent)
void GLToolbar::force_left_action(unsigned int item_id, GLCanvas3D& parent)
{
do_action(item_id, parent, false);
do_action(GLToolbarItem::Left, item_id, parent, false);
}
void GLToolbar::force_right_action(unsigned int item_id, GLCanvas3D& parent)
{
do_action(GLToolbarItem::Right, item_id, parent, false);
}
bool GLToolbar::update_items_state()
@ -476,9 +490,12 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent)
m_mouse_capture.left = true;
m_mouse_capture.parent = &parent;
processed = true;
if ((item_id != -2) && !m_items[item_id]->is_separator())
if ((item_id != -2) && !m_items[item_id]->is_separator() && ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action() == GLToolbarItem::Left)))
{
// mouse is inside an icon
do_action((unsigned int)item_id, parent, true);
do_action(GLToolbarItem::Left, (unsigned int)item_id, parent, true);
parent.set_as_dirty();
}
}
else if (evt.MiddleDown())
{
@ -489,6 +506,13 @@ bool GLToolbar::on_mouse(wxMouseEvent& evt, GLCanvas3D& parent)
{
m_mouse_capture.right = true;
m_mouse_capture.parent = &parent;
processed = true;
if ((item_id != -2) && !m_items[item_id]->is_separator() && ((m_pressed_toggable_id == -1) || (m_items[item_id]->get_last_action() == GLToolbarItem::Right)))
{
// mouse is inside an icon
do_action(GLToolbarItem::Right, (unsigned int)item_id, parent, true);
parent.set_as_dirty();
}
}
else if (evt.LeftUp())
processed = true;
@ -586,7 +610,7 @@ float GLToolbar::get_main_size() const
return size;
}
void GLToolbar::do_action(unsigned int item_id, GLCanvas3D& parent, bool check_hover)
void GLToolbar::do_action(GLToolbarItem::EActionType type, unsigned int item_id, GLCanvas3D& parent, bool check_hover)
{
if ((m_pressed_toggable_id == -1) || (m_pressed_toggable_id == item_id))
{
@ -595,7 +619,8 @@ void GLToolbar::do_action(unsigned int item_id, GLCanvas3D& parent, bool check_h
GLToolbarItem* item = m_items[item_id];
if ((item != nullptr) && !item->is_separator() && (!check_hover || item->is_hovered()))
{
if (item->is_toggable())
if (((type == GLToolbarItem::Right) && item->is_right_toggable()) ||
((type == GLToolbarItem::Left) && item->is_left_toggable()))
{
GLToolbarItem::EState state = item->get_state();
if (state == GLToolbarItem::Hover)
@ -608,9 +633,15 @@ void GLToolbar::do_action(unsigned int item_id, GLCanvas3D& parent, bool check_h
item->set_state(GLToolbarItem::Pressed);
m_pressed_toggable_id = item->is_pressed() ? item_id : -1;
item->reset_last_action();
parent.render();
item->do_action();
switch (type)
{
default:
case GLToolbarItem::Left: { item->do_left_action(); break; }
case GLToolbarItem::Right: { item->do_right_action(); break; }
}
}
else
{
@ -619,8 +650,15 @@ void GLToolbar::do_action(unsigned int item_id, GLCanvas3D& parent, bool check_h
else
item->set_state(item->is_hovered() ? GLToolbarItem::HoverPressed : GLToolbarItem::Pressed);
item->reset_last_action();
parent.render();
item->do_action();
switch (type)
{
default:
case GLToolbarItem::Left: { item->do_left_action(); break; }
case GLToolbarItem::Right: { item->do_right_action(); break; }
}
if ((m_type == Normal) && (item->get_state() != GLToolbarItem::Disabled))
{
// the item may get disabled during the action, if not, set it back to hover state

View file

@ -36,8 +36,8 @@ class GLToolbarItem
public:
typedef std::function<void()> ActionCallback;
typedef std::function<bool()> VisibilityCallback;
typedef std::function<bool()> EnabledStateCallback;
typedef std::function<void(float, float,float, float)> RenderCallback;
typedef std::function<bool()> EnablingCallback;
typedef std::function<void(float, float, float, float)> RenderCallback;
enum EType : unsigned char
{
@ -46,6 +46,14 @@ public:
Num_Types
};
enum EActionType : unsigned char
{
Undefined,
Left,
Right,
Num_Action_Types
};
enum EState : unsigned char
{
Normal,
@ -64,25 +72,33 @@ public:
#endif // ENABLE_SVG_ICONS
std::string tooltip;
unsigned int sprite_id;
bool is_toggable;
bool left_toggable;
bool right_toggable;
bool visible;
ActionCallback action_callback;
// action on left click
ActionCallback left_action_callback;
// action on right click
ActionCallback right_action_callback;
VisibilityCallback visibility_callback;
EnabledStateCallback enabled_state_callback;
RenderCallback render_callback;
EnablingCallback enabling_callback;
// render callback on left click
RenderCallback left_render_callback;
// render callback on right click
RenderCallback right_render_callback;
Data();
};
static const ActionCallback Default_Action_Callback;
static const VisibilityCallback Default_Visibility_Callback;
static const EnabledStateCallback Default_Enabled_State_Callback;
static const EnablingCallback Default_Enabling_Callback;
static const RenderCallback Default_Render_Callback;
private:
EType m_type;
EState m_state;
Data m_data;
EActionType m_last_action;
public:
GLToolbarItem(EType type, const Data& data);
@ -96,17 +112,25 @@ public:
#endif // ENABLE_SVG_ICONS
const std::string& get_tooltip() const { return m_data.tooltip; }
void do_action() { m_data.action_callback(); }
void do_left_action() { m_last_action = Left; m_data.left_action_callback(); }
void do_right_action() { m_last_action = Right; m_data.right_action_callback(); }
bool is_enabled() const { return m_state != Disabled; }
bool is_disabled() const { return m_state == Disabled; }
bool is_hovered() const { return (m_state == Hover) || (m_state == HoverPressed); }
bool is_pressed() const { return (m_state == Pressed) || (m_state == HoverPressed); }
bool is_toggable() const { return m_data.is_toggable; }
bool is_left_toggable() const { return m_data.left_toggable; }
bool is_right_toggable() const { return m_data.right_toggable; }
bool is_visible() const { return m_data.visible; }
bool is_separator() const { return m_type == Separator; }
bool has_left_render_callback() const { return m_data.left_render_callback != nullptr; }
bool has_right_render_callback() const { return m_data.right_render_callback != nullptr; }
EActionType get_last_action() const { return m_last_action; }
void reset_last_action() { m_last_action = Undefined; }
// returns true if the state changes
bool update_visibility();
// returns true if the state changes
@ -303,7 +327,8 @@ public:
unsigned int get_item_id(const std::string& name) const;
void force_action(unsigned int item_id, GLCanvas3D& parent);
void force_left_action(unsigned int item_id, GLCanvas3D& parent);
void force_right_action(unsigned int item_id, GLCanvas3D& parent);
const std::string& get_tooltip() const { return m_tooltip; }
@ -321,7 +346,7 @@ private:
float get_height_horizontal() const;
float get_height_vertical() const;
float get_main_size() const;
void do_action(unsigned int item_id, GLCanvas3D& parent, bool check_hover);
void do_action(GLToolbarItem::EActionType type, unsigned int item_id, GLCanvas3D& parent, bool check_hover);
std::string update_hover_state(const Vec2d& mouse_pos, GLCanvas3D& parent);
std::string update_hover_state_horizontal(const Vec2d& mouse_pos, GLCanvas3D& parent);
std::string update_hover_state_vertical(const Vec2d& mouse_pos, GLCanvas3D& parent);

View file

@ -3432,8 +3432,7 @@ void Plater::priv::init_view_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("3D editor view")) + " [" + GUI::shortkey_ctrl_prefix() + "5]";
item.sprite_id = 0;
item.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_3D)); };
item.is_toggable = false;
item.left_action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_3D)); };
if (!view_toolbar.add_item(item))
return;
@ -3443,8 +3442,7 @@ void Plater::priv::init_view_toolbar()
#endif // ENABLE_SVG_ICONS
item.tooltip = _utf8(L("Preview")) + " [" + GUI::shortkey_ctrl_prefix() + "6]";
item.sprite_id = 1;
item.action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_PREVIEW)); };
item.is_toggable = false;
item.left_action_callback = [this]() { if (this->q != nullptr) wxPostEvent(this->q, SimpleEvent(EVT_GLVIEWTOOLBAR_PREVIEW)); };
if (!view_toolbar.add_item(item))
return;