]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Add state loading and saving to engineer
[r2c2.git] / source / engineer / engineer.cpp
index ea87df659d7e3a7b33ccff8ed519a866295cd993..581a301d8f194df79574671c15e95d165e4714c0 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -10,6 +10,7 @@ Distributed under the GPL
 #include <GL/gl.h>
 #include <msp/core/except.h>
 #include <msp/core/getopt.h>
+#include <msp/fs/stat.h>
 #include <msp/gbase/display.h>
 #include <msp/gbase/window.h>
 #include <msp/gl/immediate.h>
@@ -19,6 +20,7 @@ Distributed under the GPL
 #include <msp/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/regex.h>
+#include "libmarklin/tracktype.h"
 #include "engineer.h"
 #include "mainpanel.h"
 #include "trainpanel.h"
@@ -28,8 +30,6 @@ using namespace std;
 using namespace Marklin;
 using namespace Msp;
 
-#include <iostream>
-
 Engineer::Engineer(int argc, char **argv):
        screen_w(1280),
        screen_h(960),
@@ -83,10 +83,12 @@ Engineer::Engineer(int argc, char **argv):
        const vector<string> &args=getopt.get_args();
        if(args.empty())
                throw UsageError("No layout given");
-       layout.load(args.front());
+       DataFile::load(layout, args.front());
 
        trfc_mgr=new TrafficManager(control, layout);
        trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+       if(FS::exists("engineer.state"))
+               DataFile::load(*trfc_mgr, "engineer.state");
 
        const map<unsigned, Sensor *> &sensors=control.get_sensors();
        for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
@@ -97,6 +99,7 @@ Engineer::Engineer(int argc, char **argv):
 
 Engineer::~Engineer()
 {
+       trfc_mgr->save("engineer.state");
        delete trfc_mgr;
 }
 
@@ -178,6 +181,18 @@ int Engineer::main()
        main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
        main_panel->set_visible(true);
 
+       const list<Train *> &trains=trfc_mgr->get_trains();
+       int y=main_panel->get_geometry().y;
+       for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+       {
+               TrainPanel *tpanel=new TrainPanel(*this, ui_res, **i);
+               root->add(*tpanel);
+               tpanel->set_position(0, y-tpanel->get_geometry().h);
+               train_panels.push_back(tpanel);
+               tpanel->set_visible(true);
+               y-=tpanel->get_geometry().h;
+       }
+
        wnd->show();
 
        Application::main();
@@ -262,6 +277,28 @@ void Engineer::tick()
                GL::pop_matrix();
        }
 
+       const list<Train *> &trains=trfc_mgr->get_trains();
+       for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+       {
+               GL::PushMatrix _push;
+
+               const Point &tp=(*i)->get_position();
+               GL::translate(tp.x, tp.y, 0.02);
+               GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
+               imm.color(0.8f, 0.8f, 1.0f);
+               imm.begin(GL::TRIANGLE_FAN);
+               imm.vertex(0, 0);
+               for(unsigned j=0; j<=12; ++j)
+                       imm.vertex(0.02*cos(j*M_PI/6), 0.02*sin(j*M_PI/6));
+               imm.end();
+
+               GL::rotate(cam_rot*180/M_PI, 0, 0, 1);
+               GL::translate(0.03, -0.02, 0);
+               GL::scale_uniform(0.04);
+               ui_res.get_default_font().draw_string((*i)->get_name());
+               GL::Texture::unbind();
+       }
+
        GL::matrix_mode(GL::PROJECTION);
        GL::load_identity();
        GL::ortho_bottomleft(screen_w, screen_h);
@@ -315,7 +352,7 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                        if(unsigned tid=track->get_track().get_turnout_id())
                        {
                                Turnout &turnout=control.get_turnout(tid);
-                               turnout.set_route(1-turnout.get_route());
+                               turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
                        }
                        else if(simulate)
                        {