18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

chore(editor): the brush and the ASCII track names cleaned up for Sonar (init-statements, std::numbers::pi, ranges, the dab of the stroke and the settings of many models in functions of their own)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
This commit is contained in:
Claude
2026-11-14 23:00:52 +01:00
parent fcebd496bf
commit 644689ca28
6 changed files with 81 additions and 75 deletions
+2
View File
@@ -219,6 +219,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri
void draw_brush_cursor();
bool brush_ground(glm::dvec3 &Point);
double brush_height(glm::dvec2 const &Point, double Around);
// one dab of the stroke of many models, at the cursor on the ground
void brush_dab();
TAnimModel *place_brush_model(glm::dvec3 const &At);
void apply_rotation_for_new_node(scene::basic_node *node, int rotation_mode, float fixed_rotation_value);
// members
+36 -35
View File
@@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <array>
#include <cmath>
#include <numbers>
namespace
{
@@ -29,7 +30,6 @@ namespace
ImU32 constexpr kCrossColour{IM_COL32(255, 220, 70, 255)};
ImU32 constexpr kCircleColour{IM_COL32(255, 220, 70, 170)};
ImU32 constexpr kShadowColour{IM_COL32(0, 0, 0, 140)};
double constexpr kPi{3.14159265358979323846};
// the last press of the left button landed in the viewport, not on a window (see editormode.cpp)
bool viewport_click()
@@ -51,8 +51,7 @@ bool editor_mode::brush_ground(glm::dvec3 &Point)
double editor_mode::brush_height(glm::dvec2 const &Point, double const Around)
{
double height{Around};
if (m_streamer.active() && m_streamer.height_at(Point.x, Point.y, height))
if (double height{Around}; m_streamer.active() && m_streamer.height_at(Point.x, Point.y, height))
return height;
return placement_on_ground({Point.x, Around, Point.y}).y;
}
@@ -80,8 +79,7 @@ void editor_mode::brush_press()
mouseHold = true;
m_brush_has_last = false;
m_brush_taken.clear();
auto const single{ui()->brush().way == brush_object_list::kind::single};
if (false == single)
if (ui()->brush().way != brush_object_list::kind::single)
{
// one step of the history for the whole stroke, the models added to it as they're put
trim_history();
@@ -114,35 +112,38 @@ void editor_mode::handle_brush_mouse_hold(int /*Action*/, int /*Button*/)
{
if (ui()->mode() != nodebank_panel::BRUSH || false == m_brush_stroke)
return;
GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) {
// picks are resolved a few frames later; drop the ones queued before the button went up, or for a press that turned out
// to belong to the UI
if (false == mouseHold || false == m_brush_stroke || false == viewport_click() || m_history.empty())
return;
glm::dvec3 centre;
if (false == brush_ground(centre))
return;
auto const &settings{ui()->brush().dab};
// the next dab once the cursor went far enough, measured on the ground, whatever the camera does meanwhile
if (m_brush_has_last && glm::distance(glm::dvec2{centre.x, centre.z}, glm::dvec2{oldPos.x, oldPos.z}) < scatter::dab_step(settings))
return;
oldPos = centre;
m_brush_has_last = true;
auto &stroke{m_history.back()};
for (auto const &point : scatter::dab({centre.x, centre.z}, settings, m_brush_taken, m_brush_random))
{
auto *model{place_brush_model({point.x, brush_height(point, centre.y), point.y})};
// the place is taken either way, so a model which failed isn't tried there on every move
m_brush_taken.push_back(point);
if (model == nullptr)
continue;
EditorSnapshot::array_copy copy;
copy.model = model;
copy.uuid = model->uuid;
stroke.layer = model->layer();
stroke.copies.push_back(std::move(copy));
}
});
GfxRenderer->Pick_Node_Callback([this](scene::basic_node * /*node*/) { brush_dab(); });
}
void editor_mode::brush_dab()
{
// picks are resolved a few frames later; drop the ones queued before the button went up, or for a press that turned out
// to belong to the UI
if (false == mouseHold || false == m_brush_stroke || false == viewport_click() || m_history.empty())
return;
glm::dvec3 centre;
if (false == brush_ground(centre))
return;
auto const &settings{ui()->brush().dab};
// the next dab once the cursor went far enough, measured on the ground, whatever the camera does meanwhile
if (m_brush_has_last && glm::distance(glm::dvec2{centre.x, centre.z}, glm::dvec2{oldPos.x, oldPos.z}) < scatter::dab_step(settings))
return;
oldPos = centre;
m_brush_has_last = true;
auto &stroke{m_history.back()};
for (auto const &point : scatter::dab({centre.x, centre.z}, settings, m_brush_taken, m_brush_random))
{
auto *model{place_brush_model({point.x, brush_height(point, centre.y), point.y})};
// the place is taken either way, so a model which failed isn't tried there on every move
m_brush_taken.push_back(point);
if (model == nullptr)
continue;
EditorSnapshot::array_copy copy;
copy.model = model;
copy.uuid = model->uuid;
stroke.layer = model->layer();
stroke.copies.push_back(std::move(copy));
}
}
void editor_mode::end_brush_stroke()
@@ -189,7 +190,7 @@ void editor_mode::draw_brush_cursor()
auto previous{ground(centre.x + radius, centre.z)};
for (int i = 1; i <= pieces; ++i)
{
auto const angle{2.0 * kPi * i / pieces};
auto const angle{2.0 * std::numbers::pi * i / pieces};
auto const next{ground(centre.x + radius * std::cos(angle), centre.z + radius * std::sin(angle))};
view.line(drawlist, previous, next, kShadowColour, 3.5f);
view.line(drawlist, previous, next, kCircleColour, 1.8f);
+31 -32
View File
@@ -600,30 +600,7 @@ void brush_object_list::render_options(nodebank_panel &Bank)
if (way == kind::single)
ImGui::TextDisabled("%s", STR_C("LMB: one model at the cross on the ground"));
else
{
ImGui::TextDisabled("%s", STR_C("Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back"));
auto radius{static_cast<float>(dab.radius)};
if (ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
dab.radius = radius;
int fillmode{static_cast<int>(dab.mode)};
std::array<char const *, 3> const fills{STR_C("Random, anywhere in the circle"), STR_C("Uniform (grid)"), STR_C("Feathered to the edge")};
ImGui::SetNextItemWidth(220.0f);
if (ImGui::Combo(STR_C("Fill##brush"), &fillmode, fills.data(), static_cast<int>(fills.size())))
dab.mode = static_cast<scatter::fill>(fillmode);
auto spacing{static_cast<float>(dab.spacing)};
if (ImGui::SliderFloat(STR_C("Spacing##brush"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
dab.spacing = spacing;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", dab.mode == scatter::fill::uniform ? STR_C("The step of the grid the models stand on") : STR_C("The least distance between two models"));
if (dab.mode != scatter::fill::uniform)
{
auto density{static_cast<float>(dab.density)};
if (ImGui::SliderFloat(STR_C("Density##brush"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic))
dab.density = density;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How many models a 100 m² the circle gets, as many as the spacing lets in"));
}
}
render_dab();
render_look();
ImGui::Separator();
ImGui::Checkbox(STR_C("Random model from set"), &useRandom);
@@ -644,16 +621,38 @@ void brush_object_list::render_options(nodebank_panel &Bank)
}
}
void brush_object_list::render_dab()
{
ImGui::TextDisabled("%s", STR_C("Hold LMB: models in the circle along the cursor's path; one Ctrl+Z takes the whole stroke back"));
if (auto radius{static_cast<float>(dab.radius)}; ImGui::SliderFloat(STR_C("Radius##brush"), &radius, 0.5f, 100.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
dab.radius = radius;
std::array<char const *, 3> const fills{STR_C("Random, anywhere in the circle"), STR_C("Uniform (grid)"), STR_C("Feathered to the edge")};
ImGui::SetNextItemWidth(220.0f);
if (int fillmode{static_cast<int>(dab.mode)}; ImGui::Combo(STR_C("Fill##brush"), &fillmode, fills.data(), static_cast<int>(fills.size())))
dab.mode = static_cast<scatter::fill>(fillmode);
if (auto spacing{static_cast<float>(dab.spacing)}; ImGui::SliderFloat(STR_C("Spacing##brush"), &spacing, 0.1f, 50.0f, "%.1f m", ImGuiSliderFlags_Logarithmic))
dab.spacing = spacing;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", dab.mode == scatter::fill::uniform ? STR_C("The step of the grid the models stand on") : STR_C("The least distance between two models"));
if (dab.mode == scatter::fill::uniform)
return;
if (auto density{static_cast<float>(dab.density)}; ImGui::SliderFloat(STR_C("Density##brush"), &density, 0.1f, 200.0f, STR_C("%.1f per 100 m²"), ImGuiSliderFlags_Logarithmic))
dab.density = density;
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", STR_C("How many models a 100 m² the circle gets, as many as the spacing lets in"));
}
void brush_object_list::render_look()
{
using enum scatter::vary;
ImGui::SeparatorText(STR_C("Turn##brush"));
auto turn_random{look.turn == scatter::vary::random};
auto turn_random{look.turn == random};
if (ImGui::RadioButton(STR_C("Fixed##brushturn"), false == turn_random))
look.turn = scatter::vary::fixed;
look.turn = fixed;
ImGui::SameLine();
if (ImGui::RadioButton(STR_C("Random##brushturn"), turn_random))
look.turn = scatter::vary::random;
if (look.turn == scatter::vary::fixed)
look.turn = random;
if (look.turn == fixed)
ImGui::DragFloat3(STR_C("Angles x y z##brush"), &look.angles.x, 0.5f, -180.0f, 180.0f, "%.1f°");
else
{
@@ -662,13 +661,13 @@ void brush_object_list::render_look()
ImGui::SetTooltip("%s", STR_C("Each model turned by a random angle up to this much each way: x and z tilt it, y turns it round (180: any way)"));
}
ImGui::SeparatorText(STR_C("Scale##brush"));
auto size_random{look.size == scatter::vary::random};
auto size_random{look.size == random};
if (ImGui::RadioButton(STR_C("Fixed##brushscale"), false == size_random))
look.size = scatter::vary::fixed;
look.size = fixed;
ImGui::SameLine();
if (ImGui::RadioButton(STR_C("Random##brushscale"), size_random))
look.size = scatter::vary::random;
if (look.size == scatter::vary::fixed)
look.size = random;
if (look.size == fixed)
ImGui::DragFloat3(STR_C("Percent x y z##brush"), &look.scale.x, 0.5f, 1.0f, 1000.0f, "%.0f%%");
else
{
+2
View File
@@ -130,6 +130,8 @@ class brush_object_list
private:
void render_look();
// the settings of the stroke of many models
void render_dab();
};
class nodebank_panel : public ui_panel
+8 -7
View File
@@ -3,6 +3,7 @@
#include <algorithm>
#include <cmath>
#include <iterator>
#include <numbers>
namespace scatter
{
@@ -10,8 +11,6 @@ namespace scatter
namespace
{
double constexpr kPi{3.14159265358979323846};
bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector<glm::dvec2> const &Taken, std::vector<glm::dvec2> const &Made)
{
auto const least{Spacing * Spacing};
@@ -19,7 +18,7 @@ bool clear_of(glm::dvec2 const &Point, double const Spacing, std::vector<glm::dv
auto const d{Point - Other};
return glm::dot(d, d) >= least;
};
return std::all_of(Taken.begin(), Taken.end(), far_enough) && std::all_of(Made.begin(), Made.end(), far_enough);
return std::ranges::all_of(Taken, far_enough) && std::ranges::all_of(Made, far_enough);
}
// the points of the grid of triangles of the world (rows Spacing·√3/2 apart, every other one shifted by half) in the circle
@@ -55,21 +54,23 @@ std::vector<glm::dvec2> dab(glm::dvec2 const &Centre, dab_settings const &Settin
// only what the stroke put near this circle matters: a long stroke isn't gone through whole for every point
std::vector<glm::dvec2> nearby;
auto const reach{radius + spacing};
std::copy_if(Taken.begin(), Taken.end(), std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
std::ranges::copy_if(Taken, std::back_inserter(nearby), [&](glm::dvec2 const &Point) { return glm::distance(Point, Centre) <= reach; });
if (Settings.mode == fill::uniform)
return grid_points(Centre, radius, spacing, nearby);
// as many as the density asks for in the circle (no more than a dab can put at once), each tried a number of times before
// it's given up for the spacing
int constexpr most{2000};
auto const wanted{static_cast<int>(std::min<double>(most, std::lround(std::max(0.0, Settings.density) * kPi * radius * radius / 100.0)))};
auto const wanted{std::min(most, static_cast<int>(std::lround(std::max(0.0, Settings.density) * std::numbers::pi * radius * radius / 100.0)))};
std::uniform_real_distribution<double> unit{0.0, 1.0};
std::vector<glm::dvec2> made;
int constexpr tries_each{30};
for (int attempt = 0; attempt < wanted * tries_each && static_cast<int>(made.size()) < wanted; ++attempt)
auto attempts{wanted * tries_each};
while (attempts > 0 && static_cast<int>(made.size()) < wanted)
{
--attempts;
// even over the area: the square root of the share of the radius
auto const share{std::sqrt(unit(Random))};
auto const angle{2.0 * kPi * unit(Random)};
auto const angle{2.0 * std::numbers::pi * unit(Random)};
if (Settings.mode == fill::feathered)
{
// fewer towards the edge: kept with the chance falling from 1 in the middle to 0 at the edge
+2 -1
View File
@@ -79,7 +79,8 @@ TEST_CASE("scatter: the feathered fill thins out towards the edge")
settings.spacing = 0.1;
settings.density = 40.0;
auto const points{scatter::dab({0.0, 0.0}, settings, {}, random)};
int inner{0}, outer{0};
int inner{0};
int outer{0};
for (auto const &p : points)
{
auto const d{glm::length(p)};