]> git.tdb.fi Git - r2c2.git/blob - source/designer/movetool.cpp
72bbc19ec3e8de206ccb64e3ce196e31709d7048
[r2c2.git] / source / designer / movetool.cpp
1 #include "libr2c2/layout.h"
2 #include "designer.h"
3 #include "movetool.h"
4
5 using namespace std;
6 using namespace Msp;
7 using namespace R2C2;
8
9 MoveTool::MoveTool(Designer &d, Input::Keyboard &k, Input::Mouse &m, const set<Object *> &o):
10         Manipulator(d, k, m, o),
11         origin(ground_pointer)
12 {
13         for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
14         {
15                 unsigned nls = i->object->get_n_link_slots();
16                 unsigned nsn = i->object->get_n_snap_nodes();
17                 unsigned j;
18                 for(j=0; j<nls; ++j)
19                         if(!o.count(i->object->get_link(j)))
20                                 boundaries.push_back(Boundary(&*i, j));
21                 for(; j<nsn; ++j)
22                         boundaries.push_back(Boundary(&*i, j));
23         }
24
25         snap_targets = designer.get_layout().get_all<Object>();
26         for(ObjectArray::const_iterator i=objects.begin(); i!=objects.end(); ++i)
27                 snap_targets.erase(i->object);
28 }
29
30 void MoveTool::pointer_motion()
31 {
32         Vector offset = center+ground_pointer-origin;
33         for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
34         {
35                 i->object->set_position(offset+i->original_position);
36                 i->object->set_rotation(i->original_rotation);
37         }
38
39         float limit = max(designer.get_layout().get_catalogue().get_gauge(),
40                 designer.get_camera_controller().get_view_scale()/100.0f);
41         MObject *snapped = 0;
42         for(list<Boundary>::iterator i=boundaries.begin(); (!snapped && i!=boundaries.end()); ++i)
43         {
44                 for(set<Object *>::const_iterator j=snap_targets.begin(); (!snapped && j!=snap_targets.end()); ++j)
45                         if((*i)->snap_to(**j, limit))
46                                 snapped = i->object;
47         }
48
49         if(snapped)
50         {
51                 Angle da = snapped->object->get_rotation()-snapped->original_rotation;
52                 Transform trans = Transform::rotation(da, Vector(0, 0, 1));
53                 const Vector &sp = snapped->object->get_position();
54                 for(ObjectArray::iterator i=objects.begin(); i!=objects.end(); ++i)
55                 {
56                         if(&*i==snapped)
57                                 continue;
58
59                         i->object->set_position(sp+trans.transform(i->original_position-snapped->original_position));
60                         i->object->set_rotation(i->original_rotation+da);
61                 }
62         }
63 }
64
65
66 MoveTool::Boundary::Boundary(MObject *o, unsigned i):
67         object(o),
68         index(i)
69 { }