/
TextureToolDragManipulator.cpp
152 lines (124 loc) · 4.2 KB
/
TextureToolDragManipulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "TextureToolDragManipulator.h"
#include "itexturetoolmodel.h"
#include "math/Matrix3.h"
#include "pivot.h"
#include "igrid.h"
#include "selection/SelectionPool.h"
namespace textool
{
void TextureTranslator::beginTransformation(const Matrix4& pivot2world,
const VolumeTest& view, const Vector2& devicePoint)
{
auto device2Pivot = constructDevice2Pivot(pivot2world, view);
auto pivotPoint = device2Pivot.transformPoint(Vector3(devicePoint.x(), devicePoint.y(), 0));
_start = Vector2(pivotPoint.x(), pivotPoint.y());
}
void TextureTranslator::transform(const Matrix4& pivot2world, const VolumeTest& view,
const Vector2& devicePoint, unsigned int constraintFlags)
{
auto device2Pivot = constructDevice2Pivot(pivot2world, view);
auto current3D = device2Pivot.transformPoint(Vector3(devicePoint.x(), devicePoint.y(), 0));
Vector2 current(current3D.x(), current3D.y());
auto diff = current - _start;
if (constraintFlags & Constraint::Type1)
{
// Locate the index of the component carrying the largest abs value
// Zero out the other component
diff[fabs(diff.y()) > fabs(diff.x()) ? 0 : 1] = 0;
}
// Snap to grid if the constraint flag is set
if (constraintFlags & Constraint::Grid)
{
auto gridSize = GlobalGrid().getGridSize(grid::Space::Texture);
diff.x() = float_snapped(diff.x(), gridSize);
diff.y() = float_snapped(diff.y(), gridSize);
}
_translateFunctor(diff);
}
TextureToolDragManipulator::TextureToolDragManipulator() :
_translator(std::bind(&TextureToolDragManipulator::translateSelected, this, std::placeholders::_1))
{}
std::size_t TextureToolDragManipulator::getId() const
{
return _id;
}
void TextureToolDragManipulator::setId(std::size_t id)
{
_id = id;
}
selection::IManipulator::Type TextureToolDragManipulator::getType() const
{
return IManipulator::Drag;
}
selection::IManipulator::Component* TextureToolDragManipulator::getActiveComponent()
{
return &_translator;
}
void TextureToolDragManipulator::setSelected(bool select)
{
_selectable.setSelected(select);
}
bool TextureToolDragManipulator::isSelected() const
{
return _selectable.isSelected();
}
void TextureToolDragManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2world)
{
selection::SelectionPool selectionPool;
auto selectionMode = GlobalTextureToolSelectionSystem().getSelectionMode();
GlobalTextureToolSceneGraph().foreachNode([&](const INode::Ptr& node)
{
if (selectionMode == SelectionMode::Surface)
{
node->testSelect(selectionPool, test);
}
else
{
auto componentSelectable = std::dynamic_pointer_cast<IComponentSelectable>(node);
if (componentSelectable)
{
componentSelectable->testSelectComponents(selectionPool, test);
}
}
return true;
});
_selectable.setSelected(false);
// The drag manipulator returns positive if we our test hit a selected node
for (const auto& pair : selectionPool)
{
if (pair.second->isSelected())
{
_selectable.setSelected(true);
break;
}
}
}
void TextureToolDragManipulator::renderComponents(const render::IRenderView& view, const Matrix4& pivot2World)
{}
void TextureToolDragManipulator::translateSelected(const Vector2& translation)
{
auto transform = Matrix3::getTranslation(translation);
if (GlobalTextureToolSelectionSystem().getSelectionMode() == SelectionMode::Surface)
{
GlobalTextureToolSelectionSystem().foreachSelectedNode([&](const textool::INode::Ptr& node)
{
node->revertTransformation();
node->transform(transform);
return true;
});
}
else
{
GlobalTextureToolSelectionSystem().foreachSelectedComponentNode([&](const INode::Ptr& node)
{
node->revertTransformation();
auto componentTransformable = std::dynamic_pointer_cast<IComponentTransformable>(node);
if (componentTransformable)
{
componentTransformable->transformComponents(transform);
}
return true;
});
}
}
}