]> git.tdb.fi Git - r2c2.git/blob - source/designer/trackwrap.cpp
Strip Id tags and copyright notices from files
[r2c2.git] / source / designer / trackwrap.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "3d/tracktype.h"
4 #include "selection.h"
5 #include "trackwrap.h"
6
7 using namespace std;
8 using namespace Msp;
9 using namespace R2C2;
10
11 TrackWrap::TrackWrap(Layout3D &l, Selection &s):
12         layout(l),
13         selection(s)
14 {
15         selection.signal_changed.connect(sigc::mem_fun(this, &TrackWrap::selection_changed));
16 }
17
18 TrackWrap::~TrackWrap()
19 {
20         for(map<const TrackType *, GL::Mesh *>::iterator i=meshes.begin(); i!=meshes.end(); ++i)
21                 delete i->second;
22 }
23
24 void TrackWrap::render(const GL::Tag &) const
25 {
26         for(list<Wrap>::const_iterator i=wraps.begin(); i!=wraps.end(); ++i)
27         {
28                 GL::PushMatrix _pushm;
29                 const Vector &pos = i->track->get_position();
30                 GL::translate(pos.x, pos.y, pos.z);
31                 GL::rotate(i->track->get_rotation()*180/M_PI, 0, 0, 1);
32                 i->mesh->draw();
33         }
34 }
35
36 void TrackWrap::selection_changed()
37 {
38         wraps.clear();
39         const set<Track *> &tracks = selection.get_tracks();
40         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
41         {
42                 Wrap wrap;
43                 wrap.track = *i;
44                 wrap.mesh = &get_mesh((*i)->get_type());
45                 wraps.push_back(wrap);
46         }
47 }
48
49 GL::Mesh &TrackWrap::get_mesh(const TrackType &type)
50 {
51         map<const TrackType *, GL::Mesh *>::iterator j = meshes.find(&type);
52         if(j!=meshes.end())
53                 return *j->second;
54
55         const TrackType3D &type3d = layout.get_catalogue().get_track(type);
56
57         float min_area = -1;
58         float angle = 0;
59         Vector center;
60         float width = 0;
61         float height = 0;
62         for(float a=0; a<M_PI; a+=0.01)
63         {
64                 Vector minp, maxp;
65                 type3d.get_bounds(a, minp, maxp);
66                 float area = (maxp.x-minp.x)*(maxp.y-minp.y);
67                 if(area<min_area || min_area<0)
68                 {
69                         float c = cos(a);
70                         float s = sin(a);
71                         float x = (minp.x+maxp.x)/2;
72                         float y = (minp.y+maxp.y)/2;
73                         center = Vector(c*x-s*y, s*x+c*y, minp.z);
74                         angle = a;
75                         width = maxp.x-minp.x+0.01;
76                         height = maxp.y-minp.y+0.01;
77
78                         min_area = area;
79                 }
80         }
81
82         GL::Mesh *mesh = new GL::Mesh((GL::COLOR4_UBYTE, GL::VERTEX2));
83         GL::MeshBuilder bld(*mesh);
84         bld.color(0.0f, 1.0f, 0.0f, 1.0f);
85
86         float c = cos(angle);
87         float s = sin(angle);
88
89         bld.begin(GL::LINE_LOOP);
90         bld.vertex(center.x-c*width/2+s*height/2, center.y-s*width/2-c*height/2);
91         bld.vertex(center.x+c*width/2+s*height/2, center.y+s*width/2-c*height/2);
92         bld.vertex(center.x+c*width/2-s*height/2, center.y+s*width/2+c*height/2);
93         bld.vertex(center.x-c*width/2-s*height/2, center.y-s*width/2+c*height/2);
94         bld.end();
95
96         meshes[&type] = mesh;
97
98         return *mesh;
99 }