17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-02 11:39:14 +01:00

refactor(editor): track observer instead of the function hooks

This commit is contained in:
maj00r
2026-10-04 00:37:26 +02:00
parent 7b8d809078
commit c85a52d5b1
5 changed files with 43 additions and 35 deletions

View File

@@ -344,9 +344,14 @@ editor_mode::editor_mode() {
m_orthophoto.ground_source([](glm::dvec2 const &Min, glm::dvec2 const &Max, std::vector<world_triangle> &Out) { m_orthophoto.ground_source([](glm::dvec2 const &Min, glm::dvec2 const &Max, std::vector<world_triangle> &Out) {
gather_ground_triangles(Min, Max, true, 50.0f, Out); gather_ground_triangles(Min, Max, true, 50.0f, Out);
}); });
infra_hooks(); editor_track::observe(this);
} }
editor_mode::~editor_mode()
{
editor_track::observe(nullptr);
}
editor_ui *editor_mode::ui() const editor_ui *editor_mode::ui() const
{ {
return static_cast<editor_ui *>(m_userinterface.get()); return static_cast<editor_ui *>(m_userinterface.get());

View File

@@ -34,12 +34,13 @@ http://mozilla.org/MPL/2.0/.
class TAnimModel; class TAnimModel;
class TTrack; class TTrack;
class editor_mode : public application_mode class editor_mode : public application_mode, private editor_track::observer
{ {
public: public:
// constructors // constructors
editor_mode(); editor_mode();
~editor_mode() override;
// methods // methods
// initializes internal data structures of the mode. returns: true on success, false otherwise // initializes internal data structures of the mode. returns: true on success, false otherwise
bool init() override; bool init() override;
@@ -576,7 +577,6 @@ class editor_mode : public application_mode
bool m_bindings_loaded{false}; bool m_bindings_loaded{false};
std::vector<infra::object_state> m_infra_buffer; // states of the objects before the change in progress std::vector<infra::object_state> m_infra_buffer; // states of the objects before the change in progress
bool m_infra_suspended{false}; bool m_infra_suspended{false};
void infra_hooks();
void infra_load(); void infra_load();
void infra_store(); void infra_store();
std::vector<TTrack *> infra_scope() const; std::vector<TTrack *> infra_scope() const;
@@ -587,9 +587,11 @@ class editor_mode : public application_mode
infra::object_state infra_state_of(infra::binding const &Binding) const; infra::object_state infra_state_of(infra::binding const &Binding) const;
void infra_restore_state(infra::object_state const &State); void infra_restore_state(infra::object_state const &State);
void infra_buffer(infra::binding const &Binding, bool const Refresh); void infra_buffer(infra::binding const &Binding, bool const Refresh);
void infra_capture(TTrack const &Track); // editor_track::observer
void infra_follow(std::vector<TTrack *> const &Tracks); void track_captured(TTrack const &Track) override;
void infra_split(TTrack &Original, TTrack &Created, segment_data const &First); void tracks_committed(std::vector<TTrack *> const &Tracks) override;
void track_retired(TTrack &Track) override;
void track_split(TTrack &Original, TTrack &Created, segment_data const &First) override;
void infra_move(infra::binding &Binding); void infra_move(infra::binding &Binding);
void infra_attach(EditorSnapshot &Snapshot); void infra_attach(EditorSnapshot &Snapshot);
void render_infra_window(); void render_infra_window();

View File

@@ -461,14 +461,6 @@ double turn_of(infra::binding const &Binding, double const Yaw)
} // namespace } // namespace
void editor_mode::infra_hooks()
{
editor_track::on_capture = [this](TTrack const &Track) { infra_capture(Track); };
editor_track::on_commit = [this](std::vector<TTrack *> const &Tracks) { infra_follow(Tracks); };
editor_track::on_retire = [this](TTrack &) { infra_follow({}); };
editor_track::on_split = [this](TTrack &Original, TTrack &Created, segment_data const &First) { infra_split(Original, Created, First); };
}
// format of the lines, coordinates in the space of the file: // format of the lines, coordinates in the space of the file:
// <kind> <category> <x y z of the object> <file or name, - if none> <turns 0/1> <anchor count> { <path> <start x y z> <end x y z> } // <kind> <category> <x y z of the object> <file or name, - if none> <turns 0/1> <anchor count> { <path> <start x y z> <end x y z> }
// the object is the model, the include (position from its parameters), the traction piece (its first point), the memory // the object is the model, the include (position from its parameters), the traction piece (its first point), the memory
@@ -908,7 +900,7 @@ void editor_mode::infra_buffer(infra::binding const &Binding, bool const Refresh
m_infra_buffer.push_back(infra_state_of(Binding)); m_infra_buffer.push_back(infra_state_of(Binding));
} }
void editor_mode::infra_capture(TTrack const &Track) void editor_mode::track_captured(TTrack const &Track)
{ {
if (m_infra_suspended || false == m_infra.follow) if (m_infra_suspended || false == m_infra.follow)
return; return;
@@ -948,7 +940,7 @@ void editor_mode::infra_capture(TTrack const &Track)
} }
} }
void editor_mode::infra_follow(std::vector<TTrack *> const &Tracks) void editor_mode::tracks_committed(std::vector<TTrack *> const &Tracks)
{ {
if (m_infra_suspended || false == m_infra.follow) if (m_infra_suspended || false == m_infra.follow)
return; return;
@@ -1027,7 +1019,12 @@ void editor_mode::infra_follow(std::vector<TTrack *> const &Tracks)
} }
} }
void editor_mode::infra_split(TTrack &Original, TTrack &Created, segment_data const &First) void editor_mode::track_retired(TTrack &)
{
tracks_committed({});
}
void editor_mode::track_split(TTrack &Original, TTrack &Created, segment_data const &First)
{ {
if (m_infra_suspended || Created.m_paths.empty()) if (m_infra_suspended || Created.m_paths.empty())
return; return;

View File

@@ -122,15 +122,12 @@ bool adjoining_direction(TTrack const *Neighbour, glm::dvec3 const &Joint, bool
} }
std::function<void(TTrack const &)> editor_track::on_capture; editor_track::observer *editor_track::s_observer{nullptr};
std::function<void(std::vector<TTrack *> const &)> editor_track::on_commit;
std::function<void(TTrack &)> editor_track::on_retire;
std::function<void(TTrack &, TTrack &, segment_data const &)> editor_track::on_split;
editor_track::state editor_track::capture(TTrack const &Track) editor_track::state editor_track::capture(TTrack const &Track)
{ {
if (on_capture) if (s_observer != nullptr)
on_capture(Track); s_observer->track_captured(Track);
state result; state result;
result.paths = Track.m_paths; result.paths = Track.m_paths;
result.velocity = Track.fVelocity; result.velocity = Track.fVelocity;
@@ -240,8 +237,8 @@ void editor_track::commit(std::vector<TTrack *> const &Tracks)
for (auto *track : affected) for (auto *track : affected)
for (auto *vehicle : track->Dynamics) for (auto *vehicle : track->Dynamics)
vehicle->Move(0.000001); vehicle->Move(0.000001);
if (on_commit) if (s_observer != nullptr)
on_commit(Tracks); s_observer->tracks_committed(Tracks);
} }
void editor_track::commit_parameters(TTrack &Track) void editor_track::commit_parameters(TTrack &Track)
@@ -2219,8 +2216,8 @@ TTrack *editor_track::split_path(TTrack &Track, double const T)
second.rolls[0] = roll; second.rolls[0] = roll;
auto *created{create_path(Track, second)}; auto *created{create_path(Track, second)};
Track.m_paths.front() = first; Track.m_paths.front() = first;
if (on_split) if (s_observer != nullptr)
on_split(Track, *created, first); s_observer->track_split(Track, *created, first);
commit({&Track, created}); commit({&Track, created});
return created; return created;
} }
@@ -2257,8 +2254,8 @@ void editor_track::retire(TTrack &Track)
update_transition(*neighbour); update_transition(*neighbour);
rebuild_geometry(*neighbour); rebuild_geometry(*neighbour);
} }
if (on_retire) if (s_observer != nullptr)
on_retire(Track); s_observer->track_retired(Track);
} }
void editor_track::rebuild_geometry(TTrack &Track) void editor_track::rebuild_geometry(TTrack &Track)

View File

@@ -232,14 +232,21 @@ class editor_track
static void velocity(TTrack &Track, double const Velocity); static void velocity(TTrack &Track, double const Velocity);
static void damage(TTrack &Track, int const Damage); static void damage(TTrack &Track, int const Damage);
// let the objects which belong to the paths follow their changes // told about the changes of the paths, to let the objects which belong to them follow
static std::function<void(TTrack const &)> on_capture; class observer
static std::function<void(std::vector<TTrack *> const &)> on_commit; {
static std::function<void(TTrack &)> on_retire; public:
// called before the commit; the first argument keeps the beginning of the path, given in the last one virtual ~observer() = default;
static std::function<void(TTrack &, TTrack &, segment_data const &)> on_split; virtual void track_captured(TTrack const &Track) = 0;
virtual void tracks_committed(std::vector<TTrack *> const &Tracks) = 0;
virtual void track_retired(TTrack &Track) = 0;
// called before the commit; Original keeps the beginning of the path, given in First
virtual void track_split(TTrack &Original, TTrack &Created, segment_data const &First) = 0;
};
static void observe(observer *Observer) { s_observer = Observer; }
private: private:
static observer *s_observer;
static void disconnect(TTrack &Track); static void disconnect(TTrack &Track);
static void join(TTrack &Track); static void join(TTrack &Track);
static void connect(TTrack &Track, bool const Prevside, TTrack *Other, int const Endpointid); static void connect(TTrack &Track, bool const Prevside, TTrack *Other, int const Endpointid);