]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/userinterface.cpp
Move user interface code to its own class
[r2c2.git] / source / engineer / userinterface.cpp
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();
+}