Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreOverlayElement.h @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 20.8 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29
30#ifndef __OverlayElement_H__
31#define __OverlayElement_H__
32
33#include "OgrePrerequisites.h"
34#include "OgreString.h"
35#include "OgreRenderable.h"
36#include "OgreUTFString.h"
37#include "OgreStringInterface.h"
38#include "OgreOverlayElementCommands.h"
39
40#include "OgreColourValue.h"
41#include "OgreRectangle.h"
42
43namespace Ogre {
44
45#if OGRE_UNICODE_SUPPORT
46        typedef UTFString DisplayString;
47#       define OGRE_DEREF_DISPLAYSTRING_ITERATOR(it) it.getCharacter()
48#else
49        typedef String DisplayString;
50#       define OGRE_DEREF_DISPLAYSTRING_ITERATOR(it) *it
51#endif
52    /** Enum describing how the position / size of an element is to be recorded.
53    */
54    enum GuiMetricsMode
55    {
56        /// 'left', 'top', 'height' and 'width' are parametrics from 0.0 to 1.0
57        GMM_RELATIVE,
58        /// Positions & sizes are in absolute pixels
59        GMM_PIXELS,
60        /// Positions & sizes are in virtual pixels
61        GMM_RELATIVE_ASPECT_ADJUSTED
62    };
63
64    /** Enum describing where '0' is in relation to the parent in the horizontal dimension.
65    @remarks Affects how 'left' is interpreted.
66    */
67    enum GuiHorizontalAlignment
68    {
69        GHA_LEFT,
70        GHA_CENTER,
71        GHA_RIGHT
72    };
73    /** Enum describing where '0' is in relation to the parent in the vertical dimension.
74    @remarks Affects how 'top' is interpreted.
75    */
76    enum GuiVerticalAlignment
77    {
78        GVA_TOP,
79        GVA_CENTER,
80        GVA_BOTTOM
81    };
82
83    /** Abstract definition of a 2D element to be displayed in an Overlay.
84    @remarks
85    This class abstracts all the details of a 2D element which will appear in
86    an overlay. In fact, not all OverlayElement instances can be directly added to an
87    Overlay, only those which are OverlayContainer instances (a subclass of this class).
88    OverlayContainer objects can contain any OverlayElement however. This is just to
89    enforce some level of grouping on widgets.
90    @par
91    OverlayElements should be managed using OverlayManager. This class is responsible for
92    instantiating / deleting elements, and also for accepting new types of element
93    from plugins etc.
94    @par
95    Note that positions / dimensions of 2D screen elements are expressed as parametric
96    values (0.0 - 1.0) because this makes them resolution-independent. However, most
97    screen resolutions have an aspect ratio of 1.3333:1 (width : height) so note that
98    in physical pixels 0.5 is wider than it is tall, so a 0.5x0.5 panel will not be
99    square on the screen (but it will take up exactly half the screen in both dimensions).
100    @par
101    Because this class is designed to be extensible, it subclasses from StringInterface
102    so its parameters can be set in a generic way.
103    */
104    class _OgreExport OverlayElement : public StringInterface, public Renderable
105    {
106    public:
107
108    protected:
109        // Command object for setting / getting parameters
110        static OverlayElementCommands::CmdLeft msLeftCmd;
111        static OverlayElementCommands::CmdTop msTopCmd;
112        static OverlayElementCommands::CmdWidth msWidthCmd;
113        static OverlayElementCommands::CmdHeight msHeightCmd;
114        static OverlayElementCommands::CmdMaterial msMaterialCmd;
115        static OverlayElementCommands::CmdCaption msCaptionCmd;
116        static OverlayElementCommands::CmdMetricsMode msMetricsModeCmd;
117        static OverlayElementCommands::CmdHorizontalAlign msHorizontalAlignCmd;
118        static OverlayElementCommands::CmdVerticalAlign msVerticalAlignCmd;
119        static OverlayElementCommands::CmdVisible msVisibleCmd;
120
121
122        String mName;
123        bool mVisible;
124        bool mCloneable;
125        Real mLeft;
126        Real mTop;
127        Real mWidth;
128        Real mHeight;
129        String mMaterialName;
130        MaterialPtr mpMaterial;
131        DisplayString mCaption;
132        ColourValue mColour;
133        Rectangle mClippingRegion;
134
135        GuiMetricsMode mMetricsMode;
136        GuiHorizontalAlignment mHorzAlign;
137        GuiVerticalAlignment mVertAlign;
138
139        // metric-mode positions, used in GMM_PIXELS & GMM_RELATIVE_ASPECT_ADJUSTED mode.
140        Real mPixelTop;
141        Real mPixelLeft;
142        Real mPixelWidth;
143        Real mPixelHeight;
144        Real mPixelScaleX;
145        Real mPixelScaleY;
146
147        // Parent pointer
148        OverlayContainer* mParent;
149        // Overlay attached to
150        Overlay* mOverlay;
151
152        // Derived positions from parent
153        Real mDerivedLeft;
154        Real mDerivedTop;
155        bool mDerivedOutOfDate;
156
157        /// Flag indicating if the vertex positons need recalculating
158        bool mGeomPositionsOutOfDate;
159                /// Flag indicating if the vertex uvs need recalculating
160                bool mGeomUVsOutOfDate;
161
162        // Zorder for when sending to render queue
163        // Derived from parent
164        ushort mZOrder;
165
166        // world transforms
167        Matrix4 mXForm;
168
169        // is element enabled
170        bool mEnabled;
171
172                // is element initialised
173                bool mInitialised;
174
175        // Used to see if this element is created from a Template
176        OverlayElement* mSourceTemplate ;
177
178        /** Internal method which is triggered when the positions of the element get updated,
179        meaning the element should be rebuilding it's mesh positions. Abstract since
180        subclasses must implement this.
181        */
182        virtual void updatePositionGeometry(void) = 0;
183                /** Internal method which is triggered when the UVs of the element get updated,
184                meaning the element should be rebuilding it's mesh UVs. Abstract since
185                subclasses must implement this.
186                */
187                virtual void updateTextureGeometry(void) = 0;
188
189        /** Internal method for setting up the basic parameter definitions for a subclass.
190        @remarks
191        Because StringInterface holds a dictionary of parameters per class, subclasses need to
192        call this to ask the base class to add it's parameters to their dictionary as well.
193        Can't do this in the constructor because that runs in a non-virtual context.
194        @par
195        The subclass must have called it's own createParamDictionary before calling this method.
196        */
197        virtual void addBaseParameters(void);
198
199    public:
200        /// Constructor: do not call direct, use OverlayManager::createElement
201        OverlayElement(const String& name);
202        virtual ~OverlayElement();
203
204        /** Initialise gui element */
205        virtual void initialise(void) = 0;
206
207        /** Gets the name of this overlay. */
208        const String& getName(void) const;
209
210
211        /** Shows this element if it was hidden. */
212        virtual void show(void);
213
214        /** Hides this element if it was visible. */
215        virtual void hide(void);
216
217        /** Returns whether or not the element is visible. */
218        bool isVisible(void) const;
219
220        bool isEnabled() const;
221        virtual void setEnabled(bool b);
222
223
224        /** Sets the dimensions of this element in relation to the screen (1.0 = screen width/height). */
225        void setDimensions(Real width, Real height);
226
227        /** Sets the position of the top-left corner of the element, relative to the screen size
228        (1.0 = screen width / height) */
229        void setPosition(Real left, Real top);
230
231        /** Sets the width of this element in relation to the screen (where 1.0 = screen width) */
232        void setWidth(Real width);
233        /** Gets the width of this element in relation to the screen (where 1.0 = screen width) */
234        Real getWidth(void) const;
235
236        /** Sets the height of this element in relation to the screen (where 1.0 = screen height) */
237        void setHeight(Real height);
238        /** Gets the height of this element in relation to the screen (where 1.0 = screen height) */
239        Real getHeight(void) const;
240
241        /** Sets the left of this element in relation to the screen (where 0 = far left, 1.0 = far right) */
242        void setLeft(Real left);
243        /** Gets the left of this element in relation to the screen (where 0 = far left, 1.0 = far right)  */
244        Real getLeft(void) const;
245
246        /** Sets the top of this element in relation to the screen (where 0 = top, 1.0 = bottom) */
247        void setTop(Real Top);
248        /** Gets the top of this element in relation to the screen (where 0 = top, 1.0 = bottom)  */
249        Real getTop(void) const;
250
251        /** Gets the left of this element in relation to the screen (where 0 = far left, 1.0 = far right)  */
252        Real _getLeft(void) const { return mLeft; }
253        /** Gets the top of this element in relation to the screen (where 0 = far left, 1.0 = far right)  */
254        Real _getTop(void) const { return mTop; }
255        /** Gets the width of this element in relation to the screen (where 1.0 = screen width)  */
256        Real _getWidth(void) const { return mWidth; }
257        /** Gets the height of this element in relation to the screen (where 1.0 = screen height)  */
258        Real _getHeight(void) const { return mHeight; }
259        /** Sets the left of this element in relation to the screen (where 1.0 = screen width) */
260        void _setLeft(Real left);
261        /** Sets the top of this element in relation to the screen (where 1.0 = screen width) */
262        void _setTop(Real top);
263        /** Sets the width of this element in relation to the screen (where 1.0 = screen width) */
264        void _setWidth(Real width);
265        /** Sets the height of this element in relation to the screen (where 1.0 = screen width) */
266        void _setHeight(Real height);
267        /** Sets the left and top of this element in relation to the screen (where 1.0 = screen width) */
268        void _setPosition(Real left, Real top);
269        /** Sets the width and height of this element in relation to the screen (where 1.0 = screen width) */
270        void _setDimensions(Real width, Real height);
271
272        /** Gets the name of the material this element uses. */
273        virtual const String& getMaterialName(void) const;
274
275        /** Sets the name of the material this element will use.
276        @remarks
277        Different elements will use different materials. One constant about them
278        all though is that a Material used for a OverlayElement must have it's depth
279        checking set to 'off', which means it always gets rendered on top. OGRE
280        will set this flag for you if necessary. What it does mean though is that
281        you should not use the same Material for rendering OverlayElements as standard
282        scene objects. It's fine to use the same textures, just not the same
283        Material.
284        */
285        virtual void setMaterialName(const String& matName);
286
287
288        // --- Renderable Overrides ---
289        /** See Renderable */
290        const MaterialPtr& getMaterial(void) const;
291
292        // NB getRenderOperation not implemented, still abstract here
293
294        /** See Renderable */
295        void getWorldTransforms(Matrix4* xform) const;
296        /** @copydoc Renderable::getWorldOrientation */
297        const Quaternion& getWorldOrientation(void) const;
298        /** @copydoc Renderable::getWorldPosition */
299        const Vector3& getWorldPosition(void) const;
300
301        /** Tell the object to recalculate */
302        virtual void _positionsOutOfDate(void);
303
304        /** Internal method to update the element based on transforms applied. */
305        virtual void _update(void);
306
307        /** Updates this elements transform based on it's parent. */
308        virtual void _updateFromParent(void);
309
310        /** Internal method for notifying the gui element of it's parent and ultimate overlay. */
311        virtual void _notifyParent(OverlayContainer* parent, Overlay* overlay);
312
313        /** Gets the 'left' position as derived from own left and that of parents. */
314        virtual Real _getDerivedLeft(void);
315
316        /** Gets the 'top' position as derived from own left and that of parents. */
317        virtual Real _getDerivedTop(void);
318
319                /** Gets the 'width' as derived from own width and metrics mode. */
320                virtual Real _getRelativeWidth(void);
321                /** Gets the 'height' as derived from own height and metrics mode. */
322                virtual Real _getRelativeHeight(void);
323
324
325        /** Gets the clipping region of the element */
326        virtual void _getClippingRegion(Rectangle &clippingRegion);
327
328        /** Internal method to notify the element when Zorder of parent overlay
329        has changed.
330        @remarks
331        Overlays have explicit Z orders. OverlayElements do not, they inherit the
332        ZOrder of the overlay, and the Zorder is incremented for every container
333        nested within this to ensure that containers are displayed behind contained
334        items. This method is used internally to notify the element of a change in
335        final zorder which is used to render the element.
336        */
337        virtual void _notifyZOrder(ushort newZOrder);
338
339        /** Internal method to notify the element when it's world transform
340         of parent overlay has changed.
341        */
342        virtual void _notifyWorldTransforms(const Matrix4& xform);
343
344        /** Internal method to notify the element when the viewport
345         of parent overlay has changed.
346        */
347        virtual void _notifyViewport();
348
349        /** Internal method to put the contents onto the render queue. */
350        virtual void _updateRenderQueue(RenderQueue* queue);
351
352        /** Gets the type name of the element. All concrete subclasses must implement this. */
353        virtual const String& getTypeName(void) const = 0;
354
355        /** Sets the caption on elements that support it.
356        @remarks
357        This property doesn't do something on all elements, just those that support it.
358        However, being a common requirement it is in the top-level interface to avoid
359        having to set it via the StringInterface all the time.
360        */
361                virtual void setCaption(const DisplayString& text);
362        /** Gets the caption for this element. */
363        virtual const DisplayString& getCaption(void) const;
364        /** Sets the colour on elements that support it.
365        @remarks
366        This property doesn't do something on all elements, just those that support it.
367        However, being a common requirement it is in the top-level interface to avoid
368        having to set it via the StringInterface all the time.
369        */
370        virtual void setColour(const ColourValue& col);
371
372        /** Gets the colour for this element. */
373        virtual const ColourValue& getColour(void) const;
374
375        /** Tells this element how to interpret the position and dimension values it is given.
376        @remarks
377        By default, OverlayElements are positioned and sized according to relative dimensions
378        of the screen. This is to ensure portability between different resolutions when you
379        want things to be positioned and sized the same way across all resolutions. However,
380        sometimes you want things to be sized according to fixed pixels. In order to do this,
381        you can call this method with the parameter GMM_PIXELS. Note that if you then want
382        to place your element relative to the center, right or bottom of it's parent, you will
383        need to use the setHorizontalAlignment and setVerticalAlignment methods.
384        */
385        virtual void setMetricsMode(GuiMetricsMode gmm);
386        /** Retrieves the current settings of how the element metrics are interpreted. */
387        virtual GuiMetricsMode getMetricsMode(void) const;
388        /** Sets the horizontal origin for this element.
389        @remarks
390        By default, the horizontal origin for a OverlayElement is the left edge of the parent container
391        (or the screen if this is a root element). You can alter this by calling this method, which is
392        especially useful when you want to use pixel-based metrics (see setMetricsMode) since in this
393        mode you can't use relative positioning.
394        @par
395        For example, if you were using GMM_PIXELS metrics mode, and you wanted to place a 30x30 pixel
396        crosshair in the center of the screen, you would use GHA_CENTER with a 'left' property of -15.
397        @par
398        Note that neither GHA_CENTER or GHA_RIGHT alter the position of the element based
399        on it's width, you have to alter the 'left' to a negative number to do that; all this
400        does is establish the origin. This is because this way you can align multiple things
401        in the center and right with different 'left' offsets for maximum flexibility.
402        */
403        virtual void setHorizontalAlignment(GuiHorizontalAlignment gha);
404        /** Gets the horizontal alignment for this element. */
405        virtual GuiHorizontalAlignment getHorizontalAlignment(void) const;
406        /** Sets the vertical origin for this element.
407        @remarks
408        By default, the vertical origin for a OverlayElement is the top edge of the parent container
409        (or the screen if this is a root element). You can alter this by calling this method, which is
410        especially useful when you want to use pixel-based metrics (see setMetricsMode) since in this
411        mode you can't use relative positioning.
412        @par
413        For example, if you were using GMM_PIXELS metrics mode, and you wanted to place a 30x30 pixel
414        crosshair in the center of the screen, you would use GHA_CENTER with a 'top' property of -15.
415        @par
416        Note that neither GVA_CENTER or GVA_BOTTOM alter the position of the element based
417        on it's height, you have to alter the 'top' to a negative number to do that; all this
418        does is establish the origin. This is because this way you can align multiple things
419        in the center and bottom with different 'top' offsets for maximum flexibility.
420        */
421        virtual void setVerticalAlignment(GuiVerticalAlignment gva);
422        /** Gets the vertical alignment for this element. */
423        virtual GuiVerticalAlignment getVerticalAlignment(void) const;
424
425
426
427
428        /** Returns true if xy is within the constraints of the component */
429        virtual bool contains(Real x, Real y) const;
430
431        /** Returns true if xy is within the constraints of the component */
432        virtual OverlayElement* findElementAt(Real x, Real y);          // relative to parent
433
434        /**
435        * returns false as this class is not a container type
436        */
437        inline virtual bool isContainer() const
438        { return false; }
439
440        inline virtual bool isKeyEnabled() const
441        { return false; }
442
443        inline virtual bool isCloneable() const
444        { return mCloneable; }
445
446        inline virtual void setCloneable(bool c)
447        { mCloneable = c; }
448
449        /**
450        * Returns the parent container.
451        */
452        OverlayContainer* getParent() ;
453        void _setParent(OverlayContainer* parent) { mParent = parent; }
454
455        /**
456        * Returns the zOrder of the element
457        */
458        inline ushort getZOrder() const
459        { return mZOrder; }
460
461        /** Overridden from Renderable */
462        Real getSquaredViewDepth(const Camera* cam) const 
463        { 
464            return 10000 - getZOrder(); 
465        }
466
467        /** @copydoc Renderable::getLights */
468        const LightList& getLights(void) const
469        {
470            // Overlayelements should not be lit by the scene, this will not get called
471            static LightList ll;
472            return ll;
473        }
474
475        virtual void copyFromTemplate(OverlayElement* templateOverlay);
476        virtual OverlayElement* clone(const String& instanceName);
477
478        // Returns the SourceTemplate for this element
479        const OverlayElement* getSourceTemplate () const {
480          return mSourceTemplate ;
481        }
482    };
483
484
485
486}
487
488
489#endif
490
Note: See TracBrowser for help on using the repository browser.