18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/application/editormodeswitchtool.cpp
T
pol102 65b45839fd Editor: track names outside of ASCII listed, not renamed; every name the editor gives is plain ASCII (#376)
* feat(editor): track names with characters outside of ASCII changed to ASCII when a scenery is opened for editing, with a 20 s warning in the middle of the screen

Tracks, and events named after a track (<track>:event0), are still found by their old names, so events, trainsets and memory cells keep working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* chore(editor): the brush and the ASCII track names cleaned up for Sonar (init-statements, std::numbers::pi, ranges, the dab of the stroke and the settings of many models in functions of their own)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* chore(editor): the rest of the Sonar notes of the PRs (bytes as numbers, std::format, no copy of the old track name, no shadowed names, try_emplace, deduced template arguments)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ

* feat(editor): the track names outside of ASCII listed, not renamed; the names the editor gives are ASCII only

Reworked after the reviews of maj00r and jerrrrycho and the proposal on the PR: renaming the tracks of
existing sceneries as they're opened, and then chasing every reference to the old names, broke references
it couldn't reach (memory cells in files the editor doesn't write, folded names colliding, the parameters
of includes naming events and cells, names outside of Latin folding to underscores), and its fallback ran
in the game too. So:

- Nothing is renamed when a scenery is opened for editing. The tracks whose names have characters outside of
  ASCII are listed in the log, and the status line says how many there are and that they work as they are.
- Tools -> "Track names outside of ASCII..." opens the list: each name as it reads, its ASCII form, whether
  that form is taken (by another path, or another name giving the same), whether the name comes from a
  parameter of an include, and whether its file is one the editor writes. It changes nothing; a track is
  renamed, if at all, by hand in its properties, where the cells, includes and events it touches are shown.
- editor_track::name_valid() refuses a name with characters outside of ASCII, so every name the editor gives
  (the track properties, the switches, the stations) is plain ASCII.
- The game is as on master: no folded lookups of paths or events (path_table::find, FindEventAscii and the
  map behind it are gone), so a reference to a missing name is reported as before and never binds to
  another track. The big warning in the middle of the screen is gone with the renaming it announced.

ascii_name (plain, fold, readable) and its tests stay, for the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1K1Qu7TCsa74zsNURd

* fix(editor): the switch tool takes no name outside of ASCII

The Name field of the switch tool went straight to switch_name_for_new(), so a typed name such as łąka1 became the name of
the new switch and of its drive events, outside the check of editor_track::name_valid(). A letter outside of ASCII is now
refused as it's typed, with the reason name_valid() gives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(editor): the switch, turntable and SRK station code fields drop letters outside of ASCII as they're typed; the include-named tracks said where they're renamed

A character filter (ImGuiInputTextFlags_CallbackCharFilter) keeps the refused letter out of the field, so it no longer shows while the switch name is typed. The turntable name and the SRK station code go into track names too and get the same filter. The window of the track names outside of ASCII says that a track named by an include parameter is renamed through that parameter, not in the track properties.

* Revert "fix(physicslog): no road-vehicle logs, formatting on the writer, a slow disk and the closing kept short (#408)"

This reverts commit d6c3593520.

* fix(editor): a letter outside ASCII refused in the turntable name goes to the status line as an error, as in the switch tool

The nit of the review of #376 (jerrrrycho, 21.11): the turntable tool set its own status only,
while the switch tool reports the same refusal through refused(), which also puts it in the
status line of the editor as an error. The text is the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QRgQ1JTXWSz4YufC1QZtrY

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: jerrrrycho <jurek.pakula@gmail.com>
2026-11-27 23:20:30 +01:00

358 lines
16 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/.
*/
// the panel of the switch tool: what it places, the templates and the options of that only, and how to place it
#include "stdafx.h"
#include "application/editormode.h"
#include "application/editorgestures.h"
#include "application/editormodeshared.h"
#include "editor/editorConnections.hpp"
#include "editor/editorFormat.hpp"
#include "editor/editorTrack.hpp"
#include "simulation/simulation.h"
#include "utilities/translation.h"
#include "world/Track.h"
#include "world/TrackSleepers.h"
#include "imgui/imgui.h"
#include <algorithm>
#include <string>
// the name of the template in the language of the editor: the words of the catalogue translated, the kind of a slip or
// a crossing left out where its type says it
std::string switch_name_translated(std::string Title)
{
auto const replace = [&](std::string const &From, std::string const &To) {
for (auto at{Title.find(From)}; at != std::string::npos; at = Title.find(From, at + To.size()))
Title.replace(at, From.size(), To);
};
for (auto const *redundant : {" single slip", " double slip", " crossing"})
replace(redundant, "");
for (auto const *word : {"the blades outside of the diamond", "movable nose", "long", "industrial"})
replace(word, STR(word));
if (auto const at{Title.find("gauge ")}; at != std::string::npos)
if (auto const gauge{std::atoi(Title.c_str() + at + 6)}; gauge > 0)
{
auto const end{Title.find_first_not_of("0123456789", at + 6)};
Title.replace(at, (end == std::string::npos ? Title.size() : end) - at, format(STR_C("gauge %d"), gauge));
}
return Title;
}
namespace
{
// a kind to place as a tile: a sketch of its tracks on the left, its name beside it; true when clicked
bool kind_tile(char const *Id, char const *Label, int const Picture, bool const Chosen, float const Width)
{
auto const font{ImGui::GetFontSize()};
auto const height{std::round(font * 2.4f)};
auto const top{ImGui::GetCursorScreenPos()};
auto const clicked{ImGui::InvisibleButton(Id, ImVec2(Width, height))};
auto const hovered{ImGui::IsItemHovered()};
auto *list{ImGui::GetWindowDrawList()};
auto const accent{family_colour(0)};
ImVec2 const bottom{top.x + Width, top.y + height};
auto const rounding{std::round(font * 0.35f)};
list->AddRectFilled(top, bottom, Chosen ? (accent & ~IM_COL32_A_MASK) | IM_COL32(0, 0, 0, 46) : ImGui::GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), rounding);
list->AddRect(top, bottom, Chosen ? accent : ImGui::GetColorU32(ImGuiCol_Border), rounding, 0, Chosen ? 2.0f : 1.0f);
auto const pad{std::round(font * 0.4f)};
auto const w{std::round(font * 2.2f)};
auto const h{height - 2.0f * pad};
ImVec2 const o{top.x + pad, top.y + pad};
auto const at = [&](float const X, float const Y) { return ImVec2(o.x + X * w, o.y + Y * h); };
auto const line{Chosen ? accent : ImGui::GetColorU32(ImGuiCol_Text, 0.7f)};
auto const branch{Chosen ? IM_COL32(255, 210, 60, 255) : ImGui::GetColorU32(ImGuiCol_Text, 0.45f)};
switch (Picture)
{
case 0: // a switch: the straight track and the diverging one
list->AddLine(at(0.0f, 0.75f), at(1.0f, 0.75f), line, 2.0f);
list->AddBezierQuadratic(at(0.15f, 0.75f), at(0.6f, 0.75f), at(1.0f, 0.1f), branch, 2.0f);
break;
case 1: // a slip switch: a crossing with the slip curves inside
list->AddLine(at(0.0f, 0.85f), at(1.0f, 0.15f), line, 2.0f);
list->AddLine(at(0.0f, 0.15f), at(1.0f, 0.85f), line, 2.0f);
list->AddBezierQuadratic(at(0.08f, 0.8f), at(0.5f, 0.45f), at(0.92f, 0.8f), branch, 1.5f);
list->AddBezierQuadratic(at(0.08f, 0.2f), at(0.5f, 0.55f), at(0.92f, 0.2f), branch, 1.5f);
break;
case 2: // a crossing
list->AddLine(at(0.0f, 0.85f), at(1.0f, 0.15f), line, 2.0f);
list->AddLine(at(0.0f, 0.15f), at(1.0f, 0.85f), line, 2.0f);
break;
case 5: // a double crossover: two crossovers facing each other
list->AddLine(at(0.0f, 0.2f), at(1.0f, 0.2f), line, 2.0f);
list->AddLine(at(0.0f, 0.8f), at(1.0f, 0.8f), line, 2.0f);
list->AddLine(at(0.05f, 0.8f), at(0.45f, 0.2f), branch, 2.0f);
list->AddLine(at(0.55f, 0.2f), at(0.95f, 0.8f), branch, 2.0f);
break;
case 6: // scissors: two crossovers through each other
list->AddLine(at(0.0f, 0.2f), at(1.0f, 0.2f), line, 2.0f);
list->AddLine(at(0.0f, 0.8f), at(1.0f, 0.8f), line, 2.0f);
list->AddLine(at(0.15f, 0.8f), at(0.85f, 0.2f), branch, 2.0f);
list->AddLine(at(0.15f, 0.2f), at(0.85f, 0.8f), branch, 2.0f);
break;
case 7: // a ladder: the tracks of a fan going off one by one
list->AddLine(at(0.0f, 0.9f), at(1.0f, 0.9f), line, 2.0f);
for (float const y : {0.6f, 0.35f, 0.1f})
list->AddLine(at(0.95f - y, y), at(1.0f, y), line, 2.0f);
list->AddLine(at(0.05f, 0.9f), at(0.85f, 0.1f), branch, 2.0f);
break;
default: // a connection of two tracks
list->AddLine(at(0.0f, 0.2f), at(1.0f, 0.2f), line, 2.0f);
list->AddLine(at(0.0f, 0.8f), at(1.0f, 0.8f), line, 2.0f);
list->AddLine(at(0.2f, 0.8f), at(0.8f, 0.2f), branch, 2.0f);
break;
}
auto const text_left{o.x + w + pad};
auto const wrap{bottom.x - pad - text_left};
auto const size{ImGui::CalcTextSize(Label, nullptr, false, wrap)};
list->AddText(nullptr, 0.0f, ImVec2(text_left, std::round(top.y + (height - size.y) * 0.5f)), ImGui::GetColorU32(Chosen ? ImGuiCol_Text : ImGuiCol_TextDisabled), Label, nullptr, wrap);
return clicked;
}
// whether the template is one of those the kind places
bool of_kind(editor_track::switch_template const &Template, int const Kind)
{
switch (Kind)
{
case 1: return Template.double_slip && Template.type != "Skrz";
case 2: return Template.double_slip && Template.type == "Skrz";
case 3: return false == Template.double_slip && false == Template.twofold();
default: return false == Template.double_slip;
}
}
} // namespace
std::string editor_mode::switch_template_label(editor_track::switch_template const &Template)
{
if (Template.source != nullptr)
return format("%s: %s (%dx)", STR_C("From the scenery"), Template.title().c_str(), Template.count);
return (Template.own ? STR("Own: ") : std::string{"PLK: "}) + switch_name_translated(Template.title());
}
editor_mode::switch_kind editor_mode::current_switch_kind() const
{
auto const &tool{m_switch};
if (tool.connection >= 0 && false == tool.recognised)
return switch_kind::connection;
if (tool.armed >= 0 && tool.armed < static_cast<int>(tool.templates.size()) && tool.templates[tool.armed].double_slip)
return tool.templates[tool.armed].type == "Skrz" ? switch_kind::crossing : switch_kind::slip;
return switch_kind::single;
}
bool editor_mode::switch_kind_offered(switch_kind const Kind) const
{
auto const &templates{m_switch.templates};
return std::any_of(templates.begin(), templates.end(), [&](editor_track::switch_template const &Template) { return of_kind(Template, static_cast<int>(Kind)); });
}
void editor_mode::choose_switch_kind(switch_kind const Kind)
{
auto &tool{m_switch};
tool.connection = (Kind == switch_kind::connection ? std::clamp(tool.connection_kind, 0, connection::kind_count - 1) : -1);
// the template chosen stays if it's of the kind, else the first one of the kind
auto const fits = [&](int const Index) { return Index >= 0 && Index < static_cast<int>(tool.templates.size()) && of_kind(tool.templates[Index], static_cast<int>(Kind)); };
if (fits(tool.armed))
return;
for (int i = 0; i < static_cast<int>(tool.templates.size()); ++i)
if (fits(i))
{
tool.last = i;
tool.armed = i;
tool.status.clear();
return;
}
tool.armed = -1;
}
void editor_mode::render_switch_ui()
{
auto &tool{m_switch};
auto const kind{current_switch_kind()};
// what to place, each with the templates and the options of its own only
struct choice
{
switch_kind kind;
char const *label;
char const *tooltip;
};
choice const choices[] = {{switch_kind::single, "Switch", "A switch in a straight or a curve"},
{switch_kind::slip, "Slip switch", "Rkp or Rkpd dragged out on one straight with its crossing track, at a crossing of two straights at its skos, or bent into a curve of a side track"},
{switch_kind::crossing, "Crossing", "A crossing of two straights"}};
{
auto const gap{std::round(ImGui::GetFontSize() * 0.4f)};
auto const width{std::floor((ImGui::GetContentRegionAvail().x - gap) * 0.5f)};
int shown{0};
for (auto const &entry : choices)
{
if (entry.kind != switch_kind::single && false == switch_kind_offered(entry.kind))
continue;
if (shown % 2 == 1)
ImGui::SameLine(0.0f, gap);
else if (shown > 0)
ImGui::Dummy(ImVec2(0.0f, gap * 0.25f));
if (kind_tile(entry.label, STR_C(entry.label), static_cast<int>(&entry - choices), kind == entry.kind, width) && kind != entry.kind)
choose_switch_kind(entry.kind);
item_tooltip(entry.tooltip);
++shown;
}
}
// how to place it with the mouse
std::vector<std::pair<char const *, char const *>> how;
switch (kind)
{
case switch_kind::slip:
how = {{"Drag on a straight", "the slip with its crossing track, through the quarter of the cursor"}, {"Drag at a crossing", "of two straights at the skos of the slip: it goes there"}, {"Drag on a curve", "bent into it, in a side track only (11.4 pkt 4b)"}};
break;
case switch_kind::crossing: how = {{"Drag at a crossing", "of two straights: it goes there"}}; break;
case switch_kind::connection:
how = {{"Drag", "from a straight onto the other track"}, {"Shift+wheel", "the kind"}, {"Ctrl+wheel", "R of the aligning curve"}};
if (tool.connection == static_cast<int>(connection::kind::double_crossover))
how.push_back({"Alt+wheel", "where they meet"});
break;
default:
how = {{"Drag on a track", "along: the direction, sideways: the side"},
{"Drag onto the other track", "a crossover; over a crossover the other way a double or a scissors one, across tracks a ladder"},
{"Drag from a free end", "a switch going on from the end of a track"}};
break;
}
gesture_rows(how);
// the templates of the kind: by the speed, by the skos for the rail of the selected track, all of them under; the lengths and the
// rest of the label in the tooltip
ImGui::TextDisabled("%s", STR_C(kind == switch_kind::connection ? "The first switch" : "Template"));
auto const rail{editor_track::rail_of(selected_track())};
if (false == rail.empty())
{
ImGui::SameLine();
ImGui::TextDisabled(STR_C("rail of the track %s"), rail.c_str());
}
else if (auto *track{selected_track()}; track != nullptr && (track->iCategoryFlag & 15) == 1 && track->m_profile1.first.empty())
render_rail_profile_hint(*track);
if (auto const picked{render_template_picker("##place", [&](editor_track::switch_template const &Template) { return of_kind(Template, static_cast<int>(kind)); }, rail, tool.armed, false)}; picked >= 0)
{
tool.last = picked;
tool.armed = picked;
}
ImGui::PushID("switchmore");
if (ImGui::TreeNode(STR_C("More settings")))
{
if (kind == switch_kind::single || kind == switch_kind::connection)
render_connection_options();
// the name and the drive of the next switch; a crossing has no moving parts
if (kind != switch_kind::crossing)
{
if (begin_fields("##switchnamefields", 0.3f))
{
field_row("Name##switchname");
// the names the editor gives are plain ASCII (editor_track::name_valid): a letter outside of it is dropped as it's typed
bool dropped{false};
ImGui::InputTextWithHint("##switchname", switch_name_for_new().c_str(), tool.name, sizeof(tool.name), ImGuiInputTextFlags_CallbackCharFilter, ascii_only_filter, &dropped);
if (dropped)
tool.status = refused(STR("The name can hold ASCII only: no Polish letters or other characters outside of it"));
item_tooltip("Name of the next switch, its drive events are named after it; empty: the next free z1, z2...; a number at the end goes up with each switch");
ImGui::EndTable();
}
if (render_switch_drive_choice())
if (auto *track{selected_track()}; track != nullptr && track->eType == tt_Switch && false == track->m_editorremoved)
apply_switch_drive_choice(*track);
}
if (ImGui::Button(STR_C("Collect from the scenery")))
{
tool.templates = editor_track::standard_switch_templates();
auto const found{editor_track::find_switch_templates()};
tool.templates.insert(tool.templates.end(), found.begin(), found.end());
tool.collected = true;
arm_switch();
}
item_tooltip("Looks for the switches of the scenery, to use their shapes as the templates");
render_switch_timbers();
ImGui::TreePop();
}
ImGui::PopID();
if (false == tool.status.empty())
{
ImGui::PushTextWrapPos(0.0f);
ImGui::TextColored(ImVec4(1.0f, 0.7f, 0.2f, 1.0f), "%s", tool.status.c_str());
ImGui::PopTextWrapPos();
}
render_stray_drives();
if (ImGui::Button(STR_C("Template editor...")))
m_switch_designer.open = true;
item_tooltip("Own switch templates: diverging tracks of straights, arcs and transition curves, double switches");
}
// the timbers of all the switches and crossings of the scenery at once: a button for each model of timbers of the scenery data
void editor_mode::render_switch_timbers()
{
ImGui::TextDisabled("%s", STR_C("Timbers of all the switches"));
auto first{true};
auto const button = [&](std::string const &Model, std::string const &Label) {
if (false == first)
ImGui::SameLine();
first = false;
if (ImGui::Button((Label + "##switchtimbers" + Model).c_str()))
m_switch.status = format(STR_C("Timbers %s set on %d tracks"), Label.c_str(), static_cast<int>(set_switch_timbers(Model)));
item_tooltip("Sets the model of the timbers, with the fastenings where it has them, on all the switches and crossings of the scenery, in one step of the undo");
};
for (auto const &model : track_sleepers::pack_models())
if (model.timber)
button(model.name, model.name.substr(model.name.find_last_of('/') + 1));
button({}, STR_C("As in eu07.ini"));
}
std::size_t editor_mode::set_switch_timbers(std::string const &Model)
{
std::vector<TTrack *> tracks;
for (auto *track : simulation::Paths.sequence())
if (track != nullptr && false == track->m_editorremoved && (track->iCategoryFlag & 15) == 1 && (track->eType == tt_Switch || false == track->m_crossings.empty())
&& track->m_timber_model_name != Model)
tracks.push_back(track);
return tracks_apply(tracks, [&](TTrack &Track) { Track.m_timber_model_name = Model; return true; }, true, "Timber model");
}
// a track with no rail profile, as the sceneries of the older editors have them: the templates of all the rails are offered.
// the profile set at once on all the tracks laid with the same rails and no profile, so a scenery is put right in a few clicks
void editor_mode::render_rail_profile_hint(TTrack &Track)
{
ImGui::SameLine();
ImGui::TextColored(ImVec4(1.0f, 0.75f, 0.3f, 1.0f), "%s", STR_C("Track with no rail profile"));
item_tooltip("The track doesn't say its rail, so the templates of all the rails are offered.\nSet the profile and the list keeps to the templates of the rail; the rails of the track take its shape");
ImGui::SameLine();
if (ImGui::SmallButton(STR_C("Set...##railprofile")))
ImGui::OpenPopup("##railprofileset");
if (false == ImGui::BeginPopup("##railprofileset"))
return;
auto const material{Track.m_material1};
auto const same = [&](TTrack const &Other) { return false == Other.m_editorremoved && (Other.iCategoryFlag & 15) == 1 && Other.m_material1 == material && Other.m_profile1.first.empty(); };
std::vector<TTrack *> tracks;
for (auto *other : simulation::Paths.sequence())
if (other != nullptr && same(*other))
tracks.push_back(other);
ImGui::TextUnformatted(format(STR_C("Tracks with the rails %s and no profile: %d"), editor_track::material_name(material).c_str(), static_cast<int>(tracks.size())).c_str());
static std::pair<char const *, char const *> const profiles[] = {{"S60 (60E1)", "s60"}, {"S49 (49E1)", "s49"}, {"S42", "s42"}};
for (auto const &[label, name] : profiles)
{
if (&label != &profiles[0].first)
ImGui::SameLine();
if (false == ImGui::Button(label))
continue;
auto const changed{tracks_apply(tracks, [&](TTrack &Other) { Other.rail_profile(name); return true; }, true, "Rail profile")};
m_switch.status = format(STR_C("Rail profile %s set on %d tracks"), label, static_cast<int>(changed));
ImGui::CloseCurrentPopup();
}
ImGui::TextDisabled("%s", STR_C("Other profiles: the properties of the track"));
ImGui::EndPopup();
}