-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui3DProjectionCtrl.cpp
297 lines (247 loc) · 8.6 KB
/
gui3DProjectionCtrl.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
//-----------------------------------------------------------------------------
// Gui3DProjectionCtrl
// Doppelganger Inc
// Orion Elenzil 200701
//
// This control is meant to be merely a container for other controls.
// What's neat is that it's easy to 'attach' this control to a point in world-space
// or, more interestingly, to an object such as a player.
//
// Usage:
// * Create the Gui3DProjectionControl - by default it will be at 0, 0, 0.
// * You can change where it's located by setting the field "offsetWorld".
// - note you can specify that right in the .gui file
// * You can attach it to any SceneObject by calling "setAttachedTo()".
//
// Behaviour:
// * If you're attaching it to a player, by default it will center on the player's head.
// * If you attach it to an object, by default it will delete itself if the object is deleted.
// * Doesn't occlude w/r/t 3D objects.
//
// Console Methods:
// * SetAttachedTo(SceneObject)
// * GetAttachedTo()
//
// Params:
// * pointWorld - read/write point in worldspace. read-only if attached to an object.
// * offsetObject - an offset in objectspace. default 0, 0, 0.
// * offsetWorld - an offset in worldspace. default 0, 0, 0.
// * offsetScreen - an offset in screenspace. default 0, 0.
// * hAlign - horizontal alignment. 0 = left, 1 = center, 2 = right. default center.
// * vAlign - vertical alignment. 0 = top, 1 = center, 2 = bottomt. default center.
// * useEyePoint - H & V usage of the eyePoint, if player object. default 0, 1. (ie - use only the vertical component)
// * autoDelete - self-delete when attachedTo object is deleted. default true.
//
// Todo:
// * occlusion - hide the control when its anchor point is occluded.
// * integrate w/ zbuffer - this would actually be a change to the whole GuiControl system.
// * allow attaching to arbitrary nodes in a skeleton.
// * avoid projection when the object is out of the frustum.
//
// oxe 20070111
//-----------------------------------------------------------------------------
#include "console/console.h"
#include "console/consoleTypes.h"
#include "scene/SceneObject.h"
#include "T3D/player.h"
#include "gui/controls/Gui3DProjectionCtrl.h"
IMPLEMENT_CONOBJECT(Gui3DProjectionCtrl);
//-----------------------------------------------------------------------------
Gui3DProjectionCtrl::Gui3DProjectionCtrl()
{
mTSCtrl = NULL;
mAttachedTo = NULL;
mAttachedToPlayer = NULL;
mAutoDelete = true;
mHAlign = center;
mVAlign = center;
mUseEyePoint.x = 0;
mUseEyePoint.y = 1;
mPtWorld .set(0, 0, 0);
mPtProj .set(0, 0);
mOffsetObject.set(0, 0, 0);
mOffsetWorld .set(0, 0, 0);
mOffsetScreen.set(0, 0);
}
void Gui3DProjectionCtrl::initPersistFields()
{
Parent::initPersistFields();
addGroup("3DProjection");
addField("pointWorld" , TypePoint3F , Offset(mPtWorld , Gui3DProjectionCtrl));
addField("offsetObject" , TypePoint3F , Offset(mOffsetObject , Gui3DProjectionCtrl));
addField("offsetWorld" , TypePoint3F , Offset(mOffsetWorld , Gui3DProjectionCtrl));
addField("offsetScreen" , TypePoint2I , Offset(mOffsetScreen , Gui3DProjectionCtrl));
addField("hAlign" , TypeS32 , Offset(mHAlign , Gui3DProjectionCtrl));
addField("vAlign" , TypeS32 , Offset(mVAlign , Gui3DProjectionCtrl));
addField("useEyePoint" , TypePoint2I , Offset(mUseEyePoint , Gui3DProjectionCtrl));
addField("autoDelete" , TypeBool , Offset(mAutoDelete , Gui3DProjectionCtrl));
endGroup("3DProjection");
}
void Gui3DProjectionCtrl::onRender(Point2I offset, const RectI &updateRect)
{
doPositioning();
doProjection();
doAlignment();
Parent::onRender(offset, updateRect);
}
void Gui3DProjectionCtrl::resizeDuringRender()
{
doPositioning();
doProjection ();
doAlignment ();
}
bool Gui3DProjectionCtrl::onWake()
{
// walk up the GUI tree until we find a GuiTSCtrl.
mTSCtrl = NULL;
GuiControl* walkCtrl = getParent();
AssertFatal(walkCtrl != NULL, "Gui3DProjectionCtrl::onWake() - NULL parent");
bool doMore = true;
while (doMore)
{
mTSCtrl = dynamic_cast<GuiTSCtrl*>(walkCtrl);
walkCtrl = walkCtrl->getParent();
doMore = (mTSCtrl == NULL) && (walkCtrl != NULL);
}
if (!mTSCtrl)
Con::errorf("Gui3DProjectionCtrl::onWake() - no TSCtrl parent");
return Parent::onWake();
}
void Gui3DProjectionCtrl::onSleep()
{
mTSCtrl = NULL;
return Parent::onSleep();
}
void Gui3DProjectionCtrl::onDeleteNotify(SimObject* obj)
{
// - SimSet assumes that obj is a member of THIS, which in our case ain't true.
// oxe 20070116 - the following doesn't compile on GCC.
// SimSet::Parent::onDeleteNotify(obj);
if (!obj)
{
Con::warnf("Gui3DProjectionCtrl::onDeleteNotify - got NULL");
return;
}
if (obj != mAttachedTo)
{
if (mAttachedTo != NULL)
Con::warnf("Gui3DProjectionCtrl::onDeleteNotify - got unexpected object: %d vs. %d", obj->getId(), mAttachedTo->getId());
return;
}
if (mAutoDelete)
this->deleteObject();
}
//-----------------------------------------------------------------------------
void Gui3DProjectionCtrl::doPositioning()
{
if (mAttachedTo == NULL)
return;
Point3F ptBase; // the regular position of the object.
Point3F ptEye; // the render position of the eye node, if a player object.
Point3F pt; // combination of ptBase and ptEye.
MatrixF mat; // utility
mAttachedTo->getRenderTransform().getColumn(3, &ptBase);
if (mAttachedToPlayer != NULL)
{
mAttachedToPlayer->getRenderEyeTransform(&mat);
mat.getColumn(3, &ptEye);
}
else
{
ptEye = ptBase;
}
// use some components from ptEye but other position from ptBase
pt = ptBase;
if (mUseEyePoint.x != 0)
{
pt.x = ptEye.x;
pt.y = ptEye.y;
}
if (mUseEyePoint.y != 0)
{
pt.z = ptEye.z;
}
// object-space offset
Point3F offsetObj;
QuatF quat(mAttachedTo->getRenderTransform());
quat.mulP(mOffsetObject, &offsetObj);
pt += offsetObj;
// world-space offset
pt += mOffsetWorld;
mPtWorld = pt;
}
void Gui3DProjectionCtrl::doProjection()
{
if (!mTSCtrl)
return;
Point3F pt;
if (!mTSCtrl->project(mPtWorld, &pt))
return;
mPtProj.x = (S32)(pt.x + 0.5f);
mPtProj.y = (S32)(pt.y + 0.5f);
}
void Gui3DProjectionCtrl::doAlignment()
{
// alignment
Point2I offsetAlign;
switch(mHAlign)
{
default:
case center:
offsetAlign.x = -getBounds().extent.x / 2;
break;
case min:
offsetAlign.x = 0;
break;
case max:
offsetAlign.x = -getBounds().extent.x;
break;
}
switch(mVAlign)
{
default:
case center:
offsetAlign.y = -getBounds().extent.y / 2;
break;
case min:
offsetAlign.y = 0;
break;
case max:
offsetAlign.y = -getBounds().extent.y;
break;
}
// projected point
mPtScreen = mPtProj;
// alignment offset
mPtScreen += offsetAlign;
// screen offset
mPtScreen += mOffsetScreen;
// setTrgPosition(mPtScreen);
RectI bounds = getBounds();
bounds.point = mPtScreen;
setBounds(bounds);
}
//-----------------------------------------------------------------------------
void Gui3DProjectionCtrl::setAttachedTo(SceneObject* obj)
{
if (obj == mAttachedTo)
return;
if (mAttachedTo)
clearNotify(mAttachedTo);
mAttachedTo = obj;
mAttachedToPlayer = dynamic_cast<Player*>(obj);
if (mAttachedTo)
deleteNotify(mAttachedTo);
}
ConsoleMethod(Gui3DProjectionCtrl, setAttachedTo, void, 3, 3, "(object)")
{
object->setAttachedTo(dynamic_cast<SceneObject*>(Sim::findObject(dAtoi(argv[2]))));
}
ConsoleMethod(Gui3DProjectionCtrl, getAttachedTo, S32, 2, 2, "()")
{
SceneObject* obj = object->getAttachedTo();
if (!obj)
return 0;
else
return obj->getId();
}