]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/path.cpp
Move gauge to TrackAppearance
[r2c2.git] / source / 3d / path.cpp
index e4e0b92989172a64780753af467a80e8f90d9cf3..8c36a3088f249f65305e9c13c5ea4e0a7f1d1e7f 100644 (file)
@@ -1,33 +1,35 @@
-/* $Id$
-
-This file is part of R²C²
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
 #include "libr2c2/tracktype.h"
 #include "layout.h"
 #include "path.h"
 #include "track.h"
 #include "tracktype.h"
 
+using namespace std;
 using namespace Msp;
 
 namespace R2C2 {
 
 Path3D::Path3D(const Track3D &t):
+       Utility3D(t.get_layout()),
        track(t),
-       paths(0),
+       path(t.get_track().get_active_path()),
+       side(0),
        automatic(true),
+       mesh(0),
        z_offs(0)
 {
-       track.get_layout().get_path_scene().add(*this);
+       update_mesh();
+
+       layout.get_path_scene().add(*this);
+       if(track.get_track().get_type().is_turnout())
+               track.get_track().signal_path_changed.connect(sigc::mem_fun(this, &Path3D::path_changed));
 }
 
 Path3D::~Path3D()
 {
-       track.get_layout().get_path_scene().remove(*this);
+       layout.get_path_scene().remove(*this);
 }
 
 void Path3D::set_automatic()
@@ -35,20 +37,19 @@ void Path3D::set_automatic()
        automatic = true;
 }
 
-void Path3D::set_path(unsigned p)
+void Path3D::set_path(int p)
 {
-       if(!(track.get_track().get_type().get_paths()&(1<<p)))
-               throw InvalidParameterValue("Invalid path");
+       if(p>=0 && !(track.get_track().get_type().get_paths()&(1<<p)))
+               throw invalid_argument("Path3D::set_path");
        automatic = false;
-       paths = 1<<p;
+       path = p;
+       update_mesh();
 }
 
-void Path3D::set_mask(unsigned p)
+void Path3D::set_side(int s)
 {
-       if(p&~track.get_track().get_type().get_paths())
-               throw InvalidParameterValue("Invalid path mask");
-       automatic = false;
-       paths = p;
+       side = (s<0 ? -1 : s>0 ? 1 : 0);
+       update_mesh();
 }
 
 void Path3D::set_color(const GL::Color &c)
@@ -58,26 +59,38 @@ void Path3D::set_color(const GL::Color &c)
 
 void Path3D::set_layer(float l)
 {
-       z_offs = l*track.get_track().get_layout().get_catalogue().get_gauge()*0.01;
+       z_offs = l*track.get_track().get_type().get_appearance().get_gauge()*0.01;
 }
 
-void Path3D::render(const GL::Tag &tag) const
+void Path3D::path_changed(unsigned p)
 {
-       if(tag=="unlit")
+       if(automatic)
        {
-               unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
-               mask &= track.get_track().get_type().get_paths();
-               if(!mask)
-                       return;
+               path = p;
+               update_mesh();
+       }
+}
+
+void Path3D::update_mesh()
+{
+       mesh = &track.get_type().get_path_mesh(path, side);
+}
+
+long Path3D::get_instance_key() const
+{
+       return reinterpret_cast<long>(&track.get_type());
+}
 
-               GL::PushMatrix push_mat;
-               track.apply_matrix();
-               GL::translate(0, 0, z_offs);
+void Path3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
+{
+       if(tag=="unlit")
+       {
+               GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
+               renderer.matrix_stack() *= track.Object3D::get_matrix();
+               renderer.matrix_stack() *= GL::Matrix::translation(0, 0, z_offs);
 
                glColor4f(color.r, color.g, color.b, color.a);
-               for(unsigned i=0; mask; ++i, mask>>=1)
-                       if(mask&1)
-                               track.get_type().get_path_mesh(i).draw();
+               mesh->draw(renderer);
        }
 }