diff --git a/CMakeLists.txt b/CMakeLists.txt index c58b5f4a3..33b764052 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -236,6 +236,7 @@ set(SOURCES "editor/project/scenarioIndexCache.cpp" "editor/project/projectSession.cpp" "editor/editorNormsReport.cpp" +"editor/editorProjectSetup.cpp" "editor/editorProfile.cpp" "editor/editorGauge.cpp" "editor/editorInfra.cpp" @@ -252,6 +253,8 @@ set(SOURCES "application/editormoderoute.cpp" "application/editormodestraights.cpp" "application/editormodelay.cpp" +"application/editorsetupui.cpp" +"application/editormodesetup.cpp" "application/editormodeswitchplace.cpp" "application/editormodetrackgizmo.cpp" "application/editormoderecords.cpp" diff --git a/application/editormode.cpp b/application/editormode.cpp index c02386c39..f0f4a415b 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -1802,7 +1802,10 @@ bool editor_mode::update() render_orthophoto_window(); draw_selection_overlay(); render_context_menu(); - // the track geometry edited through the model: the project of the map, the questions waiting for an answer + // the assumptions of the project read with the scenery; then the track geometry edited through the model: the project of the map, + // the questions waiting for an answer + setup_update(); + render_setup_window(); model_update(); render_model_windows(); steps.step("windows"); @@ -1866,6 +1869,14 @@ void editor_mode::render_settings() // the tracks laid and edited through the model of the track geometry, a trial: off, as the editor was ImGui::SeparatorText(STR_C("Tracks")); + // what the scenery is drawn to: the norms, the speed, the gauge, the rail + if (ImGui::Button(STR_C("Assumptions of the project..."))) + { + m_setup_edit = setup_now(); + m_setup_window = true; + } + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("%s", STR_C("What the scenery is drawn to: the norms, the speed of the line, the traffic, the track, the gauge and the rail; the defaults of the tools")); { bool model{EditorSettings.model_editing()}; if (ImGui::Checkbox(STR_C("Editing on the model (trial)"), &model)) @@ -3412,6 +3423,7 @@ bool editor_mode::save() if (m_profile.changed) profile_store(); + setup_store(); switch_store::tidy(); switch_store::settle(); infra_store(); diff --git a/application/editormode.h b/application/editormode.h index 1f1c0d8d3..70c98b156 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -44,6 +44,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorSpeed.hpp" #include "editor/editorRoad.hpp" #include "editor/editorProfile.hpp" +#include "editor/editorProjectSetup.hpp" #include "editor/editorRecords.hpp" #include "editor/editorGroundFollow.hpp" #include "editor/editorInfra.hpp" @@ -317,6 +318,32 @@ class editor_mode : public application_mode, private editor_track::observer, pri void first_step_done(first_step const Step) override; void open_kilometrage() override; bool m_first_steps_open{false}; // the window of the lesson + // the assumptions of the project ("Co rysujesz?"): none until they're said, the card of the inspector of the tracks asking for them; + // the defaults meanwhile. Kept with the map: in its project file where it has one, else in the scenery (//$u) + std::optional m_setup; + project_setup::setup m_setup_edit; // the fields of the card and of the window + bool m_setup_read{false}; // read for the scenery open + bool m_setup_changed{false}; // to be written with the scenery + bool m_setup_marked{false}; // the scenery holds //$u lines + bool m_setup_window{false}; + std::string m_setup_told; // what the change of them found among the lines laid before + bool m_setup_checking{false}; // the lines of the scenery being checked against the assumptions changed + project_setup::setup setup_now() const { return m_setup.value_or(project_setup::setup{}); } + void setup_update(); + void setup_read(); + // the assumptions taken: the defaults of the tools from now on; Changed: said by the user, to be written with the scenery, the lines + // laid before checked against them + void setup_apply(project_setup::setup const &Setup, bool const Changed); + // the defaults of the tools from the assumptions: the speed of the laying and of the plan, the gauge and the look of the tracks laid, + // the norms checked against, the rail and the usual switch of the switch tool + void setup_defaults(project_setup::setup const &Setup); + void setup_store(); + bool render_setup_card(); + void render_setup_window(); + void render_setup_line(); + // the assumptions into the project of the map where it's open, the profile of its scenery with them; false where it isn't + bool model_setup(); + editor_selftest_result selftest_setup(std::string const &command, std::istringstream &words); int m_first_steps_shown{0}; // frames the window of the lesson was drawn since it opened, up to 3: it's placed anew as it opens std::array(first_step::count)> m_first_steps_done{}; int m_tutorial_page{0}; @@ -1678,6 +1705,9 @@ class editor_mode : public application_mode, private editor_track::observer, pri int matching_template(switch_store::record const &Record, bool const Lengths, std::optional const Movable = std::nullopt) const; // the template the switch was laid of, -1 if none: a switch of the scenery of its own shape, say int switch_template_index(TTrack const &Switch) const; + // the usual switch of the rail for the speed on its diverging track, as the assumptions of the project ask for one: the closest speed, + // the skos usual for it; -1 for none + int usual_switch(std::string const &Rail, double const Speed) const; // why the switch can't take another template here, empty when it can std::string template_change_blocked(TTrack const &Switch) const; // the template of the catalogue a slip or a crossing was laid of, of the rail before the others; -1 if none diff --git a/application/editormodelay.cpp b/application/editormodelay.cpp index 063bff641..9b4935ab2 100644 --- a/application/editormodelay.cpp +++ b/application/editormodelay.cpp @@ -2381,6 +2381,7 @@ void editor_mode::route_drop() auto const norms{m_route.design.norms}; m_route = {}; m_route.design.norms = norms; + m_route.design.speed = setup_now().speed; } alignment::limits editor_mode::laying_norms() const diff --git a/application/editormodemodel.cpp b/application/editormodemodel.cpp index 2c65e389b..6abb3d847 100644 --- a/application/editormodemodel.cpp +++ b/application/editormodemodel.cpp @@ -340,6 +340,9 @@ void editor_mode::model_open() WriteLog(format("Editor: the project of the map %s %s, %zu lines of the model", map.c_str(), opened.migrated ? "made of its //$ lines" : "read", model().session.network().lines().size()), logtype::generic); for (auto const ¬e : opened.notes) WriteLog("Editor: project: " + note, logtype::generic); + // the assumptions of the project go into its file from now on, the profile of the scenery with them + if (model_setup()) + m_setup_changed = true; for (auto const &change : opened.hand_changes) WriteLog(format("Editor: project: the track %s of line %s in %s %s outside the editor", change.name.c_str(), line_name(change.line).c_str(), change.file.c_str(), change.missing ? "is gone" : "was changed"), logtype::generic); // the tracks the generator wrote, found in the scene by their names; written again as the files have them while nothing changes them @@ -728,6 +731,14 @@ void editor_mode::model_saved(bool const Success) }()), logtype::generic); } +bool editor_mode::model_setup() +{ + if (false == model_on() || false == m_setup.has_value()) + return false; + model().session.setup(project_setup::record(*m_setup), m_setup->profile); + return true; +} + bool editor_mode::model_lay(lay_line_plan const &Plan, editor_track::snap_target const &End, std::vector &Created, std::string &Why) { if (false == model_on()) @@ -766,7 +777,8 @@ bool editor_mode::model_lay(lay_line_plan const &Plan, editor_track::snap_target line.vertices.push_back(made); } line.transition = design.shape == alignment::transition_shape::clothoid ? layout::transition_shape::clothoid : layout::transition_shape::cubic_parabola; - line.category.vmax = design.speed; + // the class of the line of the assumptions of the project, at the speed it's designed for + line.category = project_setup::line_class(setup_now(), design.speed); // from the free end of a line of the model, or to one: the node of that end shared, the leg there on its tangent, so the line goes on // from it with no kink auto const free_node = [&](editor_track::snap_target const &Target) -> layout::node const * { @@ -1023,7 +1035,7 @@ bool editor_mode::model_plan_convert(std::string &Why) line.vertices.push_back(made); } line.transition = design.shape == alignment::transition_shape::clothoid ? layout::transition_shape::clothoid : layout::transition_shape::cubic_parabola; - line.category.vmax = design.speed; + line.category = project_setup::line_class(setup_now(), design.speed); auto const tracks{chain.tracks}; std::vector names; for (auto const *track : tracks) diff --git a/application/editormodenorms.cpp b/application/editormodenorms.cpp index fc6d717c5..04587fe9f 100644 --- a/application/editormodenorms.cpp +++ b/application/editormodenorms.cpp @@ -140,6 +140,27 @@ void norms_tool::start() state.history = m_history.history_revision(); } +void norms_tool::defaults(norms::profile_id const Profile, norms::line_class const &Line) +{ + m_state.profile = static_cast(Profile); + m_state.options.profile = Profile; + m_state.options.line = Line; +} + +void norms_tool::check_scenery() +{ + m_state.scope = 1; + m_state.open = true; + start(); +} + +std::size_t norms_tool::lines_beyond(norms::tier const Tier) const +{ + return static_cast(std::ranges::count_if(m_state.lines, [Tier](norms_report::checked_line const &Line) { + return std::ranges::any_of(Line.entries, [Tier](norms_report::entry const &Entry) { return static_cast(Entry.finding.level) >= static_cast(Tier); }); + })); +} + void norms_tool::update() { auto &state{m_state}; diff --git a/application/editormodenorms.h b/application/editormodenorms.h index e3e492acd..79e89b1f2 100644 --- a/application/editormodenorms.h +++ b/application/editormodenorms.h @@ -29,6 +29,13 @@ class norms_tool final : public editor_tool void set_open(bool const Open) { m_state.open = Open; } // what the last check found, for the expectations of the self-test std::vector const &results() const { return m_state.results; } + // the profile and the class of the line the checks are made against, as the assumptions of the project have them + void defaults(norms::profile_id const Profile, norms::line_class const &Line); + // the lines of the whole scenery checked against them, from now on, frame after frame, the window open on the report + void check_scenery(); + bool checking() const { return false == m_state.queue.empty(); } + // the lines of the check done last with a finding of the tier or worse + std::size_t lines_beyond(norms::tier const Tier) const; private: editor_selection &m_selection; diff --git a/application/editormodeprofile.cpp b/application/editormodeprofile.cpp index dbad028dd..31dec8c22 100644 --- a/application/editormodeprofile.cpp +++ b/application/editormodeprofile.cpp @@ -1046,7 +1046,7 @@ void editor_mode::profile_recognize() auto const read = [&](profile::recognition const &Options) { state.line = profile::recognize(chainages, elevations, grades, Options); state.line.norms = norms; - state.line.speed = editor_track::route_speed(state.route); + state.line.speed = editor_track::route_speed(state.route, setup_now().speed); state.selected = -1; profile_check(); }; @@ -1075,7 +1075,7 @@ void editor_mode::profile_fit_ground() return; } auto settings{state.line}; - settings.speed = editor_track::route_speed(state.route); + settings.speed = editor_track::route_speed(state.route, setup_now().speed); // the grade line is the rail top: it's fitted so the formation under it, not the rails, follows the ground auto ground{state.terrain}; for (std::size_t i = 0; i < ground.size() && i < state.beds.size(); ++i) diff --git a/application/editormoderoute.cpp b/application/editormoderoute.cpp index cdd7233b8..4951f8c54 100644 --- a/application/editormoderoute.cpp +++ b/application/editormoderoute.cpp @@ -112,7 +112,7 @@ void editor_mode::route_reset() design.norms = route.design.norms; design.shape = route.design.shape; design.transition_pieces = route.design.transition_pieces; - design.speed = chain.velocity > 0.0 ? chain.velocity : 100.0; + design.speed = chain.velocity > 0.0 ? chain.velocity : setup_now().speed; route.design = design; route_bind_ends(); design = route.design; diff --git a/application/editormodeselftest.cpp b/application/editormodeselftest.cpp index e4d238c64..ead90fb56 100644 --- a/application/editormodeselftest.cpp +++ b/application/editormodeselftest.cpp @@ -53,7 +53,7 @@ http://mozilla.org/MPL/2.0/. editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &command, std::istringstream &words) { - for (auto const group : {&editor_mode::selftest_scene, &editor_mode::selftest_sleepers, &editor_mode::selftest_sweep, &editor_mode::selftest_track, &editor_mode::selftest_switch, &editor_mode::selftest_rails, &editor_mode::selftest_join, &editor_mode::selftest_signal, &editor_mode::selftest_terrain, &editor_mode::selftest_props, &editor_mode::selftest_monkey, &editor_mode::selftest_model, &editor_mode::selftest_expect}) + for (auto const group : {&editor_mode::selftest_scene, &editor_mode::selftest_sleepers, &editor_mode::selftest_sweep, &editor_mode::selftest_track, &editor_mode::selftest_switch, &editor_mode::selftest_rails, &editor_mode::selftest_join, &editor_mode::selftest_signal, &editor_mode::selftest_terrain, &editor_mode::selftest_props, &editor_mode::selftest_monkey, &editor_mode::selftest_model, &editor_mode::selftest_setup, &editor_mode::selftest_expect}) if (auto const result{(this->*group)(command, words)}; result != selftest_result::unhandled) return result; for (auto *tool : m_tools) @@ -267,6 +267,10 @@ editor_mode::selftest_result editor_mode::selftest_dispatch(std::string const &c // the editing on the model of E4b (editormodemodel.cpp, tests/editor/README.md) // each expect logs SELFTEST PASS or SELFTEST FAIL; a failed one, or a command no group takes, // makes the run end with editor_selftest::failed_exit_code +// setup [ ]..., setupwindow [close] the assumptions of the project set as the Start of their card sets them, the keys +// of their record over the ones there are (norms, speed, traffic, track, gauge, rail); their window +// expect setup none| , expect setupmark the assumptions of the scenery, their line in it (editormodesetup.cpp) +// expect layspeed the speed the lay tool lays at // quit logs SELFTEST result: failed of , the editor closes void editor_mode::selftest_step() { diff --git a/application/editormodesetup.cpp b/application/editormodesetup.cpp new file mode 100644 index 000000000..8ca2150c5 --- /dev/null +++ b/application/editormodesetup.cpp @@ -0,0 +1,377 @@ +/* +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 assumptions of the project ("Co rysujesz?", editor-project-setup.md): what the user draws, asked for by a card of the inspector of +// the tracks in a scenery with none, or by the last step of the wizard of a new scenery, and changed later in a window of their own. Kept +// with the map, in its project file where it has one, else in the scenery as //$u setup ...; the defaults of the tools from then on. A +// change of them checks the lines laid before against them, the report in the window of the norms; nothing laid is changed by it + +#include "stdafx.h" +#include "application/editormode.h" +#include "application/editormodeselftest.h" +#include "application/editormodeshared.h" +#include "application/editorflow.h" +#include "application/editorsetupui.h" +#include "application/editoruilayer.h" +#include "editor/editorFormat.hpp" +#include "editor/project/projectFile.hpp" +#include "editor/project/projectSession.hpp" +#include "scene/scenelayers.h" +#include "simulation/simulation.h" +#include "utilities/Globals.h" +#include "utilities/Logs.h" +#include "utilities/translation.h" +#include "utilities/utilities.h" +#include "world/Track.h" + +#include "imgui/imgui.h" + +#include +#include +#include +#include + +namespace +{ + +std::filesystem::path project_file_of_map() +{ + auto const map{scene::Layers.layer(scene::layer_handle{1}).name}; + return project::path_of(std::filesystem::path{paths::scenery} / map); +} + +bool any_track() +{ + return std::ranges::any_of(simulation::Paths.sequence(), [](TTrack const *Track) { return Track != nullptr && false == Track->m_editorremoved && (Track->iCategoryFlag & 15) == 1; }); +} + +} // namespace + +void editor_mode::setup_update() +{ + if (false == m_setup_read && Global.editor_session && false == scene::Layers.empty()) + setup_read(); + // the lines laid before checked against the assumptions changed: what it came to + if (m_setup_checking && false == m_norms.checking()) + { + m_setup_checking = false; + auto const blocked{m_norms.lines_beyond(norms::tier::block)}; + auto const warned{m_norms.lines_beyond(norms::tier::warn)}; + m_setup_told = blocked + warned == 0 ? std::string{STR_C("The lines laid before meet the norms of these assumptions")} + : format(STR_C("%zu of the lines laid before don't meet these norms (%zu beyond the hard limits): the report is in the window of the norms. Nothing was changed"), + warned, blocked); + WriteLog("Editor: the assumptions of the project changed: " + m_setup_told, logtype::generic); + } +} + +void editor_mode::setup_read() +{ + m_setup_read = true; + std::optional found; + // the project file of the map first, then the scenery + std::error_code error; + if (auto const file{project_file_of_map()}; std::filesystem::exists(file, error)) + if (auto const loaded{project::load(file)}; loaded.project.has_value() && false == loaded.project->setup.empty()) + found = project_setup::read(std::string_view{loaded.project->setup}); + auto const marked{scene::Layers.marked(scene::layer_handle{1}, "//$u")}; + m_setup_marked = false == marked.empty(); + if (false == found.has_value()) + found = project_setup::read(marked); + if (found.has_value()) + { + setup_apply(*found, false); + WriteLog("Editor: the assumptions of the project: " + project_setup::record(*found), logtype::generic); + } + else + WriteLog("Editor: the scenery has no assumptions of the project yet, the defaults meanwhile", logtype::generic); +} + +void editor_mode::setup_apply(project_setup::setup const &Setup, bool const Changed) +{ + // said for the first time, or changed: only a change checks the lines laid before + auto const before{m_setup}; + m_setup = Setup; + m_setup_edit = Setup; + setup_defaults(Setup); + model_setup(); + if (false == Changed) + return; + m_setup_changed = true; + m_setup_told.clear(); + WriteLog("Editor: the assumptions of the project set: " + project_setup::record(Setup), logtype::generic); + // the lines laid before: which of them don't meet the norms of these, nothing changed + if (before.has_value() && *before != Setup && any_track()) + { + m_norms.check_scenery(); + m_setup_checking = true; + } +} + +void editor_mode::setup_defaults(project_setup::setup const &Setup) +{ + // the speed of the laying and of the plan, the look and the gauge of the tracks laid, the norms + m_lay.style.velocity = Setup.speed; + m_lay.style.gauge = Setup.gauge / 1000.0; + if (false == Setup.rails.empty()) + m_lay.style.rail = Setup.rails; + if (false == Setup.ballast.empty()) + m_lay.style.ballast = Setup.ballast; + m_route.design.norms.gauge = project_setup::rail_axes(Setup); + if (m_route.chain.tracks.empty()) + m_route.design.speed = Setup.speed; + m_norms.defaults(Setup.profile, project_setup::line_class(Setup)); + // the switches: of the rail, the usual one for the speed of their diverging track + auto &tool{m_switch}; + tool.rail_pick = Setup.rail; + if (false == tool.collected) + { + tool.templates = editor_track::standard_switch_templates(); + tool.collected = true; + } + if (auto const usual{usual_switch(Setup.rail, project_setup::diverging_speed(Setup))}; usual >= 0) + { + tool.last = usual; + if (tool.armed >= 0) + tool.armed = usual; + } +} + +void editor_mode::setup_store() +{ + if (false == m_setup.has_value() || false == m_setup_changed || scene::Layers.empty()) + return; + m_setup_changed = false; + auto const line{project_setup::record(*m_setup)}; + auto const moved_out = [this]() { + // written into the project: the lines of the scenery go + if (m_setup_marked) + scene::Layers.mark(scene::layer_handle{1}, "//$u", {}); + m_setup_marked = false; + }; + if (model_setup()) + { + moved_out(); + return; + } + std::error_code error; + if (auto const file{project_file_of_map()}; std::filesystem::exists(file, error)) + { + auto loaded{project::load(file)}; + if (loaded.project.has_value()) + { + loaded.project->setup = line; + loaded.project->network.areas.scenery = m_setup->profile; + if (std::string why; project::save(*loaded.project, file, why)) + { + moved_out(); + return; + } + else + WriteLog("Editor: the assumptions of the project not written into its file: " + why, logtype::generic); + } + } + scene::Layers.mark(scene::layer_handle{1}, "//$u", {"# the assumptions of the project: what the editor lays the tracks to", line}); + m_setup_marked = true; +} + +bool editor_mode::render_setup_card() +{ + if (m_setup.has_value() || false == m_setup_read) + return false; + auto const font{ImGui::GetFontSize()}; + ImGui::Spacing(); + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(std::round(font * 0.6f), std::round(font * 0.5f))); + if (ImGui::BeginChild("##setupcard", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysUseWindowPadding)) + { + ImGui::PushTextWrapPos(0.0f); + ImGui::SetWindowFontScale(1.12f); + ImGui::TextUnformatted(STR_C("What do you draw?")); + ImGui::SetWindowFontScale(1.0f); + ImGui::TextDisabled("%s", STR_C("The tracks are laid to these norms and this speed: a curve they don't allow can't be laid")); + ImGui::PopTextWrapPos(); + ImGui::Spacing(); + setup_ui::fields("card", m_setup_edit); + ImGui::Spacing(); + if (main_button(STR_C("Start drawing"), ImVec2(-FLT_MIN, tall_button_height()))) + setup_apply(m_setup_edit, true); + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", STR_C("Changed later in Assumptions of the project: Editor settings, or the line of them in Tracks")); + ImGui::PopTextWrapPos(); + } + ImGui::EndChild(); + ImGui::PopStyleVar(); + ImGui::PopStyleColor(); + return true; +} + +void editor_mode::render_setup_line() +{ + if (false == m_setup.has_value()) + return; + ImGui::Spacing(); + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", (STR("Assumptions: ") + setup_ui::summary(*m_setup)).c_str()); + ImGui::PopTextWrapPos(); + same_line_if_fits(STR_C("change")); + ImGui::PushID("setupline"); + if (ImGui::TextLink(STR_C("change"))) + { + m_setup_edit = *m_setup; + m_setup_window = true; + } + ImGui::PopID(); + item_tooltip("The assumptions of the project: the norms, the speed of the line, the traffic, the track, the gauge, the rail"); +} + +void editor_mode::render_setup_window() +{ + if (false == m_setup_window) + return; + // over the view, beside the window of the settings it's opened from, not over the panels + auto const pad{std::round(8.0f * Global.ui_scale)}; + ImGui::SetNextWindowPos(ImVec2(editor_ui::view_min().x + pad * 4.0f, ImGui::GetMainViewport()->WorkPos.y + pad * 4.0f), ImGuiCond_Appearing); + ImGui::SetNextWindowSize(ImVec2(std::round(ImGui::GetFontSize() * 30.0f), 0.0f), ImGuiCond_FirstUseEver); + if (ImGui::Begin((std::string{STR_C("Assumptions of the project")} + "###setupwindow").c_str(), &m_setup_window, ImGuiWindowFlags_NoCollapse)) + { + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", STR_C("The defaults of the laying, the plan, the switches and the profile; a field of a tool changes them for one line")); + ImGui::PopTextWrapPos(); + ImGui::Spacing(); + setup_ui::fields("window", m_setup_edit); + ImGui::Spacing(); + auto const same{m_setup.has_value() && *m_setup == m_setup_edit}; + ImGui::BeginDisabled(same); + if (main_button(STR_C("Apply"), ImVec2(0.0f, 0.0f))) + setup_apply(m_setup_edit, true); + ImGui::EndDisabled(); + ImGui::SameLine(); + if (ImGui::Button(STR_C("Close"))) + m_setup_window = false; + ImGui::PushTextWrapPos(0.0f); + if (m_setup_checking) + ImGui::TextDisabled("%s", STR_C("Checking the lines laid before against them...")); + else if (false == m_setup_told.empty()) + { + editor_flow::message_line(m_setup_told, m_norms.lines_beyond(norms::tier::warn) > 0 ? editor_flow::kBelow : editor_flow::kGood); + if (m_norms.lines_beyond(norms::tier::warn) > 0 && ImGui::Button(STR_C("Show the report"))) + m_norms.set_open(true); + } + ImGui::TextDisabled("%s", STR_C("A change doesn't alter what's laid: the tracks laid before stay as they are")); + ImGui::PopTextWrapPos(); + } + ImGui::End(); +} + +namespace +{ + +void expectation(bool const Passed, std::string const &What, std::string const &Got, std::string const &Wanted) +{ + ++editor_selftest::expectations(); + if (Passed) + { + WriteLog("SELFTEST PASS " + What); + return; + } + ++editor_selftest::failures(); + WriteLog("SELFTEST FAIL " + What + ": got " + Got + ", expected " + Wanted); +} + +std::string rest_of(std::istringstream &Words) +{ + std::string result; + for (std::string word; Words >> word;) + result += (result.empty() ? "" : " ") + word; + return result; +} + +} // namespace + +// setup [ ]... the assumptions of the project set as the card's Start sets them, the keys of their record over the +// ones there are (norms, speed, traffic, track, gauge, rail) +// setupwindow [close] the window of the assumptions of the project opened, as from the settings, or closed +// expect setup none | expect setup the assumptions of the scenery, none said yet, or the value of a key +// expect setupmark the scenery holds the line of the assumptions (//$u), or not +// expect layspeed the speed the lay tool lays at +editor_mode::selftest_result editor_mode::selftest_setup(std::string const &command, std::istringstream &words) +{ + if (command == "setup") + { + auto setup{setup_now()}; + auto const line{project_setup::read(std::string_view{project_setup::record(setup) + " " + rest_of(words)})}; + if (line.has_value()) + setup = *line; + setup_apply(setup, true); + WriteLog("SELFTEST setup: " + project_setup::record(setup)); + return selftest_result::handled; + } + if (command == "setupwindow") + { + std::string state; + words >> state; + m_setup_edit = setup_now(); + m_setup_window = state != "close"; + WriteLog(std::string{"SELFTEST setupwindow: "} + (m_setup_window ? "open" : "closed")); + return selftest_result::handled; + } + if (command != "expect") + return selftest_result::unhandled; + auto const start{words.tellg()}; + std::string kind; + words >> kind; + auto what{words.str()}; + what.erase(0, std::min(what.size(), what.find("expect") + 6)); + what.erase(0, std::min(what.size(), what.find_first_not_of(' '))); + auto const &lay{m_lay}; + if (kind == "setup") + { + std::string key; + std::string wanted; + words >> key >> wanted; + if (key == "none") + { + expectation(false == m_setup.has_value(), what, m_setup.has_value() ? project_setup::record(*m_setup) : "none", "none"); + return selftest_result::handled; + } + if (false == m_setup.has_value()) + { + expectation(false, what, "none", key + " " + wanted); + return selftest_result::handled; + } + // the value of the key as the record writes it + std::istringstream record{project_setup::record(*m_setup)}; + std::string got{"-"}; + for (std::string word; record >> word;) + if (word == key) + record >> got; + expectation(got == wanted, what, got, wanted); + return selftest_result::handled; + } + if (kind == "setupmark") + { + std::string wanted; + words >> wanted; + auto const marked{scene::Layers.empty() ? std::vector{} : scene::Layers.marked(scene::layer_handle{1}, "//$u")}; + auto const present{std::ranges::any_of(marked, [](std::string const &Line) { return Line.starts_with("setup"); })}; + expectation(present == (wanted == "present"), what, present ? "present" : "absent", wanted); + return selftest_result::handled; + } + if (kind == "layspeed") + { + double wanted{0.0}; + words >> wanted; + expectation(std::abs(lay.style.velocity - wanted) < 0.5, what, format("%.0f", lay.style.velocity), format("%.0f", wanted)); + return selftest_result::handled; + } + // another kind: the expectations of the other groups + words.clear(); + words.seekg(start); + return selftest_result::unhandled; +} diff --git a/application/editormodeswitchtemplate.cpp b/application/editormodeswitchtemplate.cpp index 12ce0f64e..dc3559ae6 100644 --- a/application/editormodeswitchtemplate.cpp +++ b/application/editormodeswitchtemplate.cpp @@ -649,6 +649,22 @@ int editor_mode::render_template_picker(char const *Id, std::function(templates.size()); ++i) + { + auto const &entry{templates[i]}; + if (entry.source != nullptr || entry.own || entry.double_slip || entry.twofold() || entry.type != "Rz" || entry.rail != Rail || speed_of(entry) <= 0.0) + continue; + if (best < 0 || score(entry) < score(templates[best])) + best = i; + } + return best; +} + void editor_mode::render_switch_template(TTrack &Track, bool const Combo) { // a turnout of the model: its type, its side, moved or taken out by its commands diff --git a/application/editormodetrack.cpp b/application/editormodetrack.cpp index 38c3a5816..0ebe39faa 100644 --- a/application/editormodetrack.cpp +++ b/application/editormodetrack.cpp @@ -830,12 +830,16 @@ void editor_mode::render_track_nothing_selected() show_track_tab(track_tab::lay); } } + // a scenery with no assumptions of the project: what's drawn asked for first, in place of the first steps + if (render_setup_card()) + return; if (false == render_first_steps_card() && false == m_first_steps_open && m_track_tab == track_tab::path) { ImGui::Spacing(); if (ImGui::TextLink(STR_C("Tutorial"))) m_first_steps_open = true; } + render_setup_line(); } void editor_mode::render_track_selection() diff --git a/application/editorsetupui.cpp b/application/editorsetupui.cpp new file mode 100644 index 000000000..3bdd70d3d --- /dev/null +++ b/application/editorsetupui.cpp @@ -0,0 +1,168 @@ +/* +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 "application/editorsetupui.h" +#include "application/editormodeshared.h" +#include "editor/editorFormat.hpp" +#include "editor/editorNormsReport.hpp" + +#include "imgui/imgui.h" +#include "utilities/translation.h" + +#include +#include +#include +#include +#include +#include + +namespace setup_ui +{ + +namespace +{ + +// a translated name, what follows ## in it left out: the short names of the choices, told apart from the same words elsewhere +std::string shown(char const *Translated) +{ + std::string text{Translated}; + return text.substr(0, text.find("##")); +} + +// a texture of the look, typed: empty for the one the lay tool has +bool texture_field(char const *Label, char const *Id, std::string &Value, char const *Hint) +{ + field_row(Label); + std::array text{}; + std::strncpy(text.data(), Value.c_str(), text.size() - 1); + if (false == ImGui::InputTextWithHint(Id, Hint, text.data(), text.size())) + return false; + Value = text.data(); + return true; +} + +} // namespace + +char const *profile_label(norms::profile_id const Profile) +{ + switch (Profile) + { + case norms::profile_id::tsi: return "TSI"; + case norms::profile_id::en13803: return "EN 13803"; + case norms::profile_id::ch: return "CH"; + default: return "PLK"; + } +} + +std::string summary(project_setup::setup const &Setup) +{ + return format("%s · %.0f km/h · %s · %s · %d mm · %s", profile_label(Setup.profile), Setup.speed, norms_report::traffic_name(Setup.traffic), + Setup.track == norms::track_kind::side ? STR_C("siding") : STR_C("main track"), Setup.gauge, Setup.rail.c_str()); +} + +bool fields(char const *Id, project_setup::setup &Setup) +{ + bool changed{false}; + ImGui::PushID(Id); + if (begin_fields("##setupfields", 0.3f)) + { + field_row("Norms"); + if (int profile{static_cast(Setup.profile)}; + segmented("setupnorms", profile, + {{static_cast(norms::profile_id::plk), "PLK", "PKP PLK Id-1 (ST-T1-A6): the values preferred, the normal and the extended limits"}, + {static_cast(norms::profile_id::tsi), "TSI", "TSI INF alone: the least the law of the EU allows, the loosest; its limits can't be passed either"}, + {static_cast(norms::profile_id::en13803), "EN 13803", "EN 13803: the normal limits warn, the exceptional ones can't be passed"}, + {static_cast(norms::profile_id::ch), "CH", "Swiss AB-EBV: the normal limits warn, the greatest ones can't be passed"}})) + { + Setup.profile = static_cast(profile); + changed = true; + } + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", STR_C("A map across two countries: the areas of the norms later, in the settings")); + ImGui::PopTextWrapPos(); + + field_row("Line speed"); + changed |= value_chips("setupspeeds", Setup.speed, {40.0, 60.0, 80.0, 100.0, 120.0, 140.0, 160.0, 200.0}, "%.0f"); + ImGui::SetNextItemWidth(std::round(ImGui::GetFontSize() * 5.0f)); + changed |= ImGui::DragScalar("##setupspeed", ImGuiDataType_Double, &Setup.speed, 1.0f, nullptr, nullptr, "%.0f km/h"); + Setup.speed = std::clamp(Setup.speed, 10.0, 350.0); + + field_row("Traffic"); + auto const passenger{shown(STR_C("passenger##traffic"))}; + auto const mixed{shown(STR_C("mixed##traffic"))}; + auto const freight{shown(STR_C("freight##traffic"))}; + if (int traffic{static_cast(Setup.traffic)}; + segmented("setuptraffic", traffic, + {{static_cast(norms::traffic::passenger), passenger.c_str()}, {static_cast(norms::traffic::mixed), mixed.c_str()}, {static_cast(norms::traffic::freight), freight.c_str()}})) + { + Setup.traffic = static_cast(traffic); + changed = true; + } + + field_row("Track"); + auto const main{shown(STR_C("main##track"))}; + auto const side{shown(STR_C("side (station)##track"))}; + if (int track{static_cast(Setup.track)}; + segmented("setuptrack", track, + {{static_cast(norms::track_kind::main), main.c_str(), "A running line or a main track of a station"}, + {static_cast(norms::track_kind::side), side.c_str(), "A siding of a station: the norms of the side tracks"}})) + { + Setup.track = static_cast(track); + changed = true; + } + + field_row("Gauge"); + { + std::vector options; + std::vector names; + for (auto const gauge : project_setup::gauges) + names.push_back(format("%d", gauge)); + for (std::size_t i = 0; i < names.size(); ++i) + options.push_back({project_setup::gauges[i], names[i].c_str()}); + if (int gauge{Setup.gauge}; choice_chips("setupgauge", gauge, options)) + { + Setup.gauge = gauge; + changed = true; + } + } + + field_row("Rail"); + { + int rail{0}; + for (std::size_t i = 0; i < project_setup::rail_kinds.size(); ++i) + if (project_setup::rail_kinds[i] == Setup.rail) + rail = static_cast(i); + if (segmented("setuprail", rail, {{0, "60E1"}, {1, "49E1"}, {2, "S42"}})) + { + Setup.rail = std::string{project_setup::rail_kinds[static_cast(rail)]}; + changed = true; + } + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", STR_C("the switches of this rail first")); + ImGui::PopTextWrapPos(); + } + ImGui::EndTable(); + } + if (ImGui::TreeNode(STR_C("More"))) + { + ImGui::TextDisabled("%s", STR_C("Look of the tracks laid: empty, rails and trackbed as now")); + if (begin_fields("##setuplook", 0.3f)) + { + changed |= texture_field("Rails##setuplook", "##setuprails", Setup.rails, "rail_screw_used1"); + changed |= texture_field("Trackbed##setuplook", "##setupballast", Setup.ballast, "1435mm/tpbps-new2"); + ImGui::EndTable(); + } + ImGui::TreePop(); + } + ImGui::PopID(); + return changed; +} + +} // namespace setup_ui diff --git a/application/editorsetupui.h b/application/editorsetupui.h new file mode 100644 index 000000000..e5a737862 --- /dev/null +++ b/application/editorsetupui.h @@ -0,0 +1,28 @@ +/* +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 "editor/editorProjectSetup.hpp" + +#include + +// the fields of the assumptions of the project ("Co rysujesz?"): the step of the wizard of a new scenery, the card of the inspector of the +// tracks of a scenery with none, the window they're changed in later. One screen of them, the defaults chosen, the look under More +namespace setup_ui +{ + +// the fields; true when one was changed +bool fields(char const *Id, project_setup::setup &Setup); +// "PLK · 100 km/h · mixed traffic · main track · 1435 mm · 60E1", as a line of the summary of them +std::string summary(project_setup::setup const &Setup); +// the name of the profile as the fields show it: PLK, TSI, EN 13803, CH +char const *profile_label(norms::profile_id const Profile); + +} // namespace setup_ui diff --git a/editor/editorProjectSetup.cpp b/editor/editorProjectSetup.cpp new file mode 100644 index 000000000..b3cab1399 --- /dev/null +++ b/editor/editorProjectSetup.cpp @@ -0,0 +1,201 @@ +/* +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/editorProjectSetup.hpp" + +#include +#include +#include +#include +#include + +namespace project_setup +{ + +namespace +{ + +std::array, 4> constexpr profile_keys{{ + {norms::profile_id::plk, "plk"}, + {norms::profile_id::tsi, "tsi"}, + {norms::profile_id::en13803, "en13803"}, + {norms::profile_id::ch, "ch"}, +}}; + +std::array, 3> constexpr traffic_keys{{ + {norms::traffic::passenger, "passenger"}, + {norms::traffic::mixed, "mixed"}, + {norms::traffic::freight, "freight"}, +}}; + +// a name with no blanks: %20 for a blank, %25 for a percent sign, - for none +std::string word(std::string_view const Text) +{ + if (Text.empty()) + return "-"; + std::string result; + for (auto const character : Text) + { + if (character == ' ') + result += "%20"; + else if (character == '%') + result += "%25"; + else + result += character; + } + return result; +} + +std::string unword(std::string_view const Word) +{ + if (Word == "-") + return {}; + std::string result; + for (std::size_t i = 0; i < Word.size(); ++i) + { + if (Word.substr(i, 3) == "%20") + { + result += ' '; + i += 2; + } + else if (Word.substr(i, 3) == "%25") + { + result += '%'; + i += 2; + } + else + result += Word[i]; + } + return result; +} + +std::optional number_of(std::string_view const Text) +{ + double value{0.0}; + auto const [end, error]{std::from_chars(Text.data(), Text.data() + Text.size(), value)}; + if (error != std::errc{} || end != Text.data() + Text.size() || false == std::isfinite(value)) + return std::nullopt; + return value; +} + +// the value of a key of the record into the setup; a value it can't read leaves the default +void take(setup &Setup, std::string_view const Key, std::string_view const Value) +{ + if (Key == "norms") + { + if (auto const profile{profile_of_key(Value)}) + Setup.profile = *profile; + } + else if (Key == "speed") + { + if (auto const speed{number_of(Value)}; speed.has_value() && *speed > 0.0) + Setup.speed = std::clamp(*speed, 5.0, 400.0); + } + else if (Key == "traffic") + { + for (auto const &[kind, name] : traffic_keys) + if (name == Value) + Setup.traffic = kind; + } + else if (Key == "track") + { + if (Value == "main" || Value == "side") + Setup.track = Value == "side" ? norms::track_kind::side : norms::track_kind::main; + } + else if (Key == "gauge") + { + if (auto const gauge{number_of(Value)}; gauge.has_value() && *gauge >= 300.0 && *gauge <= 2000.0) + Setup.gauge = static_cast(std::lround(*gauge)); + } + else if (Key == "rail") + Setup.rail = unword(Value); + else if (Key == "rails") + Setup.rails = unword(Value); + else if (Key == "ballast") + Setup.ballast = unword(Value); +} + +} // namespace + +std::string_view profile_key(norms::profile_id const Profile) +{ + for (auto const &[id, key] : profile_keys) + if (id == Profile) + return key; + return "plk"; +} + +std::optional profile_of_key(std::string_view const Key) +{ + for (auto const &[id, key] : profile_keys) + if (key == Key) + return id; + return std::nullopt; +} + +norms::line_class line_class(setup const &Setup, double const Speed) +{ + norms::line_class result; + result.vmax = Speed > 0.0 ? Speed : Setup.speed; + result.kind = Setup.traffic; + result.track = Setup.track; + return result; +} + +double rail_axes(setup const &Setup) +{ + return static_cast(Setup.gauge) + 65.0; +} + +double diverging_speed(setup const &Setup) +{ + if (Setup.track == norms::track_kind::side || Setup.speed < 100.0) + return 40.0; + if (Setup.speed < 160.0) + return 60.0; + return Setup.speed < 200.0 ? 80.0 : 100.0; +} + +std::string record(setup const &Setup) +{ + std::string_view traffic{"mixed"}; + for (auto const &[kind, name] : traffic_keys) + if (kind == Setup.traffic) + traffic = name; + auto text{std::format("setup norms {} speed {} traffic {} track {} gauge {} rail {}", profile_key(Setup.profile), Setup.speed, traffic, Setup.track == norms::track_kind::side ? "side" : "main", + Setup.gauge, word(Setup.rail))}; + if (false == Setup.rails.empty()) + text += " rails " + word(Setup.rails); + if (false == Setup.ballast.empty()) + text += " ballast " + word(Setup.ballast); + return text; +} + +std::optional read(std::string_view const Line) +{ + std::istringstream words{std::string{Line}}; + std::string keyword; + if (false == static_cast(words >> keyword) || keyword != "setup") + return std::nullopt; + setup result; + for (std::string key, value; words >> key >> value;) + take(result, key, value); + return result; +} + +std::optional read(std::vector const &Lines) +{ + for (auto const &line : Lines) + if (auto const found{read(std::string_view{line})}) + return found; + return std::nullopt; +} + +} // namespace project_setup diff --git a/editor/editorProjectSetup.hpp b/editor/editorProjectSetup.hpp new file mode 100644 index 000000000..a0332b158 --- /dev/null +++ b/editor/editorProjectSetup.hpp @@ -0,0 +1,65 @@ +/* +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 "editor/norms/norms.hpp" + +#include +#include +#include +#include +#include + +// The assumptions of the project ("Co rysujesz?"): what the user draws, said once at the start of a scenery and changed later in a +// window of their own: the norms, the speed of the line, the traffic, a main or a side track, the gauge, the rail and the look of the +// tracks. They're kept with the map, in its project file where it has one, else in the scenery as the line //$u setup ..., and they're +// the defaults of the laying, the plan, the switches, the parallel tool and the profile. There's no "no norms": every scenery has a +// profile, a fictional one too, PLK where nothing was said; the TSI alone are the loosest +namespace project_setup +{ + +struct setup +{ + norms::profile_id profile{norms::profile_id::plk}; + double speed{100.0}; // km/h of the line + norms::traffic traffic{norms::traffic::mixed}; + norms::track_kind track{norms::track_kind::main}; + int gauge{1435}; // mm + std::string rail{"60E1"}; // the switches of this rail first + std::string rails; // the texture of the rails of the tracks laid; empty: as the lay tool has it + std::string ballast; // the texture of the trackbed; empty: as the lay tool has it + + bool operator==(setup const &) const = default; +}; + +inline constexpr std::array gauges{1435, 1520, 1000, 750, 600}; +inline constexpr std::array rail_kinds{"60E1", "49E1", "S42"}; +inline constexpr std::array speeds{40.0, 60.0, 80.0, 100.0, 120.0, 140.0, 160.0, 200.0}; + +// the words the profiles are written with, as in the project file: plk, tsi, en13803, ch +std::string_view profile_key(norms::profile_id const Profile); +std::optional profile_of_key(std::string_view const Key); + +// the class of the line the norms judge the tracks laid by: the traffic and the track of the setup, the speed given (the one of the +// line laid), or the setup's own +norms::line_class line_class(setup const &Setup, double const Speed = 0.0); +// mm between the axes of the rails the cant is measured on: the gauge and the head of a rail, 1500 for 1435 +double rail_axes(setup const &Setup); +// km/h through the diverging track of the switches laid by default: as the line and the track have it +double diverging_speed(setup const &Setup); + +// one line: "setup norms plk speed 100 traffic mixed track main gauge 1435 rail 60E1", the textures after it where given +std::string record(setup const &Setup); +// the line of record(); none where it isn't one. The words it doesn't know are passed over, a value it can't read keeps the default +std::optional read(std::string_view const Line); +// the first line of the //$u lines (the mark taken off) which reads +std::optional read(std::vector const &Lines); + +} // namespace project_setup diff --git a/editor/editorTrack.cpp b/editor/editorTrack.cpp index e280d2699..70019f5df 100644 --- a/editor/editorTrack.cpp +++ b/editor/editorTrack.cpp @@ -2568,7 +2568,7 @@ editor_track::route editor_track::route_of(chain const &Chain) return result; } -double editor_track::route_speed(route const &Route) +double editor_track::route_speed(route const &Route, double const Default) { std::vector> speeds; auto total{0.0}; @@ -2583,7 +2583,7 @@ double editor_track::route_speed(route const &Route) for (auto const &[speed, length] : speeds) if ((passed += length) >= total * 0.5) return speed; - return 100.0; + return Default; } std::vector editor_track::sample_route(route &Route, double const Step) @@ -3339,7 +3339,7 @@ TTrack *editor_track::create_switch(switch_template const &Template, std::vector std::ostringstream text; text.precision(std::numeric_limits::digits10); // the rails of the style on both paths, its ballast under the switch - text << "switch " << Template.length << " 1.435 0.15 10 20 0 flat vis " << Style.rail << " 6 " << Style.rail << " 0.2 0.5 1.1 "; + text << "switch " << Template.length << ' ' << Style.gauge << " 0.15 10 20 0 flat vis " << Style.rail << " 6 " << Style.rail << " 0.2 0.5 1.1 "; write_points(text, Paths[0]); write_points(text, Paths[1]); text << "trackbed " << Style.ballast << ' '; @@ -3779,7 +3779,7 @@ TTrack *editor_track::create_path(path_style const &Style, segment_data const &P std::ostringstream text; text.precision(std::numeric_limits::digits10); auto const length{glm::distance(Path.points[segment_data::point::start], Path.points[segment_data::point::end])}; - text << "normal " << length << " 1.435 0.15 25 20 0 flat vis " << Style.rail << " 6 " << Style.ballast << " 0.2 0.5 1.1 "; + text << "normal " << length << ' ' << Style.gauge << " 0.15 25 20 0 flat vis " << Style.rail << " 6 " << Style.ballast << " 0.2 0.5 1.1 "; write_points(text, Path); if (Style.velocity > 0.0) text << "velocity " << Style.velocity << ' '; diff --git a/editor/editorTrack.hpp b/editor/editorTrack.hpp index ad415b0a3..2e7428adc 100644 --- a/editor/editorTrack.hpp +++ b/editor/editorTrack.hpp @@ -399,7 +399,8 @@ class editor_track static route route_of(chain const &Chain); // the speed most of the sampled route is laid for, by the length: a few tracks set faster or slower don't set the curves of // all of it. 100 where none is set - static double route_speed(route const &Route); + // the speed most of the route has, by its length; Default where none of its tracks has one + static double route_speed(route const &Route, double const Default = 100.0); struct route_sample { double chainage{0.0}; @@ -495,6 +496,7 @@ class editor_track std::string rail{"rail_screw_used1"}; std::string ballast{"1435mm/tpbps-new2"}; std::string rail_profile; + double gauge{1.435}; // m }; // the path goes to the layer receiving the items created in the editor static TTrack *create_path(path_style const &Style, segment_data const &Path); diff --git a/editor/layout/layoutCommands.hpp b/editor/layout/layoutCommands.hpp index 0355d84dc..db5c77b7c 100644 --- a/editor/layout/layoutCommands.hpp +++ b/editor/layout/layoutCommands.hpp @@ -215,6 +215,8 @@ class model explicit model(network Network) : m_network{std::move(Network)} {} network const &state() const { return m_network; } + // the profile of the norms of the scenery, where no area nor a line has its own: the assumptions of the project changed, not an edit + void scenery_profile(norms::profile_id const Profile) { m_network.areas.scenery = Profile; } outcome execute(command const &Command, edit_policy const Policy = edit_policy::refuse_block); bool undo(); bool redo(); diff --git a/editor/project/projectFile.cpp b/editor/project/projectFile.cpp index 8f16e995a..de1ee7fd0 100644 --- a/editor/project/projectFile.cpp +++ b/editor/project/projectFile.cpp @@ -373,7 +373,7 @@ std::string unword(std::string_view const Word) bool same(document const &A, document const &B) { return A.network.same(B.network) && A.network.handed() == B.network.handed() && same_areas(A.network.areas, B.network.areas) && A.generated == B.generated && A.hand_changed == B.hand_changed && A.anchors == B.anchors && - A.legacy == B.legacy; + A.legacy == B.legacy && A.setup == B.setup; } std::string write(document const &Project) @@ -390,6 +390,8 @@ std::string write(document const &Project) auto const handed{network.handed()}; put(std::format("next node {} line {} vertex {} turnout {}", handed.node + 1ull, handed.line + 1ull, handed.vertex + 1ull, handed.turnout + 1ull)); put(std::format("norms {}", profile_name(network.areas.scenery))); + if (false == Project.setup.empty()) + put(Project.setup); for (auto const &area : network.areas.areas) { auto line{std::format("area {} {} {}", word(area.name), profile_name(area.profile), area.outline.size())}; @@ -769,6 +771,11 @@ read_result read(std::string_view const Text) project.network.areas.scenery = words.profile("the profile of the scenery"); words.finish(); } + else if (keyword == "setup") + { + // the words are the assumptions' own, read by them: kept as they are written + project.setup = "setup " + words.rest(); + } else if (keyword == "area") { norms::area area; diff --git a/editor/project/projectFile.hpp b/editor/project/projectFile.hpp index a64fe0f8a..f37239c04 100644 --- a/editor/project/projectFile.hpp +++ b/editor/project/projectFile.hpp @@ -28,6 +28,7 @@ http://mozilla.org/MPL/2.0/. // eu07proj 1 // next node line vertex turnout the identifiers handed out next; none is handed out again // norms the profile of the scenery: plk, tsi, en13803, ch +// setup ... the assumptions of the project (editorProjectSetup.hpp), as written there // area ... a profile for an area outlined in the plan // lineprofile a profile for a line by name // class vmax vmin traffic track load features @@ -103,6 +104,7 @@ struct document std::set hand_changed; std::vector anchors; // of the lineside objects std::vector legacy; + std::string setup; // the line of the assumptions of the project (editorProjectSetup.hpp), the keyword with it; empty: none }; // the same contents: the lines and the nodes, the norms, the identifiers handed out and the rest diff --git a/editor/project/projectSession.cpp b/editor/project/projectSession.cpp index 37e5348a6..450ca432e 100644 --- a/editor/project/projectSession.cpp +++ b/editor/project/projectSession.cpp @@ -214,4 +214,10 @@ bool session::save(std::string &Error) return project::save(m_project, file(), Error); } +void session::setup(std::string Line, norms::profile_id const Profile) +{ + m_project.setup = std::move(Line); + m_model.scenery_profile(Profile); +} + } // namespace project diff --git a/editor/project/projectSession.hpp b/editor/project/projectSession.hpp index 3cde4c8f4..a4b9a8624 100644 --- a/editor/project/projectSession.hpp +++ b/editor/project/projectSession.hpp @@ -111,6 +111,9 @@ class session void mark_hand_changed(layout::line_id const Line); // the project written next to the map, with the network as it is now and the records of the generated tracks of the lines gone taken out bool save(std::string &Error); + // the assumptions of the project (editorProjectSetup.hpp), the line of their record, kept in the project from now on, written with it; + // the profile of the scenery the model judges the lines by with them, a change of it not a step of the history + void setup(std::string Line, norms::profile_id const Profile); private: struct step diff --git a/lang/editor_pl.po b/lang/editor_pl.po index 916d2b9d7..6815cdc6b 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -15259,3 +15259,117 @@ msgstr "łuk na granicy profili" msgid "element between turnouts" msgstr "wstawka między rozjazdami" + +# scenery editor: the assumptions of the project ("Co rysujesz?") + +msgid "What do you draw?" +msgstr "Co rysujesz?" + +msgid "%d. What do you draw?" +msgstr "%d. Co rysujesz?" + +msgid "The tracks are laid to these norms and this speed: a curve they don't allow can't be laid" +msgstr "Tory układają się według tych norm i tej prędkości: łuku, na który normy nie pozwalają, nie da się ułożyć" + +msgid "The tracks are laid to these norms and this speed, a fictional scenery's too: a curve they don't allow can't be laid. Changed later in the editor: Assumptions of the project" +msgstr "Tory układają się według tych norm i tej prędkości, także w scenerii fikcyjnej: łuku, na który normy nie pozwalają, nie da się ułożyć. Zmienisz to później w edytorze: Założenia projektu" + +msgid "Start drawing" +msgstr "Zaczynam" + +msgid "Start: create and open in the editor" +msgstr "Zaczynam: utwórz i otwórz w edytorze" + +msgid "Changed later in Assumptions of the project: Editor settings, or the line of them in Tracks" +msgstr "Zmienisz to później w Założeniach projektu: Ustawienia edytora albo linijka założeń w dziale Tory" + +msgid "Assumptions: " +msgstr "Założenia: " + +msgid "change" +msgstr "zmień" + +msgid "The assumptions of the project: the norms, the speed of the line, the traffic, the track, the gauge, the rail" +msgstr "Założenia projektu: normy, prędkość linii, rodzaj ruchu, tor, rozstaw, szyna" + +msgid "Assumptions of the project" +msgstr "Założenia projektu" + +msgid "Assumptions of the project..." +msgstr "Założenia projektu..." + +msgid "What the scenery is drawn to: the norms, the speed of the line, the traffic, the track, the gauge and the rail; the defaults of the tools" +msgstr "Według czego rysujesz scenerię: normy, prędkość linii, rodzaj ruchu, tor, rozstaw i szyna; wartości domyślne narzędzi" + +msgid "The defaults of the laying, the plan, the switches and the profile; a field of a tool changes them for one line" +msgstr "Wartości domyślne układania, Planu, rozjazdów i profilu; pole w panelu narzędzia zmienia je dla jednej linii" + +msgid "Checking the lines laid before against them..." +msgstr "Sprawdzam z nimi wcześniej ułożone linie..." + +msgid "The lines laid before meet the norms of these assumptions" +msgstr "Wcześniej ułożone linie spełniają normy tych założeń" + +msgid "%zu of the lines laid before don't meet these norms (%zu beyond the hard limits): the report is in the window of the norms. Nothing was changed" +msgstr "Wcześniej ułożone linie niespełniające tych norm: %zu (ponad twarde granice: %zu); raport jest w oknie norm. Niczego nie zmieniono" + +msgid "Show the report" +msgstr "Pokaż raport" + +msgid "A change doesn't alter what's laid: the tracks laid before stay as they are" +msgstr "Zmiana niczego nie przerabia: wcześniej ułożone tory zostają, jakie są" + +msgid "PKP PLK Id-1 (ST-T1-A6): the values preferred, the normal and the extended limits" +msgstr "PKP PLK Id-1 (ST-T1-A6): wartości zalecane, graniczne normalne i rozszerzone" + +msgid "TSI INF alone: the least the law of the EU allows, the loosest; its limits can't be passed either" +msgstr "Tylko TSI INF: minimum, na które pozwala prawo UE, najluźniejsze; jego granic też nie da się przekroczyć" + +msgid "EN 13803: the normal limits warn, the exceptional ones can't be passed" +msgstr "EN 13803: granice normalne ostrzegają, wyjątkowych nie da się przekroczyć" + +msgid "Swiss AB-EBV: the normal limits warn, the greatest ones can't be passed" +msgstr "Szwajcarskie AB-EBV: granice normalne ostrzegają, maksymalnych nie da się przekroczyć" + +msgid "A map across two countries: the areas of the norms later, in the settings" +msgstr "Dla mapy na dwa kraje obszary norm później w ustawieniach" + +msgid "passenger##traffic" +msgstr "pasażerski##traffic" + +msgid "mixed##traffic" +msgstr "mieszany##traffic" + +msgid "freight##traffic" +msgstr "towarowy##traffic" + +msgid "main##track" +msgstr "główny##track" + +msgid "side (station)##track" +msgstr "boczny (stacyjny)##track" + +msgid "A running line or a main track of a station" +msgstr "Tor szlakowy albo główny tor stacji" + +msgid "A siding of a station: the norms of the side tracks" +msgstr "Tor boczny stacji: normy torów bocznych" + +msgid "Gauge" +msgstr "Rozstaw" + +msgid "Rail" +msgstr "Szyna" + +msgid "the switches of this rail first" +msgstr "z tego domyślne rozjazdy" + +msgid "Look of the tracks laid: empty, rails and trackbed as now" +msgstr "Wygląd układanych torów: puste pole to szyny i podsypka jak dziś" + +msgid "Rails##setuplook" +msgstr "Szyny##setuplook" + +msgid "Trackbed##setuplook" +msgstr "Podsypka##setuplook" + diff --git a/launcher/scenery_wizard.cpp b/launcher/scenery_wizard.cpp index 81c332d30..f982223ff 100644 --- a/launcher/scenery_wizard.cpp +++ b/launcher/scenery_wizard.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "launcher/scenery_wizard.h" #include "application/application.h" +#include "application/editorsetupui.h" #include "editor/editorFormat.hpp" #include "editor/editorGeoMap.hpp" #include "editor/editorGeometry.hpp" @@ -251,12 +252,13 @@ void ui::scenerywizard_panel::render_form() render_place(); render_range(); } + render_setup(m_realistic ? 4 : 2); ImGui::Spacing(); ImGui::Separator(); auto const reason{missing(named, exists)}; ImGui::BeginDisabled(false == reason.empty()); - if (ImGui::Button(STR_C("Create and open in the editor"), ImVec2(-1.f, ImGui::GetFrameHeight() * 1.4f))) + if (ImGui::Button(STR_C("Start: create and open in the editor"), ImVec2(-1.f, ImGui::GetFrameHeight() * 1.4f))) create_scenery(); ImGui::EndDisabled(); if (false == reason.empty()) @@ -389,6 +391,13 @@ void ui::scenerywizard_panel::render_range() wrapped_disabled(STR("It can be changed later in the editor: Map > Orthophoto settings... > Download area")); } +void ui::scenerywizard_panel::render_setup(int const Step) +{ + ImGui::SeparatorText(format(STR_C("%d. What do you draw?"), Step).c_str()); + wrapped_disabled(STR("The tracks are laid to these norms and this speed, a fictional scenery's too: a curve they don't allow can't be laid. Changed later in the editor: Assumptions of the project")); + setup_ui::fields("wizard", m_setup); +} + bool ui::scenerywizard_panel::create_scenery() { std::string name{m_filename}; @@ -417,6 +426,9 @@ bool ui::scenerywizard_panel::create_scenery() } else std::snprintf(reference, sizeof(reference), "fictional"); + // what's drawn, the norms the tracks are laid to: a fictional scenery has them as any other + output << "//$u # the assumptions of the project: what the editor lays the tracks to\r\n"; + output << "//$u " << project_setup::record(m_setup) << "\r\n"; if (m_title[0] != '\0') output << "// " << m_title << "\r\n"; output << "// created in the editor " << date << "\r\n\r\n"; @@ -428,6 +440,7 @@ bool ui::scenerywizard_panel::create_scenery() WriteLog(format("Scenery wizard: created %s at %s, the range of the photos %zu points, %.1f km2", file.c_str(), reference, area.size(), geometry::polygon_area(area) / 1.0e6)); else WriteLog("Scenery wizard: created " + file + ", fictional"); + WriteLog("Scenery wizard: the assumptions of the project: " + project_setup::record(m_setup)); Global.SceneryFile = file; Global.local_start_vehicle = "ghostview"; diff --git a/launcher/scenery_wizard.h b/launcher/scenery_wizard.h index 64ad6a360..3ea1eca13 100644 --- a/launcher/scenery_wizard.h +++ b/launcher/scenery_wizard.h @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "application/uilayer.h" #include "editor/editorGeoMap.hpp" +#include "editor/editorProjectSetup.hpp" #include #include @@ -39,6 +40,8 @@ class scenerywizard_panel : public ui_panel void render_form(); void render_place(); void render_range(); + // the last step: what's drawn, the assumptions of the project, a fictional scenery too (it has norms as any other) + void render_setup(int const Step); void pick(glm::dvec2 const &Point); // the range as it's written to the scenery, PUWG (east, north); empty while there's none std::vector range() const; @@ -62,6 +65,7 @@ class scenerywizard_panel : public ui_panel std::vector m_drawn; // the area of the user's bool m_drawing{false}; // its corners are being clicked std::vector m_before; // the area there was before the drawing began, back with Cancel + project_setup::setup m_setup; // written into the scenery, //$u }; } // namespace ui diff --git a/scene/scenelayers.cpp b/scene/scenelayers.cpp index 82d3aa80a..fbea18ed2 100644 --- a/scene/scenelayers.cpp +++ b/scene/scenelayers.cpp @@ -822,7 +822,7 @@ namespace { // marks of the lines the editor keeps track of. The letters the starters read in a scenery file are left to them: the title //$n, // the description //$d, the attachments //$f, the image //$i, the id //$l and the old scenery //$a, in the trainsets //$o and //$r -std::array const editormarks{"//$p", "//$h", "//$r", "//$b", "//$c", "//$o", "//$s", "//$k", "//$z", "//$w", "//$t"}; +std::array const editormarks{"//$p", "//$h", "//$r", "//$b", "//$c", "//$o", "//$s", "//$k", "//$z", "//$w", "//$t", "//$u"}; // the marks some of these had before, on the letters of the starters: the download area //$z was //$a, the shared grade lines //$w // were //$n, the fitted tracks //$t were //$f. Their lines are read only by the word the editor wrote in them, or by the start of @@ -858,7 +858,7 @@ bool editor_line(std::string_view const Mark, std::string const &Text) std::string on, state, next; return words >> on >> state && on == "on" && (state == "0" || state == "1") && (false == static_cast(words >> next) || next == "origin" || next == "year"); } - static std::array>, 7> const words{{ + static std::array>, 8> const words{{ {"//$h", {"#", "alignment", "source", "ends", "norms", "pieces", "kept", "vertex", "click", "element", "end"}}, {"//$r", {"#", "switch", "slip", "crossing", "double", "connection"}}, {"//$s", {"#", "station"}}, @@ -866,6 +866,7 @@ bool editor_line(std::string_view const Mark, std::string const &Text) {"//$z", {"#", "point"}}, {"//$w", {"#", "gradeline"}}, {"//$t", {"#", "fitted"}}, + {"//$u", {"#", "setup"}}, }}; auto const lookup{std::find_if(words.begin(), words.end(), [&](auto const &Entry) { return Entry.first == Mark; })}; if (lookup == words.end()) diff --git a/tests/editor/README.md b/tests/editor/README.md index 21e8ebb07..40f36195b 100644 --- a/tests/editor/README.md +++ b/tests/editor/README.md @@ -121,6 +121,9 @@ tool's `parallel ` lay turnouts, crossovers and lines of the mod | `expect modelvertexat ` | where the vertex of a line of the model is in the plan | | `expect modelpreview ` | a handle of the route design dragged on a line of the model: the line as the model would lay it is tried and told | | `expect modelspacing ` | how far the other line runs from the point of the line at the chainage | +| `expect setup none`, `expect setup ` | the assumptions of the project of the scenery: none said yet, or the value of a key as their record writes it (`norms`, `speed`, `traffic`, `track`, `gauge`, `rail`) | +| `expect setupmark ` | the scenery holds the line of the assumptions (`//$u setup ...`), or not | +| `expect layspeed ` | the speed the lay tool lays at | Files `scenery/autotest_editor..scn` are copied next to the copy of the fixture as `autotest_editor__.scn`, the name of the fixture in them made the name of its copy: scenarios of its map, for the scenario index (`autotest_editor.uses.scn`, a trainset on `s1`). @@ -199,3 +202,10 @@ and the rest of the editing on the model (E4b), with the setting on: | `m_12_lay_from_end` | a line laid on from the free end of a line of the model shares its node, the ends to the bit before and after a reopen | | `m_13_parallel` | the parallel tool beside a line of the model lays a line of the model, its arc concentric at the spacing, one step | | `m_14_layer_origin` | a line of the model saved into a file under an origin and opened again: nothing taken for changed by hand, also after a second save | + +and the assumptions of the project and the norms enforced when laying (`editor-project-setup.md`): + +| case | checks | +| --- | --- | +| `ps_1_setup_saved` | a scenery with none has no assumptions; set as the card's Start sets them they're the defaults of the laying and of the norms, written into the scenery (`//$u`) and read back the same after a reopen | +| `ps_2_setup_project` | with the editing on the model the assumptions go into the project file, none into the scenery, and are read from there after a reopen with the setting off | diff --git a/tests/editor/cases/ps_1_setup_saved.txt b/tests/editor/cases/ps_1_setup_saved.txt new file mode 100644 index 000000000..6370db964 --- /dev/null +++ b/tests/editor/cases/ps_1_setup_saved.txt @@ -0,0 +1,27 @@ +# The assumptions of the project (editor-project-setup.md): a scenery with none asks for them, the defaults meanwhile. Set as the +# Start of the card sets them, they're written into the scenery as //$u with it and read back the same when it's opened again +# They're the defaults of the tools: the speed of the laying, the gauge of the norms +wait 5 +expect setup none +expect setupmark absent +expect layspeed 100 +setup norms tsi speed 80 traffic freight track side gauge 1000 rail 49E1 +expect setup norms tsi +expect setup speed 80 +expect layspeed 80 +expect norms gauge 1065 +save +wait 5 +expect setupmark present +quit +#> reopen +wait 5 +expect setup norms tsi +expect setup speed 80 +expect setup traffic freight +expect setup track side +expect setup gauge 1000 +expect setup rail 49E1 +expect layspeed 80 +expect norms gauge 1065 +quit diff --git a/tests/editor/cases/ps_2_setup_project.txt b/tests/editor/cases/ps_2_setup_project.txt new file mode 100644 index 000000000..552d2d5ba --- /dev/null +++ b/tests/editor/cases/ps_2_setup_project.txt @@ -0,0 +1,17 @@ +# The assumptions of the project kept in its project file where the map has one (the editing on the model): written there with the +# scenery, none of them in the scenery, read back from there when the map is opened again, the editing on the model off then +wait 5 +modelediting 1 +expect projectfile present +setup speed 120 traffic passenger +save +wait 5 +expect setupmark absent +quit +#> reopen +wait 5 +expect setup speed 120 +expect setup traffic passenger +expect setupmark absent +expect layspeed 120 +quit diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index c1bb26983..e97797169 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -57,6 +57,8 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/norms/normsEngine.cpp" "${CMAKE_SOURCE_DIR}/editor/norms/normsLine.cpp" "${CMAKE_SOURCE_DIR}/editor/editorNormsReport.cpp" + # the assumptions of the project, what the tracks are laid to (test_project_setup.cpp) + "${CMAKE_SOURCE_DIR}/editor/editorProjectSetup.cpp" # the domain of the track geometry: the elements, the lines and the nodes, the commands editing them, the tracks made of them "${CMAKE_SOURCE_DIR}/editor/layout/layoutElements.cpp" "${CMAKE_SOURCE_DIR}/editor/layout/layoutLine.cpp" diff --git a/tests/unit/test_project_setup.cpp b/tests/unit/test_project_setup.cpp new file mode 100644 index 000000000..36eb9c934 --- /dev/null +++ b/tests/unit/test_project_setup.cpp @@ -0,0 +1,87 @@ +// the assumptions of the project (editor/editorProjectSetup): the line they're kept in, read back the same, the defaults where the line +// says nothing or what it can't read, the class of the line the norms judge by, the gauge of the norms; kept in the project file as written + +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorProjectSetup.hpp" +#include "editor/project/projectFile.hpp" + +#include +#include + +TEST_CASE("project setup: the defaults are PLK, 100 km/h, mixed traffic, a main track, 1435 mm, 60E1") { + project_setup::setup const setup; + CHECK(setup.profile == norms::profile_id::plk); + CHECK(setup.speed == doctest::Approx(100.0)); + CHECK(setup.traffic == norms::traffic::mixed); + CHECK(setup.track == norms::track_kind::main); + CHECK(setup.gauge == 1435); + CHECK(setup.rail == "60E1"); + CHECK(project_setup::rail_axes(setup) == doctest::Approx(1500.0)); + CHECK(project_setup::record(setup) == "setup norms plk speed 100 traffic mixed track main gauge 1435 rail 60E1"); +} + +TEST_CASE("project setup: the line is read back the same, the textures with blanks too") { + project_setup::setup setup; + setup.profile = norms::profile_id::tsi; + setup.speed = 80.0; + setup.traffic = norms::traffic::freight; + setup.track = norms::track_kind::side; + setup.gauge = 1000; + setup.rail = "49E1"; + setup.rails = "rail_screw new"; + setup.ballast = "1000mm/ballast 100%"; + auto const line{project_setup::record(setup)}; + CAPTURE(line); + auto const read{project_setup::read(std::string_view{line})}; + REQUIRE(read.has_value()); + CHECK(*read == setup); + // among the //$u lines, the first which is one + auto const found{project_setup::read(std::vector{"# the assumptions of the project", line})}; + REQUIRE(found.has_value()); + CHECK(*found == setup); +} + +TEST_CASE("project setup: what the line doesn't say or can't be read keeps the default, another line isn't one") { + auto const read{project_setup::read(std::string_view{"setup speed fast norms xyz gauge 1520 colour red"})}; + REQUIRE(read.has_value()); + CHECK(read->speed == doctest::Approx(100.0)); + CHECK(read->profile == norms::profile_id::plk); + CHECK(read->gauge == 1520); + CHECK(project_setup::rail_axes(*read) == doctest::Approx(1585.0)); + CHECK_FALSE(project_setup::read(std::string_view{"point 1 2"}).has_value()); + CHECK_FALSE(project_setup::read(std::vector{}).has_value()); +} + +TEST_CASE("project setup: the class of the line the norms judge by, its speed the one of the line laid") { + project_setup::setup setup; + setup.speed = 120.0; + setup.traffic = norms::traffic::passenger; + setup.track = norms::track_kind::side; + auto const own{project_setup::line_class(setup)}; + CHECK(own.vmax == doctest::Approx(120.0)); + CHECK(own.kind == norms::traffic::passenger); + CHECK(own.track == norms::track_kind::side); + CHECK(project_setup::line_class(setup, 60.0).vmax == doctest::Approx(60.0)); + CHECK(project_setup::diverging_speed(setup) == doctest::Approx(40.0)); + setup.track = norms::track_kind::main; + CHECK(project_setup::diverging_speed(setup) == doctest::Approx(60.0)); +} + +TEST_CASE("project setup: kept in the project file as it was written, read back the same") { + project::document document; + project_setup::setup setup; + setup.speed = 160.0; + document.setup = project_setup::record(setup); + auto const text{project::write(document)}; + CHECK(text.find(document.setup) != std::string::npos); + auto const read{project::read(text)}; + REQUIRE(read.project.has_value()); + CHECK(read.project->setup == document.setup); + CHECK(project::same(*read.project, document)); + auto const back{project_setup::read(std::string_view{read.project->setup})}; + REQUIRE(back.has_value()); + CHECK(*back == setup); + // a project with none writes none + CHECK(project::write(project::document{}).find("setup") == std::string::npos); +}