diff --git a/application/editormode.cpp b/application/editormode.cpp index d657c3970..f8f368761 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -1888,6 +1888,9 @@ void editor_mode::render_settings() ImGui::SetTooltip("%s", STR_C("A line laid in the straights, or applied by the route design, becomes a line of the model of the track geometry: its tracks are made by the model, " "its edits checked against the norms and refused with the reasons, the project of the map kept next to it.\n" "The tracks there were stay as they are, edited the old way. Off, the editor works as it did")); + // taken when a scenery is opened: changed meanwhile, the model and the scene would go out of step + if (model != model_taken()) + ImGui::TextDisabled("%s", STR_C("Takes effect with the next scenery opened")); if (auto const error{model_error()}; model && false == error.empty()) ImGui::TextColored(ImVec4(1.0f, 0.6f, 0.2f, 1.0f), "%s", format(STR_C("The project of the map can't be opened: %s"), error.c_str()).c_str()); } diff --git a/application/editormode.h b/application/editormode.h index 2515df723..48062325b 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -3406,6 +3406,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri glm::dvec3 offset{0.0}; // of the placement the coordinates of the text are in std::string baseline; // its own export as it was made: another one tells a change by the other tools std::string kind{"track"}; // "switch": a switch track of a turnout, kept by the main track of the turnout (line) + std::size_t of{0}; // the tracks the line was made of with it: fewer of them there, the others were taken away by another tool }; // how a run of commands goes on: the lines the user let write over, what's done after the last one struct model_context @@ -3414,6 +3415,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::string surface; // of the tracks made, else the one of the tracks of the line, else the lay tool's std::function const &)> done; // with the tracks the last command made std::vector replaced; // old tracks the line made takes the place of, taken away in its step + bool joined{false}; // the step joined to the one before it: the commands of a batch after the first }; // the project, the managed tracks, the question waiting for an answer, the scenario index (editormodemodel.cpp); made as it's needed friend struct editor_model; @@ -3428,6 +3430,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri void model_plan_drop(); // the setting is on and the project of the map is open bool model_on() const; + bool model_taken() const; // the setting as it was taken when the scenery was opened, for the session // opens the project of the map once the scenery is there, and looks after the questions waiting for the scenario index void model_update(); void model_open(); @@ -3525,6 +3528,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri bool model_plan_commands(layout::line_id const Line, std::vector &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(); + std::string model_plan_key() const; // the design of the route as the preview of the model was worked out for it 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); diff --git a/application/editormodemodel.cpp b/application/editormodemodel.cpp index 8916f05a3..4a8a32b29 100644 --- a/application/editormodemodel.cpp +++ b/application/editormodemodel.cpp @@ -103,9 +103,10 @@ std::optional &run_setting() return value; } +// a self-test which sets none edits the old way, whatever the tester ticked in their own editor: the older cases are written for it bool model_setting() { - return run_setting().value_or(EditorSettings.model_editing()); + return run_setting().value_or(selftest_script("EU07_EDITOR_SELFTEST") == nullptr && EditorSettings.model_editing()); } void expectation(bool const Passed, std::string const &What, std::string const &Got, std::string const &Wanted) @@ -293,15 +294,25 @@ void editor_mode::model_plan_drop() bool editor_mode::model_on() const { - return model_setting() && model().session.is_open(); + return model().on && model().session.is_open(); +} + +bool editor_mode::model_taken() const +{ + return model().on; } void editor_mode::model_update() { - if (false == model_setting()) - return; - if (false == model().tried && Global.editor_session && false == scene::Layers.empty()) - model_open(); + if (false == model().tried) + { + if (false == Global.editor_session || scene::Layers.empty()) + return; + model().tried = true; + model().on = model_setting(); + if (model().on) + model_open(); + } if (false == model_on()) return; // the index built is taken, and kept between the sessions @@ -349,10 +360,12 @@ void editor_mode::model_open() model().tracks.clear(); std::map> files; std::map counts; + std::map written; for (auto const &node : model().session.project().generated) { if (node.kind != "track" && node.kind != "switch") continue; + ++written[node.line]; auto *track{simulation::Paths.find(node.name)}; if (track == nullptr || track->m_editorremoved) continue; @@ -374,6 +387,9 @@ void editor_mode::model_open() entry.baseline = export_of(*track); model().tracks[track] = std::move(entry); } + // a track the files had and the scene hasn't is one of the line taken away, as one the user takes away later + for (auto &[node, entry] : model().tracks) + entry.of = written[entry.line]; scene::Layers.path_writer = [this](TTrack const &Track, glm::dvec3 const &Offset) { return model_path_text(Track, Offset); }; // the scenario index of the map built in the background, kept between the sessions (not for the self-tests, which leave nothing behind) auto &store{model().store}; @@ -417,12 +433,32 @@ std::vector editor_mode::model_line_tracks(layout::line_id const Line) std::set editor_mode::model_seen_changed() const { std::set result; + // a line short of tracks: some of the ones the model made taken away by another tool, a switch put in say. The model's own steps, + // their undos and redos with them, take a line's tracks away and bring them back all together + struct line_count + { + std::size_t there{0}; // the tracks of the line in the scene + std::size_t made{0}; // of how many the model made of it with them + bool known{false}; // tracks of it were made, all of them gone now where none is there + }; + std::map counts; for (auto const &[node, entry] : model().tracks) { auto const *track{dynamic_cast(node)}; - if (track != nullptr && false == track->m_editorremoved && false == result.contains(entry.line) && export_of(*track) != entry.baseline) + if (track == nullptr) + continue; + auto &line{counts[entry.line]}; + line.known = line.known || entry.of > 0; + if (track->m_editorremoved) + continue; + ++line.there; + line.made = std::max(line.made, entry.of); + if (false == result.contains(entry.line) && export_of(*track) != entry.baseline) result.insert(entry.line); } + for (auto const &[id, line] : counts) + if ((line.there < line.made || (line.there == 0 && line.known)) && model().session.network().find(id) != nullptr) + result.insert(id); return result; } @@ -521,10 +557,20 @@ void editor_mode::model_run(std::vector Commands, model_context return; } created = model_tracks_after(applied.token, before, applied.changed, applied.removed, Context); + // the commands of one batch after the first joined to it: one Ctrl+Z takes back all of it, none leaves the line half edited + Context.joined = true; std::string what{format(STR_C("%s: done"), STR_C(layout::command_name(command)))}; if (false == applied.changed.empty()) what = format(STR_P("%s: line %s, %zu track", "%s: line %s, %zu tracks", static_cast(created.size())), STR_C(layout::command_name(command)), line_name(*applied.changed.begin()).c_str(), created.size()); model_told(applied.outcome, false, what); + // what the generator couldn't make as the line has it, told with the outcome: the tracks are laid, a piece a few millimetres + // off the line say, so it isn't refused + for (auto const &error : model().made_errors) + if (std::ranges::find(model().notices, error) == model().notices.end()) + { + model().notices.push_back(error); + model().shown_notices.push_back(error); + } Commands.erase(Commands.begin()); } if (Context.done) @@ -559,6 +605,7 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, turnouts.insert(id); std::vector removed; auto surface{Context.surface}; + model().made_errors.clear(); for (auto const line : going) for (auto *track : model_line_tracks(line)) { @@ -605,7 +652,10 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, { auto const made{model().session.generate(line)}; for (auto const &error : made.errors) + { WriteLog("Editor: model: the tracks of line " + line_name(line) + ": " + error, logtype::generic); + model().made_errors.push_back(error); + } for (auto const &piece : made.pieces) put(line, piece.text, piece.name, "track"); } @@ -616,7 +666,10 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, continue; auto const made{model().session.generate(id)}; for (auto const &error : made.errors) + { WriteLog(format("Editor: model: the tracks of turnout %u: %s", layout::number(id), error.c_str()), logtype::generic); + model().made_errors.push_back(error); + } if (made.parts.empty()) put(turnout->main, made.text, made.name, "switch"); for (auto const &part : made.parts) @@ -633,6 +686,8 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, editor_track::commit(created); for (auto &[track, entry] : entries) { + // the tracks the line was made of, its turnouts' switch tracks among them + entry.of = indices[entry.line]; entry.baseline = export_of(*track); model().tracks[track] = std::move(entry); } @@ -649,6 +704,8 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, g_redo.clear(); } m_history.back().model_token = Token; + if (Context.joined) + m_history.back().joined = true; if (std::ranges::any_of(removed, [&](TTrack const *Track) { return Track == m_node; })) { m_node = created.empty() ? nullptr : created.back(); @@ -1376,8 +1433,13 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm words >> on; // for this run, not kept in the settings of the editor run_setting() = on != 0; - if (on != 0 && false == model().tried && false == scene::Layers.empty()) - model_open(); + model().on = on != 0; + if (on != 0 && false == scene::Layers.empty()) + { + model().tried = true; + if (false == model().session.is_open()) + model_open(); + } WriteLog(format("SELFTEST modelediting %d: the project %s, %zu lines", on, model().session.is_open() ? "open" : "not open", model().session.network().lines().size())); return selftest_result::handled; } @@ -1726,18 +1788,48 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm } if (what == "modelspacing") { - // how far the other line runs from the point of the line at the chainage, within the bounds - unsigned line{0}, other{0}; - double chainage{0.0}, low{0.0}, high{0.0}; + // how far the other line runs from the point of the line at the chainage, within the bounds: above 0 to the left of the line as it + // runs (left_of, as the offset of the parallel tool), below 0 to its right, so a line laid on the other side fails it + unsigned line{0}; + unsigned other{0}; + double chainage{0.0}; + double low{0.0}; + double high{0.0}; words >> line >> other >> chainage >> low >> high; auto const *one{model_network().find(static_cast(line))}; auto const *two{model_network().find(static_cast(other))}; auto got{std::numeric_limits::quiet_NaN()}; if (one != nullptr && two != nullptr && one->solved.valid() && two->solved.valid()) - layout::chainage_of(two->solved, one->solved.position(std::clamp(chainage, 0.0, one->solved.length)), &got); + { + auto const at{std::clamp(chainage, 0.0, one->solved.length)}; + auto const point{one->solved.position(at)}; + auto const across{two->solved.position(layout::chainage_of(two->solved, point, &got))}; + if (glm::dot(across - point, layout::left_of(glm::normalize(one->solved.tangent(at)))) < 0.0) + got = -got; + } expectation(got >= low && got <= high, format("modelspacing %u %u %.1f", line, other, chainage), format("%.4f", got), format("%.4f .. %.4f", low, high)); return selftest_result::handled; } + if (what == "modelside") + { + // the side the diverging track of the switch leaves its main track to, as the switch runs from its start: right where the + // cross product of the two in the plan is above 0, as a switch record has R (switch_store::classify), left below it + std::string name; + std::string wanted; + words >> name >> wanted; + auto const *track{simulation::Paths.find(name)}; + std::string got{"none"}; + if (track != nullptr && false == track->m_editorremoved && track->eType == tt_Switch && track->m_paths.size() >= 2) + { + auto const &main{track->m_paths[0].points}; + auto const &diverging{track->m_paths[1].points}; + auto const along{glm::dvec2{main[segment_data::point::end].x - main[segment_data::point::start].x, main[segment_data::point::end].z - main[segment_data::point::start].z}}; + auto const off{glm::dvec2{diverging[segment_data::point::end].x - main[segment_data::point::start].x, diverging[segment_data::point::end].z - main[segment_data::point::start].z}}; + got = along.x * off.y - along.y * off.x > 0.0 ? "right" : "left"; + } + expectation(got == wanted, "modelside " + name, got, wanted); + return selftest_result::handled; + } if (what == "modelnodes") { // the nodes of the model: a line laid on from the free end of another one shares the node there @@ -1794,7 +1886,10 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm std::string wanted; words >> wanted; auto const &preview{model().preview}; - auto const got{preview.key.empty() ? std::string{"none"} : std::string{"shown"}}; + auto got{preview.key.empty() ? std::string{"none"} : std::string{"shown"}}; + // current: worked out for the design as it is now, not left as the first frame of the drag showed it + if (wanted == "current" && false == preview.key.empty()) + got = preview.key == model_plan_key() ? "current" : "stale"; expectation(got == wanted, "modelpreview " + wanted, got + (preview.key.empty() ? "" : (preview.refused ? " refused: " : ": ") + editor_selftest::one_line(preview.text)), wanted); return selftest_result::handled; } diff --git a/application/editormodemodel.h b/application/editormodemodel.h index 460f5d533..bf86a8099 100644 --- a/application/editormodemodel.h +++ b/application/editormodemodel.h @@ -50,7 +50,11 @@ struct model_question struct editor_model { project::session session; - bool tried{false}; // the project opened for the scenery, or failed to + bool tried{false}; // the setting taken for the scenery opened, the project opened if it's on, or failed to + // the setting as it was when the scenery was opened, kept for the session: turned on later, an edit made the old way before was + // written back as the file had it; turned off before a save, the scenery was saved without its project. A change of it takes + // effect with the next scenery opened + bool on{false}; std::string error; std::unordered_map tracks; project::index_cache index; @@ -75,6 +79,7 @@ struct editor_model glm::dvec3 offset{0.0}; }; std::vector written; + std::vector made_errors; // of the generator, by the tracks of the last command std::map marks; // the self-test's copies of the network, for expect modelsame std::string shown; // the message as the user reads it, in the language of the editor std::vector shown_notices; // the notices so diff --git a/application/editormodemodeledit.cpp b/application/editormodemodeledit.cpp index 8f737c513..4de8bb8a6 100644 --- a/application/editormodemodeledit.cpp +++ b/application/editormodemodeledit.cpp @@ -145,7 +145,13 @@ bool editor_mode::model_profile_apply() state.error = model_shown(); return true; } - if (command.grades == line->shape.grades) + // the same breaks, within what taking the chainages to the route and back changes of them: compared exactly, an Apply with no edit + // added a step + auto const unchanged{std::ranges::equal(command.grades, line->shape.grades, [](auto const &A, auto const &B) { + double constexpr kNear{1e-6}; + return std::abs(A.chainage - B.chainage) < kNear && std::abs(A.elevation - B.elevation) < kNear && std::abs(A.vertical_radius - B.vertical_radius) < kNear * std::max(1.0, B.vertical_radius); + })}; + if (unchanged) { state.status = STR_C("Nothing changed"); return true; @@ -252,6 +258,14 @@ void editor_mode::model_plan_reload(layout::line_id const Line) // 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 +std::string editor_mode::model_plan_key() const +{ + std::string key; + for (auto const &vertex : m_route.design.vertices) + key += format("%.4f %.4f %.4f %.3f %.3f %.3f %.1f;", vertex.position.x, vertex.position.y, vertex.offset, vertex.radius, vertex.transition_in, vertex.transition_out, vertex.cant); + return key; +} + void editor_mode::model_plan_preview() { auto &preview{model().preview}; @@ -263,9 +277,7 @@ void editor_mode::model_plan_preview() } // the first and the last vertex of two or more are dragged along the tangents of the ends by their offsets, which their positions // don't follow (model_plan_commands places them so): without the offset the preview stayed as its first frame showed it - std::string key; - for (auto const &vertex : m_route.design.vertices) - key += format("%.4f %.4f %.4f %.3f %.3f %.3f %.1f;", vertex.position.x, vertex.position.y, vertex.offset, vertex.radius, vertex.transition_in, vertex.transition_out, vertex.cant); + auto const key{model_plan_key()}; if (key == preview.key) return; preview = {}; diff --git a/application/editormodeprofile.cpp b/application/editormodeprofile.cpp index 31dec8c22..553d6b705 100644 --- a/application/editormodeprofile.cpp +++ b/application/editormodeprofile.cpp @@ -776,6 +776,8 @@ void editor_mode::profile_open(TTrack *From, TTrack *To) auto &state{m_profile}; state.open = true; state.status.clear(); + // what was wrong with the route opened before isn't of this one + state.error.clear(); editor_track::route route; if (false == editor_track::find_route(From, To, route, state.error)) return; diff --git a/application/editormodeselftest.cpp b/application/editormodeselftest.cpp index f54dc4c53..88d8121b2 100644 --- a/application/editormodeselftest.cpp +++ b/application/editormodeselftest.cpp @@ -138,6 +138,8 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // angles at the joints, loops, the turning wasted and the sharpest curve logged; case: that one alone // layspeed the speed set for the laying, and whether it goes also on from a track with its own // laymode , laytrace as the panel of the laying sets them +// laysettings <0|1> the settings of the lay tool opened under the line of their summary, or folded into it, as a +// click on that line does while the points are clicked // endcurves logs every free end of the rails: where, the way out and the curvature its track goes on with // curvejoint logs the paths meeting at the end nearest the point, the curvature of each and its jump from the ones before to the ones after // cantcheck