From ac24df6c1608b63ada03f66de41ca2d84e30a841 Mon Sep 17 00:00:00 2001 From: yw4z Date: Sat, 28 Nov 2026 17:12:25 +0300 Subject: [PATCH] Use labeled MultiSwitch for ortho controls in painting tools and add missing ones (#17883) * init * use helper for control on PainterBase * fix tidy * update * update * simplify changes and apply min width for switch buttons * match design colors * add tooltip for control * add mcp and test cased for control * update comment * Update ImGuiWrapper.hpp * Update test_imgui_wrapper.cpp * Update src/slic3r/GUI/ImGuiWrapper.cpp Co-authored-by: Kris Austin * remove unrelated comment Co-Authored-By: Kris Austin --------- Co-authored-by: Kris Austin --- src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp | 26 +--- src/slic3r/GUI/Gizmos/GLGizmoFuzzySkin.cpp | 4 + .../GUI/Gizmos/GLGizmoMmuSegmentation.cpp | 22 +-- .../GUI/Gizmos/GLGizmoPaintedModifier.cpp | 4 + src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp | 44 +++++- src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp | 2 + src/slic3r/GUI/Gizmos/GLGizmoSeam.cpp | 2 +- src/slic3r/GUI/ImGuiWrapper.cpp | 58 ++++++++ src/slic3r/GUI/ImGuiWrapper.hpp | 6 + tests/slic3rutils/test_imgui_wrapper.cpp | 137 ++++++++++++++++++ 10 files changed, 261 insertions(+), 44 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp index 144e60940e..d36166513a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp @@ -337,16 +337,7 @@ void GLGizmoFdmSupports::on_render_input_window(float x, float y, float bottom_l ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); - if (m_imgui->bbl_checkbox(_L("Vertical"), m_vertical_only)) { - if (m_vertical_only) { - m_horizontal_only = false; - } - } - if (m_imgui->bbl_checkbox(_L("Horizontal"), m_horizontal_only)) { - if (m_horizontal_only) { - m_vertical_only = false; - } - } + render_ortho_switch(scale); } else if (m_current_tool == ImGui::SphereButtonIcon) { m_cursor_type = TriangleSelector::CursorType::SPHERE; m_tool_type = ToolType::BRUSH; @@ -360,16 +351,7 @@ void GLGizmoFdmSupports::on_render_input_window(float x, float y, float bottom_l ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); - if (m_imgui->bbl_checkbox(_L("Vertical"), m_vertical_only)) { - if (m_vertical_only) { - m_horizontal_only = false; - } - } - if (m_imgui->bbl_checkbox(_L("Horizontal"), m_horizontal_only)) { - if (m_horizontal_only) { - m_vertical_only = false; - } - } + render_ortho_switch(scale); } else if (m_current_tool == ImGui::FillButtonIcon) { m_cursor_type = TriangleSelector::CursorType::POINTER; m_tool_type = ToolType::SMART_FILL; @@ -418,6 +400,8 @@ void GLGizmoFdmSupports::on_render_input_window(float x, float y, float bottom_l } } + ImGui::Separator(); + if (m_current_tool != ImGui::GapFillIcon) { m_imgui->bbl_checkbox(m_desc["on_overhangs_only"], m_paint_on_overhangs_only); if (ImGui::IsItemHovered()) @@ -425,8 +409,6 @@ void GLGizmoFdmSupports::on_render_input_window(float x, float y, float bottom_l max_tooltip_width); } - - ImGui::Separator(); float position_before_text_y = ImGui::GetCursorPos().y; ImGui::AlignTextToFramePadding(); m_imgui->text(m_desc["highlight_by_angle"]); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFuzzySkin.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFuzzySkin.cpp index 1e7cec9edc..d101403bf4 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFuzzySkin.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFuzzySkin.cpp @@ -262,9 +262,13 @@ void GLGizmoFuzzySkin::on_render_input_window(float x, float y, float bottom_lim ImGui::SameLine(drag_left_width + cursor_slider_left); ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); + + render_ortho_switch(scale); } else if (m_current_tool == ImGui::TriangleButtonIcon) { m_cursor_type = TriangleSelector::CursorType::POINTER; m_tool_type = ToolType::BRUSH; + + render_ortho_switch(scale); } else { assert(m_current_tool == ImGui::FillButtonIcon); m_cursor_type = TriangleSelector::CursorType::POINTER; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp index 5937f8c2b9..cd5043625c 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMmuSegmentation.cpp @@ -569,31 +569,13 @@ void GLGizmoMmuSegmentation::on_render_input_window(float x, float y, float bott ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); - if (m_imgui->bbl_checkbox(_L("Vertical"), m_vertical_only)) { - if (m_vertical_only) { - m_horizontal_only = false; - } - } - if (m_imgui->bbl_checkbox(_L("Horizontal"), m_horizontal_only)) { - if (m_horizontal_only) { - m_vertical_only = false; - } - } + render_ortho_switch(scale); } else if (m_current_tool == ImGui::TriangleButtonIcon) { m_cursor_type = TriangleSelector::CursorType::POINTER; m_tool_type = ToolType::BRUSH; - if (m_imgui->bbl_checkbox(_L("Vertical"), m_vertical_only)) { - if (m_vertical_only) { - m_horizontal_only = false; - } - } - if (m_imgui->bbl_checkbox(_L("Horizontal"), m_horizontal_only)) { - if (m_horizontal_only) { - m_vertical_only = false; - } - } + render_ortho_switch(scale); } else if (m_current_tool == ImGui::FillButtonIcon) { m_cursor_type = TriangleSelector::CursorType::POINTER; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPaintedModifier.cpp b/src/slic3r/GUI/Gizmos/GLGizmoPaintedModifier.cpp index dfcc750a42..a16b34b602 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPaintedModifier.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPaintedModifier.cpp @@ -255,9 +255,13 @@ void GLGizmoPaintedModifier::on_render_input_window(float x, float y, float bott ImGui::SameLine(drag_left_width + sliders_left_width); ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); + + render_ortho_switch(scale); } else if (m_current_tool == ImGui::TriangleButtonIcon) { m_cursor_type = TriangleSelector::CursorType::POINTER; m_tool_type = ToolType::BRUSH; + + render_ortho_switch(scale); } else { assert(m_current_tool == ImGui::FillButtonIcon); m_cursor_type = TriangleSelector::CursorType::POINTER; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp index f056eda3e7..5141daa172 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.cpp @@ -48,8 +48,11 @@ #include "slic3r/GUI/GLShader.hpp" #include "slic3r/GUI/MeshUtils.hpp" #include "slic3r/GUI/Selection.hpp" +#include "slic3r/GUI/I18N.hpp" +#include "slic3r/GUI/ImGuiWrapper.hpp" + +#include -namespace Slic3r::GUI { class ImGuiWrapper; } namespace cereal { class BinaryInputArchive; } namespace Slic3r::GUI { @@ -222,6 +225,45 @@ bool GLGizmoPainterBase::render_follows_cursor() const return m_rr.mesh_id != -1; } +// ORCA: Free / Vertical / Horizontal switch constraining brush strokes, shared by the painter gizmos. +// A gizmo can leave out the Vertical or the Horizontal option. A hidden option is also switched off, +// so that a constraint the user cannot see in the switch never stays active. +void GLGizmoPainterBase::render_ortho_switch(float scale, bool show_vertical, bool show_horizontal) +{ + m_vertical_only = m_vertical_only && show_vertical; + m_horizontal_only = m_horizontal_only && show_horizontal; + if (!show_vertical && !show_horizontal) + return; // only Free is left, so there is nothing to choose + + // Free is first and Horizontal is last, which puts Vertical at 1 whenever it is shown. + std::vector labels = { _u8L_CONTEXT("Free", "Paint stroke direction") }; + if (show_vertical) labels.emplace_back(_u8L("Vertical")); + if (show_horizontal) labels.emplace_back(_u8L("Horizontal")); + + const int vertical_idx = show_vertical ? 1 : -1; + const int horizontal_idx = show_horizontal ? int(labels.size()) - 1 : -1; + + ImGui::PushStyleColor(ImGuiCol_FrameBg , m_is_dark_mode ? ImVec4(.153f, .153f, .165f, 1.f) : ImVec4(.851f, .851f, .851f, 1.f)); // #27272A : #D9D9D9 + ImGui::PushStyleColor(ImGuiCol_Button , m_is_dark_mode ? ImGuiWrapper::COL_ORCA_DARK : ImGuiWrapper::COL_ORCA); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered , m_is_dark_mode ? ImGuiWrapper::COL_ORCA_HOVER_DARK : ImGuiWrapper::COL_ORCA_HOVER); + ImGui::PushStyleColor(ImGuiCol_Text , m_is_dark_mode ? ImVec4(1.f, 1.f, 1.f, .55f) : ImVec4(0.f, 0.f, 0.f, .55f)); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10.f * scale, 5.f * scale)); + + int ortho_mode = m_vertical_only ? vertical_idx : m_horizontal_only ? horizontal_idx : 0; + if (m_imgui->multi_switch("##ortho", labels, ortho_mode)) { + m_vertical_only = ortho_mode == vertical_idx; + m_horizontal_only = ortho_mode == horizontal_idx; + } + if (ImGui::IsItemHovered()) { + std::string tip = _u8L("Free: paint in any direction."); + if (show_vertical) tip += "\n" + _u8L("Vertical: keep the stroke on a vertical line."); + if (show_horizontal) tip += "\n" + _u8L("Horizontal: keep the stroke on a horizontal line."); + m_imgui->tooltip(tip, ImGui::GetFontSize() * 20.0f); + } + ImGui::PopStyleColor(4); + ImGui::PopStyleVar(1); +} + void GLGizmoPainterBase::render_cursor() { // First check that the mouse pointer is on an object. diff --git a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp index ba9d135bd1..22f7a5283a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoPainterBase.hpp @@ -318,6 +318,8 @@ protected: // Orca: paint behavior enchancement bool m_vertical_only = false; bool m_horizontal_only = false; + // Draws the switch that sets the two flags above. An option that is not shown is also switched off. + void render_ortho_switch(float scale, bool show_vertical = true, bool show_horizontal = true); // It stores the value of the previous mesh_id to which the seed fill was applied. // It is used to detect when the mouse has moved from one volume to another one. diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSeam.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSeam.cpp index 9b4fad9dfc..2af244858a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSeam.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSeam.cpp @@ -236,7 +236,7 @@ void GLGizmoSeam::on_render_input_window(float x, float y, float bottom_limit) ImGui::PushItemWidth(1.5 * slider_icon_width); ImGui::BBLDragFloat("##cursor_radius_input", &m_cursor_radius, 0.05f, 0.0f, 0.0f, "%.2f"); - m_imgui->bbl_checkbox(_L("Vertical"), m_vertical_only); + render_ortho_switch(scale, /* show_vertical */ true, /* show_horizontal */ false); ImGui::Separator(); diff --git a/src/slic3r/GUI/ImGuiWrapper.cpp b/src/slic3r/GUI/ImGuiWrapper.cpp index 40e13c884a..6d05bdb54d 100644 --- a/src/slic3r/GUI/ImGuiWrapper.cpp +++ b/src/slic3r/GUI/ImGuiWrapper.cpp @@ -2189,6 +2189,64 @@ void ImGuiWrapper::search_list(const ImVec2& size_, bool (*items_getter)(int, co // check_box(_L("Search in English"), view_params.english); } +// ORCA: Row of labeled segments in one rounded track, exactly one of them selected. +bool ImGuiWrapper::multi_switch(const char* id, const std::vector& labels, int& selected) +{ + if (labels.empty()) + return false; + + const ImGuiStyle& style = ImGui::GetStyle(); + const float gap = style.FrameBorderSize; + const float item_h = ImGui::GetTextLineHeight() + style.FramePadding.y; + // Minimum segment width, so a two-character label (common in Chinese) is not too narrow. + const float min_w = ImGui::CalcTextSize("0000000").x; + + std::vector widths; + float total_w = gap; + for (const std::string& label : labels) { + widths.push_back(std::max(ImGui::CalcTextSize(label.c_str()).x, min_w) + style.FramePadding.x * 2.f); + total_w += widths.back() + gap; + } + + const ImVec2 origin = ImGui::GetCursorScreenPos(); + const ImVec2 track(total_w, item_h + gap * 2.f); + ImDrawList* draw = ImGui::GetWindowDrawList(); + draw->AddRectFilled(origin, origin + track, ImGui::GetColorU32(ImGuiCol_FrameBg), track.y * 0.5f); + + // The segments are positioned by hand, so the group is what the layout sees: one item the size of the track. + ImGui::BeginGroup(); + ImGui::PushID(id); + const int shown = selected; // a click moves the highlight on the next frame, so that no frame shows two or none + ImVec2 pos = origin + ImVec2(gap, gap); + for (int i = 0; i < static_cast(labels.size()); ++i) { + const ImVec2 size(widths[i], item_h); + // A real item, so that a popup or another window over the segment takes the click, and a disabled switch ignores it. + ImGui::SetCursorScreenPos(pos); + ImGui::PushID(i); + if (ImGui::InvisibleButton("##segment", size)) + selected = i; + // Automation finds a segment by the label it shows, as it finds a labelled button, and reads which one is on. + record_named_item(labels[i], "toggle", i == shown ? 1 : 0); + ImGui::PopID(); + + if (i == shown) + draw->AddRectFilled(pos, pos + size, ImGui::GetColorU32(ImGui::IsItemHovered() ? ImGuiCol_ButtonHovered : ImGuiCol_Button), item_h * 0.5f); + // Drawn as styled text, the white label takes the style alpha too, and dims with the rest of a disabled switch. + ImGui::PushStyleColor(ImGuiCol_Text, i == shown ? ImVec4(1.f, 1.f, 1.f, 1.f) : style.Colors[ImGuiCol_Text]); + ImGui::RenderTextClipped(pos, pos + size, labels[i].c_str(), nullptr, nullptr, ImVec2(0.5f, 0.5f)); + ImGui::PopStyleColor(); + + pos.x += size.x + gap; + } + ImGui::PopID(); + + ImGui::SetCursorScreenPos(origin); + ImGui::Dummy(track); + ImGui::EndGroup(); + + return selected != shown; +} + void ImGuiWrapper::bold_text(const std::string& str) { if (bold_font){ diff --git a/src/slic3r/GUI/ImGuiWrapper.hpp b/src/slic3r/GUI/ImGuiWrapper.hpp index 4121f3f4de..a06bc68b84 100644 --- a/src/slic3r/GUI/ImGuiWrapper.hpp +++ b/src/slic3r/GUI/ImGuiWrapper.hpp @@ -280,6 +280,12 @@ public: void title(const std::string& str); void title(const std::string &str, bool suppress_seperator); + // ORCA: Row of labeled segments in one rounded track, exactly one of them selected. Returns true when + // the user picks another segment, whose index is then in "selected". Labels are UTF-8, e.g. from _u8L(). + // The track is drawn in ImGuiCol_FrameBg, the selected segment in ImGuiCol_Button / ImGuiCol_ButtonHovered, + // and ImGuiStyleVar_FramePadding sets the padding around each label + bool multi_switch(const char* id, const std::vector& labels, int& selected); + // set font const std::vector get_fonts_names() const { return m_fonts_names; } bool push_bold_font(); diff --git a/tests/slic3rutils/test_imgui_wrapper.cpp b/tests/slic3rutils/test_imgui_wrapper.cpp index fb8262bc70..b3fbc9fbc5 100644 --- a/tests/slic3rutils/test_imgui_wrapper.cpp +++ b/tests/slic3rutils/test_imgui_wrapper.cpp @@ -645,3 +645,140 @@ TEST_CASE("a combo with an empty label and its unlabelled options are named on t CHECK(object[0].kind == "option"); CHECK(object[0].label.empty()); // the Selectable's own record, so a click on it picks it } + +namespace { + +// The middle of a recorded item, where a click lands on it. +ImVec2 center(const ImGuiWrapper::RecordedItem& item) +{ + return ImVec2(item.x + item.w / 2, item.y + item.h / 2); +} + +// A left click at pos, over the frames it takes: the pointer arrives, presses, lets go, and one more +// frame shows the result. +template void click(RecordedFrames& f, const ImVec2& pos, Draw draw) +{ + ImGuiIO& io = ImGui::GetIO(); + io.MousePos = pos; + f.frame(draw); + io.MouseDown[0] = true; + f.frame(draw); + io.MouseDown[0] = false; + f.frame(draw); + f.frame(draw); +} + +bool checked(const ImGuiWrapper::RecordedItem& item) +{ + return (item.flags & ImGuiItemStatusFlags_Checked) != 0; +} + +const std::vector switch_labels = {"Free", "Vertical", "Horizontal"}; + +} // namespace + +TEST_CASE("a multi switch selects the segment that is clicked and reports the change once", "[ImGuiWrapper]") +{ + RecordedFrames f; + int selected = 0, changes = 0; + const auto draw = [&] { changes += f.imgui.multi_switch("##mode", switch_labels, selected) ? 1 : 0; }; + f.frame(draw); + REQUIRE(f.named("Horizontal").size() == 1); + click(f, center(f.named("Horizontal")[0]), draw); + CHECK(selected == 2); + CHECK(changes == 1); +} + +TEST_CASE("a click on the selected segment of a multi switch reports no change", "[ImGuiWrapper]") +{ + RecordedFrames f; + int selected = 1, changes = 0; + const auto draw = [&] { changes += f.imgui.multi_switch("##mode", switch_labels, selected) ? 1 : 0; }; + f.frame(draw); + REQUIRE(f.named("Vertical").size() == 1); + click(f, center(f.named("Vertical")[0]), draw); + CHECK(selected == 1); + CHECK(changes == 0); +} + +TEST_CASE("a window lying over a multi switch takes the click", "[ImGuiWrapper][Regression]") +{ + RecordedFrames f; + int selected = 0; + // The cover stands for a dropdown that opens over the switch: a click on one of its entries is + // not a click on the segment under it. + const auto draw = [&] { + f.imgui.multi_switch("##mode", switch_labels, selected); + ImGui::SetNextWindowPos(ImVec2(0.f, 0.f)); + ImGui::SetNextWindowSize(ImVec2(400.f, 300.f)); + ImGui::SetNextWindowFocus(); + ImGui::Begin("cover"); + ImGui::End(); + }; + f.frame(draw); + REQUIRE(f.named("Vertical").size() == 1); + click(f, center(f.named("Vertical")[0]), draw); + CHECK(selected == 0); +} + +TEST_CASE("a multi switch records each segment under its label, and only the selected one as on", "[ImGuiWrapper]") +{ + // The selection moves to a later segment, then to an earlier one: the segments are drawn in + // order, so one way a frame could show two of them on, the other way none. + const bool to_later = GENERATE(true, false); + const int from = to_later ? 0 : 2, to = to_later ? 2 : 0; + RecordedFrames f; + int selected = from; + const auto draw = [&] { f.imgui.multi_switch("##mode", switch_labels, selected); }; + const auto exactly_one_on = [&] { + int on = 0; + for (const std::string& label : switch_labels) { + const auto segment = f.named(label); + REQUIRE(segment.size() == 1); + CHECK(segment[0].kind == "toggle"); + CHECK((segment[0].flags & ImGuiItemStatusFlags_Checkable) != 0); + on += checked(segment[0]) ? 1 : 0; + } + CHECK(on == 1); + }; + f.frame(draw); + exactly_one_on(); + CHECK(checked(f.named(switch_labels[from])[0])); + CHECK(f.listable() == 3); + + ImGui::GetIO().MousePos = center(f.named(switch_labels[to])[0]); + const bool button_down[] = {false, true, false, false}; // arrive, press, let go, settle + for (const bool down : button_down) { + ImGui::GetIO().MouseDown[0] = down; + f.frame(draw); + exactly_one_on(); + } + CHECK(selected == to); + CHECK(checked(f.named(switch_labels[to])[0])); +} + +TEST_CASE("a multi switch is one item to the layout, as large as its track", "[ImGuiWrapper]") +{ + RecordedFrames f; + int selected = 0; + const float gap = 2.f; // the frame border size is the gap around and between the segments + ImVec2 item_min, item_max, next; + f.frame([&] { + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, gap); + f.imgui.multi_switch("##mode", switch_labels, selected); + ImGui::PopStyleVar(); + item_min = ImGui::GetItemRectMin(); + item_max = ImGui::GetItemRectMax(); + next = ImGui::GetCursorScreenPos(); + }); + const auto first = f.named("Free"), last = f.named("Horizontal"); + REQUIRE(first.size() == 1); + REQUIRE(last.size() == 1); + // The track reaches a gap beyond the first and the last segment on every side, and the next + // item starts under it, so a tooltip or SameLine() after the switch applies to all of it. + CHECK_THAT(item_min.x, WithinAbs(first[0].x - gap, 0.01)); + CHECK_THAT(item_min.y, WithinAbs(first[0].y - gap, 0.01)); + CHECK_THAT(item_max.x, WithinAbs(last[0].x + last[0].w + gap, 0.01)); + CHECK_THAT(item_max.y, WithinAbs(last[0].y + last[0].h + gap, 0.01)); + CHECK(next.y >= item_max.y); +}