18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 09:27:10 +01:00
Files
maszyna/editor/project/projectSession.hpp
T
maj00r 6c2d467c5a feat(editor): the switches of the switch tool on the lines of the model its turnouts, inserted, moved, changed and taken out by its commands
Continues E4a (editor-tracks-domain.md §7, §8; stage E4b): with the editing on the model on, a switch dragged on a track of a line of
the model is a turnout of the catalogue of the model, and so on, each a command, refused with the reasons or one step of the history.
The switches of the old tracks are laid the old way.

- The drag of the switch tool, let go on a line of the model (on a straight or along a curve), is insert_turnout: the type of the
  catalogue the template is of (a template of the user's own isn't), its start where it was pressed, its chainage on the line
  (layout::chainage_of), the side of the drag its hand, the way it was dragged its direction. While it's dragged the panel tells in its
  red line why the model would refuse it, tried on a copy; let go, a refusal is told there with the value, the limit and the paragraph
  ("(9.6 pkt 16)"), nothing changed. Laid: its switch track made by the generator (t<n>), selected, the panel placed, its speed told.
- A turnout of the model selected: the inspector shows its type, side, speeds and bending, the templates of its kind of the catalogue
  (change_turnout), the other side (change_turnout of the hand), moved by a distance (move_turnout), taken out (remove_turnout). The
  slide of the switch tool moves it, another template changes it, Del takes it out (after the scenarios are looked through), each a
  command. Plan on a track of a turnout doesn't open: the turnout is edited through itself.
- A crossover dragged between two lines of the model is connect_tracks of the solver of the connections; refused, the solver's best
  proposal is told with the reasons ("Proposal: Rz 60E1-300-1:9 needs them 4.498 m apart").
- The tracks after a command: of a turnout one of whose tracks changed, its tracks of the simulator made again whole, kept by its main
  track (kind switch, the plain tracks of a double switch, a slip or a crossing with them); written with the scenery as the generator
  made them, recorded in the project with their kind and the file each went into (session::record_written of pieces of several files),
  found again when the map is opened, nothing taken for changed by hand. project::session::generate(turnout_id).

Self-test: modelturnoutside <track>; expect modelproposal <holds|lacks> <text>, expect handlines <count>; expect modelends checks the
paths of the switch tracks at the numbers of the nodes; a crossover of the connection command between two lines of the model goes to
the solver of the model. The cases m_8_turnout (a turnout dragged on, taken back and brought again, moved, given another type and side,
taken out by Del; one 2 m off the end of a cant ramp refused, nothing changed; saved and found after a reopen) and m_9_crossover (a
crossover between lines 4.75 m apart, one between lines 4 m apart refused with the proposal).

Tests: U: the chainage of a point on a line and its distance; the switch track of a turnout made by the session, the tracks of a line
written into two files recorded.

lang/editor_pl.po: the Polish of the new texts.
2026-11-23 18:48:19 +01:00

134 lines
6.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/.
*/
#pragma once
#include "editor/layout/layoutCommands.hpp"
#include "editor/layout/layoutGenerator.hpp"
#include "editor/project/projectFile.hpp"
#include "editor/project/projectGenerated.hpp"
#include <cstdint>
#include <filesystem>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
// The project of the map open in the editor, edited through the commands of the model (editor-tracks-domain.md §3, §4, §9; stage E4):
// the project file read when the map is opened (made of its //$ lines the first time) and written with the scenery, the model of the
// track geometry its commands edit, and what the editor needs around them: which lines a command lays anew or takes away, so their tracks
// are made again or go; which lines were changed by hand, so they aren't written over unasked; the steps of the model's history, which
// the editor's history takes back and brings again by their tokens, one step of the editor a command. No scene: the editor puts the tracks
// the generator makes into its scene and tells which it wrote into which file.
namespace project
{
// the lines which differ between two states of the network: Changed, in the second one and not the same there (laid anew, or new), and
// Removed, gone from it
void differences(layout::network const &Before, layout::network const &After, std::set<layout::line_id> &Changed, std::set<layout::line_id> &Removed);
class session
{
public:
struct open_result
{
bool done{false};
bool migrated{false}; // there was no project file: made of the //$ lines of the files of the map and written
std::string error; // why there's no project: a file of a newer schema, one which can't be read or written
std::vector<std::string> notes; // of the migration, and the lines the file has which can't be laid as written
std::vector<hand_change> hand_changes; // the tracks of the generator changed in the files since they were written
};
// the project of the map (relative to the folder of the sceneries): its file read, or made of the //$ lines of the map; the tracks the
// generator wrote compared with the files as they are now, the lines of the ones changed outside the editor marked as changed by hand
open_result open(std::filesystem::path const &Folder, std::string const &Map, norms::area_map const &Areas = {});
void close();
bool is_open() const { return m_open; }
std::filesystem::path const &folder() const { return m_folder; }
std::string const &map() const { return m_map; }
// <map>.eu07proj next to the map
std::filesystem::path file() const;
layout::network const &network() const { return m_model.state(); }
// the generated tracks, the lines changed by hand, the //$ lines kept; its network is the one at the last open or save, network() is now
document const &project() const { return m_project; }
// what a command does
struct effect
{
layout::outcome outcome;
std::set<layout::line_id> changed; // laid anew or new: their tracks are made again
std::set<layout::line_id> removed; // gone: their tracks go
};
// the command tried on a copy of the network, nothing of the session changed: refused with the reasons, or the lines it changes
effect trial(layout::command const &Command, layout::edit_policy const Policy = layout::edit_policy::refuse_block) const;
// the lines of the effect, changed or taken away, which may not be written over unasked: the ones the project marks as changed by hand
// (found so in the files) and the ones given (whose tracks the editor sees changed by other tools since they were made)
std::set<layout::line_id> hand_changed(effect const &Effect, std::set<layout::line_id> const &Seen = {}) const;
struct applied : effect
{
std::uint64_t token{0}; // of the step of the history kept; 0 for a command refused
};
// the command carried out: the lines of Overwrite may be written over, their marks of a change by hand taken off with it. A command
// kept is a step of the history, the marks with it; refused, nothing changes
applied execute(layout::command const &Command, std::set<layout::line_id> const &Overwrite = {}, layout::edit_policy const Policy = layout::edit_policy::refuse_block);
// the step of the token taken back, or brought again; only the latest one kept (undo) or taken back (redo) can be: false for another one,
// nothing changed. The lines it changes, as the effect of a command tells them
bool undo(std::uint64_t const Token, effect *Effect = nullptr);
bool redo(std::uint64_t const Token, effect *Effect = nullptr);
std::size_t undoable() const { return m_done.size(); }
std::size_t redoable() const { return m_undone.size(); }
// the tracks of the line as the generator makes them now, named as the options name them
layout::generated_line generate(layout::line_id const Line) const;
// the tracks of the simulator the turnout is made of now: its switch tracks, and the plain tracks of a double switch, a slip or a crossing
layout::generated_turnout generate(layout::turnout_id const Turnout) const;
layout::generator_options &options() { return m_options; }
layout::generator_options const &options() const { return m_options; }
// a piece written into a scenery file: its name and the text written, the coordinates those of the file
struct written_piece
{
std::string name;
std::string text;
std::string file; // where it went, of the folder of the sceneries; empty: the one given with the pieces
std::string kind{"track"}; // "switch" for the switch track of a turnout, kept by its main track
};
// the tracks of the line written with the scenery into the file, as they were written: the project records them, by the checksums of
// their text, in the place of the ones it had of the line, and doesn't take the line for changed by hand any more
void record_written(layout::line_id const Line, std::vector<written_piece> const &Pieces, std::string const &File);
// the same, each piece with the file it went into: the tracks of a line may go into more files than one
void record_written(layout::line_id const Line, std::vector<written_piece> const &Pieces);
// the line's tracks were changed by other tools: so marked in the project, not written over unasked from now on
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);
private:
struct step
{
std::uint64_t token{0};
std::set<layout::line_id> hand_before;
std::set<layout::line_id> hand_after;
};
bool m_open{false};
std::filesystem::path m_folder;
std::string m_map;
document m_project;
layout::model m_model;
layout::generator_options m_options;
std::vector<step> m_done;
std::vector<step> m_undone;
std::uint64_t m_last_token{0};
};
} // namespace project