]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/layout.cpp
Add an internal layout to Catalogue for selecting tracks
[r2c2.git] / source / 3d / layout.cpp
index 9c174fbcf34d7db4ad8c9ba97f4ebeeefd9b7b01..b95d4e59c835798d2df81530d060e6e7303bff7d 100644 (file)
@@ -1,12 +1,14 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
 #include <algorithm>
-#include <fstream>
+#include <limits>
+#include <msp/gl/clip.h>
+#include <msp/gl/matrix.h>
 #include <msp/gl/rendermode.h>
 #include <msp/gl/select.h>
 #include <msp/gl/texture.h>
@@ -20,40 +22,38 @@ namespace Marklin {
 
 Layout3D::Layout3D(Layout &l):
        layout(l),
+       catalogue(layout.get_catalogue()),
        quality(4)
 {
        layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
+
+       const set<Track *> &ltracks = layout.get_tracks();
+       for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+               track_added(**i);
 }
 
 Layout3D::~Layout3D()
 {
-       for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               delete *i;
+       while(!tracks.empty())
+               delete tracks.front();
 }
 
 void Layout3D::set_quality(unsigned q)
 {
-       quality=q;
-       for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               (*i)->set_quality(quality);
+       quality = q;
 }
 
-void Layout3D::render(bool endpoints) const
+void Layout3D::add_track(Track3D &t)
 {
-       GL::Texture::unbind();
-       glEnable(GL_DEPTH_TEST);
-
-       for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               (*i)->render();
+       tracks.push_back(&t);
+}
 
-       if(endpoints)
-       {
-               glDepthMask(false);
-               for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-                       (*i)->render_endpoints();
-               glDepthMask(true);
-       }
+void Layout3D::remove_track(Track3D &t)
+{
+       list<Track3D *>::iterator i = find(tracks.begin(), tracks.end(), &t);
+       if(i!=tracks.end())
+               tracks.erase(i);
 }
 
 Track3D &Layout3D::get_track(const Track &t) const
@@ -61,7 +61,7 @@ Track3D &Layout3D::get_track(const Track &t) const
        for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if(&(*i)->get_track()==&t)
                        return **i;
-       
+
        throw KeyError("Unknown track");
 }
 
@@ -71,50 +71,31 @@ Track3D *Layout3D::pick_track(float x, float y, float size) const
        GL::select_buffer(select_buf);
        GL::render_mode(GL::SELECT);
 
-       glPushMatrix();
-       glLoadIdentity();
-
-       double clip[4];
-       clip[0]=1;
-       clip[1]=0;
-       clip[2]=x-size;
-       clip[3]=0;
-       glClipPlane(GL_CLIP_PLANE0, clip);
-       glEnable(GL_CLIP_PLANE0);
-
-       clip[0]=-1;
-       clip[2]=-(x+size);
-       glClipPlane(GL_CLIP_PLANE1, clip);
-       glEnable(GL_CLIP_PLANE1);
-
-       clip[0]=0;
-       clip[1]=1;
-       clip[2]=y-size;
-       glClipPlane(GL_CLIP_PLANE2, clip);
-       glEnable(GL_CLIP_PLANE2);
-
-       clip[1]=-1;
-       clip[2]=-(y+size);
-       glClipPlane(GL_CLIP_PLANE3, clip);
-       glEnable(GL_CLIP_PLANE3);
+       {
+               GL::PushMatrix push_mat;
+               GL::load_identity();
 
-       glPopMatrix();
+               GL::ClipPlane(1, 0, x-size, 0).apply_to(0);
+               GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1);
+               GL::ClipPlane(0, 1, y-size, 0).apply_to(2);
+               GL::ClipPlane(0, -1, -y-size, 0).apply_to(3);
+       }
 
-       render();
+       scene.render(0);
 
-       glDisable(GL_CLIP_PLANE0);
-       glDisable(GL_CLIP_PLANE1);
-       glDisable(GL_CLIP_PLANE2);
-       glDisable(GL_CLIP_PLANE3);
+       GL::ClipPlane::disable(0);
+       GL::ClipPlane::disable(1);
+       GL::ClipPlane::disable(2);
+       GL::ClipPlane::disable(3);
 
        GL::render_mode(GL::RENDER);
-       Track3D *track=0;
-       unsigned track_depth=numeric_limits<unsigned>::max();
+       Track3D *track = 0;
+       unsigned track_depth = numeric_limits<unsigned>::max();
        for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
                if(i->min_depth<track_depth)
                {
-                       track=reinterpret_cast<Track3D *>(i->names.back());
-                       track_depth=i->min_depth;
+                       track = reinterpret_cast<Track3D *>(i->names.back());
+                       track_depth = i->min_depth;
                }
 
        return track;
@@ -122,7 +103,7 @@ Track3D *Layout3D::pick_track(float x, float y, float size) const
 
 void Layout3D::track_added(Track &t)
 {
-       tracks.push_back(new Track3D(t, quality));
+       new Track3D(*this, t);
 }
 
 void Layout3D::track_removed(Track &t)
@@ -131,7 +112,6 @@ void Layout3D::track_removed(Track &t)
                if(&(*i)->get_track()==&t)
                {
                        delete *i;
-                       tracks.erase(i);
                        return;
                }
 }