[692] | 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-2005 The OGRE Team
|
---|
| 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 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 |
|
---|
| 27 | #include "OgreOverlay.h"
|
---|
| 28 | #include "OgreRoot.h"
|
---|
| 29 | #include "OgreSceneManager.h"
|
---|
| 30 | #include "OgreOverlayContainer.h"
|
---|
| 31 | #include "OgreCamera.h"
|
---|
| 32 | #include "OgreOverlayManager.h"
|
---|
| 33 | #include "OgreQuaternion.h"
|
---|
| 34 | #include "OgreVector3.h"
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | namespace Ogre {
|
---|
| 38 |
|
---|
| 39 | //---------------------------------------------------------------------
|
---|
| 40 | Overlay::Overlay(const String& name) :
|
---|
| 41 | mName(name),
|
---|
| 42 | mRotate(0.0f),
|
---|
| 43 | mScrollX(0.0f), mScrollY(0.0f),
|
---|
| 44 | mScaleX(1.0f), mScaleY(1.0f),
|
---|
| 45 | mTransformOutOfDate(true), mTransformUpdated(true),
|
---|
| 46 | mZOrder(100), mVisible(false), mInitialised(false)
|
---|
| 47 |
|
---|
| 48 | {
|
---|
| 49 | mRootNode = new SceneNode(NULL);
|
---|
| 50 |
|
---|
| 51 | }
|
---|
| 52 | //---------------------------------------------------------------------
|
---|
| 53 | Overlay::~Overlay()
|
---|
| 54 | {
|
---|
| 55 | delete mRootNode;
|
---|
| 56 | }
|
---|
| 57 | //---------------------------------------------------------------------
|
---|
| 58 | const String& Overlay::getName(void) const
|
---|
| 59 | {
|
---|
| 60 | return mName;
|
---|
| 61 | }
|
---|
| 62 | //---------------------------------------------------------------------
|
---|
| 63 | void Overlay::setZOrder(ushort zorder)
|
---|
| 64 | {
|
---|
| 65 | // Limit to 650 since this is multiplied by 100 to pad out for containers
|
---|
| 66 | assert (zorder <= 650 && "Overlay ZOrder cannot be greater than 650!");
|
---|
| 67 |
|
---|
| 68 | mZOrder = zorder;
|
---|
| 69 |
|
---|
| 70 | // Notify attached 2D elements
|
---|
| 71 | OverlayContainerList::iterator i, iend;
|
---|
| 72 | iend = m2DElements.end();
|
---|
| 73 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 74 | {
|
---|
| 75 | (*i)->_notifyZOrder(zorder);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 | //---------------------------------------------------------------------
|
---|
| 80 | ushort Overlay::getZOrder(void) const
|
---|
| 81 | {
|
---|
| 82 | return mZOrder;
|
---|
| 83 | }
|
---|
| 84 | //---------------------------------------------------------------------
|
---|
| 85 | bool Overlay::isVisible(void) const
|
---|
| 86 | {
|
---|
| 87 | return mVisible;
|
---|
| 88 | }
|
---|
| 89 | //---------------------------------------------------------------------
|
---|
| 90 | void Overlay::show(void)
|
---|
| 91 | {
|
---|
| 92 | mVisible = true;
|
---|
| 93 | if (!mInitialised)
|
---|
| 94 | {
|
---|
| 95 | initialise();
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | //---------------------------------------------------------------------
|
---|
| 99 | void Overlay::hide(void)
|
---|
| 100 | {
|
---|
| 101 | mVisible = false;
|
---|
| 102 | }
|
---|
| 103 | //---------------------------------------------------------------------
|
---|
| 104 | void Overlay::initialise(void)
|
---|
| 105 | {
|
---|
| 106 | OverlayContainerList::iterator i, iend;
|
---|
| 107 | iend = m2DElements.end();
|
---|
| 108 | for (i = m2DElements.begin(); i != m2DElements.end(); ++i)
|
---|
| 109 | {
|
---|
| 110 | (*i)->initialise();
|
---|
| 111 | }
|
---|
| 112 | mInitialised = true;
|
---|
| 113 | }
|
---|
| 114 | //---------------------------------------------------------------------
|
---|
| 115 | void Overlay::add2D(OverlayContainer* cont)
|
---|
| 116 | {
|
---|
| 117 | m2DElements.push_back(cont);
|
---|
| 118 | // Notify parent
|
---|
| 119 | cont->_notifyParent(0, this);
|
---|
| 120 | // Set Z order, scaled to separate overlays
|
---|
| 121 | // NB max 100 container levels per overlay, should be plenty
|
---|
| 122 | cont->_notifyZOrder(mZOrder * 100);
|
---|
| 123 |
|
---|
| 124 | Matrix4 xform;
|
---|
| 125 | _getWorldTransforms(&xform);
|
---|
| 126 | cont->_notifyWorldTransforms(xform);
|
---|
| 127 | cont->_notifyViewport();
|
---|
| 128 | }
|
---|
| 129 | //---------------------------------------------------------------------
|
---|
| 130 | void Overlay::remove2D(OverlayContainer* cont)
|
---|
| 131 | {
|
---|
| 132 | m2DElements.remove(cont);
|
---|
| 133 | }
|
---|
| 134 | //---------------------------------------------------------------------
|
---|
| 135 | void Overlay::add3D(SceneNode* node)
|
---|
| 136 | {
|
---|
| 137 | mRootNode->addChild(node);
|
---|
| 138 | }
|
---|
| 139 | //---------------------------------------------------------------------
|
---|
| 140 | void Overlay::remove3D(SceneNode* node)
|
---|
| 141 | {
|
---|
| 142 | mRootNode->removeChild(node->getName());
|
---|
| 143 | }
|
---|
| 144 | //---------------------------------------------------------------------
|
---|
| 145 | void Overlay::clear(void)
|
---|
| 146 | {
|
---|
| 147 | mRootNode->removeAllChildren();
|
---|
| 148 | m2DElements.clear();
|
---|
| 149 | // Note no deallocation, memory handled by OverlayManager & SceneManager
|
---|
| 150 | }
|
---|
| 151 | //---------------------------------------------------------------------
|
---|
| 152 | void Overlay::setScroll(Real x, Real y)
|
---|
| 153 | {
|
---|
| 154 | mScrollX = x;
|
---|
| 155 | mScrollY = y;
|
---|
| 156 | mTransformOutOfDate = true;
|
---|
| 157 | mTransformUpdated = true;
|
---|
| 158 | }
|
---|
| 159 | //---------------------------------------------------------------------
|
---|
| 160 | Real Overlay::getScrollX(void) const
|
---|
| 161 | {
|
---|
| 162 | return mScrollX;
|
---|
| 163 | }
|
---|
| 164 | //---------------------------------------------------------------------
|
---|
| 165 | Real Overlay::getScrollY(void) const
|
---|
| 166 | {
|
---|
| 167 | return mScrollY;
|
---|
| 168 | }
|
---|
| 169 | //---------------------------------------------------------------------
|
---|
| 170 | OverlayContainer* Overlay::getChild(const String& name)
|
---|
| 171 | {
|
---|
| 172 |
|
---|
| 173 | OverlayContainerList::iterator i, iend;
|
---|
| 174 | iend = m2DElements.end();
|
---|
| 175 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 176 | {
|
---|
| 177 | if ((*i)->getName() == name)
|
---|
| 178 | {
|
---|
| 179 | return *i;
|
---|
| 180 |
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | return NULL;
|
---|
| 184 | }
|
---|
| 185 | //---------------------------------------------------------------------
|
---|
| 186 | void Overlay::scroll(Real xoff, Real yoff)
|
---|
| 187 | {
|
---|
| 188 | mScrollX += xoff;
|
---|
| 189 | mScrollY += yoff;
|
---|
| 190 | mTransformOutOfDate = true;
|
---|
| 191 | mTransformUpdated = true;
|
---|
| 192 | }
|
---|
| 193 | //---------------------------------------------------------------------
|
---|
| 194 | void Overlay::setRotate(const Radian& angle)
|
---|
| 195 | {
|
---|
| 196 | mRotate = angle;
|
---|
| 197 | mTransformOutOfDate = true;
|
---|
| 198 | mTransformUpdated = true;
|
---|
| 199 | }
|
---|
| 200 | //---------------------------------------------------------------------
|
---|
| 201 | void Overlay::rotate(const Radian& angle)
|
---|
| 202 | {
|
---|
| 203 | setRotate(mRotate + angle);
|
---|
| 204 | }
|
---|
| 205 | //---------------------------------------------------------------------
|
---|
| 206 | void Overlay::setScale(Real x, Real y)
|
---|
| 207 | {
|
---|
| 208 | mScaleX = x;
|
---|
| 209 | mScaleY = y;
|
---|
| 210 | mTransformOutOfDate = true;
|
---|
| 211 | mTransformUpdated = true;
|
---|
| 212 | }
|
---|
| 213 | //---------------------------------------------------------------------
|
---|
| 214 | Real Overlay::getScaleX(void) const
|
---|
| 215 | {
|
---|
| 216 | return mScaleX;
|
---|
| 217 | }
|
---|
| 218 | //---------------------------------------------------------------------
|
---|
| 219 | Real Overlay::getScaleY(void) const
|
---|
| 220 | {
|
---|
| 221 | return mScaleY;
|
---|
| 222 | }
|
---|
| 223 | //---------------------------------------------------------------------
|
---|
| 224 | void Overlay::_getWorldTransforms(Matrix4* xform) const
|
---|
| 225 | {
|
---|
| 226 | if (mTransformOutOfDate)
|
---|
| 227 | {
|
---|
| 228 | updateTransform();
|
---|
| 229 | }
|
---|
| 230 | *xform = mTransform;
|
---|
| 231 |
|
---|
| 232 | }
|
---|
| 233 | //-----------------------------------------------------------------------
|
---|
| 234 | const Quaternion& Overlay::getWorldOrientation(void) const
|
---|
| 235 | {
|
---|
| 236 | // n/a
|
---|
| 237 | return Quaternion::IDENTITY;
|
---|
| 238 | }
|
---|
| 239 | //-----------------------------------------------------------------------
|
---|
| 240 | const Vector3& Overlay::getWorldPosition(void) const
|
---|
| 241 | {
|
---|
| 242 | // n/a
|
---|
| 243 | return Vector3::ZERO;
|
---|
| 244 | }
|
---|
| 245 | //---------------------------------------------------------------------
|
---|
| 246 | void Overlay::_findVisibleObjects(Camera* cam, RenderQueue* queue)
|
---|
| 247 | {
|
---|
| 248 | OverlayContainerList::iterator i, iend;
|
---|
| 249 |
|
---|
| 250 | if (OverlayManager::getSingleton().hasViewportChanged())
|
---|
| 251 | {
|
---|
| 252 | iend = m2DElements.end();
|
---|
| 253 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 254 | {
|
---|
| 255 | (*i)->_notifyViewport();
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | // update elements
|
---|
| 260 | if (mTransformUpdated)
|
---|
| 261 | {
|
---|
| 262 | OverlayContainerList::iterator i, iend;
|
---|
| 263 | Matrix4 xform;
|
---|
| 264 |
|
---|
| 265 | _getWorldTransforms(&xform);
|
---|
| 266 | iend = m2DElements.end();
|
---|
| 267 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 268 | {
|
---|
| 269 | (*i)->_notifyWorldTransforms(xform);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | mTransformUpdated = false;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | if (mVisible)
|
---|
| 276 | {
|
---|
| 277 | // Add 3D elements
|
---|
| 278 | mRootNode->setPosition(cam->getDerivedPosition());
|
---|
| 279 | mRootNode->setOrientation(cam->getDerivedOrientation());
|
---|
| 280 | mRootNode->_update(true, false);
|
---|
| 281 | // Set up the default queue group for the objects about to be added
|
---|
| 282 | uint8 oldgrp = queue->getDefaultQueueGroup();
|
---|
| 283 | ushort oldPriority = queue-> getDefaultRenderablePriority();
|
---|
| 284 | queue->setDefaultQueueGroup(RENDER_QUEUE_OVERLAY);
|
---|
| 285 | queue->setDefaultRenderablePriority((mZOrder*100)-1);
|
---|
| 286 | mRootNode->_findVisibleObjects(cam, queue, true, false);
|
---|
| 287 | // Reset the group
|
---|
| 288 | queue->setDefaultQueueGroup(oldgrp);
|
---|
| 289 | queue->setDefaultRenderablePriority(oldPriority);
|
---|
| 290 | // Add 2D elements
|
---|
| 291 | iend = m2DElements.end();
|
---|
| 292 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 293 | {
|
---|
| 294 | (*i)->_update();
|
---|
| 295 |
|
---|
| 296 | (*i)->_updateRenderQueue(queue);
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 |
|
---|
| 302 |
|
---|
| 303 | }
|
---|
| 304 | //---------------------------------------------------------------------
|
---|
| 305 | void Overlay::updateTransform(void) const
|
---|
| 306 | {
|
---|
| 307 | // Ordering:
|
---|
| 308 | // 1. Scale
|
---|
| 309 | // 2. Rotate
|
---|
| 310 | // 3. Translate
|
---|
| 311 |
|
---|
| 312 | Matrix3 rot3x3, scale3x3;
|
---|
| 313 | rot3x3.FromEulerAnglesXYZ(Radian(0),Radian(0),mRotate);
|
---|
| 314 | scale3x3 = Matrix3::ZERO;
|
---|
| 315 | scale3x3[0][0] = mScaleX;
|
---|
| 316 | scale3x3[1][1] = mScaleY;
|
---|
| 317 | scale3x3[2][2] = 1.0f;
|
---|
| 318 |
|
---|
| 319 | mTransform = Matrix4::IDENTITY;
|
---|
| 320 | mTransform = rot3x3 * scale3x3;
|
---|
| 321 | mTransform.setTrans(Vector3(mScrollX, mScrollY, 0));
|
---|
| 322 |
|
---|
| 323 | mTransformOutOfDate = false;
|
---|
| 324 | }
|
---|
| 325 | //---------------------------------------------------------------------
|
---|
| 326 | OverlayElement* Overlay::findElementAt(Real x, Real y)
|
---|
| 327 | {
|
---|
| 328 | OverlayElement* ret = NULL;
|
---|
| 329 | int currZ = -1;
|
---|
| 330 | OverlayContainerList::iterator i, iend;
|
---|
| 331 | iend = m2DElements.end();
|
---|
| 332 | for (i = m2DElements.begin(); i != iend; ++i)
|
---|
| 333 | {
|
---|
| 334 | int z = (*i)->getZOrder();
|
---|
| 335 | if (z > currZ)
|
---|
| 336 | {
|
---|
| 337 | OverlayElement* elementFound = (*i)->findElementAt(x,y);
|
---|
| 338 | if(elementFound)
|
---|
| 339 | {
|
---|
| 340 | currZ = elementFound->getZOrder();
|
---|
| 341 | ret = elementFound;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | return ret;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | }
|
---|
| 349 |
|
---|