#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);
}
#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();
#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;
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");
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));
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();
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;
main_view.render();
- root->render();
+ ui.render();
window.swap_buffers();
}
{
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();
++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
{
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());
}
}
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);
}
}
-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));
#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"
#include "3d/view.h"
#include "network/server.h"
#include "options.h"
-
-class MainWindow;
-class DynamicDialog;
+#include "userinterface.h"
class Engineer: public Msp::RegisteredApplication<Engineer>
{
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;
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;
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:
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);
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;
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),
--- /dev/null
+#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();
+}
--- /dev/null
+#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