]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Fix remaining exception class names
[r2c2.git] / source / engineer / engineer.cpp
index 897c7ca1ea8121f21d205a26d007b4ec35e26f8a..b9e252301b5adbd1b3d77bc0ec6f068ba85239d9 100644 (file)
@@ -1,18 +1,12 @@
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
+#include <algorithm>
 #include <cmath>
 #include <cstdlib>
 #include <limits>
 #include <signal.h>
-#include <msp/core/except.h>
 #include <msp/fs/stat.h>
-#include <msp/gbase/display.h>
-#include <msp/gbase/window.h>
+#include <msp/fs/utils.h>
+#include <msp/graphics/display.h>
+#include <msp/graphics/window.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/framebuffer.h>
 #include <msp/gl/matrix.h>
@@ -21,11 +15,11 @@ Distributed under the GPL
 #include <msp/gl/tests.h>
 #include <msp/gl/transform.h>
 #include <msp/io/print.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
-#include "libmarklin/driver.h"
-#include "libmarklin/tracktype.h"
+#include "libr2c2/driver.h"
+#include "libr2c2/tracktype.h"
 #include "3d/path.h"
 #include "3d/track.h"
 #include "3d/vehicle.h"
@@ -33,9 +27,10 @@ Distributed under the GPL
 #include "mainpanel.h"
 #include "trainpanel.h"
 #include "trainproperties.h"
+#include "trainview.h"
 
 using namespace std;
-using namespace Marklin;
+using namespace R2C2;
 using namespace Msp;
 
 Application::RegApp<Engineer> Engineer::reg;
@@ -50,24 +45,27 @@ Engineer::Engineer(int argc, char **argv):
        picking(false),
        picking_track(0),
        picking_entry(0),
-       picking_path(0)
+       picking_path(0),
+       pointer_x(0),
+       pointer_y(0),
+       pointer_moved(false)
 {
        // Setup GUI
        window.set_title("Railroad Engineer");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
 
-       DataFile::load(ui_res, "marklin.res");
+       DataFile::load(ui_res, "r2c2.res");
        root = new GLtk::Root(ui_res, window);
        root->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
        root->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
        root->set_visible(true);
 
-       main_panel = new MainPanel(*this, ui_res);
+       main_panel = new MainPanel(*this);
        root->add(*main_panel);
        main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h);
        main_panel->set_visible(true);
 
-       overlay = new Overlay3D(window, camera, ui_res.get_default_font());
+       overlay = new Overlay3D(ui_res.get_default_font());
 
        // Setup railroad control
        DataFile::load(catalogue, "tracks.dat");
@@ -76,11 +74,12 @@ Engineer::Engineer(int argc, char **argv):
        DataFile::load(layout, options.layout_fn);
 
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
-       layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
+       layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
+       layout.signal_block_state_changed.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
        layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
-       layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event));
-       if(FS::exists("engineer.state"))
-               DataFile::load(layout, "engineer.state");
+
+       if(FS::exists(options.state_fn))
+               DataFile::load(layout, options.state_fn);
 
        if(options.network)
        {
@@ -92,14 +91,24 @@ Engineer::Engineer(int argc, char **argv):
        DataFile::load(arrow_mesh, "arrow.mesh");
 
        pipeline.set_camera(&camera);
-       pipeline.add_renderable(layout_3d.get_scene());
+       pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0);
+       pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
+       pipeline.add_renderable_for_pass(*overlay, "overlay");
 
-       light.set_position(0, -0.259, 0.966, 0);
+       light.set_position(GL::Vector4(0, -0.259, 0.966, 0));
+       light.set_diffuse(GL::Color(0.9));
+       lighting.set_ambient(GL::Color(0.4));
        lighting.attach(0, light);
 
-       GL::PipelinePass &pass = pipeline.add_pass(0);
-       pass.depth_test = &GL::DepthTest::lequal();
-       pass.lighting = &lighting;
+       GL::PipelinePass *pass = &pipeline.add_pass(0);
+       pass->depth_test = &GL::DepthTest::lequal();
+       pass->lighting = &lighting;
+
+       pass = &pipeline.add_pass("unlit");
+       pass->depth_test = &GL::DepthTest::lequal();
+
+       pass = &pipeline.add_pass("overlay");
+       pass->blend = &GL::Blend::alpha();
 
        view_all();
 
@@ -120,7 +129,10 @@ Engineer::~Engineer()
        layout.get_driver().flush();
 
        if(!options.simulate)
-               layout.save_trains("engineer.state");
+       {
+               layout.save_dynamic(options.state_fn+".tmp");
+               FS::rename(options.state_fn+".tmp", options.state_fn);
+       }
 
        delete overlay;
        delete root;
@@ -134,6 +146,26 @@ void Engineer::set_status(const string &text)
        status_timeout = Time::now()+10*Time::sec;
 }
 
+void Engineer::rearrange_panels()
+{
+       int y = main_panel->get_geometry().y;
+       for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
+       {
+               y -= (*i)->get_geometry().h;
+               (*i)->set_position(0, y);
+       }
+}
+
+void Engineer::add_train_view(TrainView &tv)
+{
+       train_views.push_back(&tv);
+}
+
+void Engineer::remove_train_view(TrainView &tv)
+{
+       train_views.erase(remove(train_views.begin(), train_views.end(), &tv), train_views.end());
+}
+
 void Engineer::pick(bool with_ep)
 {
        picking = true;
@@ -155,22 +187,42 @@ void Engineer::tick()
        layout.tick();
        event_disp.tick(Time::zero);
 
+       for(list<TrainView *>::iterator i=train_views.begin(); i!=train_views.end(); ++i)
+               (*i)->prepare();
+
        if(status_timeout && Time::now()>status_timeout)
        {
                main_panel->set_status_text(string());
                status_timeout = Time::TimeStamp();
        }
 
-       GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
+       GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
        pipeline.render_all();
+
+       if(pointer_moved)
        {
-               GL::Bind depth(GL::DepthTest::lequal());
-               layout_3d.get_path_scene().render();
-       }
-       {
-               GL::Bind blend(GL::Blend::alpha());
-               overlay->render(0);
+               pointer_moved = false;
+
+               if(picking)
+               {
+                       Track *track = pick_track(pointer_x, window.get_height()-pointer_y-1);
+                       if(track && track!=picking_track)
+                       {
+                               picking_track = track;
+                               if(picking_entry>=0)
+                                       picking_entry = 0;
+
+                               delete picking_path;
+                               picking_path = new Path3D(layout_3d.get_track(*track));
+                               if(picking_entry>=0)
+                                       picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths);
+                               else
+                                       picking_path->set_mask(picking_track->get_type().get_paths());
+                               picking_path->set_color(GL::Color(0));
+                               picking_path->set_layer(1);
+                       }
+               }
        }
 
        if(picking && picking_track && picking_entry>=0)
@@ -178,7 +230,7 @@ void Engineer::tick()
                GL::PushMatrix push_mat;
 
                float rot = picking_track->get_endpoint_direction(picking_entry);
-               Point pos = picking_track->get_endpoint_position(picking_entry);
+               Vector pos = picking_track->get_endpoint_position(picking_entry);
 
                GL::translate(pos.x, pos.y, pos.z+0.03);
                GL::rotate(rot*180/M_PI+180, 0, 0, 1);
@@ -216,24 +268,23 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                else if(btn==3 && picking_entry>=0)
                {
                        picking_entry = (picking_entry+1)%picking_track->get_type().get_endpoints().size();
-                       picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths);
+                       picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths);
                }
        }
        else
        {
-               Track3D *t3d = pick_track(x, window.get_height()-y-1);
-               if(t3d)
+               Track *track = pick_track(x, window.get_height()-y-1);
+               if(track)
                {
-                       Track &track = t3d->get_track();
-                       if(track.get_turnout_id())
+                       if(track->get_turnout_id())
                        {
-                               Block &block = layout.get_block_by_track(track);
+                               Block &block = track->get_block();
                                if(block.get_train() && !block.get_train()->free_block(block))
                                        set_status("Turnout is busy");
                                else
                                {
-                                       unsigned paths = track.get_type().get_paths();
-                                       unsigned i = track.get_active_path()+1;
+                                       unsigned paths = track->get_type().get_paths();
+                                       unsigned i = track->get_active_path()+1;
                                        while(!(paths&(1<<i)))
                                        {
                                                if(!(paths>>i))
@@ -241,13 +292,15 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                                                else
                                                        ++i;
                                        }
-                                       track.set_active_path(i);
+                                       track->set_active_path(i);
+                                       set_status(format("Turnout %d", track->get_turnout_id()));
                                }
                        }
-                       else if(options.simulate)
+                       if(unsigned sid = track->get_sensor_id())
                        {
-                               if(unsigned sid=track.get_sensor_id())
+                               if(options.simulate)
                                        layout.get_driver().set_sensor(sid, !layout.get_driver().get_sensor(sid));
+                               set_status(format("Sensor %d", sid));
                        }
                }
        }
@@ -255,30 +308,14 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
 
 void Engineer::pointer_motion(int x, int y)
 {
-       if(picking)
-       {
-               Track3D *track = pick_track(x, window.get_height()-y-1);
-               if(track && &track->get_track()!=picking_track)
-               {
-                       picking_track = &track->get_track();
-                       if(picking_entry>=0)
-                               picking_entry = 0;
-
-                       delete picking_path;
-                       picking_path = new Path3D(*track);
-                       if(picking_entry>=0)
-                               picking_path->set_mask(picking_track->get_type().get_endpoints()[picking_entry].paths);
-                       else
-                               picking_path->set_mask(picking_track->get_type().get_paths());
-                       picking_path->set_color(GL::Color(0));
-                       picking_path->set_layer(1);
-               }
-       }
+       pointer_x = x;
+       pointer_y = y;
+       pointer_moved = true;
 }
 
 void Engineer::view_all()
 {
-       const list<Track3D *> &tracks = layout_3d.get_tracks();
+       const Layout3D::TrackMap &tracks = layout_3d.get_tracks();
 
        float view_aspect = float(window.get_width()-200)/window.get_height();
        float view_height = tan(camera.get_field_of_view()/2)*2;
@@ -291,10 +328,10 @@ void Engineer::view_all()
                float max_x = 0;
                float min_y = 0;
                float max_y = 0;
-               for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                {
-                       Point minp, maxp;
-                       (*i)->get_bounds(angle, minp, maxp);
+                       Vector minp, maxp;
+                       i->second->get_bounds(angle, minp, maxp);
                        min_x = min(min_x, minp.x);
                        max_x = max(max_x, maxp.x);
                        min_y = min(min_y, minp.y);
@@ -338,9 +375,7 @@ void Engineer::set_block_color(const Block &block, const GL::Color &color)
 
 void Engineer::reset_block_color(const Block &block)
 {
-       bool active = false;
-       if(unsigned sid=block.get_sensor_id())
-               active = layout.get_driver().get_sensor(sid);
+       bool active = block.get_state()>Block::INACTIVE;
 
        if(block.get_train())
        {
@@ -360,41 +395,22 @@ void Engineer::reset_block_color(const Block &block)
                set_block_color(block, GL::Color(1));
 }
 
-void Engineer::sensor_event(unsigned addr, bool)
-{
-       const set<Block *> &blocks = layout.get_blocks();
-       for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               if((*i)->get_sensor_id()==addr)
-                       reset_block_color(**i);
-}
-
-void Engineer::block_reserved(const Block &block, const Train *)
-{
-       reset_block_color(block);
-}
-
-Track3D *Engineer::pick_track(int x, int y)
+Track *Engineer::pick_track(int x, int y)
 {
-       float view_height = tan(camera.get_field_of_view()/2)*2;
-       float xx = ((float(x)-window.get_width()/2)/window.get_height())*view_height;
-       float yy = (float(y)/window.get_height()-0.5)*view_height;
-       float size = 4.0/window.get_height()*view_height;
-
-       camera.apply();
+       const GL::Vector3 &start = camera.get_position();
+       float xx = x*2.0/window.get_width()-1.0;
+       float yy = y*2.0/window.get_height()-1.0;
+       GL::Vector4 ray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
 
-       return layout_3d.pick_track(xx, yy, size);
+       return layout.pick_track(Vector(start.x, start.y, start.z), Vector(ray.x, ray.y, ray.z));
 }
 
 void Engineer::train_added(Train &train)
 {
-       TrainPanel *tpanel = new TrainPanel(*this, ui_res, train);
+       TrainPanel *tpanel = new TrainPanel(*this, train);
        root->add(*tpanel);
-       int y = main_panel->get_geometry().y;
-       for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
-               y -= (*i)->get_geometry().h;
-       tpanel->set_position(0, y-tpanel->get_geometry().h);
        train_panels.push_back(tpanel);
-       tpanel->set_visible(true);
+       rearrange_panels();
 
        Vehicle3D &loco3d = layout_3d.get_vehicle(train.get_vehicle(0));
        overlay->set_label(loco3d, train.get_name());