]> git.tdb.fi Git - r2c2.git/blob - source/designer/manipulator.cpp
Rewrite goal initialization for TrainRouteMetric
[r2c2.git] / source / designer / manipulator.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include <msp/strings/format.h>
4 #include "libr2c2/tracktype.h"
5 #include "designer.h"
6 #include "manipulator.h"
7 #include "selection.h"
8
9 using namespace std;
10 using namespace R2C2;
11 using namespace Msp;
12
13 Manipulator::Manipulator(Designer &d, Input::Keyboard &k, Input::Mouse &m, const set<Object *> &objs):
14         Tool(d, k, m)
15 {
16         set<Object *> pending = objs;
17         while(!pending.empty())
18         {
19                 for(set<Object *>::iterator i=pending.begin(); i!=pending.end(); )
20                 {
21                         if((*i)->get_parent() && pending.count((*i)->get_parent()))
22                                 ++i;
23                         else
24                         {
25                                 objects.push_back(*i);
26                                 pending.erase(i++);
27                         }
28                 }
29         }
30
31         Geometry::BoundingBox<float, 3> bbox;
32         for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
33                 bbox = bbox|i->object->get_bounding_box();
34
35         const Vector &minp = bbox.get_minimum_point();
36         const Vector &maxp = bbox.get_maximum_point();
37
38         center = (minp+maxp)/2.0f;
39         center.z = minp.z;
40         for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
41         {
42                 i->original_position = i->object->get_position()-center;
43                 i->original_rotation = i->object->get_rotation();
44         }
45 }
46
47 Manipulator::~Manipulator()
48 {
49         if(!accepted)
50         {
51                 for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
52                 {
53                         i->object->set_position(center+i->original_position);
54                         i->object->set_rotation(i->original_rotation);
55                 }
56         }
57 }
58
59 void Manipulator::finish()
60 {
61         set<Object *> object_set;
62         for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
63                 object_set.insert(i->object);
64         for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
65         {
66                 unsigned nls = i->object->get_n_link_slots();
67                 for(unsigned j=0; j<nls; ++j)
68                         if(Object *link = i->object->get_link(j))
69                                 if(!object_set.count(link))
70                                         i->object->break_link(j);
71         }
72
73         const set<Track *> &ltracks = designer.get_layout().get_all<Track>();
74         for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
75         {
76                 bool ok = true;
77                 for(vector<MObject>::iterator j=objects.begin(); (j!=objects.end() && ok); ++j)
78                         ok = (j->object!=*i);
79                 if(!ok) continue;
80
81                 for(vector<MObject>::iterator j=objects.begin(); j!=objects.end(); ++j)
82                         j->object->link_to(**i);
83         }
84 }
85
86
87 Manipulator::MObject::MObject(Object *o):
88         object(o),
89         original_position(object->get_position()),
90         original_rotation(object->get_rotation())
91 { }