]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainpanel.cpp
Add graphics for toggle widget
[r2c2.git] / source / engineer / trainpanel.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/button.h>
9 #include <msp/strings/formatter.h>
10 #include "libmarklin/locomotive.h"
11 #include "engineer.h"
12 #include "trainpanel.h"
13
14 using namespace std;
15 using namespace Msp;
16 using namespace Marklin;
17
18 TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t):
19         Panel(r),
20         engineer(e),
21         train(t)
22 {
23         set_size(200, 145);
24
25         add(*(lbl_addr=new GLtk::Label(res, format("%2d", train.get_locomotive().get_address()))));
26         lbl_addr->set_style("digital");
27         lbl_addr->set_geometry(GLtk::Geometry(10, geom.h-34, 35, 24));
28
29         add(*(lbl_name=new GLtk::Label(res, train.get_name())));
30         lbl_name->set_style("digital");
31         lbl_name->set_geometry(GLtk::Geometry(45, geom.h-34, geom.w-55, 24));
32         train.signal_name_changed.connect(sigc::mem_fun(lbl_name, &GLtk::Label::set_text));
33
34         add(*(lbl_speed=new GLtk::Label(res, format("%2d", train.get_locomotive().get_speed()))));
35         lbl_speed->set_style("digital");
36         lbl_speed->set_geometry(GLtk::Geometry(10, geom.h-58, 35, 24));
37         train.get_locomotive().signal_speed_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_speed_changed));
38
39         add(*(sld_speed=new GLtk::HSlider(res)));
40         sld_speed->set_geometry(GLtk::Geometry(50, geom.h-51, geom.w-80, 10));
41         sld_speed->set_range(0, 14);
42         sld_speed->set_step(1);
43         sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::speed_slider_changed));
44
45         add(*(tgl_forward=new GLtk::Toggle(res)));
46         tgl_forward->set_text("Fwd");
47         tgl_forward->set_geometry(GLtk::Geometry(geom.w-30, geom.h-59, 20, 27));
48         tgl_forward->set_value(!train.get_locomotive().get_reverse());
49         tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
50
51         add(*(lbl_status=new GLtk::Label(res, train.get_status())));
52         lbl_status->set_style("digital");
53         lbl_status->set_geometry(GLtk::Geometry(10, 34, geom.w-20, 24));
54         train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::train_status_changed));
55
56         const map<unsigned, string> &funcs = train.get_locomotive().get_type().get_functions();
57         unsigned x = 10;
58         for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=35)
59         {
60                 string fname = i->second;
61                 fname[0] = toupper(fname[0]);
62                 GLtk::Toggle *tgl;
63                 add(*(tgl=new GLtk::Toggle(res)));
64                 tgl->set_text(fname);
65                 tgl->set_geometry(GLtk::Geometry(x, geom.h-85, 35, 27));
66                 tgl->set_value(train.get_locomotive().get_function(i->first));
67                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_toggled), i->first));
68
69                 tgl_funcs[i->first] = tgl;
70         }
71         train.get_locomotive().signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_function_changed));
72
73         GLtk::Button *btn;
74
75         add(*(btn=new GLtk::Button(res, "Edit")));
76         btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24));
77
78         add(*(btn=new GLtk::Button(res, "Place")));
79         btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24));
80         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
81
82         add(*(btn=new GLtk::Button(res, "GoTo")));
83         btn->set_geometry(GLtk::Geometry(geom.w-130, 10, 40, 24));
84 }
85
86 void TrainPanel::speed_slider_changed(double v)
87 {
88         train.set_speed(static_cast<unsigned>(v));
89 }
90
91 void TrainPanel::loco_speed_changed(unsigned speed)
92 {
93         lbl_speed->set_text(format("%2d", speed));
94 }
95
96 void TrainPanel::loco_function_changed(unsigned func, bool value)
97 {
98         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
99         if(i!=tgl_funcs.end())
100                 i->second->set_value(value);
101 }
102
103 void TrainPanel::train_status_changed(const string &s)
104 {
105         lbl_status->set_text(s);
106 }
107
108 void TrainPanel::place_clicked()
109 {
110         engineer.place_train(train);
111 }
112
113 void TrainPanel::forward_toggled(bool value)
114 {
115         train.set_reverse(!value);
116 }
117
118 void TrainPanel::func_toggled(bool value, unsigned func)
119 {
120         train.get_locomotive().set_function(func, value);
121 }