]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainpanel.cpp
Add networking library and a remote control program
[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         Widget(r),
20         Panel(r),
21         engineer(e),
22         train(t)
23 {
24         set_size(200, 145);
25
26         add(*(lbl_addr=new GLtk::Label(res, format("%2d", train.get_locomotive().get_address()))));
27         lbl_addr->set_style("digital");
28         lbl_addr->set_geometry(GLtk::Geometry(10, geom.h-34, 35, 24));
29
30         add(*(lbl_name=new GLtk::Label(res, train.get_name())));
31         lbl_name->set_style("digital");
32         lbl_name->set_geometry(GLtk::Geometry(45, geom.h-34, geom.w-55, 24));
33         train.signal_name_changed.connect(sigc::mem_fun(lbl_name, &GLtk::Label::set_text));
34
35         add(*(lbl_speed=new GLtk::Label(res, format("%2d", train.get_locomotive().get_speed()))));
36         lbl_speed->set_style("digital");
37         lbl_speed->set_geometry(GLtk::Geometry(10, geom.h-58, 35, 24));
38         train.signal_target_speed_changed.connect(sigc::mem_fun(this, &TrainPanel::train_speed_changed));
39
40         add(*(sld_speed=new GLtk::HSlider(res)));
41         sld_speed->set_geometry(GLtk::Geometry(50, geom.h-51, geom.w-80, 10));
42         sld_speed->set_range(0, 14);
43         sld_speed->set_step(1);
44         sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::speed_slider_changed));
45
46         add(*(tgl_forward=new GLtk::Toggle(res)));
47         tgl_forward->set_text("Fwd");
48         tgl_forward->set_geometry(GLtk::Geometry(geom.w-30, geom.h-59, 20, 27));
49         tgl_forward->set_value(!train.get_locomotive().get_reverse());
50         tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
51
52         add(*(lbl_status=new GLtk::Label(res, train.get_status())));
53         lbl_status->set_style("digital");
54         lbl_status->set_geometry(GLtk::Geometry(10, 34, geom.w-20, 24));
55         train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::train_status_changed));
56
57         const map<unsigned, string> &funcs = train.get_locomotive().get_type().get_functions();
58         unsigned x = 10;
59         for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=35)
60         {
61                 string fname = i->second;
62                 fname[0] = toupper(fname[0]);
63                 GLtk::Toggle *tgl;
64                 add(*(tgl=new GLtk::Toggle(res)));
65                 tgl->set_text(fname);
66                 tgl->set_geometry(GLtk::Geometry(x, geom.h-85, 35, 27));
67                 tgl->set_value(train.get_locomotive().get_function(i->first));
68                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_toggled), i->first));
69
70                 tgl_funcs[i->first] = tgl;
71         }
72         train.get_locomotive().signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_function_changed));
73
74         GLtk::Button *btn;
75
76         add(*(btn=new GLtk::Button(res, "Edit")));
77         btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24));
78
79         add(*(btn=new GLtk::Button(res, "Place")));
80         btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24));
81         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
82
83         add(*(btn=new GLtk::Button(res, "GoTo")));
84         btn->set_geometry(GLtk::Geometry(geom.w-130, 10, 40, 24));
85 }
86
87 void TrainPanel::speed_slider_changed(double v)
88 {
89         train.set_speed(static_cast<unsigned>(v));
90 }
91
92 void TrainPanel::train_speed_changed(unsigned speed)
93 {
94         lbl_speed->set_text(format("%2d", speed));
95         sld_speed->set_value(speed);
96 }
97
98 void TrainPanel::loco_function_changed(unsigned func, bool value)
99 {
100         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
101         if(i!=tgl_funcs.end())
102                 i->second->set_value(value);
103 }
104
105 void TrainPanel::train_status_changed(const string &s)
106 {
107         lbl_status->set_text(s);
108 }
109
110 void TrainPanel::place_clicked()
111 {
112         engineer.place_train(train);
113 }
114
115 void TrainPanel::forward_toggled(bool value)
116 {
117         train.set_reverse(!value);
118 }
119
120 void TrainPanel::func_toggled(bool value, unsigned func)
121 {
122         train.get_locomotive().set_function(func, value);
123 }