/
selectionlib.h
225 lines (194 loc) · 4.65 KB
/
selectionlib.h
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
#pragma once
#include <stdexcept>
#include "iselection.h"
#include "ibrush.h"
#include "igroupnode.h"
#include "iscenegraph.h"
#include "ientity.h"
#include "ipatch.h"
#include "math/Vector3.h"
#include "math/AABB.h"
/** greebo: A structure containing information about the current
* Selection. An instance of this is maintained by the
* RadiantSelectionSystem, and a const reference can be
* retrieved via the according getSelectionInfo() method.
*/
class SelectionInfo {
public:
int totalCount; // number of selected items
int patchCount; // number of selected patches
int brushCount; // -- " -- brushes
int entityCount; // -- " -- entities
int componentCount; // -- " -- components (faces, edges, vertices)
SelectionInfo() :
totalCount(0),
patchCount(0),
brushCount(0),
entityCount(0),
componentCount(0)
{}
// Zeroes all the counters
void clear() {
totalCount = 0;
patchCount = 0;
brushCount = 0;
entityCount = 0;
componentCount = 0;
}
};
namespace selection
{
/**
* The selection "WorkZone" defines the bounds of the most
* recent selection. On each selection, the workzone is
* recalculated, nothing happens on deselection.
*/
struct WorkZone
{
// The corner points defining the selection workzone
Vector3 min;
Vector3 max;
// The bounds of the selection workzone (equivalent to min/max)
AABB bounds;
WorkZone() :
min(-64,-64,-64),
max(64,64,64),
bounds(AABB::createFromMinMax(min, max))
{}
};
namespace detail
{
class AmbiguousShaderException :
public std::runtime_error
{
public:
// Constructor
AmbiguousShaderException(const std::string& what) :
std::runtime_error(what)
{}
};
/**
* greebo: Helper objects that compares each object passed
* into it, throwing an AmbiguousShaderException as soon as
* at least two different non-empty shader names are found.
*/
class UniqueShaderFinder
{
// The string containing the result
std::string _shader;
public:
const std::string& getFoundShader() const
{
return _shader;
}
void checkFace(IFace& face)
{
checkShader(face.getShader());
}
void checkBrush(IBrush& brush)
{
for (std::size_t i = 0; i < brush.getNumFaces(); ++i)
{
checkFace(brush.getFace(i));
}
}
void checkPatch(IPatch& patch)
{
checkShader(patch.getShader());
}
private:
void checkShader(const std::string& foundShader)
{
if (foundShader.empty())
{
return;
}
if (_shader.empty())
{
// No shader encountered yet, take this one
_shader = foundShader;
}
else if (_shader != foundShader)
{
throw AmbiguousShaderException(foundShader);
}
// found shader is the same as _shader, all is well
}
};
}
/**
* greebo: Retrieves the shader name from the current selection.
* @returns: the name of the shader that is shared by every selected instance or
* the empty string "" otherwise.
*/
inline std::string getShaderFromSelection()
{
try
{
detail::UniqueShaderFinder finder;
if (GlobalSelectionSystem().countSelectedComponents() > 0)
{
// Check each selected face
GlobalSelectionSystem().foreachFace([&](IFace& face)
{
finder.checkFace(face);
});
}
else
{
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
if (Node_isBrush(node))
{
finder.checkBrush(*Node_getIBrush(node));
}
else if (Node_isPatch(node))
{
finder.checkPatch(*Node_getIPatch(node));
}
});
}
return finder.getFoundShader();
}
catch (const detail::AmbiguousShaderException&)
{
return std::string(); // return an empty string
}
}
/**
* Applies the given shader to the current selection.
* This doesn't create an UndoableCommand, this has to be done by the caller.
*/
inline void applyShaderToSelection(const std::string& shaderName)
{
GlobalSelectionSystem().foreachFace([&](IFace& face) { face.setShader(shaderName); });
GlobalSelectionSystem().foreachPatch([&](IPatch& patch) { patch.setShader(shaderName); });
SceneChangeNotify();
}
/**
* Tests the current selection and returns true if the selection is suitable
* for reparenting the selected primitives to the (last) selected entity.
*/
inline bool curSelectionIsSuitableForReparent()
{
// Retrieve the selection information structure
const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
if (info.totalCount <= 1 || info.entityCount != 1)
{
return false;
}
scene::INodePtr lastSelected = GlobalSelectionSystem().ultimateSelected();
Entity* entity = Node_getEntity(lastSelected);
// Reject non-entities or models
if (entity == nullptr || entity->isModel())
{
return false;
}
// Accept only group nodes as parent
if (!Node_getGroupNode(lastSelected))
{
return false;
}
return true;
}
} // namespace selection