Skip to content

Commit c87336b

Browse files
committed
refactor: extract toolbar grid controls
1 parent d4055ef commit c87336b

2 files changed

Lines changed: 27 additions & 29 deletions

File tree

include/MeshCraft/Application/UI/Toolbar.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Toolbar final {
2121
static void drawSurfaceSnap(bool& enabled);
2222
static void drawSnapToggle(bool& enabled, float translate, float rotate, float scale);
2323
static void drawProportionalEdit(bool& enabled, float& radius);
24+
static void drawGrid(float& spacing, const std::function<void(float)>& setSpacing);
2425
};
2526

2627
} // namespace MeshCraft::Application::UI

src/MeshCraft/Application/UI/Toolbar.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,35 +111,7 @@ float MeshCraftApplication::drawToolbar(float menuBarH, int screenW)
111111

112112
UI::Toolbar::drawProportionalEdit(propEditEnabled_, propEditRadius_);
113113

114-
// Grid cell size button (right-click to configure)
115-
if (ImGui::Button("Grid", ImVec2(40, 30))) {}
116-
if (ImGui::IsItemHovered())
117-
ImGui::SetTooltip("Grid cell size: %.4g u\nRight-click to change", gridSpacing_);
118-
if (ImGui::BeginPopupContextItem("##gridcfg")) {
119-
ImGui::TextDisabled("Grid Cell Size");
120-
ImGui::Separator();
121-
for (float v : {0.25f, 0.5f, 1.0f, 2.0f, 5.0f, 10.0f}) {
122-
bool sel = (gridSpacing_ == v);
123-
if (sel) ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.20f, 0.45f, 0.20f, 1.f));
124-
char lbl[32]; std::snprintf(lbl, sizeof(lbl), "%.4g u##g%.4g", v, v);
125-
if (ImGui::Button(lbl, ImVec2(72, 0))) {
126-
gridSpacing_ = v;
127-
gridRenderer_->setSpacing(v);
128-
}
129-
if (sel) ImGui::PopStyleColor();
130-
}
131-
ImGui::Spacing();
132-
ImGui::SetNextItemWidth(120);
133-
float tmp = gridSpacing_;
134-
// AlwaysClamp (AUDIT-0046): a zero/negative grid spacing would break
135-
// GridRenderer::setSpacing() with no other downstream guard.
136-
if (ImGui::DragFloat("##gs", &tmp, 0.05f, 0.05f, 50.0f, "%.4g u", ImGuiSliderFlags_AlwaysClamp)) {
137-
gridSpacing_ = tmp;
138-
gridRenderer_->setSpacing(tmp);
139-
}
140-
ImGui::EndPopup();
141-
}
142-
ImGui::SameLine();
114+
UI::Toolbar::drawGrid(gridSpacing_, [this](float spacing) { gridRenderer_->setSpacing(spacing); });
143115

144116
float toolbarH = ImGui::GetWindowHeight();
145117
imguiTopH_ = static_cast<int>(menuBarH + toolbarH);
@@ -258,4 +230,29 @@ void Toolbar::drawProportionalEdit(bool& enabled, float& radius) {
258230
ImGui::SameLine();
259231
}
260232

233+
void Toolbar::drawGrid(float& spacing, const std::function<void(float)>& setSpacing) {
234+
ImGui::Button("Grid", ImVec2(40, 30));
235+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Grid cell size: %.4g u\nRight-click to change", spacing);
236+
if (ImGui::BeginPopupContextItem("##gridcfg")) {
237+
ImGui::TextDisabled("Grid Cell Size");
238+
ImGui::Separator();
239+
for (float value : {0.25f, 0.5f, 1.0f, 2.0f, 5.0f, 10.0f}) {
240+
const bool selected = spacing == value;
241+
if (selected) ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.20f, 0.45f, 0.20f, 1.f));
242+
char label[32]; std::snprintf(label, sizeof(label), "%.4g u##g%.4g", value, value);
243+
if (ImGui::Button(label, ImVec2(72, 0))) { spacing = value; setSpacing(value); }
244+
if (selected) ImGui::PopStyleColor();
245+
}
246+
ImGui::Spacing();
247+
ImGui::SetNextItemWidth(120);
248+
float value = spacing;
249+
if (ImGui::DragFloat("##gs", &value, 0.05f, 0.05f, 50.0f, "%.4g u", ImGuiSliderFlags_AlwaysClamp)) {
250+
spacing = value;
251+
setSpacing(value);
252+
}
253+
ImGui::EndPopup();
254+
}
255+
ImGui::SameLine();
256+
}
257+
261258
} // namespace MeshCraft::Application::UI

0 commit comments

Comments
 (0)