From: Mikko Rasa Date: Fri, 3 Apr 2015 16:29:46 +0000 (+0300) Subject: Move user interface code to its own class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c0e076401ee76fd9a5a96ff919a8e7b4d058c51b;p=r2c2.git Move user interface code to its own class --- diff --git a/source/engineer/dynamicdialog.cpp b/source/engineer/dynamicdialog.cpp index 87c7437..6d9afb4 100644 --- a/source/engineer/dynamicdialog.cpp +++ b/source/engineer/dynamicdialog.cpp @@ -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); } diff --git a/source/engineer/dynamicdialog.h b/source/engineer/dynamicdialog.h index fc18cbd..0bf1996 100644 --- a/source/engineer/dynamicdialog.h +++ b/source/engineer/dynamicdialog.h @@ -3,14 +3,14 @@ #include -class Engineer; +class UserInterface; class DynamicDialog: public Msp::GLtk::Dialog { protected: - Engineer &engineer; + UserInterface &ui; - DynamicDialog(Engineer &); + DynamicDialog(UserInterface &); public: virtual ~DynamicDialog(); diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index da95505..7327d6b 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -12,10 +12,8 @@ #include #include #include -#include #include #include -#include #include #include "libr2c2/driver.h" #include "libr2c2/trackcircuit.h" @@ -26,12 +24,7 @@ #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::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::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(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(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)); diff --git a/source/engineer/engineer.h b/source/engineer/engineer.h index 333af73..712dc16 100644 --- a/source/engineer/engineer.h +++ b/source/engineer/engineer.h @@ -7,10 +7,9 @@ #include #include #include -#include -#include +#include +#include #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 { @@ -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 train_colors; R2C2::View3D main_view; + UserInterface ui; - MainWindow *main_wnd; std::list new_trains; - std::list dyn_dialogs; - Msp::Time::TimeStamp status_timeout; R2C2::Vector pointer; std::list 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); diff --git a/source/engineer/telemetrydialog.cpp b/source/engineer/telemetrydialog.cpp index bceea54..09bddbc 100644 --- a/source/engineer/telemetrydialog.cpp +++ b/source/engineer/telemetrydialog.cpp @@ -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; diff --git a/source/engineer/trainview.cpp b/source/engineer/trainview.cpp index aec1027..c76e123 100644 --- a/source/engineer/trainview.cpp +++ b/source/engineer/trainview.cpp @@ -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 index 0000000..95f064a --- /dev/null +++ b/source/engineer/userinterface.cpp @@ -0,0 +1,109 @@ +#include +#include +#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::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 index 0000000..9dd4d42 --- /dev/null +++ b/source/engineer/userinterface.h @@ -0,0 +1,48 @@ +#ifndef USERINTERFACE_H_ +#define USERINTERFACE_H_ + +#include +#include +#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 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