]> git.tdb.fi Git - r2c2.git/blob - source/designer/selectionwrap.cpp
Make use of the geometry part of libmspmath
[r2c2.git] / source / designer / selectionwrap.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/renderer.h>
4 #include "selection.h"
5 #include "selectionwrap.h"
6
7 using namespace std;
8 using namespace Msp;
9 using namespace R2C2;
10
11 SelectionWrap::SelectionWrap(Selection &s):
12         selection(s)
13 {
14         selection.signal_changed.connect(sigc::mem_fun(this, &SelectionWrap::selection_changed));
15 }
16
17 SelectionWrap::~SelectionWrap()
18 {
19         for(map<const ObjectType *, GL::Mesh *>::iterator i=meshes.begin(); i!=meshes.end(); ++i)
20                 delete i->second;
21 }
22
23 void SelectionWrap::render(GL::Renderer &renderer, const GL::Tag &) const
24 {
25         for(list<Wrap>::const_iterator i=wraps.begin(); i!=wraps.end(); ++i)
26         {
27                 GL::MatrixStack::Push push(renderer.matrix_stack());
28                 const Vector &pos = i->object->get_position();
29                 renderer.matrix_stack() *= GL::Matrix::translation(pos);
30                 renderer.matrix_stack() *= GL::Matrix::rotation(i->object->get_rotation(), 0, 0, 1);
31                 i->mesh->draw(renderer);
32         }
33 }
34
35 void SelectionWrap::selection_changed()
36 {
37         wraps.clear();
38         const set<Object *> &objects = selection.get_objects();
39         for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
40         {
41                 Wrap wrap;
42                 wrap.object = *i;
43                 wrap.mesh = &get_mesh((*i)->get_type());
44                 wraps.push_back(wrap);
45         }
46 }
47
48 GL::Mesh &SelectionWrap::get_mesh(const ObjectType &type)
49 {
50         map<const ObjectType *, GL::Mesh *>::iterator j = meshes.find(&type);
51         if(j!=meshes.end())
52                 return *j->second;
53
54         GL::Mesh *mesh = new GL::Mesh((GL::COLOR4_UBYTE, GL::VERTEX2));
55         GL::MeshBuilder bld(*mesh);
56         bld.color(0.0f, 1.0f, 0.0f, 1.0f);
57
58         const Shape *shape = type.get_shape();
59         if(!shape)
60                 throw logic_error("No shape");
61         
62         Geometry::BoundingBox<float, 3> bbox = shape->get_axis_aligned_bounding_box();
63         const Vector &min_pt = bbox.get_minimum_point();
64         const Vector &max_pt = bbox.get_maximum_point();
65
66         bld.begin(GL::LINE_LOOP);
67         bld.vertex(min_pt.x-0.005, min_pt.y-0.005);
68         bld.vertex(max_pt.x+0.005, min_pt.y-0.005);
69         bld.vertex(max_pt.x+0.005, max_pt.y+0.005);
70         bld.vertex(min_pt.x-0.005, max_pt.y+0.005);
71         bld.end();
72
73         meshes[&type] = mesh;
74
75         return *mesh;
76 }