From: Mikko Rasa Date: Thu, 19 Feb 2015 13:31:56 +0000 (+0200) Subject: Add a common base class for dialogs that need to update dynamically X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=32cf18ff4306b36f2ce59d41056f888638a85578;p=r2c2.git Add a common base class for dialogs that need to update dynamically --- diff --git a/source/engineer/dynamicdialog.cpp b/source/engineer/dynamicdialog.cpp new file mode 100644 index 0000000..87c7437 --- /dev/null +++ b/source/engineer/dynamicdialog.cpp @@ -0,0 +1,13 @@ +#include "dynamicdialog.h" +#include "engineer.h" + +DynamicDialog::DynamicDialog(Engineer &e): + engineer(e) +{ + engineer.add_dynamic_dialog(*this); +} + +DynamicDialog::~DynamicDialog() +{ + engineer.remove_dynamic_dialog(*this); +} diff --git a/source/engineer/dynamicdialog.h b/source/engineer/dynamicdialog.h new file mode 100644 index 0000000..fc18cbd --- /dev/null +++ b/source/engineer/dynamicdialog.h @@ -0,0 +1,20 @@ +#ifndef DYNAMICDIALOG_H_ +#define DYNAMICDIALOG_H_ + +#include + +class Engineer; + +class DynamicDialog: public Msp::GLtk::Dialog +{ +protected: + Engineer &engineer; + + DynamicDialog(Engineer &); +public: + virtual ~DynamicDialog(); + + virtual void update() = 0; +}; + +#endif diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 68533bc..36412cf 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -25,11 +25,11 @@ #include "3d/track.h" #include "3d/trackcircuit.h" #include "3d/vehicle.h" +#include "dynamicdialog.h" #include "engineer.h" #include "mainwindow.h" #include "newtraindialog.h" #include "traindialog.h" -#include "trainview.h" using namespace std; using namespace R2C2; @@ -145,14 +145,14 @@ void Engineer::set_status(const string &text) status_timeout = Time::now()+10*Time::sec; } -void Engineer::add_train_view(TrainView &tv) +void Engineer::add_dynamic_dialog(DynamicDialog &tv) { - train_views.push_back(&tv); + dyn_dialogs.push_back(&tv); } -void Engineer::remove_train_view(TrainView &tv) +void Engineer::remove_dynamic_dialog(DynamicDialog &tv) { - train_views.erase(remove(train_views.begin(), train_views.end(), &tv), train_views.end()); + dyn_dialogs.erase(remove(dyn_dialogs.begin(), dyn_dialogs.end(), &tv), dyn_dialogs.end()); } int Engineer::main() @@ -174,8 +174,8 @@ void Engineer::tick() layout_3d.tick(); event_disp.tick(Time::zero); - for(list::iterator i=train_views.begin(); i!=train_views.end(); ++i) - (*i)->prepare(); + for(list::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i) + (*i)->update(); Time::TimeStamp t = Time::now(); if(status_timeout && t>status_timeout) diff --git a/source/engineer/engineer.h b/source/engineer/engineer.h index 0769473..333af73 100644 --- a/source/engineer/engineer.h +++ b/source/engineer/engineer.h @@ -21,7 +21,7 @@ #include "options.h" class MainWindow; -class TrainView; +class DynamicDialog; class Engineer: public Msp::RegisteredApplication { @@ -48,7 +48,7 @@ private: MainWindow *main_wnd; std::list new_trains; - std::list train_views; + std::list dyn_dialogs; Msp::Time::TimeStamp status_timeout; R2C2::Vector pointer; std::list emergencies; @@ -66,8 +66,8 @@ public: R2C2::Layout3D &get_layout_3d() { return layout_3d; } R2C2::View3D &get_main_view() { return main_view; } void set_status(const std::string &); - void add_train_view(TrainView &); - void remove_train_view(TrainView &); + void add_dynamic_dialog(DynamicDialog &); + void remove_dynamic_dialog(DynamicDialog &); int main(); void quit() { exit(0); } private: diff --git a/source/engineer/trainview.cpp b/source/engineer/trainview.cpp index 4e43993..aec1027 100644 --- a/source/engineer/trainview.cpp +++ b/source/engineer/trainview.cpp @@ -13,7 +13,7 @@ using namespace Msp; using namespace R2C2; TrainView::TrainView(Engineer &e, const Train &t): - engineer(e), + DynamicDialog(e), train(t), mode(SIDE), forward(true), @@ -38,13 +38,6 @@ TrainView::TrainView(Engineer &e, const Train &t): view.get_camera().set_depth_clip(0.01, 10); dynamic_cast(get_item(widgets, "img_view"))->set_image(&tex); - - engineer.add_train_view(*this); -} - -TrainView::~TrainView() -{ - engineer.remove_train_view(*this); } void TrainView::set_mode(Mode m) @@ -63,7 +56,7 @@ void TrainView::set_forward(bool f) forward = f; } -void TrainView::prepare() +void TrainView::update() { const Vehicle &veh = train.get_vehicle(0); const Vector &pos = veh.get_position(); diff --git a/source/engineer/trainview.h b/source/engineer/trainview.h index 598b43f..1e484cb 100644 --- a/source/engineer/trainview.h +++ b/source/engineer/trainview.h @@ -4,13 +4,11 @@ #include #include #include -#include #include #include "libr2c2/train.h" +#include "dynamicdialog.h" -class Engineer; - -class TrainView: public Msp::GLtk::Dialog +class TrainView: public DynamicDialog { public: enum Mode @@ -21,7 +19,6 @@ public: }; private: - Engineer &engineer; const R2C2::Train &train; Msp::GLtk::Toggle *tgl_forward; Mode mode; @@ -33,14 +30,13 @@ private: public: TrainView(Engineer &, const R2C2::Train &); - ~TrainView(); void set_mode(Mode); private: void ui_mode_toggled(bool, Mode); public: void set_forward(bool); - void prepare(); + virtual void update(); }; #endif