]> git.tdb.fi Git - r2c2.git/blob - source/engineer/userinterface.cpp
Minor adjustments to UI code
[r2c2.git] / source / engineer / userinterface.cpp
1 #include <msp/gltk/floatingarrangement.h>
2 #include <msp/time/utils.h>
3 #include "departuresdialog.h"
4 #include "engineer.h"
5 #include "newtraindialog.h"
6 #include "traindialog.h"
7 #include "userinterface.h"
8
9 using namespace std;
10 using namespace Msp;
11 using namespace R2C2;
12
13 UserInterface::UserInterface(Engineer &e, Graphics::Window &window, Input::Keyboard &keyboard, Input::Mouse &mouse):
14         engineer(e),
15         resources("data/r2c2.res"),
16         root(resources, &window, &keyboard, &mouse),
17         main_wnd(engineer),
18         import_active(false)
19 {
20         GLtk::Layout *root_layout = new GLtk::Layout;
21         root.set_layout(root_layout);
22         root_layout->set_margin(GLtk::Sides());
23         root_arrangement = new GLtk::FloatingArrangement(*root_layout);
24         root.set_visible(true);
25
26         root.add(main_wnd);
27         main_wnd.autosize();
28         main_wnd.set_position(0, window.get_height()-main_wnd.get_geometry().h);
29
30         Layout &layout = engineer.get_layout();
31         if(layout.has_driver())
32         {
33                 Driver &driver = layout.get_driver();
34                 driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &UserInterface::locomotive_detected));
35         }
36
37         engineer.get_layout().signal_emergency.connect(sigc::hide<0>(sigc::mem_fun(this, &UserInterface::set_status)));
38 }
39
40 UserInterface::~UserInterface()
41 {
42         while(!dyn_dialogs.empty())
43                 delete *dyn_dialogs.begin();
44         delete root_arrangement;
45 }
46
47 void UserInterface::set_status(const string &text)
48 {
49         main_wnd.set_status_text(text);
50         status_timeout = Time::now()+10*Time::sec;
51 }
52
53 void UserInterface::add_dynamic_dialog(DynamicDialog &dd)
54 {
55         dyn_dialogs.insert(&dd);
56 }
57
58 void UserInterface::remove_dynamic_dialog(DynamicDialog &dd)
59 {
60         dyn_dialogs.erase(&dd);
61 }
62
63 void UserInterface::show_train(Train &train)
64 {
65         TrainDialog *dlg = new TrainDialog(engineer, train);
66         root.add(*dlg);
67 }
68
69 void UserInterface::show_zone(Zone &zone)
70 {
71         DeparturesDialog *dlg = new DeparturesDialog(engineer.get_layout(), zone.get_group());
72         root.add(*dlg);
73 }
74
75 void UserInterface::locomotive_detected(const Driver::DetectedLocomotive &loco)
76 {
77         if(!import_active)
78         {
79                 NewTrainDialog *dlg = new NewTrainDialog(engineer);
80                 dlg->prefill(loco);
81                 dlg->signal_response.connect(sigc::mem_fun(this, &UserInterface::import_finished));
82                 root.add(*dlg);
83                 import_active = true;
84         }
85 }
86
87 void UserInterface::import_finished(int)
88 {
89         import_active = false;
90 }
91
92 void UserInterface::tick()
93 {
94         Time::TimeStamp t = Time::now();
95         if(status_timeout && t>status_timeout)
96         {
97                 main_wnd.set_status_text(string());
98                 status_timeout = Time::TimeStamp();
99         }
100
101         for(set<DynamicDialog *>::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i)
102                 (*i)->update();
103 }
104
105 void UserInterface::render() const
106 {
107         root.render();
108 }