From d320a03c545de70a24d2ced024334934f5823a44 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Mon, 3 Feb 2020 21:27:53 +0100 Subject: [PATCH] DoubleSlider: Added using of default colors for color changes in SingleExtruder Mode (#3449) + added missed input parameter to the BitmapCache::mksolid() which was cause a wrong drawing of the Extruder selector under OSX --- src/slic3r/GUI/BitmapCache.hpp | 2 +- src/slic3r/GUI/DoubleSlider.cpp | 41 +++++++++++++++++++++++++-------- src/slic3r/GUI/DoubleSlider.hpp | 14 ++++++++--- src/slic3r/GUI/GUI_Preview.cpp | 7 ++++++ 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/slic3r/GUI/BitmapCache.hpp b/src/slic3r/GUI/BitmapCache.hpp index dd3e6ffc0..e12beb5c7 100644 --- a/src/slic3r/GUI/BitmapCache.hpp +++ b/src/slic3r/GUI/BitmapCache.hpp @@ -35,7 +35,7 @@ public: wxBitmap* load_svg(const std::string &bitmap_key, unsigned width = 0, unsigned height = 0, const bool grayscale = false, const bool dark_mode = false); /*static */wxBitmap mksolid(size_t width, size_t height, unsigned char r, unsigned char g, unsigned char b, unsigned char transparency, bool suppress_scaling = false); - /*static */wxBitmap mksolid(size_t width, size_t height, const unsigned char rgb[3], bool suppress_scaling = false) { return mksolid(width, height, rgb[0], rgb[1], rgb[2], wxALPHA_OPAQUE); } + /*static */wxBitmap mksolid(size_t width, size_t height, const unsigned char rgb[3], bool suppress_scaling = false) { return mksolid(width, height, rgb[0], rgb[1], rgb[2], wxALPHA_OPAQUE, suppress_scaling); } /*static */wxBitmap mkclear(size_t width, size_t height) { return mksolid(width, height, 0, 0, 0, wxALPHA_TRANSPARENT); } static bool parse_color(const std::string& scolor, unsigned char* rgb_out); diff --git a/src/slic3r/GUI/DoubleSlider.cpp b/src/slic3r/GUI/DoubleSlider.cpp index 4a5bea9a1..5990984b0 100644 --- a/src/slic3r/GUI/DoubleSlider.cpp +++ b/src/slic3r/GUI/DoubleSlider.cpp @@ -1,4 +1,5 @@ #include "wxExtensions.hpp" +#include "libslic3r/GCode/PreviewData.hpp" #include "GUI.hpp" #include "GUI_App.hpp" #include "I18N.hpp" @@ -322,7 +323,8 @@ void Control::SetTicksValues(const CustomGCode::Info& custom_gcode_per_print_z) // Switch to the "Feature type"/"Tool" from the very beginning of a new object slicing after deleting of the old one post_ticks_changed_event(); - m_ticks.mode = custom_gcode_per_print_z.mode; + if (custom_gcode_per_print_z.mode) + m_ticks.mode = custom_gcode_per_print_z.mode; Refresh(); Update(); @@ -1169,6 +1171,8 @@ void Control::OnKeyDown(wxKeyEvent &event) m_ticks.suppress_minus(true); delete_current_tick(); } + else if (event.GetKeyCode() == WXK_SHIFT) + UseDefaultColors(false); else if (is_horizontal()) { if (key == WXK_LEFT || key == WXK_RIGHT) @@ -1194,6 +1198,9 @@ void Control::OnKeyUp(wxKeyEvent &event) { if (event.GetKeyCode() == WXK_CONTROL) m_is_one_layer = false; + else if (event.GetKeyCode() == WXK_SHIFT) + UseDefaultColors(true); + Refresh(); Update(); event.Skip(); @@ -1278,9 +1285,11 @@ std::array Control::get_active_extruders_for_tick(int tick) const // Get used extruders for tick. // Means all extruders(tools) which will be used during printing from current tick to the end -std::set TickCodeInfo::get_used_extruders_for_tick(int tick, int only_extruder, double print_z) const +std::set TickCodeInfo::get_used_extruders_for_tick(int tick, int only_extruder, double print_z, t_mode force_mode/* = t_mode::Undef*/) const { - if (mode == t_mode::MultiExtruder) + t_mode e_mode = !force_mode ? mode : force_mode; + + if (e_mode == t_mode::MultiExtruder) { // #ys_FIXME: get tool ordering from _correct_ place const ToolOrdering& tool_ordering = GUI::wxGetApp().plater()->fff_print().get_tool_ordering(); @@ -1301,8 +1310,8 @@ std::set TickCodeInfo::get_used_extruders_for_tick(int tick, int only_extru return used_extruders; } - const int default_initial_extruder = mode == t_mode::MultiAsSingle ? std::max(only_extruder, 1) : 1; - if (ticks.empty()) + const int default_initial_extruder = e_mode == t_mode::MultiAsSingle ? std::max(only_extruder, 1) : 1; + if (ticks.empty() || e_mode == t_mode::SingleExtruder) return {default_initial_extruder}; std::set used_extruders; @@ -1346,10 +1355,13 @@ void Control::OnRightUp(wxMouseEvent& event) if (m_show_context_menu) { wxMenu menu; - if (m_mode == t_mode::SingleExtruder) + if (m_mode == t_mode::SingleExtruder) { append_menu_item(&menu, wxID_ANY, _(L("Add color change")) + " (M600)", "", [this](wxCommandEvent&) { add_code_as_tick(ColorChangeCode); }, "colorchange_add_m", &menu, [](){return true;}, this); + + UseDefaultColors(false); + } else { append_change_extruder_menu_item(&menu); @@ -1688,9 +1700,18 @@ bool Control::check_ticks_changed_event(const std::string& gcode) return true; } - std::string TickCodeInfo::get_color_for_tick(TickCode tick, const std::string& code, const int extruder) { + if (mode == t_mode::SingleExtruder && code == ColorChangeCode && m_use_default_colors) + { + const std::vector& colors = GCodePreviewData::ColorPrintColors(); + if (ticks.empty()) + return colors[0]; + m_default_color_idx++; + + return colors[m_default_color_idx % colors.size()]; + } + std::vector colors = GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); std::string color = colors[extruder - 1]; @@ -1740,6 +1761,9 @@ bool TickCodeInfo::add_tick(const int tick, std::string& code, const int extrude return false; } + if (mode == t_mode::SingleExtruder) + m_use_default_colors = true; + ticks.emplace(TickCode{ tick, code, extruder, color }); return true; } @@ -1840,7 +1864,7 @@ ConflictType TickCodeInfo::is_conflict_tick(const TickCode& tick, t_mode out_mod { // We should mark a tick as a "MeaninglessColorChange", // if it has a ColorChange for unused extruder from current print to end of the print - std::set used_extruders_for_tick = get_used_extruders_for_tick(tick.tick, only_extruder, print_z); + std::set used_extruders_for_tick = get_used_extruders_for_tick(tick.tick, only_extruder, print_z, out_mode); if (used_extruders_for_tick.find(tick.extruder) == used_extruders_for_tick.end()) return ctMeaninglessColorChange; @@ -1868,7 +1892,6 @@ ConflictType TickCodeInfo::is_conflict_tick(const TickCode& tick, t_mode out_mod { // We should mark a tick as a "MeaninglessToolChange", // if it has a ToolChange to the same extruder - auto it = ticks.find(tick); if (it == ticks.begin()) return tick.extruder == std::max(only_extruder, 1) ? ctMeaninglessToolChange : ctNone; diff --git a/src/slic3r/GUI/DoubleSlider.hpp b/src/slic3r/GUI/DoubleSlider.hpp index f14af621f..01181fc2f 100644 --- a/src/slic3r/GUI/DoubleSlider.hpp +++ b/src/slic3r/GUI/DoubleSlider.hpp @@ -67,12 +67,14 @@ class TickCodeInfo std::string pause_print_msg; bool m_suppress_plus = false; bool m_suppress_minus = false; + bool m_use_default_colors= false; + int m_default_color_idx = 0; std::string get_color_for_tick(TickCode tick, const std::string& code, const int extruder); public: - std::set ticks {}; - t_mode mode = t_mode::SingleExtruder; + std::set ticks {}; + t_mode mode = t_mode::Undef; bool empty() const { return ticks.empty(); } void set_pause_print_msg(const std::string& message) { pause_print_msg = message; } @@ -88,12 +90,13 @@ public: // Get used extruders for tick. // Means all extruders(tools) which will be used during printing from current tick to the end - std::set get_used_extruders_for_tick(int tick, int only_extruder, double print_z) const; + std::set get_used_extruders_for_tick(int tick, int only_extruder, double print_z, t_mode force_mode = t_mode::Undef) const; void suppress_plus (bool suppress) { m_suppress_plus = suppress; } void suppress_minus(bool suppress) { m_suppress_minus = suppress; } bool suppressed_plus () { return m_suppress_plus; } bool suppressed_minus() { return m_suppress_minus; } + void set_default_colors(bool default_colors_on) { m_use_default_colors = default_colors_on; } }; @@ -186,7 +189,11 @@ public: m_mode = !is_one_extruder_printed_model ? t_mode::MultiExtruder : only_extruder < 0 ? t_mode::SingleExtruder : t_mode::MultiAsSingle; + if (!m_ticks.mode) + m_ticks.mode = m_mode; m_only_extruder = only_extruder; + + UseDefaultColors(m_mode == t_mode::SingleExtruder); } bool is_horizontal() const { return m_style == wxSL_HORIZONTAL; } @@ -201,6 +208,7 @@ public: void OnLeftUp(wxMouseEvent& event); void OnEnterWin(wxMouseEvent& event) { enter_window(event, true); } void OnLeaveWin(wxMouseEvent& event) { enter_window(event, false); } + void UseDefaultColors(bool def_colors_on) { m_ticks.set_default_colors(def_colors_on); } void OnWheel(wxMouseEvent& event); void OnKeyDown(wxKeyEvent &event); void OnKeyUp(wxKeyEvent &event); diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index e42f8ed21..25862b72d 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -591,6 +591,11 @@ void Preview::create_double_slider() // sizer, m_canvas_widget m_canvas_widget->Bind(wxEVT_KEY_DOWN, &Preview::update_double_slider_from_canvas, this); + m_canvas_widget->Bind(wxEVT_KEY_UP, [this](wxKeyEvent& event) { + if (event.GetKeyCode() == WXK_SHIFT) + m_slider->UseDefaultColors(true); + event.Skip(); + }); m_slider->Bind(wxEVT_SCROLL_CHANGED, &Preview::on_sliders_scroll_changed, this); @@ -776,6 +781,8 @@ void Preview::update_double_slider_from_canvas(wxKeyEvent& event) } else if (key == 'S') m_slider->ChangeOneLayerLock(); + else if (key == WXK_SHIFT) + m_slider->UseDefaultColors(false); else event.Skip(); }