]> git.tdb.fi Git - r2c2.git/blobdiff - source/remote/trainpanel.cpp
Add a new remote control program with GLtk-based UI
[r2c2.git] / source / remote / trainpanel.cpp
diff --git a/source/remote/trainpanel.cpp b/source/remote/trainpanel.cpp
new file mode 100644 (file)
index 0000000..58dcd67
--- /dev/null
@@ -0,0 +1,98 @@
+#include <msp/core/maputils.h>
+#include <msp/core/raii.h>
+#include <msp/gltk/button.h>
+#include <msp/gltk/column.h>
+#include <msp/gltk/label.h>
+#include <msp/gltk/toggle.h>
+#include <msp/strings/format.h>
+#include "network/client.h"
+#include "trainpanel.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+TrainPanel::TrainPanel(NetTrain &t):
+       train(t),
+       updating(false)
+{
+       Loader::WidgetMap widgets;
+       DataFile::load(*this, "data/remote/trainpanel.ui", widgets);
+
+       Msp::GLtk::Button *btn = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_forward"));
+       btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(&train, &NetTrain::set_reverse), false));
+       btn = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_reverse"));
+       btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(&train, &NetTrain::set_reverse), true));
+
+       ind_forward = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_forward"));
+       ind_reverse = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_reverse"));
+
+       sld_speed = dynamic_cast<GLtk::Slider *>(get_item(widgets, "sld_speed"));
+       sld_speed->set_range(0, train.get_loco_type().get_maximum_speed()/train.get_client().get_catalogue().get_scale()*3.6);
+       sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::ui_speed_changed));
+
+       lbl_speed = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_speed"));
+
+       lbl_status = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_status"));
+       train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::status_changed));
+       lbl_status->set_text(train.get_status());
+
+       GLtk::Panel *pnl_functions = dynamic_cast<GLtk::Panel *>(get_item(widgets, "pnl_functions"));
+       const VehicleType::FunctionMap &functions = train.get_loco_type().get_functions();
+       GLtk::Column column(*pnl_functions->get_layout());
+       for(VehicleType::FunctionMap::const_iterator i=functions.begin(); i!=functions.end(); ++i)
+       {
+               GLtk::Toggle *tgl = new GLtk::Toggle(i->second);
+               pnl_functions->add(*tgl);
+               tgl->set_value(train.get_function(i->first));
+               tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::ui_function_toggled), i->first));
+               tgl_functions[i->first] = tgl;
+       }
+
+       train.signal_target_speed_changed.connect(sigc::mem_fun(this, &TrainPanel::update_speed));
+       update_speed(train.get_target_speed());
+       train.signal_reverse_changed.connect(sigc::mem_fun(this, &TrainPanel::update_reverse));
+       update_reverse(train.get_reverse());
+}
+
+void TrainPanel::update_reverse(bool r)
+{
+       ind_forward->set_active(!r);
+       ind_reverse->set_active(r);
+}
+
+void TrainPanel::update_speed(float s)
+{
+       SetFlag setf(updating);
+       float scale_speed = s/train.get_client().get_catalogue().get_scale()*3.6;
+       sld_speed->set_value(scale_speed);
+       lbl_speed->set_text(format("%3.0f", scale_speed));
+}
+
+void TrainPanel::ui_speed_changed(float s)
+{
+       if(!updating)
+       {
+               float real_speed = s*train.get_client().get_catalogue().get_scale()/3.6;
+               train.set_target_speed(real_speed);
+       }
+}
+
+void TrainPanel::function_changed(unsigned f, bool a)
+{
+       SetFlag setf(updating);
+       map<unsigned, GLtk::Toggle *>::iterator i = tgl_functions.find(f);
+       if(i!=tgl_functions.end())
+               get_item(tgl_functions, f)->set_value(a);
+}
+
+void TrainPanel::ui_function_toggled(bool a, unsigned f)
+{
+       if(!updating)
+               train.set_function(f, a);
+}
+
+void TrainPanel::status_changed(const string &s)
+{
+       lbl_status->set_text(s);
+}