]> git.tdb.fi Git - r2c2.git/blobdiff - source/remote/remote.cpp
Add a new remote control program with GLtk-based UI
[r2c2.git] / source / remote / remote.cpp
diff --git a/source/remote/remote.cpp b/source/remote/remote.cpp
new file mode 100644 (file)
index 0000000..f6e8692
--- /dev/null
@@ -0,0 +1,98 @@
+#include <msp/gl/framebuffer.h>
+#include <msp/gltk/layout.h>
+#include <msp/time/utils.h>
+#include "connectdialog.h"
+#include "remote.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+Remote::Remote(int, char **):
+       window(1024, 768),
+       client(catalogue),
+       ui_resources("data/r2c2.res"),
+       root(ui_resources, window),
+       root_layout(new GLtk::Layout),
+       status_bar(client)
+{
+       window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Remote::exit), 0));
+
+       root.set_size(window.get_width()/2, window.get_height()/2);
+       root.set_layout(root_layout);
+       root_layout->set_margin(GLtk::Sides());
+       root_layout->set_spacing(0);
+
+       for(unsigned i=0; i<2; ++i)
+       {
+               selectors[i] = new TrainSelector(client);
+               root.add(*selectors[i]);
+               root_layout->set_gravity(*selectors[i], i*2-1, 1);
+               root_layout->set_expand(*selectors[i], true, false);
+               selectors[i]->signal_train_selected.connect(sigc::bind(sigc::mem_fun(this, &Remote::train_selected), i));
+
+               if(i>0)
+               {
+                       root_layout->add_constraint(*selectors[i], GLtk::Layout::RIGHT_OF, *selectors[i-1]);
+                       root_layout->add_constraint(*selectors[i], GLtk::Layout::COPY_WIDTH, *selectors[i-1]);
+               }
+
+               panels[i] = 0;
+       }
+
+       root.add(status_bar);
+       root_layout->set_gravity(status_bar, -1, -1);
+       root_layout->set_expand(status_bar, true, false);
+
+       catalogue.add_source("data/Märklin/H0");
+
+       client.use_event_dispatcher(ev_disp);
+}
+
+int Remote::main()
+{
+       ConnectDialog *dlg = new ConnectDialog(client);
+       root.add(*dlg);
+       root_layout->set_gravity(*dlg, 0, 0);
+
+       window.show();
+
+       return Application::main();
+}
+
+void Remote::tick()
+{
+       for(unsigned i=0;; ++i)
+       {
+               Time::TimeStamp t = Time::now();
+               if(i>0 && t>=next_frame)
+               {
+                       next_frame = t+Time::sec/30;
+                       break;
+               }
+               ev_disp.tick(max(next_frame-t, Time::zero));
+       }
+
+       window.tick();
+
+       GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT);
+       root.render();
+       window.swap_buffers();
+}
+
+void Remote::train_selected(NetTrain *train, unsigned index)
+{
+       delete panels[index];
+       panels[index] = 0;
+
+       if(train)
+       {
+               panels[index] = new TrainPanel(*train);
+               root.add(*panels[index]);
+               root_layout->set_expand(*panels[index], true, true);
+               root_layout->add_constraint(*panels[index], GLtk::Layout::BELOW, *selectors[index]);
+               root_layout->add_constraint(*panels[index], GLtk::Layout::ALIGN_LEFT, *selectors[index]);
+               root_layout->add_constraint(*panels[index], GLtk::Layout::ALIGN_RIGHT, *selectors[index]);
+               root_layout->add_constraint(*panels[index], GLtk::Layout::ABOVE, status_bar);
+       }
+}