/
TextureToolDragManipulator.cpp
110 lines (88 loc) · 2.9 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
#include "TextureToolDragManipulator.h"
#include "itexturetoolmodel.h"
#include "math/Matrix3.h"
#include "pivot.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;
}
_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;
GlobalTextureToolSceneGraph().foreachNode([&](const INode::Ptr& node)
{
node->testSelect(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 Matrix4& pivot2World)
{}
void TextureToolDragManipulator::translateSelected(const Vector2& translation)
{
auto transform = Matrix3::getTranslation(translation);
GlobalTextureToolSelectionSystem().foreachSelectedNode([&](const textool::INode::Ptr& node)
{
node->revertTransformation();
node->applyTransformToSelected(transform);
return true;
});
}
}