File: TranslateManipulator.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (263 lines) | stat: -rw-r--r-- 8,135 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "TranslateManipulator.h"

#include "selection/SelectionPool.h"
#include "selection/BestPoint.h"

namespace selection
{

constexpr const char* const RKEY_TRANSLATE_CONSTRAINED = "user/ui/xyview/translateConstrained";

TranslateManipulator::TranslateManipulator(ManipulationPivot& pivot, std::size_t segments, float length) :
	_pivot(pivot),
	_translator(std::bind(&ManipulationPivot::applyTranslation, &_pivot, std::placeholders::_1)),
    _translateFree(_translator),
    _translateAxis(_translator),
    _arrowX({ length,0,0 }, _pivot2World._worldSpace),
    _arrowY({ 0,length,0 }, _pivot2World._worldSpace),
    _arrowZ({ 0,0,length }, _pivot2World._worldSpace),
    _arrowHeadX({ length,0,0 }, _pivot2World._axisScreen, length / 8, length / 3, _pivot2World._worldSpace),
    _arrowHeadY({ 0,length,0 }, _pivot2World._axisScreen, length / 8, length / 3, _pivot2World._worldSpace),
    _arrowHeadZ({ 0,0,length }, _pivot2World._axisScreen, length / 8, length / 3, _pivot2World._worldSpace),
    _quadScreen(16, _pivot2World._viewplaneSpace),
    _translateConstrained(RKEY_TRANSLATE_CONSTRAINED)
{}

void TranslateManipulator::updateColours()
{
    _arrowX.setColour(colourSelected(COLOUR_X(), _selectableX.isSelected()));
    _arrowHeadX.setColour(colourSelected(COLOUR_X(), _selectableX.isSelected()));
    _arrowY.setColour(colourSelected(COLOUR_Y(), _selectableY.isSelected()));
    _arrowHeadY.setColour(colourSelected(COLOUR_Y(), _selectableY.isSelected()));
    _arrowZ.setColour(colourSelected(COLOUR_Z(), _selectableZ.isSelected()));
    _arrowHeadZ.setColour(colourSelected(COLOUR_Z(), _selectableZ.isSelected()));
    _quadScreen.setColour(colourSelected(COLOUR_SCREEN(), _selectableScreen.isSelected()));
}

bool TranslateManipulator::axisIsVisible(const Vector3& axis) const
{
    return fabs(_pivot2World._axisScreen.dot(axis)) < 0.95;
}

void TranslateManipulator::onPreRender(const RenderSystemPtr& renderSystem, const VolumeTest& volume)
{
    if (!renderSystem)
    {
        clearRenderables();
        return;
    }

    if (!_lineShader)
    {
        _lineShader = renderSystem->capture(BuiltInShaderType::ManipulatorWireframe);
    }

    if (!_arrowHeadShader)
    {
        _arrowHeadShader = renderSystem->capture(BuiltInShaderType::FlatshadeOverlay);
    }

    _pivot2World.update(_pivot.getMatrix4(), volume.GetModelview(), volume.GetProjection(), volume.GetViewport());

    updateColours();

    auto x = _pivot2World._worldSpace.xCol3().getNormalised();
    auto y = _pivot2World._worldSpace.yCol3().getNormalised();
    auto z = _pivot2World._worldSpace.zCol3().getNormalised();

    if (axisIsVisible(x))
    {
        _arrowX.update(_lineShader);
        _arrowHeadX.update(_arrowHeadShader);
    }
    else
    {
        _arrowX.clear();
        _arrowHeadX.clear();
    }

    if (axisIsVisible(y))
    {
        _arrowY.update(_lineShader);
        _arrowHeadY.update(_arrowHeadShader);
    }
    else
    {
        _arrowY.clear();
        _arrowHeadY.clear();
    }

    if (axisIsVisible(z))
    {
        _arrowZ.update(_lineShader);
        _arrowHeadZ.update(_arrowHeadShader);
    }
    else
    {
        _arrowZ.clear();
        _arrowHeadZ.clear();
    }

    _quadScreen.update(_lineShader);
}

void TranslateManipulator::clearRenderables()
{
    _arrowX.clear();
    _arrowY.clear();
    _arrowZ.clear();
    _arrowHeadX.clear();
    _arrowHeadY.clear();
    _arrowHeadZ.clear();
    _quadScreen.clear();

    _lineShader.reset();
    _arrowHeadShader.reset();
}

void TranslateManipulator::testSelect(SelectionTest& test, const Matrix4& pivot2world)
{
    _pivot2World.update(_pivot.getMatrix4(), test.getVolume().GetModelview(),
        test.getVolume().GetProjection(), test.getVolume().GetViewport());

    SelectionPool selector;

    auto x = _pivot2World._worldSpace.xCol3().getNormalised();
    auto y = _pivot2World._worldSpace.yCol3().getNormalised();
    auto z = _pivot2World._worldSpace.zCol3().getNormalised();

    bool show_x = axisIsVisible(x);
    bool show_y = axisIsVisible(y);
    bool show_z = axisIsVisible(z);

    {
		Matrix4 local2view(test.getVolume().GetViewProjection().getMultipliedBy(_pivot2World._viewpointSpace));

      {
        SelectionIntersection best;
        Quad_BestPoint(local2view, eClipCullCW, &_quadScreen.getRawPoints().front(), best);
        if(best.isValid())
        {
          best = SelectionIntersection(0, 0);
          selector.addSelectable(best, &_selectableScreen);
        }
      }
    }

    {
		Matrix4 local2view(test.getVolume().GetViewProjection().getMultipliedBy(_pivot2World._worldSpace));

      if(show_x)
      {
        SelectionIntersection best;
        Line_BestPoint(local2view, &_arrowX.getRawPoints().front(), best);
        Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadX.getRawPoints().front(), &*(_arrowHeadX.getRawPoints().end()-1)+1, best);
        selector.addSelectable(best, &_selectableX);
      }

      if(show_y)
      {
        SelectionIntersection best;
        Line_BestPoint(local2view, &_arrowY.getRawPoints().front(), best);
        Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadY.getRawPoints().front(), &*(_arrowHeadY.getRawPoints().end()-1)+1, best);
        selector.addSelectable(best, &_selectableY);
      }

      if(show_z)
      {
        SelectionIntersection best;
        Line_BestPoint(local2view, &_arrowZ.getRawPoints().front(), best);
        Triangles_BestPoint(local2view, eClipCullCW, &_arrowHeadZ.getRawPoints().front(), &*(_arrowHeadZ.getRawPoints().end()-1)+1, best);
        selector.addSelectable(best, &_selectableZ);
      }
    }

	// greebo: If any of the above arrows could be selected, select the first in the SelectionPool
    if(!selector.empty())
    {
        selector.begin()->second->setSelected(true);
    }
    else
    {
    	ISelectable* selectable = nullptr;

    	if (_translateConstrained.get())
        {
	    	// None of the shown arrows (or quad) has been selected, select an axis based on the precedence
	    	Matrix4 local2view(test.getVolume().GetViewProjection().getMultipliedBy(_pivot2World._worldSpace));

	    	// Get the (relative?) distance from the mouse pointer to the manipulator
	    	Vector3 delta = local2view.tCol().getProjected();

	    	// Get the precedence (which axis has the absolute largest value in it)
	    	bool xGreaterY = (fabs(delta.x()) > fabs(delta.y()));

	    	// The precedence has to be interpreted according to which axes are visible
	    	if (show_z) {
	    		// Either XZ or YZ
	    		if (show_y) {
	    			// YZ
	    			selectable = (xGreaterY) ? &_selectableY : &_selectableZ;
	    		}
	    		else {
	    			// XZ
	    			selectable = (xGreaterY) ? &_selectableX : &_selectableZ;
	    		}
	    	}
	    	else {
	    		// XY
	    		selectable = (xGreaterY) ? &_selectableX : &_selectableY;
	    	}
    	}
    	else {
    		// Don't constrain to axis, choose the freemove translator
    		selectable = &_selectableScreen;
    	}

		// If everything went ok, there is a selectable available, add it
    	if (selectable)
        {
    		selector.addSelectable(SelectionIntersection(0,0), selectable);
    		selectable->setSelected(true);
    	}
    }
}

TranslateManipulator::Component* TranslateManipulator::getActiveComponent()
{
    if(_selectableX.isSelected())
    {
      _translateAxis.SetAxis(g_vector3_axis_x);
      return &_translateAxis;
    }
    else if(_selectableY.isSelected())
    {
      _translateAxis.SetAxis(g_vector3_axis_y);
      return &_translateAxis;
    }
    else if(_selectableZ.isSelected())
    {
      _translateAxis.SetAxis(g_vector3_axis_z);
      return &_translateAxis;
    }
    else
    {
      return &_translateFree;
    }
}

void TranslateManipulator::setSelected(bool select)
{
    _selectableX.setSelected(select);
    _selectableY.setSelected(select);
    _selectableZ.setSelected(select);
    _selectableScreen.setSelected(select);
}

bool TranslateManipulator::isSelected() const
{
    return _selectableX.isSelected() || _selectableY.isSelected()
        || _selectableZ.isSelected() || _selectableScreen.isSelected();
}

}