diff --git a/CMakeLists.txt b/CMakeLists.txt index f3bcd6daf..9df50017c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,6 +274,7 @@ set(SOURCES "application/editormodespeed.cpp" "application/editormodenorms.cpp" "application/editormodemodel.cpp" +"application/editormodemodelturnout.cpp" "application/editormodesrk.cpp" "application/editormodekm.cpp" "application/editormodejoints.cpp" diff --git a/application/editormode.h b/application/editormode.h index 8502b8194..35c741077 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -3360,6 +3360,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri std::string text; // its entry as written while it's as it was made: the generator's, or the file's it was read from 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) }; // how a run of commands goes on: the lines the user let write over, what's done after the last one struct model_context @@ -3395,7 +3396,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri // hand asks first void model_run(std::vector Commands, model_context Context); // the tracks of the lines the command changed made again, those of the lines gone taken away: one step of the history with its token - std::vector model_tracks_after(std::uint64_t const Token, std::set const &Changed, std::set const &Removed, model_context const &Context); + std::vector model_tracks_after(std::uint64_t const Token, layout::network const &Before, std::set const &Changed, std::set const &Removed, model_context const &Context); void model_told(layout::outcome const &Outcome, bool const Refused, std::string const &What = {}); // the editor's step of a command taken back or brought again void model_history(std::uint64_t const Token, bool const Undo); @@ -3433,6 +3434,35 @@ class editor_mode : public application_mode, private editor_track::observer, pri static std::string model_reasons_shown(layout::outcome const &Outcome); static std::string model_notice_shown(layout::notice const &Notice); + // the turnouts of the lines of the model through the switch tool (editormodemodelturnout.cpp, stage E4b): the turnout whose track the + // managed track is, nullptr for none + layout::turnout const *model_turnout(TTrack const *Track) const; + // the type of the catalogue the template is of, empty for a template of the user's own + static std::string model_type_of(editor_track::switch_template const &Template); + // the line of the model one of the tracks of which runs through the point, within half a metre, and the chainage of the point on it + bool model_line_at(glm::dvec3 const &Point, std::vector const &Tracks, layout::line_id &Line, double &Chainage) const; + // the turnout the switch dragged lays into a line of the model as it's let go now; none where it isn't dragged on a line of the model. + // Why: what keeps the template from being laid there + std::optional model_switch_drag(std::string &Why) const; + std::optional model_switch_command(std::vector const &Tracks, glm::dvec3 const &Start, glm::dvec2 const &Heading, int const Side, editor_track::switch_template const &Shape, std::string &Why) const; + // the template armed laid as a turnout of the model, its start at the point running along the heading, its diverging track to the + // side (1 as the switch tool has it on the right); false where the point isn't on a line of the model, for the old way + bool model_switch_place(std::vector const &Tracks, glm::dvec3 const &Start, glm::dvec2 const &Heading, int const Side); + // the switch dragged let go on a line of the model: laid as a turnout of the model, or refused; false for the old way + bool model_switch_finish(); + bool model_switch_run(std::optional const &Command, std::string const &Why); + void model_switch_done(std::vector const &Created); + // the switch track of a turnout of the model moved along its line, given another template: true, taken care of here + bool model_switch_slide(TTrack &Switch, double const Distance); + bool model_switch_template(TTrack &Switch, editor_track::switch_template const &Shape); + void model_turnout_run(layout::command const &Command); + void model_turnout_remove(layout::turnout_id const Turnout); + // the crossover dragged between two lines of the model, as the solver of the connections plans it; false for the old way + bool model_connection(); + // the first reason the switch dragged on a line of the model would be refused for as it's let go now, empty where it'd be laid + std::string model_switch_trial() const; + void render_model_turnout(TTrack &Track); + 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(); diff --git a/application/editormodeconnection.cpp b/application/editormodeconnection.cpp index 275e5754e..f575f9496 100644 --- a/application/editormodeconnection.cpp +++ b/application/editormodeconnection.cpp @@ -447,6 +447,9 @@ std::string editor_mode::connection_readout(connection::result const &Plan) cons void editor_mode::insert_connection() { + // a crossover between two lines of the model: the solver's, a command of the model + if (model_connection()) + return; auto &tool{m_switch}; connection::request request; connection::result plan; diff --git a/application/editormodemodel.cpp b/application/editormodemodel.cpp index a84ba2852..9825cbb33 100644 --- a/application/editormodemodel.cpp +++ b/application/editormodemodel.cpp @@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/project/projectSession.hpp" #include "editor/project/scenarioIndexCache.hpp" #include "editor/project/sceneryText.hpp" +#include "editor/layout/layoutTurnout.hpp" #include "scene/scenelayers.h" #include "simulation/simulation.h" #include "utilities/Globals.h" @@ -347,7 +348,7 @@ void editor_mode::model_open() std::map counts; for (auto const &node : model().session.project().generated) { - if (node.kind != "track") + if (node.kind != "track" && node.kind != "switch") continue; auto *track{simulation::Paths.find(node.name)}; if (track == nullptr || track->m_editorremoved) @@ -361,6 +362,7 @@ void editor_mode::model_open() managed_track entry; entry.line = node.line; entry.index = counts[node.line]++; + entry.kind = node.kind; if (found->second.has_value()) if (auto const text{scenery_text::node_text(*found->second, node.name)}) entry.text = *text; @@ -434,6 +436,7 @@ void editor_mode::model_told(layout::outcome const &Outcome, bool const Refused, model().refused = Refused; model().notices.clear(); model().shown_notices.clear(); + model().proposal.clear(); if (Refused) { model().message = "Refused, nothing changed:"; @@ -495,13 +498,14 @@ void editor_mode::model_run(std::vector Commands, model_context for (auto const line : *lines) if (Context.overwrite.contains(line)) overwrite.insert(line); + auto const before{session.network()}; auto const applied{session.execute(command, overwrite)}; if (false == applied.outcome.done) { model_told(applied.outcome, true); return; } - created = model_tracks_after(applied.token, applied.changed, applied.removed, Context); + created = model_tracks_after(applied.token, before, applied.changed, applied.removed, Context); 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()); @@ -512,18 +516,41 @@ void editor_mode::model_run(std::vector Commands, model_context Context.done(created); } -std::vector editor_mode::model_tracks_after(std::uint64_t const Token, std::set const &Changed, std::set const &Removed, model_context const &Context) +std::vector editor_mode::model_tracks_after(std::uint64_t const Token, layout::network const &Before, std::set const &Changed, std::set const &Removed, model_context const &Context) { - std::vector removed; - auto surface{Context.surface}; + auto const &after{model().session.network()}; + // the tracks which go: of the lines changed or gone; of a turnout one of whose tracks is, its switch tracks, kept by its main track, + // before and after the change. Made again: the lines of their own changed, the turnouts there are now + std::set going; + std::set own; + std::set turnouts; for (auto const *lines : {&Changed, &Removed}) for (auto const line : *lines) - for (auto *track : model_line_tracks(line)) + { + going.insert(line); + if (auto const *earlier{layout::owner_of(Before, line)}) + going.insert(earlier->main); + if (auto const *now{layout::owner_of(after, line)}) { - if (surface.empty()) - surface = surface_of(*track); - removed.push_back(track); + going.insert(now->main); + turnouts.insert(now->id); } + else if (lines == &Changed && after.find(line) != nullptr) + own.insert(line); + } + // a turnout whose main track was there before and goes now is made again whole + for (auto const &[id, turnout] : after.turnouts()) + if (going.contains(turnout.main)) + turnouts.insert(id); + std::vector removed; + auto surface{Context.surface}; + for (auto const line : going) + for (auto *track : model_line_tracks(line)) + { + if (surface.empty() && track->eType == tt_Normal) + surface = surface_of(*track); + removed.push_back(track); + } // the old tracks the line takes the place of for (auto *track : Context.replaced) if (false == track->m_editorremoved && std::ranges::find(removed, track) == removed.end()) @@ -536,32 +563,56 @@ std::vector editor_mode::model_tracks_after(std::uint64_t const Token, model().session.options().surface = surface; std::vector created; std::vector> entries; - for (auto const line : Changed) + std::map indices; + // an entry of the generator put into the scene, the track marked as the line's + auto const put = [&](layout::line_id const Line, std::string const &Text, std::string const &Name, std::string const &Kind) { + auto const at{Text.find(" track ")}; + if (at == std::string::npos) + return; + auto *track{editor_track::load_entry(Text.substr(at + 7), Name)}; + if (track == nullptr) + return; + managed_track entry; + entry.line = Line; + entry.index = indices[Line]++; + entry.text = Text; + entry.kind = Kind; + if (track->name() != Name) + { + // another track has the name: the entry written under the one it got + WriteLog("Editor: model: the name " + Name + " is taken, the track is " + track->name(), logtype::generic); + entry.text = "node -1 0 " + track->name() + Text.substr(at); + } + created.push_back(track); + entries.emplace_back(track, std::move(entry)); + }; + for (auto const line : own) { 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); - std::size_t index{0}; for (auto const &piece : made.pieces) + put(line, piece.text, piece.name, "track"); + } + for (auto const id : turnouts) + { + auto const *turnout{after.find(id)}; + if (turnout == nullptr) + 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); + if (made.parts.empty()) + put(turnout->main, made.text, made.name, "switch"); + for (auto const &part : made.parts) { - auto const at{piece.text.find(" track ")}; - if (at == std::string::npos) - continue; - auto *track{editor_track::load_entry(piece.text.substr(at + 7), piece.name)}; - if (track == nullptr) - continue; - managed_track entry; - entry.line = line; - entry.index = index++; - entry.text = piece.text; - if (track->name() != piece.name) + if (part.is_switch) { - // another track has the name: the entry written under the one it got - WriteLog("Editor: model: the name " + piece.name + " is taken, the track is " + track->name(), logtype::generic); - entry.text = "node -1 0 " + track->name() + piece.text.substr(at); + put(turnout->main, part.text, part.name, "switch"); + continue; } - created.push_back(track); - entries.emplace_back(track, std::move(entry)); + for (auto const &piece : part.pieces[0]) + put(turnout->main, piece.text, piece.name, "track"); } } editor_track::commit(created); @@ -637,19 +688,19 @@ void editor_mode::model_saved(bool const Success) model().written.clear(); return; } - // the tracks written, by their lines and the files they went into - std::map, std::vector> lines; + // the tracks written, by their lines, each with the file it went into + std::map> lines; for (auto const &[track, text] : model().written) { auto const found{model().tracks.find(track)}; auto const *source{scene::Layers.source(track)}; if (found == model().tracks.end() || source == nullptr) continue; - lines[{found->second.line, scene::Layers.layer(source->layer).name}].push_back({track->name(), text}); + lines[found->second.line].push_back({track->name(), text, scene::Layers.layer(source->layer).name, found->second.kind}); } model().written.clear(); - for (auto const &[key, pieces] : lines) - model().session.record_written(key.first, pieces, key.second); + for (auto const &[line, pieces] : lines) + model().session.record_written(line, pieces); for (auto const line : model_seen_changed()) model().session.mark_hand_changed(line); std::string error; @@ -733,7 +784,8 @@ bool editor_mode::route_from_model(layout::line_id const Line) auto const *line{model().session.network().find(Line)}; auto const tracks{model_line_tracks(Line)}; model().plan = {}; - if (line == nullptr || tracks.empty()) + // a track of a turnout is edited through its turnout, not in the route design + if (line == nullptr || tracks.empty() || line->turnout != layout::turnout_id::none) return false; auto &route{m_route}; route.from = tracks.front(); @@ -1012,7 +1064,11 @@ bool editor_mode::model_delete(TTrack *Track) return false; if (auto const *managed{model_track(Track)}) { - model_delete_line(managed->line); + // a track of a turnout: the turnout taken out + if (auto const *turnout{layout::owner_of(model_network(), managed->line)}) + model_turnout_remove(turnout->id); + else + model_delete_line(managed->line); return true; } if (Track->name().empty() || Track->name() == "none") @@ -1318,6 +1374,18 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm WriteLog(format("SELFTEST routemove %d: valid %d", index, m_route.result.valid ? 1 : 0)); return selftest_result::handled; } + if (command == "modelturnoutside") + { + // the turnout of the switch track given the other side, as the inspector's button gives it + std::string name; + words >> name; + auto *track{resolve(name)}; + auto const *turnout{model_turnout(track)}; + if (turnout != nullptr) + model_turnout_run(layout::change_turnout{turnout->id, turnout->type, -turnout->hand}); + WriteLog(format("SELFTEST modelturnoutside %s: %s", name.c_str(), turnout != nullptr ? editor_selftest::one_line(model().message).c_str() : "no turnout")); + return selftest_result::handled; + } if (command == "modelsplit" || command == "modeldelete" || command == "modeljoin") { auto const line{model_plan_line()}; @@ -1408,9 +1476,33 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm auto const &network{model().session.network()}; for (auto const &[id, line] : network.lines()) { - auto const tracks{model_line_tracks(id)}; auto const *start{network.find(line.start)}; auto const *end{network.find(line.end)}; + // a track of a turnout: the path of its switch track from the start of the turnout, at the numbers of the nodes at its ends + if (line.turnout != layout::turnout_id::none) + { + auto const *turnout{network.find(line.turnout)}; + if (turnout == nullptr || start == nullptr || end == nullptr || layout::kind_of(*turnout) != layout::turnout_kind::ordinary) + continue; + TTrack const *switched{nullptr}; + for (auto const *track : model_line_tracks(turnout->main)) + if (track->eType == tt_Switch && track->m_paths.size() >= 2) + switched = track; + auto const path{id == turnout->main ? 0 : id == turnout->diverging ? 1 : -1}; + if (switched == nullptr || path < 0) + { + problems += " " + line_name(id) + " has no switch track"; + continue; + } + auto const &points{switched->m_paths[path].points}; + auto const from{points[segment_data::point::start]}; + auto const to{points[segment_data::point::end]}; + if (false == ((bits_equal(from, start->position) && bits_equal(to, end->position)) || (bits_equal(from, end->position) && bits_equal(to, start->position)))) + problems += format(" %s: the path %d of %s off its nodes", line_name(id).c_str(), path, switched->name().c_str()); + ++checked; + continue; + } + auto const tracks{model_line_tracks(id)}; if (tracks.empty() || start == nullptr || end == nullptr) { problems += " " + line_name(id) + " has no tracks"; @@ -1465,6 +1557,30 @@ editor_mode::selftest_result editor_mode::selftest_model(std::string const &comm expectation(holds == (mode == "holds"), "modelmessage " + mode + " " + text, editor_selftest::one_line(message), (mode == "holds" ? "holding " : "lacking ") + text); return selftest_result::handled; } + if (what == "handlines") + { + // the lines taken for changed by hand: in the project, or seen changed by the other tools since they were made + std::size_t wanted{0}; + words >> wanted; + auto marked{model_seen_changed()}; + marked.insert(model().session.project().hand_changed.begin(), model().session.project().hand_changed.end()); + std::string lines; + for (auto const line : marked) + lines += " " + line_name(line); + expectation(marked.size() == wanted, "handlines", std::to_string(marked.size()) + lines, std::to_string(wanted)); + return selftest_result::handled; + } + if (what == "modelproposal") + { + // the solver's best proposal for the crossover refused last + std::string mode, text; + words >> mode; + std::getline(words, text); + text.erase(0, text.find_first_not_of(' ')); + auto const holds{false == model().proposal.empty() && model().proposal.find(text) != std::string::npos}; + expectation(holds == (mode == "holds"), "modelproposal " + mode + " " + text, model().proposal.empty() ? std::string{"none"} : model().proposal, (mode == "holds" ? "holding " : "lacking ") + text); + return selftest_result::handled; + } if (what == "hand") { unsigned line{0}; diff --git a/application/editormodemodel.h b/application/editormodemodel.h index 3a37dbf86..31dcc7c58 100644 --- a/application/editormodemodel.h +++ b/application/editormodemodel.h @@ -71,5 +71,8 @@ struct editor_model 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 + std::string proposal; // the solver's best proposal for a crossover refused + std::pair 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 }; diff --git a/application/editormodemodelturnout.cpp b/application/editormodemodelturnout.cpp new file mode 100644 index 000000000..c96efe08c --- /dev/null +++ b/application/editormodemodelturnout.cpp @@ -0,0 +1,408 @@ +/* +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/. +*/ + +// The turnouts of the lines of the model laid, moved, changed and taken out through the switch tool (editor-tracks-domain.md §7, §8; +// stage E4b), with the setting "Editing on the model (trial)" on. A switch dragged on a track of a line of the model is a turnout of the +// catalogue inserted into the line (insert_turnout), its switch track made by the generator; moved along the line, given another type or +// the other side, or taken out, a command of the model each; a crossover dragged between two lines of the model is the solver's +// (connect_tracks). A command refused changes nothing and the tool tells why in its red line, with the value, the limit and the paragraph; +// one kept is one step of the history. The switches of the old tracks are laid the old way. + +#include "stdafx.h" +#include "application/editormode.h" +#include "application/editormodemodel.h" +#include "application/editorflow.h" +#include "application/editormodeselftest.h" +#include "application/editormodeshared.h" +#include "application/editoruilayer.h" +#include "editor/editorFormat.hpp" +#include "editor/layout/layoutTurnout.hpp" +#include "utilities/Logs.h" +#include "utilities/translation.h" +#include "world/Track.h" + +#include "imgui/imgui.h" + +#include +#include + +namespace +{ + +std::string line_name(layout::line_id const Line) +{ + return format("L%u", layout::number(Line)); +} + +std::string turnout_name(layout::turnout_id const Turnout) +{ + return format("T%u", layout::number(Turnout)); +} + +double cross(glm::dvec2 const &A, glm::dvec2 const &B) +{ + return A.x * B.y - A.y * B.x; +} + +} // namespace + +layout::turnout const *editor_mode::model_turnout(TTrack const *Track) const +{ + auto const *managed{model_track(Track)}; + if (managed == nullptr) + return nullptr; + return layout::owner_of(model_network(), managed->line); +} + +std::string editor_mode::model_type_of(editor_track::switch_template const &Template) +{ + // the label begins with the name of the type of the catalogue, the lengths after it: the longest name it begins with + std::string best; + if (Template.own) + return best; + for (auto const &name : layout::turnout_types()) + if (name.size() > best.size() && Template.label.compare(0, name.size(), name) == 0 && (Template.label.size() == name.size() || Template.label[name.size()] == ' ')) + best = name; + return best; +} + +bool editor_mode::model_line_at(glm::dvec3 const &Point, std::vector const &Tracks, layout::line_id &Line, double &Chainage) const +{ + if (false == model_on()) + return false; + double constexpr kReach{0.5}; // m, of the point from the line + auto best{kReach}; + auto found{false}; + std::set seen; + for (auto const *track : Tracks) + { + auto const *managed{model_track(track)}; + if (managed == nullptr || false == seen.insert(managed->line).second) + continue; + auto const *line{model_network().find(managed->line)}; + if (line == nullptr || false == line->solved.valid()) + continue; + double distance{0.0}; + auto const at{layout::chainage_of(line->solved, {Point.x, Point.z}, &distance)}; + if (distance < best) + { + best = distance; + Line = managed->line; + Chainage = at; + found = true; + } + } + return found; +} + +std::optional editor_mode::model_switch_drag(std::string &Why) const +{ + Why.clear(); + auto const &tool{m_switch}; + if (false == model_on() || tool.free || tool.connection >= 0 || tool.armed < 0 || tool.armed >= static_cast(tool.templates.size())) + return std::nullopt; + glm::dvec3 start{0.0}; + glm::dvec2 heading{0.0, 1.0}; + int side{1}; + std::vector tracks; + if (tool.curved) + { + if (tool.frame.size() < 2) + return std::nullopt; + int forward{1}; + curved_switch_pose(forward, side); + auto const sample{sample_at(tool.frame, tool.along)}; + start = sample.position; + heading = sample.tangent * static_cast(forward); + tracks = tool.frame_tracks; + } + else + { + if (tool.line.tracks.empty()) + return std::nullopt; + glm::dvec2 const offset{tool.mouse.x - tool.point.x, tool.mouse.z - tool.point.z}; + auto const forward{glm::dot(offset, tool.line.direction) >= 0.0 ? 1 : -1}; + heading = tool.line.direction * static_cast(forward); + side = cross(heading, offset) >= 0.0 ? 1 : -1; + start = tool.point; + tracks = tool.line.tracks; + } + return model_switch_command(tracks, start, heading, side, tool.templates[tool.armed], Why); +} + +std::optional editor_mode::model_switch_command(std::vector const &Tracks, glm::dvec3 const &Start, glm::dvec2 const &Heading, int const Side, editor_track::switch_template const &Shape, std::string &Why) const +{ + layout::line_id line{layout::line_id::none}; + double chainage{0.0}; + if (false == model_line_at(Start, Tracks, line, chainage)) + return std::nullopt; + layout::insert_turnout command; + command.line = line; + command.chainage = chainage; + command.type = model_type_of(Shape); + if (command.type.empty()) + Why = format(STR_C("%s isn't of the catalogue of the turnouts: a line of the model takes the types of the catalogue"), template_name(Shape).c_str()); + // the side the diverging track leaves to as the switch runs from its start: the side of the drag, in the plan as the model takes it + command.hand = Side; + auto const *found{model_network().find(line)}; + command.direction = found != nullptr && glm::dot(Heading, found->solved.tangent(chainage)) < 0.0 ? -1 : 1; + return command; +} + +bool editor_mode::model_switch_place(std::vector const &Tracks, glm::dvec3 const &Start, glm::dvec2 const &Heading, int const Side) +{ + auto &tool{m_switch}; + if (false == model_on() || tool.armed < 0 || tool.armed >= static_cast(tool.templates.size())) + return false; + std::string why; + return model_switch_run(model_switch_command(Tracks, Start, Heading, Side, tool.templates[tool.armed], why), why); +} + +bool editor_mode::model_switch_finish() +{ + std::string why; + return model_switch_run(model_switch_drag(why), why); +} + +bool editor_mode::model_switch_run(std::optional const &Command, std::string const &Why) +{ + auto &tool{m_switch}; + if (false == Command.has_value()) + return false; + auto const &command{Command}; + auto const why{Why}; + tool.placing = false; + if (false == why.empty()) + { + tool.status = switch_refused(why); + return true; + } + model_context context; + context.done = [this](std::vector const &Created) { model_switch_done(Created); }; + model_run({*command}, context); + if (model_refused()) + tool.status = switch_refused(model_shown()); + WriteLog(format("Editor: model: a turnout %s into line %s at %.3f m, hand %d, direction %d: %s", command->type.c_str(), line_name(command->line).c_str(), command->chainage, command->hand, command->direction, editor_selftest::one_line(model_message()).c_str()), logtype::generic); + return true; +} + +void editor_mode::model_switch_done(std::vector const &Created) +{ + auto &tool{m_switch}; + tool.placing = false; + ui()->drop_refusal(); + tool.refusal.clear(); + auto const found{std::ranges::find_if(Created, [](TTrack const *Track) { return Track->eType == tt_Switch; })}; + if (found != Created.end()) + { + first_step_done(first_step::turnout); + m_track_point = {}; + m_node = *found; + ui()->set_node(m_node); + } + tool.status = model_shown(); + if (auto const *turnout{found != Created.end() ? model_turnout(*found) : nullptr}) + tool.status = format(STR_C("Turnout %s: %s, %s, %.0f km/h on the diverging track"), turnout_name(turnout->id).c_str(), turnout->type.c_str(), turnout->hand > 0 ? STR_C("right") : STR_C("left"), turnout->speed_diverging) + "\n" + model_shown(); + straight_refresh(); +} + +bool editor_mode::model_switch_slide(TTrack &Switch, double const Distance) +{ + auto const *turnout{model_turnout(&Switch)}; + if (turnout == nullptr) + return false; + model_turnout_run(layout::move_turnout{turnout->id, Distance}); + return true; +} + +bool editor_mode::model_switch_template(TTrack &Switch, editor_track::switch_template const &Shape) +{ + auto const *turnout{model_turnout(&Switch)}; + if (turnout == nullptr) + return false; + auto const type{model_type_of(Shape)}; + if (type.empty()) + { + m_switch.status = switch_refused(format(STR_C("%s isn't of the catalogue of the turnouts: a line of the model takes the types of the catalogue"), template_name(Shape).c_str())); + return true; + } + model_turnout_run(layout::change_turnout{turnout->id, type, std::nullopt}); + return true; +} + +void editor_mode::model_turnout_run(layout::command const &Command) +{ + auto &tool{m_switch}; + model_context context; + context.done = [this](std::vector const &Created) { model_switch_done(Created); }; + model_run({Command}, context); + if (model_refused()) + tool.status = switch_refused(model_shown()); +} + +void editor_mode::model_turnout_remove(layout::turnout_id const Turnout) +{ + auto const *turnout{model_network().find(Turnout)}; + if (turnout == nullptr) + return; + std::vector names; + for (auto const *track : model_line_tracks(turnout->main)) + names.push_back(track->name()); + model_check_users(names, [this, Turnout]() { + model_context context; + context.done = [this](std::vector const &) { + m_node = nullptr; + ui()->set_node(nullptr); + m_track_point = {}; + m_switch.status = model_shown(); + }; + model_run({layout::remove_turnout{Turnout}}, context); + if (model_refused()) + m_switch.status = switch_refused(model_shown()); + }); +} + +bool editor_mode::model_connection() +{ + auto &tool{m_switch}; + if (false == model_on() || tool.curved || tool.connection != static_cast(connection::kind::crossover) || tool.armed < 0 || tool.armed >= static_cast(tool.templates.size()) || tool.line.tracks.empty()) + return false; + layout::line_id from{layout::line_id::none}; + double chainage{0.0}; + if (false == model_line_at(tool.point, tool.line.tracks, from, chainage)) + return false; + // the line beside, where the drag is let go + std::vector const exclude(tool.line.tracks.begin(), tool.line.tracks.end()); + auto const other{straight_near(tool.mouse, exclude, 8.0)}; + layout::line_id to{layout::line_id::none}; + double target{0.0}; + auto const across{other.start + glm::dvec3{other.direction.x, other.grade, other.direction.y} * std::clamp(other.along(tool.mouse), 0.0, other.length)}; + if (other.tracks.empty() || false == model_line_at(across, other.tracks, to, target)) + { + // the other track an old one: no crossover of the model, the old way + return false; + } + tool.placing = false; + layout::connect_tracks command; + command.from = from; + command.chainage = chainage; + command.to = to; + command.type = model_type_of(tool.templates[tool.armed]); + if (command.type.empty()) + { + tool.status = switch_refused(format(STR_C("%s isn't of the catalogue of the turnouts: a line of the model takes the types of the catalogue"), template_name(tool.templates[tool.armed]).c_str())); + return true; + } + glm::dvec2 const offset{tool.mouse.x - tool.point.x, tool.mouse.z - tool.point.z}; + auto const *line{model_network().find(from)}; + command.direction = line != nullptr && glm::dot(offset, line->solved.tangent(chainage)) < 0.0 ? -1 : 1; + // the solver's plan first: refused, its best proposal told with the reasons + auto const plan{layout::plan_connection(model_network(), command)}; + model_context context; + context.done = [this](std::vector const &Created) { model_switch_done(Created); }; + model_run({command}, context); + if (model_refused()) + { + auto shown{model_shown()}; + if (false == plan.proposal.empty()) + shown += "\n" + format(STR_C("Proposal: %s"), model_tr(plan.proposal).c_str()); + tool.status = switch_refused(shown); + model().proposal = plan.proposal; + } + WriteLog(format("Editor: model: a crossover %s from line %s at %.3f m to line %s: %s%s", command.type.c_str(), line_name(from).c_str(), chainage, line_name(to).c_str(), editor_selftest::one_line(model_message()).c_str(), + plan.proposal.empty() ? "" : (" · proposal: " + plan.proposal).c_str()), + logtype::generic); + return true; +} + +std::string editor_mode::model_switch_trial() const +{ + // what the switch dragged on a line of the model comes to as it's let go: the first reason it's refused for, empty where it's laid + std::string why; + auto const command{model_switch_drag(why)}; + if (false == command.has_value()) + return {}; + if (false == why.empty()) + return why; + auto &memo{model().drag}; + auto const key{format("%u %.2f %s %d %d", layout::number(command->line), command->chainage, command->type.c_str(), command->hand, command->direction)}; + if (memo.first != key) + { + auto const effect{model().session.trial(*command)}; + memo = {key, effect.outcome.done ? std::string{} : model_reasons_shown(effect.outcome)}; + } + return memo.second; +} + +void editor_mode::render_model_turnout(TTrack &Track) +{ + auto const *turnout{model_turnout(&Track)}; + if (turnout == nullptr) + return; + auto &tool{m_switch}; + if (false == tool.collected) + { + tool.templates = editor_track::standard_switch_templates(); + tool.collected = true; + } + auto const id{turnout->id}; + ImGui::TextColored(ImVec4(0.45f, 0.85f, 0.55f, 1.0f), "%s", format(STR_C("Turnout %s (managed)"), turnout_name(id).c_str()).c_str()); + item_tooltip("A turnout of the model of the track geometry: moved, given another type or side, or taken out by its commands, checked against the norms; its tracks made anew"); + auto summary{format(STR_C("%s · %s · %.0f / %.0f km/h"), turnout->type.c_str(), turnout->hand > 0 ? STR_C("right") : STR_C("left"), turnout->speed_main, turnout->speed_diverging)}; + if (turnout->bent()) + summary += format(STR_C(" · bent R %.0f m"), std::abs(turnout->main_radius)); + ImGui::TextDisabled("%s", summary.c_str()); + // the type: the ones of the catalogue of the same kind + auto const kind{layout::kind_of(*turnout)}; + int current{-1}; + for (int i = 0; i < static_cast(tool.templates.size()); ++i) + if (model_type_of(tool.templates[i]) == turnout->type) + { + current = i; + break; + } + auto const fits = [this, kind](editor_track::switch_template const &Template) { + auto const type{model_type_of(Template)}; + auto const known{type.empty() ? std::nullopt : layout::turnout_type_of(type)}; + return known.has_value() && known->kind == kind; + }; + if (auto const picked{render_template_picker("##modelturnout", fits, editor_track::rail_of(&Track), current, false)}; picked >= 0 && picked != current) + { + model_switch_template(Track, tool.templates[picked]); + return; + } + if (ImGui::Button(STR_C("Other side"))) + { + model_turnout_run(layout::change_turnout{id, turnout->type, -turnout->hand}); + return; + } + item_tooltip("The diverging track to the other side, the start of the turnout where it is"); + ImGui::SameLine(); + auto &distance{model().turnout_move}; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 5.0f); + ImGui::InputDouble("##modelturnoutmove", &distance, 0.0, 0.0, "%.1f m"); + ImGui::SameLine(); + if (ImGui::Button(STR_C("Move")) && std::abs(distance) > 0.005) + { + model_turnout_run(layout::move_turnout{id, distance}); + return; + } + item_tooltip("Moved along its line by the distance, above 0 toward the end of its main track: checked as when it's laid"); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.55f, 0.18f, 0.15f, 1.0f)); + if (ImGui::Button(STR_C("Take the turnout out"))) + { + ImGui::PopStyleColor(); + model_turnout_remove(id); + return; + } + ImGui::PopStyleColor(); + item_tooltip("Its diverging track goes, the line under it is one line again where it can be. Ctrl+Z brings it back"); + if (false == tool.status.empty()) + editor_flow::message_line(tool.status, tool.status == tool.refusal ? editor_flow::kRefused : ImVec4(0.7f, 0.7f, 0.7f, 1.0f)); +} diff --git a/application/editormodeselftest.cpp b/application/editormodeselftest.cpp index 08a05873b..e4d238c64 100644 --- a/application/editormodeselftest.cpp +++ b/application/editormodeselftest.cpp @@ -259,6 +259,12 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // expect modelends, expect modelrefused <0|1>, expect modelmessage , expect modelvertex // , expect modelsame