source: OGRE/trunk/ogrenew/OgreMain/src/OgreOverlayContainer.cpp @ 692

Revision 692, 12.1 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1
2/*
3-----------------------------------------------------------------------------
4This source file is part of OGRE
5    (Object-oriented Graphics Rendering Engine)
6For the latest info, see http://www.ogre3d.org/
7
8Copyright (c) 2000-2005 The OGRE Team
9Also see acknowledgements in Readme.html
10
11This program is free software you can redistribute it and/or modify it under
12the terms of the GNU Lesser General Public License as published by the Free Software
13Foundation either version 2 of the License, or (at your option) any later
14version.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public License along with
21this program if not, write to the Free Software Foundation, Inc., 59 Temple
22Place - Suite 330, Boston, MA 02111-1307, USA, or go to
23http://www.gnu.org/copyleft/lesser.txt.
24-----------------------------------------------------------------------------
25*/
26#include "OgreStableHeaders.h"
27
28#include "OgreOverlayContainer.h"
29#include "OgreException.h"
30#include "OgreOverlayManager.h"
31
32namespace Ogre {
33
34    //---------------------------------------------------------------------
35    OverlayContainer::OverlayContainer(const String& name)
36        : OverlayElement(name),
37                mChildrenProcessEvents(true)
38    {
39    }
40    //---------------------------------------------------------------------
41    OverlayContainer::~OverlayContainer()
42    {
43        OverlayContainer::ChildIterator ci = getChildIterator();
44        while (ci.hasMoreElements())
45        {
46            OverlayElement* child = ci.getNext();
47            child->_setParent(0);
48        }
49    }
50    //---------------------------------------------------------------------
51    void OverlayContainer::addChild(OverlayElement* elem)
52    {
53        if (elem->isContainer())
54                {
55                        addChildImpl(static_cast<OverlayContainer*>(elem));
56                }
57                else
58                {
59                        addChildImpl(elem);
60                }
61
62        }
63    //---------------------------------------------------------------------
64    void OverlayContainer::addChildImpl(OverlayElement* elem)
65    {
66        String name = elem->getName();
67        ChildMap::iterator i = mChildren.find(name);
68        if (i != mChildren.end())
69        {
70            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Child with name " + name +
71                " already defined.", "OverlayContainer::addChild");
72        }
73
74        mChildren.insert(ChildMap::value_type(name, elem));
75        // tell child about parent & ZOrder
76        elem->_notifyParent(this, mOverlay);
77            elem->_notifyZOrder(mZOrder + 1);
78            elem->_notifyWorldTransforms(mXForm);
79            elem->_notifyViewport();
80
81    }
82    //---------------------------------------------------------------------
83    void OverlayContainer::addChildImpl(OverlayContainer* cont)
84    {
85        // Add to main map first
86        // This will pick up duplicates
87        OverlayElement* pElem = cont;
88        addChildImpl(pElem);
89
90        /*
91        cont->_notifyParent(this, mOverlay);
92        cont->_notifyZOrder(mZOrder + 1);
93            cont->_notifyWorldTransforms(mXForm);
94
95                // tell children of new container the current overlay
96        ChildIterator it = cont->getChildIterator();
97        while (it.hasMoreElements())
98        {
99            // Give children ZOrder 1 higher than this
100            GuiElement* pElemChild = it.getNext();
101                        pElemChild->_notifyParent(cont, mOverlay);
102            pElemChild->_notifyZOrder(cont->getZOrder() + 1);
103            pElemChild->_notifyWorldTransforms(mXForm);
104        }
105        */
106
107        // Now add to specific map too
108        mChildContainers.insert(ChildContainerMap::value_type(cont->getName(), cont));
109
110    }
111    //---------------------------------------------------------------------
112    void OverlayContainer::removeChild(const String& name)
113    {
114        ChildMap::iterator i = mChildren.find(name);
115        if (i == mChildren.end())
116        {
117            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Child with name " + name +
118                " not found.", "OverlayContainer::removeChild");
119        }
120
121        OverlayElement* element = i->second;
122        mChildren.erase(i);
123
124            // remove from container list (if found)
125        ChildContainerMap::iterator j = mChildContainers.find(name);
126        if (j != mChildContainers.end())
127            mChildContainers.erase(j);
128
129        element->_setParent(0);
130    }
131    //---------------------------------------------------------------------
132    void OverlayContainer::_addChild(OverlayElement* elem)
133    {
134        if (elem->isContainer())
135                {
136                        addChildImpl(static_cast<OverlayContainer*>(elem));
137                }
138                else
139                {
140                        addChildImpl(elem);
141                }
142        }
143    //---------------------------------------------------------------------
144    void OverlayContainer::_removeChild(const String& name)
145    {
146        ChildMap::iterator i = mChildren.find(name);
147        if (i == mChildren.end())
148        {
149            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Child with name " + name +
150                " not found.", "OverlayContainer::removeChild");
151        }
152
153        OverlayElement* element = i->second;
154        mChildren.erase(i);
155
156            // remove from container list (if found)
157        ChildContainerMap::iterator j = mChildContainers.find(name);
158        if (j != mChildContainers.end())
159            mChildContainers.erase(j);
160
161        element->_setParent(0);
162    }
163    //---------------------------------------------------------------------
164    OverlayElement* OverlayContainer::getChild(const String& name)
165    {
166        ChildMap::iterator i = mChildren.find(name);
167        if (i == mChildren.end())
168        {
169            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Child with name " + name +
170                " not found.", "OverlayContainer::getChild");
171        }
172
173        return i->second;
174    }
175    //---------------------------------------------------------------------
176    OverlayContainer::ChildIterator OverlayContainer::getChildIterator(void)
177    {
178        return ChildIterator(mChildren.begin(), mChildren.end());
179    }
180    //---------------------------------------------------------------------
181    OverlayContainer::ChildContainerIterator OverlayContainer::getChildContainerIterator(void)
182    {
183        return ChildContainerIterator(mChildContainers.begin(), mChildContainers.end());
184    }
185        //---------------------------------------------------------------------
186        void OverlayContainer::initialise(void)
187        {
188                ChildContainerMap::iterator coni;
189                for (coni =  mChildContainers.begin(); coni != mChildContainers.end(); ++coni)
190                {
191                        coni->second->initialise();
192                }
193                ChildMap::iterator ci;
194                for (ci =  mChildren.begin(); ci != mChildren.end(); ++ci)
195                {
196                        ci->second->initialise();
197                }
198        }
199    //---------------------------------------------------------------------
200        void OverlayContainer::_positionsOutOfDate(void)
201        {
202                OverlayElement::_positionsOutOfDate();
203
204        ChildIterator it = getChildIterator();
205        while (it.hasMoreElements())
206        {
207                        it.getNext()->_positionsOutOfDate();
208        }
209        }
210
211    //---------------------------------------------------------------------
212    void OverlayContainer::_update(void)
213    {
214        // call superclass
215        OverlayElement::_update();
216
217        // Update children
218        ChildIterator it = getChildIterator();
219        while (it.hasMoreElements())
220        {
221            it.getNext()->_update();
222        }
223
224
225    }
226    //---------------------------------------------------------------------
227    void OverlayContainer::_notifyZOrder(ushort newZOrder)
228    {
229        OverlayElement::_notifyZOrder(newZOrder);
230
231        // Update children
232        ChildIterator it = getChildIterator();
233        while (it.hasMoreElements())
234        {
235            // Give children ZOrder 1 higher than this
236            it.getNext()->_notifyZOrder(newZOrder + 1);
237        }
238    }
239    //---------------------------------------------------------------------
240    void OverlayContainer::_notifyWorldTransforms(const Matrix4& xform)
241    {
242        OverlayElement::_notifyWorldTransforms(xform);
243
244        // Update children
245        ChildIterator it = getChildIterator();
246        while (it.hasMoreElements())
247        {
248            it.getNext()->_notifyWorldTransforms(xform);
249        }
250    }
251    //---------------------------------------------------------------------
252    void OverlayContainer::_notifyViewport()
253    {
254        OverlayElement::_notifyViewport();
255
256        // Update children
257        ChildIterator it = getChildIterator();
258        while (it.hasMoreElements())
259        {
260            it.getNext()->_notifyViewport();
261        }
262    }
263    //---------------------------------------------------------------------
264    void OverlayContainer::_notifyParent(OverlayContainer* parent, Overlay* overlay)
265    {
266        OverlayElement::_notifyParent(parent, overlay);
267
268        // Update children
269        ChildIterator it = getChildIterator();
270        while (it.hasMoreElements())
271        {
272            // Notify the children of the overlay
273            it.getNext()->_notifyParent(this, overlay);
274        }
275    }
276
277    //---------------------------------------------------------------------
278    void OverlayContainer::_updateRenderQueue(RenderQueue* queue)
279    {
280        if (mVisible)
281        {
282
283            OverlayElement::_updateRenderQueue(queue);
284
285            // Also add children
286            ChildIterator it = getChildIterator();
287            while (it.hasMoreElements())
288            {
289                // Give children ZOrder 1 higher than this
290                it.getNext()->_updateRenderQueue(queue);
291            }
292        }
293
294    }
295
296
297        OverlayElement* OverlayContainer::findElementAt(Real x, Real y)                 // relative to parent
298        {
299
300                OverlayElement* ret = NULL;
301
302                int currZ = -1;
303
304                if (mVisible)
305                {
306                        ret = OverlayElement::findElementAt(x,y);       //default to the current container if no others are found
307                        if (ret && mChildrenProcessEvents)
308                        {
309                                ChildIterator it = getChildIterator();
310                                while (it.hasMoreElements())
311                                {
312                                        OverlayElement* currentOverlayElement = it.getNext();
313                                        if (currentOverlayElement->isVisible() && currentOverlayElement->isEnabled())
314                                        {
315                                                int z = currentOverlayElement->getZOrder();
316                                                if (z > currZ)
317                                                {
318                                                        OverlayElement* elementFound = currentOverlayElement->findElementAt(x ,y );
319                                                        if (elementFound)
320                                                        {
321                                                                currZ = z;
322                                                                ret = elementFound;
323                                                        }
324                                                }
325                                        }
326                                }
327                        }
328                }
329                return ret;
330        }
331
332    void OverlayContainer::copyFromTemplate(OverlayElement* templateOverlay)
333    {
334        OverlayElement::copyFromTemplate(templateOverlay);
335
336                    if (templateOverlay->isContainer() && isContainer())
337                    {
338             OverlayContainer::ChildIterator it = static_cast<OverlayContainer*>(templateOverlay)->getChildIterator();
339                         while (it.hasMoreElements())
340                         {
341                                 OverlayElement* oldChildElement = it.getNext();
342                                 if (oldChildElement->isCloneable())
343                                 {
344                                         OverlayElement* newChildElement =
345                                                 OverlayManager::getSingleton().createOverlayElement(
346                                                        oldChildElement->getTypeName(),
347                                                        mName+"/"+oldChildElement->getName());
348                                         oldChildElement->copyParametersTo(newChildElement);
349                                         addChild((OverlayContainer*)newChildElement);
350                                 }
351                         }
352        }
353    }
354
355    OverlayElement* OverlayContainer::clone(const String& instanceName)
356    {
357        OverlayContainer *newContainer;
358
359        newContainer = static_cast<OverlayContainer*>(OverlayElement::clone(instanceName));
360
361          ChildIterator it = getChildIterator();
362                  while (it.hasMoreElements())
363                          {
364                                    OverlayElement* oldChildElement = it.getNext();
365                                    if (oldChildElement->isCloneable())
366                                    {
367                OverlayElement* newChildElement = oldChildElement->clone(instanceName);
368                newContainer->_addChild(newChildElement);
369            }
370        }
371
372        return newContainer;
373    }
374
375}
376
Note: See TracBrowser for help on using the repository browser.