mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 05:04:58 +01:00
feat(editor): area fill: Enter finishes an area, Esc drops the outline, a list of filled areas to change later (#241)
* feat(editor): fill_store, the filled areas kept in the scenery as //$f lines
* feat(editor): fill_store: reading and writing the filled areas
* build: editorFillStore.cpp
* feat(scene): //$f lines of the filled areas kept with the layer files
* feat(editor): area fill: the members of the list of filled areas
* lang(pl): area fill list
* feat(editor): area fill: Enter finishes an area, Esc drops the outline, filled areas listed to be changed later
* fix(editor): the filled areas kept as //$w lines, //$f being the fitted tracks of the profile tool
* fix(editor): a changed area keeps its own rotation and its layer; its models go to the layer of the area, on an undo as on a refill
Change + Enter took the rotation of the Functions panel instead of the area's own:
the area keeps it now, unless the rotation of the panel is changed while the area
is picked up.
The models of an area were made in the active layer, the area record stayed in its
own: on an undo of a change or a removal, with another layer active, they were
saved apart. create_model() takes the layer to put the model in (none: the active
one, as before), and the models of an area go to the layer of the area (the active
one if that's gone). The active layer isn't switched for it: Layers.active() also
shows and unlocks the layer it's given.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
* chore(editor): the seeded generator of the area fill marked as not security (Sonar cpp:S2245)
The fill must repeat from its seed (the same area filled again gets the
same objects), so a cryptographic generator is not wanted there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
* ci(windows): build lua51.dll for the unit tests
(cherry picked from commit b3e6e02779)
* fix(editor): the filled areas kept under //$u, as //$w is the mark of the shared grade lines on master
After the review of jerrrrycho: on master //$w is the mark of the shared grade lines (scenelayers.cpp,
editormodegradeline.cpp), and both stores save through Layers.mark(Layer, Mark, Lines), which replaces the
lines of the mark. Once master reached this branch, a layer with a grade line and a filled area would have
kept only the lines of whichever store saved last; before that, a master build reads the lines of the areas
as grade lines it doesn't know.
The area fill takes //$u, free on master, on feat/gl46-migration and in the letters of the starters
(n d f i l a), and the same letter the ficzery2 builds the testers have already save the areas under, so
their sceneries keep their areas. A scenery saved by a build of this PR before this commit has its areas
under //$w: they are no longer read as areas; saved again, the editor keeps those lines as lines it doesn't
know.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1K1Qu7TCsa74zsNURd
---------
Co-authored-by: miloszunio <miloszunio@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Hirek193 <polomarket73@gmail.com>
This commit is contained in:
220
editor/editorFillStore.cpp
Normal file
220
editor/editorFillStore.cpp
Normal file
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "editor/editorFillStore.hpp"
|
||||
#include "editor/editorFormat.hpp"
|
||||
|
||||
#include "model/AnimModel.h"
|
||||
#include "simulation/simulation.h"
|
||||
#include "utilities/Logs.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
|
||||
namespace fill_store
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
std::string const kMark{"//$u"};
|
||||
|
||||
std::vector<area> g_areas;
|
||||
bool g_loaded{false};
|
||||
|
||||
// format of the lines, coordinates in the space of the file:
|
||||
// area <id> seed <seed> density <per ha> spacing <m> scale <min> <max> rotation <random|fixed> <deg> ground <0|1>
|
||||
// set <kind> <id> objects <count> label <text to the end of the line>
|
||||
// outline <count> <x y z>...
|
||||
// template <node template to the end of the line>
|
||||
// end
|
||||
|
||||
std::string trimmed(std::string Text)
|
||||
{
|
||||
auto const first{Text.find_first_not_of(" \t")};
|
||||
if (first == std::string::npos)
|
||||
return {};
|
||||
auto const last{Text.find_last_not_of(" \t\r\n")};
|
||||
return Text.substr(first, last - first + 1);
|
||||
}
|
||||
|
||||
void mark(scene::layer_handle const Layer)
|
||||
{
|
||||
if (false == scene::Layers.valid(Layer))
|
||||
return;
|
||||
auto const &context{scene::Layers.layer(Layer).context_insert()};
|
||||
std::vector<std::string> lines;
|
||||
for (auto const &entry : g_areas)
|
||||
{
|
||||
if (entry.layer != Layer)
|
||||
continue;
|
||||
if (lines.empty())
|
||||
lines.emplace_back("# areas filled with models in the editor, the simulation skips these lines");
|
||||
auto header{format("area %s seed %u density %.3f spacing %.3f", entry.id.c_str(), static_cast<unsigned>(entry.seed), entry.density, entry.spacing)};
|
||||
header += format(" scale %.3f %.3f rotation %s %.3f", entry.scale_min, entry.scale_max, entry.random_rotation ? "random" : "fixed", entry.rotation);
|
||||
header += format(" ground %d set %d %d objects %zu label %s", entry.models_as_ground ? 1 : 0, entry.set_kind, entry.set_id, entry.objects, entry.label.c_str());
|
||||
lines.push_back(std::move(header));
|
||||
auto outline{format("outline %zu", entry.outline.size())};
|
||||
for (auto const &point : entry.outline)
|
||||
{
|
||||
auto const local{context.to_local(point)};
|
||||
outline += format(" %.4f %.4f %.4f", local.x, local.y, local.z);
|
||||
}
|
||||
lines.push_back(std::move(outline));
|
||||
for (auto const &entry_template : entry.templates)
|
||||
lines.push_back("template " + entry_template);
|
||||
lines.emplace_back("end");
|
||||
}
|
||||
scene::Layers.mark(Layer, kMark, std::move(lines));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void reset()
|
||||
{
|
||||
g_areas.clear();
|
||||
g_loaded = false;
|
||||
}
|
||||
|
||||
void load()
|
||||
{
|
||||
if (g_loaded || scene::Layers.empty())
|
||||
return;
|
||||
g_loaded = true;
|
||||
g_areas.clear();
|
||||
for (auto const layer : scene::Layers.marked_layers(kMark))
|
||||
{
|
||||
auto const &context{scene::Layers.layer(layer).context_insert()};
|
||||
area entry;
|
||||
bool open{false};
|
||||
for (auto const &text : scene::Layers.marked(layer, kMark))
|
||||
{
|
||||
std::istringstream line{text};
|
||||
std::string keyword;
|
||||
line >> keyword;
|
||||
if (keyword == "area")
|
||||
{
|
||||
entry = area{};
|
||||
entry.layer = layer;
|
||||
std::string seed, density, spacing, scale, rotation, rotationkind, ground, set, objects, label;
|
||||
int groundvalue{1};
|
||||
unsigned seedvalue{0};
|
||||
line >> entry.id >> seed >> seedvalue >> density >> entry.density >> spacing >> entry.spacing >> scale >> entry.scale_min >> entry.scale_max;
|
||||
line >> rotation >> rotationkind >> entry.rotation >> ground >> groundvalue >> set >> entry.set_kind >> entry.set_id >> objects >> entry.objects >> label;
|
||||
open = (false == line.fail()) && (false == entry.id.empty()) && label == "label";
|
||||
if (false == open)
|
||||
continue;
|
||||
entry.seed = seedvalue;
|
||||
entry.random_rotation = (rotationkind != "fixed");
|
||||
entry.models_as_ground = (groundvalue != 0);
|
||||
std::string rest;
|
||||
std::getline(line, rest);
|
||||
entry.label = trimmed(rest);
|
||||
}
|
||||
else if (keyword == "outline" && open)
|
||||
{
|
||||
std::size_t count{0};
|
||||
line >> count;
|
||||
for (std::size_t i = 0; i < count && i < 100000; ++i)
|
||||
{
|
||||
glm::dvec3 point{0.0};
|
||||
line >> point.x >> point.y >> point.z;
|
||||
if (line.fail())
|
||||
break;
|
||||
entry.outline.push_back(context.to_world(point));
|
||||
}
|
||||
}
|
||||
else if (keyword == "template" && open)
|
||||
{
|
||||
std::string rest;
|
||||
std::getline(line, rest);
|
||||
rest = trimmed(rest);
|
||||
if (false == rest.empty())
|
||||
entry.templates.push_back(std::move(rest));
|
||||
}
|
||||
else if (keyword == "end" && open)
|
||||
{
|
||||
open = false;
|
||||
if (entry.outline.size() >= 3)
|
||||
g_areas.push_back(std::move(entry));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (false == g_areas.empty())
|
||||
WriteLog("Editor: " + std::to_string(g_areas.size()) + " filled area(s) read from the scenery", logtype::generic);
|
||||
}
|
||||
|
||||
std::vector<area> const &areas()
|
||||
{
|
||||
load();
|
||||
return g_areas;
|
||||
}
|
||||
|
||||
area const *find(std::string const &Id)
|
||||
{
|
||||
load();
|
||||
auto const lookup{std::find_if(g_areas.begin(), g_areas.end(), [&](area const &Entry) { return Entry.id == Id; })};
|
||||
return lookup != g_areas.end() ? &(*lookup) : nullptr;
|
||||
}
|
||||
|
||||
std::string next_id()
|
||||
{
|
||||
load();
|
||||
for (std::size_t number = g_areas.size() + 1;; ++number)
|
||||
{
|
||||
auto const id{"fill" + std::to_string(number)};
|
||||
if (find(id) == nullptr && simulation::Instances.find(model_name(id, 0)) == nullptr)
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
void put(area Entry)
|
||||
{
|
||||
load();
|
||||
std::set<scene::layer_handle> layers{Entry.layer};
|
||||
auto const existing{std::find_if(g_areas.begin(), g_areas.end(), [&](area const &Other) { return Other.id == Entry.id; })};
|
||||
if (existing != g_areas.end())
|
||||
{
|
||||
layers.insert(existing->layer);
|
||||
*existing = std::move(Entry);
|
||||
}
|
||||
else
|
||||
g_areas.push_back(std::move(Entry));
|
||||
for (auto const layer : layers)
|
||||
mark(layer);
|
||||
}
|
||||
|
||||
void forget(std::string const &Id)
|
||||
{
|
||||
load();
|
||||
std::set<scene::layer_handle> layers;
|
||||
for (auto const &entry : g_areas)
|
||||
if (entry.id == Id)
|
||||
layers.insert(entry.layer);
|
||||
g_areas.erase(std::remove_if(g_areas.begin(), g_areas.end(), [&](area const &Entry) { return Entry.id == Id; }), g_areas.end());
|
||||
for (auto const layer : layers)
|
||||
mark(layer);
|
||||
}
|
||||
|
||||
std::string model_name(std::string const &Id, std::size_t const Number)
|
||||
{
|
||||
return Id + "_" + std::to_string(Number);
|
||||
}
|
||||
|
||||
bool owns(std::string const &Id, std::string const &Name)
|
||||
{
|
||||
if (Name.size() <= Id.size() + 1 || Name.compare(0, Id.size(), Id) != 0 || Name[Id.size()] != '_')
|
||||
return false;
|
||||
return std::all_of(Name.begin() + static_cast<std::ptrdiff_t>(Id.size()) + 1, Name.end(), [](char const Character) { return Character >= '0' && Character <= '9'; });
|
||||
}
|
||||
|
||||
} // namespace fill_store
|
||||
59
editor/editorFillStore.hpp
Normal file
59
editor/editorFillStore.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "scene/scenelayers.h"
|
||||
|
||||
// areas filled with models in the editor, kept in the scenery files as "//$u" comment lines: the outline, the models and the
|
||||
// settings of the fill and the seed of its random numbers, so the area can be picked up again, changed and filled anew. the
|
||||
// models of an area are named after it, <id>_<number>, which is how they're found again in a scenery loaded later
|
||||
namespace fill_store
|
||||
{
|
||||
|
||||
struct area
|
||||
{
|
||||
std::string id; // fill<number>, the prefix of the names of its models
|
||||
std::string label; // given by the user, shown in the list
|
||||
scene::layer_handle layer{null_handle};
|
||||
std::vector<glm::dvec3> outline; // world space, in the order it was clicked
|
||||
std::vector<std::string> templates; // the node templates the models were drawn from
|
||||
int set_kind{0}; // the model set the templates came from (model_set_ref): 0 the list of the tool, 1 a user set, 2 a node bank group
|
||||
int set_id{-1};
|
||||
float density{200.0f}; // objects per hectare
|
||||
float spacing{2.0f}; // minimal distance between the objects, m
|
||||
float scale_min{1.0f};
|
||||
float scale_max{1.0f};
|
||||
bool random_rotation{true};
|
||||
float rotation{0.0f}; // yaw of all objects without the random rotation, degrees
|
||||
bool models_as_ground{true};
|
||||
std::uint32_t seed{0}; // of the random numbers of the fill: the same seed and settings make the same objects
|
||||
std::size_t objects{0}; // placed by the last fill
|
||||
};
|
||||
|
||||
void reset();
|
||||
void load();
|
||||
std::vector<area> const &areas();
|
||||
area const *find(std::string const &Id);
|
||||
// an id no area has and no model is named after yet
|
||||
std::string next_id();
|
||||
// the area of the id is replaced, or added; it's kept in the file of its layer
|
||||
void put(area Entry);
|
||||
void forget(std::string const &Id);
|
||||
// the name of the model of the area with the number
|
||||
std::string model_name(std::string const &Id, std::size_t const Number);
|
||||
// whether the model name is one of the models of the area
|
||||
bool owns(std::string const &Id, std::string const &Name);
|
||||
|
||||
} // namespace fill_store
|
||||
Reference in New Issue
Block a user