1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreStableHeaders.h" |
---|
30 | |
---|
31 | #include "OgreOverlay.h" |
---|
32 | #include "OgreRoot.h" |
---|
33 | #include "OgreSceneManager.h" |
---|
34 | #include "OgreOverlayContainer.h" |
---|
35 | #include "OgreCamera.h" |
---|
36 | #include "OgreOverlayManager.h" |
---|
37 | #include "OgreQuaternion.h" |
---|
38 | #include "OgreVector3.h" |
---|
39 | |
---|
40 | |
---|
41 | namespace Ogre { |
---|
42 | |
---|
43 | //--------------------------------------------------------------------- |
---|
44 | Overlay::Overlay(const String& name) : |
---|
45 | mName(name), |
---|
46 | mRotate(0.0f), |
---|
47 | mScrollX(0.0f), mScrollY(0.0f), |
---|
48 | mScaleX(1.0f), mScaleY(1.0f), |
---|
49 | mTransformOutOfDate(true), mTransformUpdated(true), |
---|
50 | mZOrder(100), mVisible(false), mInitialised(false) |
---|
51 | |
---|
52 | { |
---|
53 | mRootNode = new SceneNode(NULL); |
---|
54 | |
---|
55 | } |
---|
56 | //--------------------------------------------------------------------- |
---|
57 | Overlay::~Overlay() |
---|
58 | { |
---|
59 | delete mRootNode; |
---|
60 | } |
---|
61 | //--------------------------------------------------------------------- |
---|
62 | const String& Overlay::getName(void) const |
---|
63 | { |
---|
64 | return mName; |
---|
65 | } |
---|
66 | //--------------------------------------------------------------------- |
---|
67 | void Overlay::setZOrder(ushort zorder) |
---|
68 | { |
---|
69 | // Limit to 650 since this is multiplied by 100 to pad out for containers |
---|
70 | assert (zorder <= 650 && "Overlay ZOrder cannot be greater than 650!"); |
---|
71 | |
---|
72 | mZOrder = zorder; |
---|
73 | |
---|
74 | // Notify attached 2D elements |
---|
75 | OverlayContainerList::iterator i, iend; |
---|
76 | iend = m2DElements.end(); |
---|
77 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
78 | { |
---|
79 | (*i)->_notifyZOrder(zorder * 100); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|
83 | //--------------------------------------------------------------------- |
---|
84 | ushort Overlay::getZOrder(void) const |
---|
85 | { |
---|
86 | return mZOrder; |
---|
87 | } |
---|
88 | //--------------------------------------------------------------------- |
---|
89 | bool Overlay::isVisible(void) const |
---|
90 | { |
---|
91 | return mVisible; |
---|
92 | } |
---|
93 | //--------------------------------------------------------------------- |
---|
94 | void Overlay::show(void) |
---|
95 | { |
---|
96 | mVisible = true; |
---|
97 | if (!mInitialised) |
---|
98 | { |
---|
99 | initialise(); |
---|
100 | } |
---|
101 | } |
---|
102 | //--------------------------------------------------------------------- |
---|
103 | void Overlay::hide(void) |
---|
104 | { |
---|
105 | mVisible = false; |
---|
106 | } |
---|
107 | //--------------------------------------------------------------------- |
---|
108 | void Overlay::initialise(void) |
---|
109 | { |
---|
110 | OverlayContainerList::iterator i, iend; |
---|
111 | iend = m2DElements.end(); |
---|
112 | for (i = m2DElements.begin(); i != m2DElements.end(); ++i) |
---|
113 | { |
---|
114 | (*i)->initialise(); |
---|
115 | } |
---|
116 | mInitialised = true; |
---|
117 | } |
---|
118 | //--------------------------------------------------------------------- |
---|
119 | void Overlay::add2D(OverlayContainer* cont) |
---|
120 | { |
---|
121 | m2DElements.push_back(cont); |
---|
122 | // Notify parent |
---|
123 | cont->_notifyParent(0, this); |
---|
124 | // Set Z order, scaled to separate overlays |
---|
125 | // NB max 100 container levels per overlay, should be plenty |
---|
126 | cont->_notifyZOrder(mZOrder * 100); |
---|
127 | |
---|
128 | Matrix4 xform; |
---|
129 | _getWorldTransforms(&xform); |
---|
130 | cont->_notifyWorldTransforms(xform); |
---|
131 | cont->_notifyViewport(); |
---|
132 | } |
---|
133 | //--------------------------------------------------------------------- |
---|
134 | void Overlay::remove2D(OverlayContainer* cont) |
---|
135 | { |
---|
136 | m2DElements.remove(cont); |
---|
137 | } |
---|
138 | //--------------------------------------------------------------------- |
---|
139 | void Overlay::add3D(SceneNode* node) |
---|
140 | { |
---|
141 | mRootNode->addChild(node); |
---|
142 | } |
---|
143 | //--------------------------------------------------------------------- |
---|
144 | void Overlay::remove3D(SceneNode* node) |
---|
145 | { |
---|
146 | mRootNode->removeChild(node->getName()); |
---|
147 | } |
---|
148 | //--------------------------------------------------------------------- |
---|
149 | void Overlay::clear(void) |
---|
150 | { |
---|
151 | mRootNode->removeAllChildren(); |
---|
152 | m2DElements.clear(); |
---|
153 | // Note no deallocation, memory handled by OverlayManager & SceneManager |
---|
154 | } |
---|
155 | //--------------------------------------------------------------------- |
---|
156 | void Overlay::setScroll(Real x, Real y) |
---|
157 | { |
---|
158 | mScrollX = x; |
---|
159 | mScrollY = y; |
---|
160 | mTransformOutOfDate = true; |
---|
161 | mTransformUpdated = true; |
---|
162 | } |
---|
163 | //--------------------------------------------------------------------- |
---|
164 | Real Overlay::getScrollX(void) const |
---|
165 | { |
---|
166 | return mScrollX; |
---|
167 | } |
---|
168 | //--------------------------------------------------------------------- |
---|
169 | Real Overlay::getScrollY(void) const |
---|
170 | { |
---|
171 | return mScrollY; |
---|
172 | } |
---|
173 | //--------------------------------------------------------------------- |
---|
174 | OverlayContainer* Overlay::getChild(const String& name) |
---|
175 | { |
---|
176 | |
---|
177 | OverlayContainerList::iterator i, iend; |
---|
178 | iend = m2DElements.end(); |
---|
179 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
180 | { |
---|
181 | if ((*i)->getName() == name) |
---|
182 | { |
---|
183 | return *i; |
---|
184 | |
---|
185 | } |
---|
186 | } |
---|
187 | return NULL; |
---|
188 | } |
---|
189 | //--------------------------------------------------------------------- |
---|
190 | void Overlay::scroll(Real xoff, Real yoff) |
---|
191 | { |
---|
192 | mScrollX += xoff; |
---|
193 | mScrollY += yoff; |
---|
194 | mTransformOutOfDate = true; |
---|
195 | mTransformUpdated = true; |
---|
196 | } |
---|
197 | //--------------------------------------------------------------------- |
---|
198 | void Overlay::setRotate(const Radian& angle) |
---|
199 | { |
---|
200 | mRotate = angle; |
---|
201 | mTransformOutOfDate = true; |
---|
202 | mTransformUpdated = true; |
---|
203 | } |
---|
204 | //--------------------------------------------------------------------- |
---|
205 | void Overlay::rotate(const Radian& angle) |
---|
206 | { |
---|
207 | setRotate(mRotate + angle); |
---|
208 | } |
---|
209 | //--------------------------------------------------------------------- |
---|
210 | void Overlay::setScale(Real x, Real y) |
---|
211 | { |
---|
212 | mScaleX = x; |
---|
213 | mScaleY = y; |
---|
214 | mTransformOutOfDate = true; |
---|
215 | mTransformUpdated = true; |
---|
216 | } |
---|
217 | //--------------------------------------------------------------------- |
---|
218 | Real Overlay::getScaleX(void) const |
---|
219 | { |
---|
220 | return mScaleX; |
---|
221 | } |
---|
222 | //--------------------------------------------------------------------- |
---|
223 | Real Overlay::getScaleY(void) const |
---|
224 | { |
---|
225 | return mScaleY; |
---|
226 | } |
---|
227 | //--------------------------------------------------------------------- |
---|
228 | void Overlay::_getWorldTransforms(Matrix4* xform) const |
---|
229 | { |
---|
230 | if (mTransformOutOfDate) |
---|
231 | { |
---|
232 | updateTransform(); |
---|
233 | } |
---|
234 | *xform = mTransform; |
---|
235 | |
---|
236 | } |
---|
237 | //----------------------------------------------------------------------- |
---|
238 | const Quaternion& Overlay::getWorldOrientation(void) const |
---|
239 | { |
---|
240 | // n/a |
---|
241 | return Quaternion::IDENTITY; |
---|
242 | } |
---|
243 | //----------------------------------------------------------------------- |
---|
244 | const Vector3& Overlay::getWorldPosition(void) const |
---|
245 | { |
---|
246 | // n/a |
---|
247 | return Vector3::ZERO; |
---|
248 | } |
---|
249 | //--------------------------------------------------------------------- |
---|
250 | void Overlay::_findVisibleObjects(Camera* cam, RenderQueue* queue) |
---|
251 | { |
---|
252 | OverlayContainerList::iterator i, iend; |
---|
253 | |
---|
254 | if (OverlayManager::getSingleton().hasViewportChanged()) |
---|
255 | { |
---|
256 | iend = m2DElements.end(); |
---|
257 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
258 | { |
---|
259 | (*i)->_notifyViewport(); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | // update elements |
---|
264 | if (mTransformUpdated) |
---|
265 | { |
---|
266 | OverlayContainerList::iterator i, iend; |
---|
267 | Matrix4 xform; |
---|
268 | |
---|
269 | _getWorldTransforms(&xform); |
---|
270 | iend = m2DElements.end(); |
---|
271 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
272 | { |
---|
273 | (*i)->_notifyWorldTransforms(xform); |
---|
274 | } |
---|
275 | |
---|
276 | mTransformUpdated = false; |
---|
277 | } |
---|
278 | |
---|
279 | if (mVisible) |
---|
280 | { |
---|
281 | // Add 3D elements |
---|
282 | mRootNode->setPosition(cam->getDerivedPosition()); |
---|
283 | mRootNode->setOrientation(cam->getDerivedOrientation()); |
---|
284 | mRootNode->_update(true, false); |
---|
285 | // Set up the default queue group for the objects about to be added |
---|
286 | uint8 oldgrp = queue->getDefaultQueueGroup(); |
---|
287 | ushort oldPriority = queue-> getDefaultRenderablePriority(); |
---|
288 | queue->setDefaultQueueGroup(RENDER_QUEUE_OVERLAY); |
---|
289 | queue->setDefaultRenderablePriority((mZOrder*100)-1); |
---|
290 | mRootNode->_findVisibleObjects(cam, queue, NULL, true, false); |
---|
291 | // Reset the group |
---|
292 | queue->setDefaultQueueGroup(oldgrp); |
---|
293 | queue->setDefaultRenderablePriority(oldPriority); |
---|
294 | // Add 2D elements |
---|
295 | iend = m2DElements.end(); |
---|
296 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
297 | { |
---|
298 | (*i)->_update(); |
---|
299 | |
---|
300 | (*i)->_updateRenderQueue(queue); |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | } |
---|
308 | //--------------------------------------------------------------------- |
---|
309 | void Overlay::updateTransform(void) const |
---|
310 | { |
---|
311 | // Ordering: |
---|
312 | // 1. Scale |
---|
313 | // 2. Rotate |
---|
314 | // 3. Translate |
---|
315 | |
---|
316 | Matrix3 rot3x3, scale3x3; |
---|
317 | rot3x3.FromEulerAnglesXYZ(Radian(0),Radian(0),mRotate); |
---|
318 | scale3x3 = Matrix3::ZERO; |
---|
319 | scale3x3[0][0] = mScaleX; |
---|
320 | scale3x3[1][1] = mScaleY; |
---|
321 | scale3x3[2][2] = 1.0f; |
---|
322 | |
---|
323 | mTransform = Matrix4::IDENTITY; |
---|
324 | mTransform = rot3x3 * scale3x3; |
---|
325 | mTransform.setTrans(Vector3(mScrollX, mScrollY, 0)); |
---|
326 | |
---|
327 | mTransformOutOfDate = false; |
---|
328 | } |
---|
329 | //--------------------------------------------------------------------- |
---|
330 | OverlayElement* Overlay::findElementAt(Real x, Real y) |
---|
331 | { |
---|
332 | OverlayElement* ret = NULL; |
---|
333 | int currZ = -1; |
---|
334 | OverlayContainerList::iterator i, iend; |
---|
335 | iend = m2DElements.end(); |
---|
336 | for (i = m2DElements.begin(); i != iend; ++i) |
---|
337 | { |
---|
338 | int z = (*i)->getZOrder(); |
---|
339 | if (z > currZ) |
---|
340 | { |
---|
341 | OverlayElement* elementFound = (*i)->findElementAt(x,y); |
---|
342 | if(elementFound) |
---|
343 | { |
---|
344 | currZ = elementFound->getZOrder(); |
---|
345 | ret = elementFound; |
---|
346 | } |
---|
347 | } |
---|
348 | } |
---|
349 | return ret; |
---|
350 | } |
---|
351 | |
---|
352 | } |
---|
353 | |
---|