mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 05:57:08 +01:00
a7a641944f
The first part of E4a (editor-tracks-domain.md §3, §9, §14): the domain model of E2 and the project file of E3 brought into the running editor, behind a setting, "Editing on the model (trial)" in the settings of the editor, off by default. Off, nothing of it runs and the editor is as it was. - editor/project/projectSession: the project of the map open in the editor, no scene. open() reads the project file of the map, or makes it of the //$ lines the first time (open_project of E3), and compares the tracks the generator wrote with the files as they are now: a track changed or gone outside the editor marks its line changed by hand. trial() tries a command on a copy of the network and tells the lines it lays anew or takes away (differences()), nothing changed; execute() carries it out, the lines of Overwrite no longer taken for changed by hand with it. A command kept is a step of the model's history with a token, which the editor's step of it takes back and brings again (undo(Token), redo(Token)): only the latest step kept, or taken back, can be, so the two histories can't go out of step; the marks of a change by hand go with the step. record_written() records the tracks of a line as the scenery save wrote them, save() writes the project with the network as it is, the records of the lines gone taken out. - The setting on, the project of the map (the file of the scenery opened, <map>.eu07proj next to it) is opened with the scenery and written with it. The scenery save asks for the text of each track it writes (scene::node_layers::path_writer): a track the generator made, as long as nothing else changed it, is written as the generator made it (the numbers in the shortest form which reads back to the same double, not the 15 digits of the export), and its checksum recorded; one changed by the other tools is written by its own export and its line marked changed by hand. The tracks of the lines of the project are found in the scene by their names, with the text the file has for each (scene::node_layers::source() tells the file and the placement of a node). Tests: U: a map opened gets its project file; a command refused, tried or carried out, changes nothing and isn't a step; a command tried changes nothing, carried out tells its lines (new, split, a curve changed, deleted); the steps taken back and brought again by their tokens in the order of the history, another token refused, a command after an undo dropping the step taken back, the identifiers not given again; the tracks of two lines written and recorded, a reopen finding nothing changed and the line the same to the bit as the model laid it, a speed edited in the file marking its line alone, deleting it writing over it too, the mark taken off by a command writing over it and back with its undo, the records of a line deleted gone with it at the save. Self-test: modelediting <0|1> (the setting for the run, not kept), expect modellines, expect projectfile <present|absent|lines n>. lang/editor_pl.po: the Polish of the new texts.
246 lines
10 KiB
C++
246 lines
10 KiB
C++
// the project of the map open in the editor (editor/project/projectSession.cpp, editor-tracks-domain.md §4 "Edycja = polecenie", §9; E4):
|
|
// a map opened gets its project file; a command tried changes nothing, carried out tells the lines whose tracks go or are made again;
|
|
// each command kept a step the editor's history takes back and brings again by its token, the marks of the lines changed by hand with it;
|
|
// the tracks written with the scenery recorded, a track changed in the file afterwards marking its line, the line written over only when
|
|
// the author decides
|
|
|
|
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "layout_fixture.h"
|
|
#include "scratch.h"
|
|
#include "editor/project/projectGenerated.hpp"
|
|
#include "editor/project/projectSession.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <string>
|
|
|
|
namespace {
|
|
|
|
using namespace test::layout_fixture;
|
|
using layout::line_id;
|
|
|
|
std::string file_text(std::filesystem::path const &Path) {
|
|
std::ifstream file{Path, std::ios::binary};
|
|
REQUIRE(file.is_open());
|
|
return {std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
|
|
}
|
|
|
|
// a straight of 400 m along z
|
|
layout::create_line straight(double const X) {
|
|
layout::create_line result;
|
|
result.start = {X, 10.0, 0.0};
|
|
result.end = {X, 10.0, 400.0};
|
|
result.category = line_class();
|
|
return result;
|
|
}
|
|
|
|
// the tracks of the line written into the map as the editor writes them: the generator's text
|
|
void write_tracks(project::session &Session, test::scratch_folder const &Folder, std::string const &Map, std::vector<line_id> const &Lines) {
|
|
std::string text{"// a map of the tests of the project of the editor\n"};
|
|
for (auto const line : Lines) {
|
|
auto const made{Session.generate(line)};
|
|
REQUIRE(made.errors.empty());
|
|
std::vector<project::session::written_piece> written;
|
|
for (auto const &piece : made.pieces) {
|
|
text += piece.text;
|
|
written.push_back({piece.name, piece.text});
|
|
}
|
|
Session.record_written(line, written, Map);
|
|
}
|
|
Folder.write(Map, text);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("project session: the map opened gets its project, a command tried changes nothing, carried out tells its lines") {
|
|
test::scratch_folder folder{"project_session_commands"};
|
|
folder.write("mapa.scn", "// a new scenery\n");
|
|
project::session session;
|
|
auto const opened{session.open(folder.root(), "mapa.scn")};
|
|
REQUIRE(opened.done);
|
|
CHECK(opened.migrated);
|
|
CHECK(std::filesystem::exists(folder.root() / "mapa.eu07proj"));
|
|
CHECK(session.file() == folder.root() / "mapa.eu07proj");
|
|
CHECK(session.network().lines().empty());
|
|
|
|
// a curve sharper than the norms let a line of 80 km/h have: refused with its reasons, tried or carried out, nothing changed
|
|
auto sharp{three_curves()};
|
|
sharp.vertices[1].radius = 120.0;
|
|
auto const tried{session.trial(sharp)};
|
|
CHECK_FALSE(tried.outcome.done);
|
|
CHECK_FALSE(tried.outcome.reasons.empty());
|
|
auto const refused{session.execute(sharp)};
|
|
CHECK_FALSE(refused.outcome.done);
|
|
CHECK(refused.token == 0);
|
|
CHECK(session.network().lines().empty());
|
|
CHECK(session.undoable() == 0);
|
|
|
|
// tried: what it'd make, the network as it was (the identifiers a refused command took aren't given again)
|
|
auto const trial{session.trial(three_curves())};
|
|
REQUIRE(trial.outcome.done);
|
|
REQUIRE(trial.outcome.lines.size() == 1);
|
|
auto const id{trial.outcome.lines.front()};
|
|
CHECK(trial.changed == std::set<line_id>{id});
|
|
CHECK(session.network().lines().empty());
|
|
auto const laid{session.execute(three_curves())};
|
|
REQUIRE(laid.outcome.done);
|
|
CHECK(laid.token != 0);
|
|
CHECK(laid.changed == std::set<line_id>{id});
|
|
CHECK(laid.removed.empty());
|
|
|
|
// split: the line laid again as it was is the same, the part after the node is new
|
|
auto const *line{session.network().find(id)};
|
|
REQUIRE(line != nullptr);
|
|
auto const split{session.execute(layout::split_line{id, 0.5 * line->solved.length})};
|
|
REQUIRE(split.outcome.done);
|
|
CHECK(split.changed.contains(id));
|
|
CHECK(split.changed.size() == 2);
|
|
auto const second{*std::prev(split.changed.end())};
|
|
// a curve of the second part made wider: that part alone
|
|
auto const *part{session.network().find(second)};
|
|
REQUIRE(part != nullptr);
|
|
REQUIRE_FALSE(part->shape.vertices.empty());
|
|
auto const vertex{part->shape.vertices.back()};
|
|
auto const moved{session.execute(layout::set_vertex{second, vertex.id, vertex.radius + 100.0})};
|
|
REQUIRE(moved.outcome.done);
|
|
CHECK(moved.changed == std::set<line_id>{second});
|
|
// deleted: gone, nothing laid anew
|
|
auto const deleted{session.execute(layout::delete_line{second})};
|
|
REQUIRE(deleted.outcome.done);
|
|
CHECK(deleted.changed.empty());
|
|
CHECK(deleted.removed == std::set<line_id>{second});
|
|
CHECK(session.undoable() == 4);
|
|
}
|
|
|
|
TEST_CASE("project session: each command a step the editor takes back and brings again by its token, in the order of its history") {
|
|
test::scratch_folder folder{"project_session_undo"};
|
|
folder.write("mapa.scn", "// a new scenery\n");
|
|
project::session session;
|
|
REQUIRE(session.open(folder.root(), "mapa.scn").done);
|
|
auto const empty{session.network()};
|
|
auto const first{session.execute(straight(0.0))};
|
|
REQUIRE(first.outcome.done);
|
|
auto const one{session.network()};
|
|
auto const second{session.execute(straight(50.0))};
|
|
REQUIRE(second.outcome.done);
|
|
auto const two{session.network()};
|
|
|
|
// only the latest step is taken back, the one before it next; a token of another step changes nothing
|
|
CHECK_FALSE(session.undo(first.token));
|
|
CHECK(session.network().same(two));
|
|
project::session::effect effect;
|
|
REQUIRE(session.undo(second.token, &effect));
|
|
CHECK(session.network().same(one));
|
|
CHECK(effect.removed == std::set<line_id>{line_id{2}});
|
|
CHECK_FALSE(session.redo(first.token));
|
|
REQUIRE(session.undo(first.token, &effect));
|
|
CHECK(session.network().same(empty));
|
|
CHECK(effect.removed == std::set<line_id>{line_id{1}});
|
|
// brought again in the order taken back, the last one taken back first
|
|
CHECK_FALSE(session.redo(second.token));
|
|
REQUIRE(session.redo(first.token, &effect));
|
|
CHECK(effect.changed == std::set<line_id>{line_id{1}});
|
|
CHECK(session.network().same(one));
|
|
REQUIRE(session.redo(second.token));
|
|
CHECK(session.network().same(two));
|
|
|
|
// a command kept after a step taken back: that step is gone, as the editor's redo of it is
|
|
REQUIRE(session.undo(second.token));
|
|
auto const third{session.execute(straight(100.0))};
|
|
REQUIRE(third.outcome.done);
|
|
CHECK_FALSE(session.redo(second.token));
|
|
CHECK(session.redoable() == 0);
|
|
// identifiers are never handed out again: the new line isn't the one taken back
|
|
CHECK(third.changed == std::set<line_id>{line_id{3}});
|
|
// a refused command isn't a step
|
|
auto sharp{three_curves()};
|
|
sharp.vertices[1].radius = 120.0;
|
|
CHECK(session.execute(sharp).token == 0);
|
|
CHECK(session.undoable() == 2);
|
|
}
|
|
|
|
TEST_CASE("project session: the tracks written recorded, one changed in the file marks its line, written over only when the author decides") {
|
|
test::scratch_folder folder{"project_session_hand"};
|
|
folder.write("mapa.scn", "// a new scenery\n");
|
|
{
|
|
project::session session;
|
|
REQUIRE(session.open(folder.root(), "mapa.scn").done);
|
|
REQUIRE(session.execute(three_curves()).outcome.done);
|
|
REQUIRE(session.execute(straight(-100.0)).outcome.done);
|
|
write_tracks(session, folder, "mapa.scn", {line_id{1}, line_id{2}});
|
|
std::string error;
|
|
REQUIRE(session.save(error));
|
|
CHECK(session.project().generated.size() > 2);
|
|
}
|
|
auto const saved{file_text(folder.root() / "mapa.eu07proj")};
|
|
project::session session;
|
|
{
|
|
// opened again: nothing changed in the file, the network the same to the bit
|
|
auto const opened{session.open(folder.root(), "mapa.scn")};
|
|
REQUIRE(opened.done);
|
|
CHECK_FALSE(opened.migrated);
|
|
CHECK(opened.hand_changes.empty());
|
|
CHECK(session.project().hand_changed.empty());
|
|
layout::model laid{norms::area_map{}};
|
|
REQUIRE(laid.execute(three_curves()).done);
|
|
CHECK(*session.network().find(line_id{1}) == *laid.state().find(line_id{1}));
|
|
// a vertex moved changes line 1 alone, which nobody changed by hand
|
|
auto const *line{session.network().find(line_id{1})};
|
|
auto const &vertex{line->shape.vertices.front()};
|
|
auto const effect{session.trial(layout::set_vertex{line_id{1}, vertex.id, 850.0})};
|
|
REQUIRE(effect.outcome.done);
|
|
CHECK(session.hand_changed(effect).empty());
|
|
// one the editor saw changed by other tools is
|
|
CHECK(session.hand_changed(effect, {line_id{1}}) == std::set<line_id>{line_id{1}});
|
|
}
|
|
|
|
// the speed of a track of line 1 edited outside the editor
|
|
auto text{file_text(folder.root() / "mapa.scn")};
|
|
auto const name{session.generate(line_id{1}).pieces.front().name};
|
|
auto const at{text.find("velocity 80", text.find(name))};
|
|
REQUIRE(at != std::string::npos);
|
|
text.replace(at, 11, "velocity 60");
|
|
folder.write("mapa.scn", text);
|
|
auto const opened{session.open(folder.root(), "mapa.scn")};
|
|
REQUIRE(opened.done);
|
|
REQUIRE(opened.hand_changes.size() == 1);
|
|
CHECK(opened.hand_changes.front().name == name);
|
|
CHECK(session.project().hand_changed == std::set<line_id>{line_id{1}});
|
|
auto const *line{session.network().find(line_id{1})};
|
|
auto const vertex{line->shape.vertices.front()};
|
|
layout::set_vertex const wider{line_id{1}, vertex.id, 850.0};
|
|
auto const effect{session.trial(wider)};
|
|
REQUIRE(effect.outcome.done);
|
|
CHECK(session.hand_changed(effect) == std::set<line_id>{line_id{1}});
|
|
// deleting it is writing over it too
|
|
CHECK(session.hand_changed(session.trial(layout::delete_line{line_id{1}})) == std::set<line_id>{line_id{1}});
|
|
// line 2 isn't
|
|
CHECK(session.hand_changed(session.trial(layout::delete_line{line_id{2}})).empty());
|
|
|
|
// written over: the mark goes with the step, and comes back with its undo
|
|
auto const done{session.execute(wider, {line_id{1}})};
|
|
REQUIRE(done.outcome.done);
|
|
CHECK(session.project().hand_changed.empty());
|
|
REQUIRE(session.undo(done.token));
|
|
CHECK(session.project().hand_changed == std::set<line_id>{line_id{1}});
|
|
REQUIRE(session.redo(done.token));
|
|
CHECK(session.project().hand_changed.empty());
|
|
// written with the scenery: recorded again, the file the same as written, nothing changed by hand
|
|
write_tracks(session, folder, "mapa.scn", {line_id{1}, line_id{2}});
|
|
std::string error;
|
|
REQUIRE(session.save(error));
|
|
CHECK(file_text(folder.root() / "mapa.eu07proj") != saved);
|
|
REQUIRE(session.open(folder.root(), "mapa.scn").hand_changes.empty());
|
|
CHECK(session.network().find(line_id{1})->shape.vertices.front().radius == 850.0);
|
|
|
|
// a line deleted and saved: its tracks' records go with it, a mark of it too
|
|
session.mark_hand_changed(line_id{2});
|
|
REQUIRE(session.execute(layout::delete_line{line_id{2}}, {line_id{2}}).outcome.done);
|
|
REQUIRE(session.save(error));
|
|
for (auto const &node : session.project().generated) {
|
|
CHECK(node.line == line_id{1});
|
|
}
|
|
CHECK(session.project().hand_changed.empty());
|
|
}
|