Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreOverlay.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #include "OgreStableHeaders.h"
00026 
00027 #include "OgreOverlay.h"
00028 #include "OgreRoot.h"
00029 #include "OgreSceneManager.h"
00030 #include "OgreGuiContainer.h"
00031 #include "OgreCamera.h"
00032 #include "OgreQuaternion.h"
00033 #include "OgreVector3.h"
00034 
00035 
00036 namespace Ogre {
00037 
00038     //---------------------------------------------------------------------
00039     Overlay::Overlay(const String& name)
00040     {
00041         mName = name;
00042         mRotate = 0.0f;
00043         mScaleX = 1.0f;
00044         mScaleY = 1.0f;
00045         mScrollX = 0.0f;
00046         mScrollY = 0.0f;
00047         mVisible = false;
00048         mTransformOutOfDate = true;
00049         mZOrder = 100; // Default
00050         mRootNode = Root::getSingleton().getSceneManager(ST_GENERIC)->createSceneNode();
00051 
00052     }
00053     //---------------------------------------------------------------------
00054     Overlay::~Overlay()
00055     {
00056         //mRootNode->getCreator()->destroySceneNode(mRootNode->getName());
00057     }
00058     //---------------------------------------------------------------------
00059     const String& Overlay::getName(void) const
00060     {
00061         return mName;
00062     }
00063     //---------------------------------------------------------------------
00064     void Overlay::setZOrder(ushort zorder)
00065     {
00066         mZOrder = zorder;
00067 
00068         // Notify attached 2D elements
00069         GuiContainerList::iterator i, iend;
00070         iend = m2DElements.end();
00071         for (i = m2DElements.begin(); i != iend; ++i)
00072         {
00073             (*i)->_notifyZOrder(zorder);
00074         }
00075 
00076     }
00077     //---------------------------------------------------------------------
00078     ushort Overlay::getZOrder(void) const
00079     {
00080         return mZOrder;
00081     }
00082     //---------------------------------------------------------------------
00083     bool Overlay::isVisible(void) const
00084     {
00085         return mVisible;
00086     }
00087     //---------------------------------------------------------------------
00088     void Overlay::show(void)
00089     {
00090         mVisible = true;
00091     }
00092     //---------------------------------------------------------------------
00093     void Overlay::hide(void)
00094     {
00095         mVisible = false;
00096     }
00097     //---------------------------------------------------------------------
00098     void Overlay::add2D(GuiContainer* cont)
00099     {
00100         m2DElements.push_back(cont);
00101         // Notify parent
00102         cont->_notifyParent(0, this);
00103         // Set Z order, scaled to separate overlays
00104         // NB max 100 container levels per overlay, should be plenty
00105         cont->_notifyZOrder(mZOrder * 100);
00106     }
00107     //---------------------------------------------------------------------
00108     void Overlay::remove2D(GuiContainer* cont)
00109     {
00110         m2DElements.remove(cont);
00111     }
00112     //---------------------------------------------------------------------
00113     void Overlay::add3D(SceneNode* node)
00114     {
00115         mRootNode->addChild(node);
00116     }
00117     //---------------------------------------------------------------------
00118     void Overlay::remove3D(SceneNode* node)
00119     {
00120         mRootNode->removeChild(node->getName());
00121     }
00122     //---------------------------------------------------------------------
00123     void Overlay::clear(void)
00124     {
00125         mRootNode->removeAllChildren();
00126         m2DElements.clear();
00127         // Note no deallocation, memory handled by GuiManager & SceneManager
00128     }
00129     //---------------------------------------------------------------------
00130     void Overlay::setScroll(Real x, Real y)
00131     {
00132         mScrollX = x;
00133         mScrollY = y;
00134         mTransformOutOfDate = true;
00135     }
00136     //---------------------------------------------------------------------
00137     Real Overlay::getScrollX(void) const
00138     {
00139         return mScrollX;
00140     }
00141     //---------------------------------------------------------------------
00142     Real Overlay::getScrollY(void) const
00143     {
00144         return mScrollY;
00145     }
00146       //---------------------------------------------------------------------
00147     GuiContainer* Overlay::getChild(const String& name)
00148     {
00149 
00150         GuiContainerList::iterator i, iend;
00151         iend = m2DElements.end();
00152         for (i = m2DElements.begin(); i != iend; ++i)
00153         {
00154             if ((*i)->getName() == name)
00155             {
00156                 return *i;
00157 
00158             }
00159         }
00160         return NULL;
00161     }
00162   //---------------------------------------------------------------------
00163     void Overlay::scroll(Real xoff, Real yoff)
00164     {
00165         mScrollX += xoff;
00166         mScrollY += yoff;
00167         mTransformOutOfDate = true;
00168     }
00169     //---------------------------------------------------------------------
00170     void Overlay::setRotate(Real angleunits)
00171     {
00172         mRotate = angleunits;
00173         mTransformOutOfDate = true;
00174     }
00175     //---------------------------------------------------------------------
00176     Real Overlay::getRotate(void) const
00177     {
00178         return mRotate;
00179     }
00180     //---------------------------------------------------------------------
00181     void Overlay::rotate(Real angleunits)
00182     {
00183         setRotate(mRotate += angleunits);
00184     }
00185     //---------------------------------------------------------------------
00186     void Overlay::setScale(Real x, Real y)
00187     {
00188         mScaleX = x;
00189         mScaleY = y;
00190         mTransformOutOfDate = true;
00191     }
00192     //---------------------------------------------------------------------
00193     Real Overlay::getScaleX(void) const
00194     {
00195         return mScaleX;
00196     }
00197     //---------------------------------------------------------------------
00198     Real Overlay::getScaleY(void) const
00199     {
00200         return mScaleY;
00201     }
00202     //---------------------------------------------------------------------
00203     void Overlay::_getWorldTransforms(Matrix4* xform) const
00204     {
00205         if (mTransformOutOfDate)
00206         {
00207             updateTransform();
00208         }
00209         *xform = mTransform;
00210 
00211     }
00212     //-----------------------------------------------------------------------
00213     const Quaternion& Overlay::getWorldOrientation(void) const
00214     {
00215         // n/a
00216         return Quaternion::IDENTITY;
00217     }
00218     //-----------------------------------------------------------------------
00219     const Vector3& Overlay::getWorldPosition(void) const
00220     {
00221         // n/a
00222         return Vector3::ZERO;
00223     }
00224     //---------------------------------------------------------------------
00225     void Overlay::_findVisibleObjects(Camera* cam, RenderQueue* queue)
00226     {
00227         if (!mVisible)
00228             return;
00229 
00230         // Add 3D elements
00231         mRootNode->setPosition(cam->getDerivedPosition());
00232         mRootNode->setOrientation(cam->getDerivedOrientation());
00233         mRootNode->_update(true, false);
00234         // Set up the default queue group for the objects about to be added
00235         RenderQueueGroupID oldgrp = queue->getDefaultQueueGroup();
00236         queue->setDefaultQueueGroup(RENDER_QUEUE_OVERLAY);
00237         mRootNode->_findVisibleObjects(cam, queue, true, false);
00238         // Reset the group
00239         queue->setDefaultQueueGroup(oldgrp);
00240 
00241 
00242         // Add 2D elements
00243         GuiContainerList::iterator i, iend;
00244         iend = m2DElements.end();
00245         for (i = m2DElements.begin(); i != iend; ++i)
00246         {
00247             (*i)->_update();
00248 
00249             (*i)->_updateRenderQueue(queue);
00250         }
00251        
00252     }
00253     //---------------------------------------------------------------------
00254     void Overlay::updateTransform(void) const
00255     {
00256         // Ordering:
00257         //    1. Scale
00258         //    2. Rotate
00259         //    3. Translate
00260 
00261         
00262         Matrix3 rot3x3, scale3x3;
00263         rot3x3.FromEulerAnglesXYZ(0,0,Math::AngleUnitsToRadians(mRotate));
00264         scale3x3 = Matrix3::ZERO;
00265         scale3x3[0][0] = mScaleX;
00266         scale3x3[1][1] = mScaleY;
00267         scale3x3[2][2] = 1.0f;
00268 
00269         mTransform = Matrix4::IDENTITY;
00270         mTransform = rot3x3 * scale3x3;
00271         mTransform.setTrans(Vector3(mScrollX, mScrollY, 0));
00272 
00273         mTransformOutOfDate = false;
00274     }
00275     //---------------------------------------------------------------------
00276     void Overlay::load(void)
00277     {
00278         // Do nothing
00279     }
00280     //---------------------------------------------------------------------
00281     void Overlay::unload(void)
00282     {
00283         // Do nothing
00284     }
00285 
00286     GuiElement* Overlay::findElementAt(Real x, Real y)
00287     {
00288         GuiElement* ret = NULL;
00289         int currZ = -1;
00290         GuiContainerList::iterator i, iend;
00291         iend = m2DElements.end();
00292         for (i = m2DElements.begin(); i != iend; ++i)
00293         {
00294             int z = (*i)->getZOrder();
00295             if (z > currZ)
00296             {
00297                 GuiElement* elementFound = (*i)->findElementAt(x,y);
00298                 if(elementFound)
00299                 {
00300                     currZ = elementFound->getZOrder();
00301                     ret = elementFound;
00302                 }
00303             }
00304         }
00305         return ret;
00306     }
00307 }
00308 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:20 2004