-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
hand.cpp
307 lines (250 loc) · 7.38 KB
/
hand.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#if 0
/** @file hand.cpp Hand (metaphor) for the manipulation of "grabbables".
*
* @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_base.h"
#include "world/hand.h"
#include "BiasSource" // remove me
using namespace de;
enum PropertyFlag
{
Color = 0x1,
Intensity = 0x2
};
Q_DECLARE_FLAGS(PropertyFlags, PropertyFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(PropertyFlags)
namespace world {
DENG2_PIMPL(Hand)
, DENG2_OBSERVES(Grabbable, Deletion)
, DENG2_OBSERVES(Grabbable, OriginChange)
{
/// Origin of the hand in the map coordinate space.
Vector3d origin;
Vector3d oldOrigin; // For tracking changes.
/// All currently held grabbables if any (not owned).
Grab grab;
/// Averaged origin of eveything currently grabbed.
Vector3d grabOrigin;
bool needUpdateGrabOrigin = false;
/// Edit properties (applied to the grabbables).
Vector3f editColor;
dfloat editIntensity = 0;
PropertyFlags applyProps = 0;
Impl(Public *i) : Base(i)
{}
void notifyGrabbed(Grabbable &grabbed)
{
DENG2_FOR_PUBLIC_AUDIENCE(Grabbed, i)
{
i->handGrabbed(self(), grabbed);
}
}
void notifyUngrabbed(Grabbable &ungrabbed)
{
DENG2_FOR_PUBLIC_AUDIENCE(Ungrabbed, i)
{
i->handUngrabbed(self(), ungrabbed);
}
}
void grabOne(Grabbable &grabbable)
{
// Ignore attempts to re-grab.
if(grab.contains(&grabbable)) return;
grabbable.grab();
// Ensure the grabbable is locked.
grabbable.lock();
grab.append(&grabbable);
grabbable.audienceForDeletion += this;
grabbable.audienceForOriginChange += this;
needUpdateGrabOrigin = true;
notifyGrabbed(grabbable);
}
void ungrabOne(Grabbable &grabbable)
{
// Ignore attempts to ungrab what isn't grabbed.
if(!grab.contains(&grabbable)) return;
grabbable.ungrab();
// Ensure the grabbable is unlocked.
grabbable.unlock();
grabbable.audienceForDeletion -= this;
grabbable.audienceForOriginChange -= this;
grab.removeOne(&grabbable);
needUpdateGrabOrigin = true;
notifyUngrabbed(grabbable);
}
// Observes Grabbable Deletion.
void grabbableBeingDeleted(Grabbable &grabbable)
{
DENG2_ASSERT(grab.contains(&grabbable)); //sanity check.
grab.removeOne(&grabbable);
needUpdateGrabOrigin = true;
notifyUngrabbed(grabbable);
}
// Observes Grabbable OriginChange.
void grabbableOriginChanged(Grabbable &grabbable)
{
DENG_UNUSED(grabbable);
DENG2_ASSERT(grab.contains(&grabbable)); //sanity check.
needUpdateGrabOrigin = true;
}
void updateGrabOrigin()
{
needUpdateGrabOrigin = false;
grabOrigin = Vector3d();
for(Grabbable *grabbable : grab)
{
grabOrigin += grabbable->origin();
}
if(grab.count() > 1)
grabOrigin /= grab.count();
//qDebug() << "Hand new grab origin" << grabOrigin.asText();
}
};
Hand::Hand(Vector3d const &origin) : d(new Impl(this))
{
d->oldOrigin = d->origin = origin;
}
Vector3d const &Hand::origin() const
{
return d->origin;
}
void Hand::setOrigin(Vector3d const &newOrigin)
{
if(d->origin != newOrigin)
{
//qDebug() << "Hand new origin" << newOrigin.asText();
d->origin = newOrigin;
}
}
bool Hand::isEmpty() const
{
return grabbed().isEmpty();
}
bool Hand::hasGrabbed(Grabbable const &grabbable) const
{
return grabbed().contains(const_cast<Grabbable *>(&grabbable));
}
void Hand::grab(Grabbable &grabbable)
{
if(hasGrabbed(grabbable)) return;
ungrab();
d->grabOne(grabbable);
}
void Hand::grabMulti(Grabbable &grabbable)
{
d->grabOne(grabbable);
}
void Hand::ungrab(Grabbable &grabbable)
{
d->ungrabOne(grabbable);
}
void Hand::ungrab()
{
//qDebug() << "Hand ungrabbing all";
while(!isEmpty())
{
ungrab(*grabbed().last());
}
}
Hand::Grab const &Hand::grabbed() const
{
return d->grab;
}
de::Vector3d const &Hand::grabbedOrigin() const
{
if(d->needUpdateGrabOrigin)
{
d->updateGrabOrigin();
}
return d->grabOrigin;
}
float Hand::editIntensity() const
{
return d->editIntensity;
}
void Hand::setEditIntensity(float newIntensity)
{
d->editIntensity = newIntensity;
d->applyProps |= Intensity;
}
void Hand::setEditColor(Vector3f const &newColor)
{
d->editColor = newColor;
d->applyProps |= Color;
}
Vector3f const &Hand::editColor() const
{
return d->editColor;
}
void Hand::worldSystemFrameEnds()
{
if(grabbedCount())
{
// Do we need to move the grab?
bool moveGrab = (d->origin != d->oldOrigin);
if(moveGrab && d->needUpdateGrabOrigin)
{
// Determine the center of the grab.
d->updateGrabOrigin();
}
foreach(Grabbable *grabbable, grabbed())
{
if(moveGrab)
{
// The move will be denied if the grabbable is locked.
if(grabbedCount() == 1)
{
grabbable->move(d->origin);
}
else
{
grabbable->move(d->origin + (grabbable->origin() - d->grabOrigin));
}
}
#if 0
if(!!d->applyProps)
{
/// @todo There should be a generic mechanism for applying the user's
/// edits to the grabbables. The editable values are properties of the
/// hand (an extension of the user's will) however the hand should not
/// be responsible for their application as this requires knowledge of
/// their meaning. Instead the hand should provide the values and let
/// the grabbable(s) update themselves.
if(!internal::cannotCastGrabbableTo<BiasSource>(grabbable))
{
BiasSource &biasSource = grabbable->as<BiasSource>();
if(d->applyProps & Color)
{
biasSource.setColor(d->editColor);
}
if(d->applyProps & Intensity)
{
biasSource.setIntensity(d->editIntensity);
}
}
}
#endif
}
}
// Any property changes will have now been applied to the grabbed elements.
d->applyProps = 0;
// Update the hand origin tracking buffer.
d->oldOrigin = d->origin;
}
} // namespace world
#endif