]> git.tdb.fi Git - r2c2.git/blob - source/engineer/userinterface.cpp
Move user interface code to its own class
[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.front();
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.push_back(&dd);
56 }
57
58 void UserInterface::remove_dynamic_dialog(DynamicDialog &dd)
59 {
60         dyn_dialogs.erase(remove(dyn_dialogs.begin(), dyn_dialogs.end(), &dd), dyn_dialogs.end());
61 }
62
63 void UserInterface::show_train(Train &train)
64 {
65         TrainDialog *dlg = new TrainDialog(engineer, train);
66         root.add(*dlg);
67         dlg->autosize();
68 }
69
70 void UserInterface::show_zone(Zone &zone)
71 {
72         DeparturesDialog *dlg = new DeparturesDialog(engineer.get_layout(), zone.get_group());
73         root.add(*dlg);
74 }
75
76 void UserInterface::locomotive_detected(const Driver::DetectedLocomotive &loco)
77 {
78         if(!import_active)
79         {
80                 NewTrainDialog *dlg = new NewTrainDialog(engineer);
81                 dlg->prefill(loco);
82                 dlg->signal_response.connect(sigc::mem_fun(this, &UserInterface::import_finished));
83                 root.add(*dlg);
84                 import_active = true;
85         }
86 }
87
88 void UserInterface::import_finished(int)
89 {
90         import_active = false;
91 }
92
93 void UserInterface::tick()
94 {
95         Time::TimeStamp t = Time::now();
96         if(status_timeout && t>status_timeout)
97         {
98                 main_wnd.set_status_text(string());
99                 status_timeout = Time::TimeStamp();
100         }
101
102         for(list<DynamicDialog *>::iterator i=dyn_dialogs.begin(); i!=dyn_dialogs.end(); ++i)
103                 (*i)->update();
104 }
105
106 void UserInterface::render() const
107 {
108         root.render();
109 }