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

feat(editor): the vertices of a line of the model dragged on the map, the line as the model would lay it shown before they're let go

Continues E4b (editor-tracks-domain.md §0 pkt 2, §8 "Przesuń wierzchołek", "Zmień R / L / D wierzchołka"): the route design opened on a
line of the model has its handles on the map, a vertex and the grip of the radius of each curve.

- While a handle is dragged the change is tried on a copy of the network as it goes: the line as the model would lay it drawn over the
  map, green, or red where it's refused, and by the mouse and in the panel why it's refused or what the norms say of it ("The norms:
  2 notes"). Let go, it's one command of the model: move_vertex for a vertex moved, set_vertex for a radius dragged; a vertex moved and
  its curve changed at once one set_vertex with its place (layout::set_vertex::point), one step. The first and the last vertex of a
  line of two or more go along the tangents of its ends, as the route design drags them.
- No gizmo on the tracks of a line of the model, nor the points of their paths dragged by hand: the line is edited through the route
  design. The gizmo stays for the old tracks and the models.

Self-test: modelplan opens the route design in the field of the tracks; expect modelvertexat <line> <index> <x> <z> <tolerance>, expect
modelpreview <shown|none>. The case m_11_vertex_drag (the vertex dragged with the mouse, the preview told while it's held, one step).

Tests: U: a vertex moved and its curve changed by one set_vertex, one step, taken back.

lang/editor_pl.po: the Polish of the new texts.
This commit is contained in:
maj00r
2026-11-23 17:58:15 +01:00
parent 6e8383df6f
commit b3a2f1fef2
13 changed files with 227 additions and 21 deletions
+5
View File
@@ -3471,6 +3471,11 @@ class editor_mode : public application_mode, private editor_track::observer, pri
// the grade line of the line of the model the profile is open on read from the model, applied as its command (set_grades)
void model_profile_load();
bool model_profile_apply();
// the commands the changes of the route design come to, or why they can't be (English, model_tr shows it)
bool model_plan_commands(layout::line_id const Line, std::vector<layout::command> &Commands, std::string &Why) const;
// a handle of the route design dragged on a line of the model: tried as it goes, drawn by the line and the mouse
void model_plan_preview();
void draw_model_plan_preview() const;
bool m_model_delete_confirmed{false}; // the old delete let through, the scenarios looked through
selftest_result selftest_dispatch(std::string const &command, std::istringstream &words);
void selftest_step();
+2 -1
View File
@@ -507,7 +507,8 @@ void editor_mode::point_drag_start(editor_track::point_ref const &Point)
auto *track{selected_track()};
m_track_point = Point;
m_point_drag = {};
if (track == nullptr || false == Point.valid() || Point.path >= static_cast<int>(track->m_paths.size()))
// the points of a track of a line of the model aren't dragged by hand: the line is edited in the route design
if (track == nullptr || false == Point.valid() || Point.path >= static_cast<int>(track->m_paths.size()) || model_track(track) != nullptr)
return;
auto const &io{ImGui::GetIO()};
m_point_drag.active = true;
+73 -18
View File
@@ -825,29 +825,40 @@ bool editor_mode::route_from_model(layout::line_id const Line)
return true;
}
bool editor_mode::model_plan_apply()
bool editor_mode::model_plan_commands(layout::line_id const Line, std::vector<layout::command> &Commands, std::string &Why) const
{
auto const line{model_plan_line()};
if (line == layout::line_id::none)
return false;
auto const &plan{model().plan};
auto const &design{m_route.design};
layout::outcome refusal;
if (design.vertices.size() != plan.vertices.size())
refusal.reasons.push_back(STR("Curves added or taken out: not yet for a line of the model, only their places, radii, transitions and cants"));
std::vector<layout::command> commands;
for (std::size_t i = 0; refusal.reasons.empty() && i < design.vertices.size(); ++i)
if (design.vertices.size() != plan.vertices.size() || plan.original.vertices.size() != plan.vertices.size())
{
auto const &was{plan.original.vertices[i]};
auto const &now{design.vertices[i]};
Why = "Curves added or taken out here aren't applied to a line of the model: add a curve or take one out with the buttons of the line";
return false;
}
// where the vertex is: the first and the last of two or more along the tangents of the ends, by their offsets, as the route design
// draws and drags them; the others where they are
auto const place = [](alignment::design const &Design, std::size_t const Index) {
auto const &vertex{Design.vertices[Index]};
auto const count{Design.vertices.size()};
if (count >= 2 && Index == 0 && glm::length(Design.start_direction) > 1e-9)
return glm::dvec2{Design.start.x, Design.start.z} + glm::normalize(Design.start_direction) * vertex.offset;
if (count >= 2 && Index + 1 == count && glm::length(Design.end_direction) > 1e-9)
return glm::dvec2{Design.end.x, Design.end.z} - glm::normalize(Design.end_direction) * vertex.offset;
return vertex.position;
};
for (std::size_t i = 0; i < design.vertices.size(); ++i)
{
auto was{plan.original.vertices[i]};
auto now{design.vertices[i]};
was.position = place(plan.original, i);
now.position = place(design, i);
if (now.compound || now.spirals || now.own_ramps())
{
refusal.reasons.push_back(STR("A compound curve, or one with cant ramps of its own, isn't laid by the model yet"));
break;
Why = "A compound curve, or one with cant ramps of its own, isn't laid by the model yet";
return false;
}
if (glm::distance(now.position, was.position) > 1e-4)
commands.push_back(layout::move_vertex{line, plan.vertices[i], now.position});
layout::set_vertex set{line, plan.vertices[i]};
// a vertex moved: move_vertex; its curve changed besides: one set_vertex with its place, one step for the vertex
auto const moved{glm::distance(now.position, was.position) > 1e-4};
layout::set_vertex set{Line, plan.vertices[i]};
if (now.radius != was.radius)
set.radius = now.radius;
if (now.transition_in != was.transition_in)
@@ -857,10 +868,28 @@ bool editor_mode::model_plan_apply()
if (now.cant != was.cant)
set.cant = now.cant;
if (set.radius || set.transition_in || set.transition_out || set.cant)
commands.push_back(set);
{
if (moved)
set.point = now.position;
Commands.push_back(set);
}
else if (moved)
Commands.push_back(layout::move_vertex{Line, plan.vertices[i], now.position});
}
if (false == refusal.reasons.empty())
return true;
}
bool editor_mode::model_plan_apply()
{
auto const line{model_plan_line()};
if (line == layout::line_id::none)
return false;
model().preview = {};
std::vector<layout::command> commands;
if (std::string why; false == model_plan_commands(line, commands, why))
{
layout::outcome refusal;
refusal.reasons.push_back(why);
model_told(refusal, true);
return true;
}
@@ -1312,6 +1341,7 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm
std::string name;
words >> name;
auto *track{resolve(name)};
show_track_tab(track_tab::route);
m_track_tab = track_tab::route;
m_route.single = false;
auto const loaded{track != nullptr && route_load(*track)};
@@ -1612,6 +1642,31 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm
expectation(found != nullptr && got == wanted, format("modelgrades %u", line), std::to_string(got) + breaks, std::to_string(wanted));
return selftest_result::handled;
}
if (what == "modelvertexat")
{
// where the vertex of the line of the model is, in the plan
unsigned line{0};
std::size_t index{0};
glm::dvec2 wanted{0.0};
double tolerance{0.01};
words >> line >> index >> wanted.x >> wanted.y >> tolerance;
auto const *found{model_network().find(static_cast<layout::line_id>(line))};
glm::dvec2 got{std::numeric_limits<double>::quiet_NaN()};
if (found != nullptr && index < found->shape.vertices.size())
got = found->shape.vertices[index].point;
expectation(glm::distance(got, wanted) <= tolerance, format("modelvertexat %u %zu", line, index), format("%.3f %.3f", got.x, got.y), format("%.3f %.3f within %.3f", wanted.x, wanted.y, tolerance));
return selftest_result::handled;
}
if (what == "modelpreview")
{
// a handle of the route design dragged on a line of the model: the line as the model would lay it tried, and told
std::string wanted;
words >> wanted;
auto const &preview{model().preview};
auto const got{preview.key.empty() ? std::string{"none"} : std::string{"shown"}};
expectation(got == wanted, "modelpreview " + wanted, got + (preview.key.empty() ? "" : (preview.refused ? " refused: " : ": ") + editor_selftest::one_line(preview.text)), wanted);
return selftest_result::handled;
}
if (what == "modelproposal")
{
// the solver's best proposal for the crossover refused last
+9
View File
@@ -74,5 +74,14 @@ struct editor_model
std::string proposal; // the solver's best proposal for a crossover refused
std::pair<std::string, std::string> drag; // the switch dragged on a line of the model as it was tried: its command, why it's refused
double turnout_move{5.0}; // m, the distance the inspector moves a turnout by
// a handle of the route design dragged on a line of the model: its changes tried on a copy of the network as they're made, the line as
// the model lays it then and what the norms say of it, before the drag is let go
struct plan_preview
{
std::string key; // the design tried
bool refused{false};
std::string text; // why it's refused, or the norms' line
std::vector<glm::dvec3> points; // of the line as laid, 2 m apart
} preview;
};
+71
View File
@@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/.
#include "application/editormode.h"
#include "application/editormodemodel.h"
#include "application/editormodeselftest.h"
#include "application/editorprojection.h"
#include "application/editormodeshared.h"
#include "application/editoruilayer.h"
#include "editor/editorFormat.hpp"
@@ -166,3 +167,73 @@ bool editor_mode::model_profile_apply()
WriteLog(format("Editor: model: the grade line of line %s, %zu breaks: %s", line_name(id).c_str(), command.grades.size(), editor_selftest::one_line(model_message()).c_str()), logtype::generic);
return true;
}
// a handle of the route design dragged on a line of the model: the line as the model lays it and the norms' line, before it's let go
void editor_mode::model_plan_preview()
{
auto &preview{model().preview};
auto const line{model_plan_line()};
if (line == layout::line_id::none || false == m_route_dragging)
{
preview = {};
return;
}
std::string key;
for (auto const &vertex : m_route.design.vertices)
key += format("%.4f %.4f %.3f %.3f %.3f %.1f;", vertex.position.x, vertex.position.y, vertex.radius, vertex.transition_in, vertex.transition_out, vertex.cant);
if (key == preview.key)
return;
preview = {};
preview.key = key;
std::vector<layout::command> commands;
if (std::string why; false == model_plan_commands(line, commands, why))
{
preview.refused = true;
preview.text = model_tr(why);
return;
}
// tried on a copy of the network, one after another as they'd be carried out
layout::model copy{model_network()};
std::vector<std::string> notices;
for (auto const &command : commands)
{
auto const outcome{copy.execute(command)};
if (false == outcome.done)
{
preview.refused = true;
preview.text = model_reasons_shown(outcome);
return;
}
for (auto const &notice : outcome.notices)
if (auto const text{model_notice_shown(notice)}; std::ranges::find(notices, text) == notices.end())
notices.push_back(text);
}
preview.text = notices.empty() ? std::string{STR_C("Within the norms")} : format(STR_P("The norms: %zu note", "The norms: %zu notes", static_cast<long long>(notices.size())), notices.size());
for (auto const &notice : notices)
preview.text += "\n" + notice;
if (auto const *laid{copy.state().find(line)}; laid != nullptr && laid->solved.valid())
{
auto const steps{std::clamp(static_cast<int>(laid->solved.length / 2.0), 1, 2000)};
for (int i = 0; i <= steps; ++i)
preview.points.push_back(laid->solved.point(laid->solved.length * i / steps));
}
}
void editor_mode::draw_model_plan_preview() const
{
if (m_model == nullptr)
return;
auto const &preview{m_model->preview};
if (preview.key.empty())
return;
screen_projection const projection;
auto *drawlist{ImGui::GetForegroundDrawList(ImGui::GetMainViewport())};
auto const colour{preview.refused ? IM_COL32(255, 90, 70, 230) : IM_COL32(90, 220, 110, 230)};
for (std::size_t i = 0; i + 1 < preview.points.size(); ++i)
projection.line(drawlist, preview.points[i], preview.points[i + 1], colour, 2.0f);
// by the mouse: refused and why, or the norms' line
auto const &mouse{ImGui::GetIO().MousePos};
auto const text{(preview.refused ? std::string{STR_C("Refused, nothing changes:")} + "\n" : std::string{}) + preview.text};
drawlist->AddText(ImVec2(mouse.x + 18.0f, mouse.y + 18.0f), colour, text.c_str());
}
+4
View File
@@ -1416,6 +1416,8 @@ void editor_mode::draw_route_overlay() const
}
drawlist->AddText(ImVec2(screen.x + 9.0f, screen.y - 18.0f), overlay_color::marked, format("W%d", i + 1).c_str());
}
// a handle of a line of the model dragged: the line as the model lays it, its norms by the mouse
draw_model_plan_preview();
// the points clicked when the line was laid, in the colours of their departure; how far each is, along the curve in focus
for (std::size_t i = 0; i < route.clicks.size() && i < route.click_states.size(); ++i)
{
@@ -1623,6 +1625,7 @@ void editor_mode::render_route_gizmo()
vertex.radius = std::max(10.0, std::round(distance / secant));
}
route_update();
model_plan_preview();
return;
}
auto &vertex{route.design.vertices[route.vertex]};
@@ -1637,6 +1640,7 @@ void editor_mode::render_route_gizmo()
vertex.position = point;
}
route_update();
model_plan_preview();
}
void editor_mode::render_route_ui()
+2 -1
View File
@@ -698,7 +698,8 @@ void editor_mode::track_gizmo_moved_point(TTrack &Track, glm::dvec3 const &Posit
void editor_mode::render_track_gizmo()
{
auto *track = selected_track();
if (track == nullptr || false == editable(*track))
// a track of a line of the model is edited through the line, not moved by hand
if (track == nullptr || false == editable(*track) || model_track(track) != nullptr)
{
m_track_gizmo_using = false;
return;
+1
View File
@@ -159,6 +159,7 @@ reasons propose(transaction &Change, outcome &, set_vertex const &Command)
set->transition_in = Command.transition_in.value_or(set->transition_in);
set->transition_out = Command.transition_out.value_or(set->transition_out);
set->cant = Command.cant.value_or(set->cant);
set->point = Command.point.value_or(set->point);
return why;
}
+2 -1
View File
@@ -45,7 +45,7 @@ struct move_vertex
glm::dvec2 point{0.0};
};
// the radius, the transitions or the cant of the vertex, those given
// the radius, the transitions, the cant or the place of the vertex, those given: a curve moved and changed at once is one edit
struct set_vertex
{
line_id line{line_id::none};
@@ -54,6 +54,7 @@ struct set_vertex
std::optional<double> transition_in;
std::optional<double> transition_out;
std::optional<double> cant;
std::optional<glm::dvec2> point;
};
// the line cut at the chainage by a new node: the part before it keeps the line, the part after it is a new line; the track stays as
+9
View File
@@ -15110,6 +15110,15 @@ msgstr "Rozjazd %s: %s, %s, %.0f km/h na torze zwrotnym"
msgid "Line %s of the model: its grade line, %zu breaks"
msgstr "Linia %s modelu: jej niweleta, załomów: %zu"
msgid "Refused, nothing changes:"
msgstr "Odmowa, nic się nie zmieni:"
msgid "Within the norms"
msgstr "Zgodne z normami"
msgid "Curves added or taken out here aren't applied to a line of the model: add a curve or take one out with the buttons of the line"
msgstr "Łuki dodane lub usunięte tutaj nie trafiają do linii modelu: dodaj łuk albo go usuń przyciskami linii"
msgid "line {1} at {2} m: {3}"
msgstr "linia {1} w {2} m: {3}"
+1
View File
@@ -193,3 +193,4 @@ and the rest of the editing on the model (E4b), with the setting on:
| --- | --- |
| `m_8_turnout` | a switch dragged on a line of the model is a turnout of the catalogue inserted by the model, one step, taken back and brought again; moved, given another type and the other side, taken out by Del, a step each; one bent into an arc 2 m off the end of a cant ramp refused (9.6 pkt 16), nothing changed; the switch track saved and found after a reopen, nothing changed by hand |
| `m_9_crossover` | a crossover dragged between two lines of the model 4.75 m apart is the solver's, one step; between lines 4 m apart refused with the solver's proposal, nothing changed |
| `m_11_vertex_drag` | the vertex of a curve dragged on the map with the mouse: the line as the model would lay it tried while it's dragged, one command let go |
+30
View File
@@ -0,0 +1,30 @@
# Editing on the model (E4b): the vertex of a curve of a line of the model dragged on the map in the route design, the line as the model
# would lay it tried as it's dragged; let go, one command of the model, one step. The first vertex of a line of two curves goes along the
# tangent of the start. The empty ground east of k0..k3 (x 800..1400), the camera straight above the vertex at 800 200
wait 5
modelediting 1
layclicks 0 600 800 -600 800 200 1400 800 1400 1600
wait 2
expect modellines 1
expect steps 1
modeltracks
camera 800 500 200 800 0 200.5
wait 3
modelplan @1.0
wait 3
expect tool track
mouseat 800 0 200
wait 3
button left press
wait 2
glide 800 0 180 8 2
wait 2
expect modelpreview shown
button left release
wait 5
expect modelrefused 0
expect modelvertexat 1 0 800 180 0.5
expect steps 2
expect modelends
expect modelpreview none
quit
+18
View File
@@ -70,6 +70,24 @@ TEST_CASE("layout editing: the chainage of a point of the plan on a line, and ho
CHECK(std::isinf(distance));
}
TEST_CASE("layout editing: a vertex moved and its curve changed by one set_vertex, one step") {
layout::model model;
REQUIRE(model.execute(three_curves()).done);
auto const id{model.state().lines().begin()->first};
auto const vertex{model.state().find(id)->shape.vertices[1]};
auto const steps{model.undoable()};
layout::set_vertex set{id, vertex.id, vertex.radius + 100.0};
set.point = vertex.point + glm::dvec2{0.0, 5.0};
auto const done{model.execute(set)};
REQUIRE_MESSAGE(done.done, reasons_of(done));
CHECK(model.undoable() == steps + 1);
auto const &now{model.state().find(id)->shape.vertices[1]};
CHECK(now.point == vertex.point + glm::dvec2{0.0, 5.0});
CHECK(now.radius == vertex.radius + 100.0);
REQUIRE(model.undo());
CHECK(model.state().find(id)->shape.vertices[1] == vertex);
}
TEST_CASE("project session: the switch track of a turnout made by the session, the tracks of a line written into two files recorded") {
test::scratch_folder folder{"layout_editing_session"};
folder.write("mapa.scn", "// a new scenery\n");