From f2120c11227bafa5fa8d3f32e865eebe5c95d92d Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 7 Nov 2019 16:36:24 +0100 Subject: [PATCH] Fixed a sequence of the legend items --- src/libslic3r/GCode/PreviewData.cpp | 6 +-- src/libslic3r/GCode/PreviewData.hpp | 2 +- src/slic3r/GUI/GLCanvas3D.cpp | 78 ++++++++++++++++++++++------- src/slic3r/GUI/GLCanvas3D.hpp | 6 ++- src/slic3r/GUI/wxExtensions.cpp | 14 ++++-- 5 files changed, 75 insertions(+), 31 deletions(-) diff --git a/src/libslic3r/GCode/PreviewData.cpp b/src/libslic3r/GCode/PreviewData.cpp index 69f685fd4..ba0584e57 100644 --- a/src/libslic3r/GCode/PreviewData.cpp +++ b/src/libslic3r/GCode/PreviewData.cpp @@ -382,7 +382,7 @@ std::string GCodePreviewData::get_legend_title() const // #ys_FIXME_COLOR // GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std::vector& tool_colors, const std::vector>& cp_values) const GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std::vector& tool_colors, - const std::vector& cp_items, bool is_single_material_print) const + const std::vector& cp_items) const { struct Helper { @@ -508,9 +508,7 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: if (color_cnt != color_print_cnt) break; - for (int i = is_single_material_print ? color_print_cnt-1 : 0 ; - is_single_material_print ? i >= 0 : i < color_print_cnt; - is_single_material_print ? --i : ++i) + for (int i = 0 ; i < color_print_cnt; ++i) { Color color; ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + i * 4), 4 * sizeof(float)); diff --git a/src/libslic3r/GCode/PreviewData.hpp b/src/libslic3r/GCode/PreviewData.hpp index 149627a32..78de04d0a 100644 --- a/src/libslic3r/GCode/PreviewData.hpp +++ b/src/libslic3r/GCode/PreviewData.hpp @@ -239,7 +239,7 @@ public: std::string get_legend_title() const; // #ys_FIXME_COLOR // LegendItemsList get_legend_items(const std::vector& tool_colors, const std::vector>& cp_values) const; - LegendItemsList get_legend_items(const std::vector& tool_colors, const std::vector& cp_items, bool is_single_material_print) const; + LegendItemsList get_legend_items(const std::vector& tool_colors, const std::vector& cp_items) const; // Return an estimate of the memory consumed by the time estimator. size_t memory_used() const; diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 3fde9a084..074713f82 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -886,12 +886,11 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_values(const GCodePrevie } } -void GLCanvas3D::LegendTexture::fill_color_print_legend_items(const GCodePreviewData& preview_data, const GLCanvas3D& canvas, - std::vector& cp_legend_items) +void GLCanvas3D::LegendTexture::fill_color_print_legend_items( const GLCanvas3D& canvas, + const std::vector& colors_in, + std::vector& colors, + std::vector& cp_legend_items) { - if (preview_data.extrusion.view_type != GCodePreviewData::Extrusion::ColorPrint ) - return; - std::vector custom_gcode_per_height = wxGetApp().plater()->model().custom_gcode_per_height; const int extruders_cnt = wxGetApp().extruders_edited_cnt(); @@ -899,6 +898,7 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_items(const GCodePreview { if (custom_gcode_per_height.empty()) { cp_legend_items.push_back(I18N::translate_utf8(L("Default print color"))); + colors = colors_in; return; } std::vector> cp_values; @@ -923,18 +923,36 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_items(const GCodePreview } const auto items_cnt = (int)cp_values.size(); - - for (int i = 0; items_cnt > 0 && i <= items_cnt; ++i) + if (items_cnt == 0) // There is no one color change, but there is/are some pause print or custom Gcode { + cp_legend_items.push_back(I18N::translate_utf8(L("Default print color"))); + cp_legend_items.push_back(I18N::translate_utf8(L("Pause print or custom G-code"))); + colors = colors_in; + return; + } + + const int color_cnt = (int)colors_in.size() / 4; + colors.resize(colors_in.size(), 0.0); + + ::memcpy((void*)(colors.data()), (const void*)(colors_in.data() + (color_cnt - 1) * 4), 4 * sizeof(float)); + cp_legend_items.push_back(I18N::translate_utf8(L("Pause print or custom G-code"))); + size_t color_pos = 4; + + for (int i = items_cnt; i >= 0; --i, color_pos+=4) + { + // update colors for color print item + ::memcpy((void*)(colors.data() + color_pos), (const void*)(colors_in.data() + i * 4), 4 * sizeof(float)); + + // create label for color print item std::string id_str = std::to_string(i + 1) + ": "; if (i == 0) { cp_legend_items.push_back(id_str + (boost::format(I18N::translate_utf8(L("up to %.2f mm"))) % cp_values[0].first).str()); - continue; + break; } if (i == items_cnt) { cp_legend_items.push_back(id_str + (boost::format(I18N::translate_utf8(L("above %.2f mm"))) % cp_values[i - 1].second).str()); - break; + continue; } cp_legend_items.push_back(id_str + (boost::format(I18N::translate_utf8(L("%.2f - %.2f mm"))) % cp_values[i - 1].second % cp_values[i].first).str()); @@ -942,18 +960,34 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_items(const GCodePreview } else { + // colors = colors_in; + const int color_cnt = (int)colors_in.size() / 4; + colors.resize(colors_in.size(), 0.0); + + ::memcpy((void*)(colors.data()), (const void*)(colors_in.data()), 4 * extruders_cnt * sizeof(float)); + size_t color_pos = 4 * extruders_cnt; + size_t color_in_pos = 4 * (color_cnt - 1); + for (unsigned int i = 0; i < extruders_cnt; ++i) cp_legend_items.push_back((boost::format(I18N::translate_utf8(L("Extruder %d"))) % (i + 1)).str()); - - for (auto custom_code : custom_gcode_per_height) - if (custom_code.gcode == "M600") - cp_legend_items.push_back((boost::format(I18N::translate_utf8(L("Color change for Extruder %d at %.2f mm"))) % custom_code.extruder % custom_code.height).str()); - } - cp_legend_items.push_back(I18N::translate_utf8(L("Pause print or custom G-code"))); + ::memcpy((void*)(colors.data() + color_pos), (const void*)(colors_in.data() + color_in_pos), 4 * sizeof(float)); + color_pos += 4; + color_in_pos -= 4; + cp_legend_items.push_back(I18N::translate_utf8(L("Pause print or custom G-code"))); + + int cnt = custom_gcode_per_height.size(); + for (int i = cnt-1; i >= 0; --i) + if (custom_gcode_per_height[i].gcode == "M600") { + ::memcpy((void*)(colors.data() + color_pos), (const void*)(colors_in.data() + color_in_pos), 4 * sizeof(float)); + color_pos += 4; + color_in_pos -= 4; + cp_legend_items.push_back((boost::format(I18N::translate_utf8(L("Color change for Extruder %d at %.2f mm"))) % custom_gcode_per_height[i].extruder % custom_gcode_per_height[i].height).str()); + } + } } -bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas, bool compress) +bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, const std::vector& tool_colors_in, const GLCanvas3D& canvas, bool compress) { reset(); @@ -967,10 +1001,16 @@ bool GLCanvas3D::LegendTexture::generate(const GCodePreviewData& preview_data, c // const GCodePreviewData::LegendItemsList& items = preview_data.get_legend_items(tool_colors, cp_legend_values); std::vector cp_legend_items; - cp_legend_items.reserve(tool_colors.size()); - fill_color_print_legend_items(preview_data, canvas, cp_legend_items); + std::vector cp_colors; - const GCodePreviewData::LegendItemsList& items = preview_data.get_legend_items(tool_colors, cp_legend_items, wxGetApp().extruders_edited_cnt() == 1); + if (preview_data.extrusion.view_type == GCodePreviewData::Extrusion::ColorPrint) + { + cp_legend_items.reserve(cp_colors.size()); + fill_color_print_legend_items(canvas, tool_colors_in, cp_colors, cp_legend_items); + } + + const std::vector& tool_colors = preview_data.extrusion.view_type == GCodePreviewData::Extrusion::ColorPrint ? cp_colors : tool_colors_in; + const GCodePreviewData::LegendItemsList& items = preview_data.get_legend_items(tool_colors, cp_legend_items); unsigned int items_count = (unsigned int)items.size(); if (items_count == 0) diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 01d109fd6..43de4b35b 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -348,8 +348,10 @@ private: void fill_color_print_legend_values(const GCodePreviewData& preview_data, const GLCanvas3D& canvas, std::vector>& cp_legend_values); - void fill_color_print_legend_items(const GCodePreviewData& preview_data, const GLCanvas3D& canvas, - std::vector& cp_legend_items); + void fill_color_print_legend_items(const GLCanvas3D& canvas, + const std::vector& colors_in, + std::vector& colors, + std::vector& cp_legend_items); bool generate(const GCodePreviewData& preview_data, const std::vector& tool_colors, const GLCanvas3D& canvas, bool compress); diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index bbba3a2fd..f4c46c0f9 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -3520,7 +3521,6 @@ void DoubleSlider::OnRightDown(wxMouseEvent& event) int DoubleSlider::get_extruder_for_tick(int tick) { - int extruder = 0; if (m_ticks_.empty()) return 0; @@ -3639,12 +3639,16 @@ void DoubleSlider::add_code(std::string code, int selected_extruder/* = -1*/) if (m_state == msSingleExtruder && !m_ticks_.empty()) { auto before_tick_it = std::lower_bound(m_ticks_.begin(), m_ticks_.end(), tick); - if (before_tick_it == m_ticks_.begin()) - color = colors[0]; - else { + while (before_tick_it != m_ticks_.begin()) { --before_tick_it; - color = before_tick_it->color; + if (before_tick_it->gcode == "M600") { + color = before_tick_it->color; + break; + } } + + if (color.empty()) + color = colors[0]; } else color = colors[selected_extruder > 0 ? selected_extruder-1 : 0];