]> git.tdb.fi Git - r2c2.git/commitdiff
Move user interface code to its own class
authorMikko Rasa <tdb@tdb.fi>
Fri, 3 Apr 2015 16:29:46 +0000 (19:29 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 3 Apr 2015 16:29:46 +0000 (19:29 +0300)
source/engineer/dynamicdialog.cpp
source/engineer/dynamicdialog.h
source/engineer/engineer.cpp
source/engineer/engineer.h
source/engineer/telemetrydialog.cpp
source/engineer/trainview.cpp
source/engineer/userinterface.cpp [new file with mode: 0644]
source/engineer/userinterface.h [new file with mode: 0644]

index 87c74377795948a03e3583f17a1d071a3eefecdc..6d9afb4861432c208a305ce673a16d6f0a0b68b8 100644 (file)
@@ -1,13 +1,13 @@
 #include "dynamicdialog.h"
-#include "engineer.h"
+#include "userinterface.h"
 
-DynamicDialog::DynamicDialog(Engineer &e):
-       engineer(e)
+DynamicDialog::DynamicDialog(UserInterface &u):
+       ui(u)
 {
-       engineer.add_dynamic_dialog(*this);
+       ui.add_dynamic_dialog(*this);
 }
 
 DynamicDialog::~DynamicDialog()
 {
-       engineer.remove_dynamic_dialog(*this);
+       ui.remove_dynamic_dialog(*this);
 }
index fc18cbd184cb54b4c978605bfae597c769dd6656..0bf1996b2aae8e757679efbe9a71522e36f0f98c 100644 (file)
@@ -3,14 +3,14 @@
 
 #include <msp/gltk/dialog.h>
 
-class Engineer;
+class UserInterface;
 
 class DynamicDialog: public Msp::GLtk::Dialog
 {
 protected:
-       Engineer &engineer;
+       UserInterface &ui;
 
-       DynamicDialog(Engineer &);
+       DynamicDialog(UserInterface &);
 public:
        virtual ~DynamicDialog();
 
index da95505a1c2956191ab94086ddde7c6e54338a12..7327d6bf901ade03795f5ba45bcb7acc5bcf6fa4 100644 (file)
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
 #include <msp/gl/tests.h>
-#include <msp/gltk/floatingarrangement.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
-#include <msp/time/units.h>
 #include <msp/time/utils.h>
 #include "libr2c2/driver.h"
 #include "libr2c2/trackcircuit.h"
 #include "3d/track.h"
 #include "3d/trackcircuit.h"
 #include "3d/vehicle.h"
-#include "departuresdialog.h"
-#include "dynamicdialog.h"
 #include "engineer.h"
-#include "mainwindow.h"
-#include "newtraindialog.h"
-#include "traindialog.h"
 
 using namespace std;
 using namespace R2C2;
@@ -42,33 +35,21 @@ Engineer::Engineer(int argc, char **argv):
        window(options.screen_w, options.screen_h, options.fullscreen),
        keyboard(window),
        mouse(window),
-       ui_res("data/r2c2.res"),
-       import_active(false),
        layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))),
        layout_3d(layout),
        server(0),
        main_view(layout_3d, window.get_width(), window.get_height()),
+       ui(*this, window, keyboard, mouse),
        emergency_blink_state(0)
 {
        // Setup GUI
        window.set_title("Railroad Engineer");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
 
-       root = new GLtk::Root(ui_res, &window, &keyboard, &mouse);
-       GLtk::Layout *root_layout = new GLtk::Layout;
-       root->set_layout(root_layout);
-       root_layout->set_margin(GLtk::Sides());
-       root_arrangement = new GLtk::FloatingArrangement(*root_layout);
        mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::button_press), false));
        mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Engineer::axis_motion), false));
-       root->set_visible(true);
 
-       main_wnd = new MainWindow(*this);
-       root->add(*main_wnd);
-       main_wnd->autosize();
-       main_wnd->set_position(0, window.get_height()-main_wnd->get_geometry().h);
-
-       overlay = new Overlay3D(ui_res.get_default_font());
+       overlay = new Overlay3D(ui.get_resources().get_default_font());
 
        // Setup railroad control
        catalogue.add_source("data/Märklin/H0");
@@ -77,7 +58,6 @@ Engineer::Engineer(int argc, char **argv):
        if(layout.has_driver())
        {
                Driver &driver = layout.get_driver();
-               driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &Engineer::locomotive_detected));
                driver.signal_halt.connect(sigc::mem_fun(this, &Engineer::halt_event));
        }
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
@@ -135,28 +115,10 @@ Engineer::~Engineer()
        layout.get_driver().flush();
 
        delete overlay;
-       delete root_arrangement;
-       delete root;
 
        delete server;
 }
 
-void Engineer::set_status(const string &text)
-{
-       main_wnd->set_status_text(text);
-       status_timeout = Time::now()+10*Time::sec;
-}
-
-void Engineer::add_dynamic_dialog(DynamicDialog &tv)
-{
-       dyn_dialogs.push_back(&tv);
-}
-
-void Engineer::remove_dynamic_dialog(DynamicDialog &tv)
-{
-       dyn_dialogs.erase(remove(dyn_dialogs.begin(), dyn_dialogs.end(), &tv), dyn_dialogs.end());
-}
-
 int Engineer::main()
 {
        window.show();
@@ -176,15 +138,9 @@ void Engineer::tick()
        layout_3d.tick();
        event_disp.tick(Time::zero);
 
-       for(list<DynamicDialog *>::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i)
-               (*i)->update();
+       ui.tick();
 
        Time::TimeStamp t = Time::now();
-       if(status_timeout && t>status_timeout)
-       {
-               main_wnd->set_status_text(string());
-               status_timeout = Time::TimeStamp();
-       }
        if(!emergencies.empty() && t>emergency_blink_timeout)
        {
                emergency_blink_state = (emergency_blink_state+1)%2;
@@ -198,7 +154,7 @@ void Engineer::tick()
 
        main_view.render();
 
-       root->render();
+       ui.render();
 
        window.swap_buffers();
 }
@@ -214,7 +170,7 @@ void Engineer::button_press(unsigned btn)
                        {
                                Block &block = track->get_block();
                                if(block.get_train() && block.get_train()->is_block_critical(block))
-                                       set_status("Turnout is busy");
+                                       ui.set_status("Turnout is busy");
                                else
                                {
                                        unsigned paths = track->get_type().get_paths();
@@ -227,14 +183,14 @@ void Engineer::button_press(unsigned btn)
                                                        ++i;
                                        }
                                        track->set_active_path(i);
-                                       set_status(format("Turnout %d", track->get_turnout_address()));
+                                       ui.set_status(format("Turnout %d", track->get_turnout_address()));
                                }
                        }
                        else if(unsigned saddr = track->get_sensor_address())
                        {
                                if(options.simulate)
                                        layout.get_driver().set_sensor(saddr, !layout.get_driver().get_sensor(saddr));
-                               set_status(format("Sensor %d", saddr));
+                               ui.set_status(format("Sensor %d", saddr));
                        }
                        else
                        {
@@ -242,18 +198,13 @@ void Engineer::button_press(unsigned btn)
                                for(set<Zone *>::const_iterator i=zones.begin(); i!=zones.end(); ++i)
                                        if((*i)->has_track(*track))
                                        {
-                                               DeparturesDialog *dlg = new DeparturesDialog(layout, (*i)->get_group());
-                                               root->add(*dlg);
+                                               ui.show_zone(**i);
                                                break;
                                        }
                        }
                }
                else if(Vehicle *veh = dynamic_cast<Vehicle *>(obj))
-               {
-                       TrainDialog *dlg = new TrainDialog(*this, *veh->get_train());
-                       root->add(*dlg);
-                       dlg->autosize();
-               }
+                       ui.show_train(*veh->get_train());
        }
 }
 
@@ -324,9 +275,8 @@ Object *Engineer::pick_object(const Vector &p)
        return layout.pick<Track>(Ray(start, ray.slice<3>(0)));
 }
 
-void Engineer::emergency(Block *block, const string &msg)
+void Engineer::emergency(Block *block, const string &)
 {
-       set_status(msg);
        if(block)
        {
                TrackChain3D *tch3d = new TrackChain3D(layout_3d, *block);
@@ -346,23 +296,6 @@ void Engineer::halt_event(bool h)
        }
 }
 
-void Engineer::locomotive_detected(const Driver::DetectedLocomotive &loco)
-{
-       if(!import_active)
-       {
-               NewTrainDialog *dlg = new NewTrainDialog(*this);
-               dlg->prefill(loco);
-               dlg->signal_response.connect(sigc::mem_fun(this, &Engineer::import_finished));
-               root->add(*dlg);
-               import_active = true;
-       }
-}
-
-void Engineer::import_finished(int)
-{
-       import_active = false;
-}
-
 void Engineer::process_new_train(Train &train)
 {
        Vehicle3D &loco3d = layout_3d.get_3d(train.get_vehicle(0));
index 333af73a871362d698903965b91319aad1d9f760..712dc1638bfa9f72c7965b80556655b0565350a1 100644 (file)
@@ -7,10 +7,9 @@
 #include <msp/gl/light.h>
 #include <msp/gl/lighting.h>
 #include <msp/gl/pipeline.h>
-#include <msp/gltk/resources.h>
-#include <msp/gltk/root.h>
+#include <msp/input/keyboard.h>
+#include <msp/input/mouse.h>
 #include "libr2c2/catalogue.h"
-#include "libr2c2/driver.h"
 #include "libr2c2/train.h"
 #include "3d/layout.h"
 #include "3d/overlay.h"
@@ -19,9 +18,7 @@
 #include "3d/view.h"
 #include "network/server.h"
 #include "options.h"
-
-class MainWindow;
-class DynamicDialog;
+#include "userinterface.h"
 
 class Engineer: public Msp::RegisteredApplication<Engineer>
 {
@@ -31,10 +28,6 @@ private:
        Msp::Graphics::SimpleGLWindow window;
        Msp::Input::Keyboard keyboard;
        Msp::Input::Mouse mouse;
-       Msp::GLtk::Resources ui_res;
-       Msp::GLtk::Root *root;
-       Msp::GLtk::Arrangement *root_arrangement;
-       bool import_active;
 
        R2C2::Catalogue catalogue;
        R2C2::Layout layout;
@@ -45,11 +38,9 @@ private:
        std::map<R2C2::Train *, Msp::GL::Color> train_colors;
 
        R2C2::View3D main_view;
+       UserInterface ui;
 
-       MainWindow *main_wnd;
        std::list<R2C2::Train *> new_trains;
-       std::list<DynamicDialog *> dyn_dialogs;
-       Msp::Time::TimeStamp status_timeout;
        R2C2::Vector pointer;
        std::list<R2C2::TrackChain3D *> emergencies;
        Msp::Time::TimeStamp emergency_blink_timeout;
@@ -59,15 +50,11 @@ public:
        Engineer(int argc, char **argv);
        ~Engineer();
 
-       const Msp::GLtk::Resources &get_ui_resources() const { return ui_res; }
-       Msp::GLtk::Root &get_root() const { return *root; }
        const R2C2::Catalogue &get_catalogue() const { return catalogue; }
        R2C2::Layout &get_layout() { return layout; }
        R2C2::Layout3D &get_layout_3d() { return layout_3d; }
        R2C2::View3D &get_main_view() { return main_view; }
-       void set_status(const std::string &);
-       void add_dynamic_dialog(DynamicDialog &);
-       void remove_dynamic_dialog(DynamicDialog &);
+       UserInterface &get_user_interface() { return ui; }
        int main();
        void quit() { exit(0); }
 private:
@@ -78,8 +65,6 @@ private:
        R2C2::Object *pick_object(const R2C2::Vector &);
        void emergency(R2C2::Block *, const std::string &);
        void halt_event(bool);
-       void locomotive_detected(const R2C2::Driver::DetectedLocomotive &);
-       void import_finished(int);
        void train_added(R2C2::Train &);
        void process_new_train(R2C2::Train &);
        virtual void sighandler(int);
index bceea54062af6148f6b6a01f0b5281d838ef3bea..09bddbc9857c7a4b43f53e165185d5eca524221e 100644 (file)
@@ -7,8 +7,8 @@ using namespace std;
 using namespace Msp;
 using namespace R2C2;
 
-TelemetryDialog::TelemetryDialog(Engineer &e):
-       DynamicDialog(e),
+TelemetryDialog::TelemetryDialog(Engineer &engineer):
+       DynamicDialog(engineer.get_user_interface()),
        driver(engineer.get_layout().get_driver())
 {
        Loader::WidgetMap widgets;
index aec102705e93960a5f150114009fdc787b9c378e..c76e123bce7450e12d4a065f08ed47f2bb93a2aa 100644 (file)
@@ -12,8 +12,8 @@
 using namespace Msp;
 using namespace R2C2;
 
-TrainView::TrainView(Engineer &e, const Train &t):
-       DynamicDialog(e),
+TrainView::TrainView(Engineer &engineer, const Train &t):
+       DynamicDialog(engineer.get_user_interface()),
        train(t),
        mode(SIDE),
        forward(true),
diff --git a/source/engineer/userinterface.cpp b/source/engineer/userinterface.cpp
new file mode 100644 (file)
index 0000000..95f064a
--- /dev/null
@@ -0,0 +1,109 @@
+#include <msp/gltk/floatingarrangement.h>
+#include <msp/time/utils.h>
+#include "departuresdialog.h"
+#include "engineer.h"
+#include "newtraindialog.h"
+#include "traindialog.h"
+#include "userinterface.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+UserInterface::UserInterface(Engineer &e, Graphics::Window &window, Input::Keyboard &keyboard, Input::Mouse &mouse):
+       engineer(e),
+       resources("data/r2c2.res"),
+       root(resources, &window, &keyboard, &mouse),
+       main_wnd(engineer),
+       import_active(false)
+{
+       GLtk::Layout *root_layout = new GLtk::Layout;
+       root.set_layout(root_layout);
+       root_layout->set_margin(GLtk::Sides());
+       root_arrangement = new GLtk::FloatingArrangement(*root_layout);
+       root.set_visible(true);
+
+       root.add(main_wnd);
+       main_wnd.autosize();
+       main_wnd.set_position(0, window.get_height()-main_wnd.get_geometry().h);
+
+       Layout &layout = engineer.get_layout();
+       if(layout.has_driver())
+       {
+               Driver &driver = layout.get_driver();
+               driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &UserInterface::locomotive_detected));
+       }
+
+       engineer.get_layout().signal_emergency.connect(sigc::hide<0>(sigc::mem_fun(this, &UserInterface::set_status)));
+}
+
+UserInterface::~UserInterface()
+{
+       while(!dyn_dialogs.empty())
+               delete dyn_dialogs.front();
+       delete root_arrangement;
+}
+
+void UserInterface::set_status(const string &text)
+{
+       main_wnd.set_status_text(text);
+       status_timeout = Time::now()+10*Time::sec;
+}
+
+void UserInterface::add_dynamic_dialog(DynamicDialog &dd)
+{
+       dyn_dialogs.push_back(&dd);
+}
+
+void UserInterface::remove_dynamic_dialog(DynamicDialog &dd)
+{
+       dyn_dialogs.erase(remove(dyn_dialogs.begin(), dyn_dialogs.end(), &dd), dyn_dialogs.end());
+}
+
+void UserInterface::show_train(Train &train)
+{
+       TrainDialog *dlg = new TrainDialog(engineer, train);
+       root.add(*dlg);
+       dlg->autosize();
+}
+
+void UserInterface::show_zone(Zone &zone)
+{
+       DeparturesDialog *dlg = new DeparturesDialog(engineer.get_layout(), zone.get_group());
+       root.add(*dlg);
+}
+
+void UserInterface::locomotive_detected(const Driver::DetectedLocomotive &loco)
+{
+       if(!import_active)
+       {
+               NewTrainDialog *dlg = new NewTrainDialog(engineer);
+               dlg->prefill(loco);
+               dlg->signal_response.connect(sigc::mem_fun(this, &UserInterface::import_finished));
+               root.add(*dlg);
+               import_active = true;
+       }
+}
+
+void UserInterface::import_finished(int)
+{
+       import_active = false;
+}
+
+void UserInterface::tick()
+{
+       Time::TimeStamp t = Time::now();
+       if(status_timeout && t>status_timeout)
+       {
+               main_wnd.set_status_text(string());
+               status_timeout = Time::TimeStamp();
+       }
+
+       for(list<DynamicDialog *>::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i)
+               (*i)->update();
+}
+
+void UserInterface::render() const
+{
+       root.render();
+}
diff --git a/source/engineer/userinterface.h b/source/engineer/userinterface.h
new file mode 100644 (file)
index 0000000..9dd4d42
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef USERINTERFACE_H_
+#define USERINTERFACE_H_
+
+#include <msp/gltk/resources.h>
+#include <msp/gltk/root.h>
+#include "libr2c2/driver.h"
+#include "libr2c2/train.h"
+#include "libr2c2/zone.h"
+#include "mainwindow.h"
+
+class DynamicDialog;
+
+class UserInterface
+{
+private:
+       Engineer &engineer;
+       Msp::GLtk::Resources resources;
+       Msp::GLtk::Root root;
+       Msp::GLtk::Arrangement *root_arrangement;
+
+       MainWindow main_wnd;
+       std::list<DynamicDialog *> dyn_dialogs;
+       bool import_active;
+
+       Msp::Time::TimeStamp status_timeout;
+
+public:
+       UserInterface(Engineer &, Msp::Graphics::Window &, Msp::Input::Keyboard &, Msp::Input::Mouse &);
+       ~UserInterface();
+
+       const Msp::GLtk::Resources &get_resources() const { return resources; }
+
+       void set_status(const std::string &);
+       void add_dynamic_dialog(DynamicDialog &);
+       void remove_dynamic_dialog(DynamicDialog &);
+
+       void show_train(R2C2::Train &);
+       void show_zone(R2C2::Zone &);
+private:
+       void locomotive_detected(const R2C2::Driver::DetectedLocomotive &);
+       void import_finished(int);
+
+public:
+       void tick();
+       void render() const;
+};
+
+#endif