mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 12:57:06 +01:00
075a66d169
editor-project-setup.md: a new user drew with no word of what: the norms, the speed, the gauge and the rail lay about the panels, set on the way. Now one short step, "What do you draw?" (Co rysujesz?), one screen of defaults and one button: - The fields: the norms (PLK ST-T1-A6, TSI alone, EN 13803, CH; PLK by default, no "none": a fictional scenery has a profile too), the speed of the line (chips 40..200, 100 km/h), the traffic, a main or a side track, the gauge (1435 / 1520 / 1000 / 750 / 600 mm), the rail (60E1 / 49E1 / S42), the look of the tracks under More (the rails and the trackbed as now where left empty). application/editorsetupui.* draws them for all three places below. - Asked for as the last step of the wizard of a new scenery (4., or 2. for a fictional one; its button Start: create and open in the editor), which writes them into the new scenery; and in a scenery with none, as a card in the inspector of the tracks in place of the first steps, until its Start. Changed later in their window, Assumptions of the project, from the editor settings or the line of them under the first steps in Tracks; a change checks the lines laid before against them, the report in the window of the norms, nothing laid changed by it. - Kept with the map (editor/editorProjectSetup.*, one record "setup norms plk speed 100 traffic mixed track main gauge 1435 rail 60E1"): in its project file where it has one (a record of its own; the profile of the scenery the model judges by set with it), else in the scenery as //$u lines, which the simulator skips and the scene layers keep as a mark of the editor. Read when the map is opened, the project file first. A scenery saved without them holds nothing new. - The defaults of the tools from them: the speed of the laying and of the route design, the gauge and the rails and the trackbed of the tracks laid, the norms checked against, the rail of the switch tool and the usual switch of the rail for the speed on its diverging track (the closest speed, the ratio usual for it). A route with no speed of its own takes the speed of the assumptions. Self-test: setup [<key> <value>]..., setupwindow [close]; expect setup none|<key> <value>, expect setupmark <present|absent>, expect layspeed <km/h>. The cases ps_1_setup_saved (set, saved into the scenery, read back after a reopen) and ps_2_setup_project (into the project file with the editing on the model, read from there after a reopen with it off). Tests: U: the record written and read back, the defaults, what it can't read, the class of the line, kept in the project file. lang/editor_pl.po: the Polish of the new texts.
202 lines
4.9 KiB
C++
202 lines
4.9 KiB
C++
/*
|
|
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 <algorithm>
|
|
#include <charconv>
|
|
#include <cmath>
|
|
#include <format>
|
|
#include <sstream>
|
|
|
|
namespace project_setup
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
std::array<std::pair<norms::profile_id, std::string_view>, 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<std::pair<norms::traffic, std::string_view>, 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<double> 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<int>(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<norms::profile_id> 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<double>(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<setup> read(std::string_view const Line)
|
|
{
|
|
std::istringstream words{std::string{Line}};
|
|
std::string keyword;
|
|
if (false == static_cast<bool>(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<setup> read(std::vector<std::string> const &Lines)
|
|
{
|
|
for (auto const &line : Lines)
|
|
if (auto const found{read(std::string_view{line})})
|
|
return found;
|
|
return std::nullopt;
|
|
}
|
|
|
|
} // namespace project_setup
|