Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/render2D/element_2d.h @ 7352

Last change on this file since 7352 was 7332, checked in by bensch, 19 years ago

orxonox/trunk: Layer Sorting works as expected

File size: 13.7 KB
RevLine 
[4838]1/*!
[4839]2 * @file element_2d.h
[6299]3 * Definition of the 2D elements rendered on top of all other stuff.
[5401]4 */
[1853]5
[4838]6#ifndef _ELEMENT_2D_H
7#define _ELEMENT_2D_H
[1853]8
[3543]9#include "base_object.h"
[5089]10
[5081]11#include "vector.h"
[7031]12#include "vector2D.h"
[5775]13#include <list>
[1853]14
[4838]15// FORWARD DECLARATION
[4843]16class PNode;
[4858]17class TiXmlElement;
[3543]18
[4839]19//!< An enumerator defining the Depth of a 2D-element.
20typedef enum
21{
[5398]22  E2D_LAYER_BELOW_ALL           =     0,        //!< Will be rendered below the 3D-scene. @todo make this work.
23  E2D_LAYER_BOTTOM              =     1,        //!< Will be rendered on the bottom Layer
24  E2D_LAYER_MEDIUM              =     2,        //!< Will be rendered on the medium Layer.
25  E2D_LAYER_TOP                 =     3,        //!< Will be rendered on top of everything else
[7330]26  E2D_LAYER_ABOVE_ALL           =     4,        //!< Will be rendered above everything else.
[4848]27
[7330]28  E2D_LAYER_COUNT               =     5,         //!< The count of Layers.
[4862]29} E2D_LAYER;
[5398]30#define E2D_DEFAULT_LAYER       E2D_LAYER_MEDIUM
[7330]31#define E2D_LAYER_ALL           5
[4839]32
[4843]33typedef enum
34{
[4856]35  E2D_ALIGN_NONE                =     0,
36  E2D_ALIGN_LEFT                =     1,
37  E2D_ALIGN_RIGHT               =     2,
38  E2D_ALIGN_CENTER              =     4,
39  E2D_ALIGN_SCREEN_CENTER       =     8
[4848]40} E2D_ALIGNMENT;
[4843]41
[5081]42typedef enum
43{
[6299]44  E2D_PARENT_NONE                    = 0x0000,
45  E2D_PARENT_LOCAL_ROTATE            = 0x0001,    //!< Rotates all the children around their centers.
46  E2D_PARENT_ROTATE_MOVEMENT         = 0x0002,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
[5081]47
[6299]48  E2D_PARENT_MOVEMENT                = 0x0004,    //!< Moves all children along with the parent.
[5081]49// special linkage modes
[6299]50  E2D_PARENT_ALL                     = 0x0003,    //!< Moves all children around the center of their parent, and also rotates their centers
51  E2D_PARENT_ROTATE_AND_MOVE         = 0x0005,    //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent.
52
53
54  // REPARENTING
55  E2D_REPARENT_TO_NULL               = 0x0010,    //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore.
56  E2D_REPARENT_TO_PARENTS_PARENT     = 0x0020,    //!< Reparents the Node to the parents (old) parent it the parent gets removed.
57  /////////////////////////////////////////////   //  ELSE: Reparents to the NullParent.
58  E2D_REPARENT_DELETE_CHILDREN       = 0x0040,    //!< Deletes the Children of the node when This Node is Removed. (Use with care).
59  /// FIXME
60   E2D_REPARENT_KEEP_POSITION         = 0x0080,   //!< Tries to keep the Position if the Node is reparented.
61
62
63  // DELETION
64  E2D_PROHIBIT_CHILD_DELETE          = 0x0100,    //!< Prohibits the Children from being deleted if this Node gets deleted.
65  E2D_PROHIBIT_DELETE_WITH_PARENT    = 0x0200,    //!< Prohibits the Node to be deleted if the Parent is. Child will be reparented according to the Repaenting-Rules
66  E2D_REPARENT_CHILDREN_ON_REMOVE    = 0x0400,    //!< Reparents the Children of the Node if the Node gets Removed.
67  E2D_REPARENT_ON_PARENTS_REMOVE     = 0x0800,    //!< The Node gets Reparented if its Parent gets removed. Child will be reparented according to the Reparenting-Rules.
68
69  // VISIBILITY/ACTIVITY
70  E2D_HIDE_CHILDREN_IF_HIDDEN        = 0x1000,    //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw))
71  E2D_HIDE_IF_PARENT_HIDDEN          = 0x2000,    //!< Prohibits the node from being drawn if the Parent is invisible.
72  E2D_UPDATE_CHILDREN_IF_INACTIVE    = 0x4000,    //!< Updates the Children of this Node even if the Parent is Inactive (note if this's parent is inactive children won't be updated.)
73  E2D_STATIC_NODE                    = 0x8000,    //!< Used for nodes that do not have any moving children, and that do not move.
74
[5081]75} E2D_PARENT_MODE;
[6299]76#define     E2D_PARENT_MODE_DEFAULT     E2D_PARENT_ALL | \
[6841]77                                        E2D_REPARENT_KEEP_POSITION
[5081]78
[5404]79//! A class for 2D-elements
80/**
81 * this class mainly tries to imitate PNode in its functionality, but on a 2D-level
82 * it extends PNode in the Sense of the tick-ability/draw-alility layering (and size)
83 *
84 * Layering: Layers are sorted into the Tree. e.g:
85 * the roor is in the lowest Layer, the leaves in the highest (of each branche)
86 * the first child of each node is in the lowest layer of all children, the last in the highest
87 * -> the tree is sorted on insertion of a new Child: @see Element2D::addChild2D()
88 */
[4849]89class Element2D : virtual public BaseObject {
[1853]90
[4850]91  public:
[6299]92    Element2D(Element2D* parent = Element2D::getNullElement(), E2D_LAYER layer = E2D_DEFAULT_LAYER, short nodeFlags = E2D_PARENT_MODE_DEFAULT);
[4850]93    virtual ~Element2D();
[1853]94
[6512]95    virtual void loadParams(const TiXmlElement* root);
[4858]96
[6299]97    // ACTIVATION //
98    inline void activate2D() { this->bActive = this->bRelCoorChanged = this->bRelDirChanged = true; };
99    inline void deactivate2D() { this->bActive = false; };
100    inline bool get2DActiveState() { return this->bActive; };
101
102    // ALIGNMENT //
[4856]103    /** @param alignment the new Alignment of the 2D-Element */
104    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
[7221]105    void setAlignment(const std::string& alignment);
[5089]106    inline E2D_ALIGNMENT getAlignment() const { return this->alignment; };
[4862]107
[6299]108    // LAYERING //
[4862]109    void setLayer(E2D_LAYER layer);
[7221]110    void setLayer(const std::string& layer);
[4862]111    /** @returns the Layer this Element is drawn to */
[5398]112    inline E2D_LAYER getLayer() const { return this->layer; };
[4862]113
[6299]114    // VISIBILITY //
[4850]115    /** @param visible true if the Element should be visible false otherwise (will not be rendered) */
[6299]116    inline void setVisibility(bool visible) { this->bVisible = visible; };
117    /** @returns the visibility state */
[6848]118    inline bool isVisible() const { return this->bVisible; };
[4862]119
[5068]120
[6299]121    // POSITIONAL (E2D-specials) //
[4850]122    /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */
[4856]123    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
[7221]124    void setBindNode(const std::string& bindNode);
[5089]125    inline const PNode* getBindNode() const { return this->bindNode; };
[4840]126
[5378]127    inline void setSize2D(float x, float y) { this->sizeX = x, this->sizeY = y; };
[7031]128    void setSizeSoft2D(float x, float y, float bias = 1.0);
[5378]129    inline void setSizeX2D(float x) { this->sizeX = x; };
130    inline void setSizeY2D(float y) { this->sizeY = y; };
131    inline float getSizeX2D() const { return this->sizeX; };
132    inline float getSizeY2D() const { return this->sizeY; };
133
[5401]134  public:
135    virtual void tick(float dt) {};
[6299]136    virtual void draw() const  {};
[5401]137    void tick2D(float dt);
[5404]138    void draw2D(short layer) const;
[5401]139
[5081]140    // LIKE PNODE
141  public:
[7316]142    void setRelCoor2D (const Vector2D& relCoord);
143    void setRelCoor2D (float x, float y);
[5089]144    void setRelCoor2Dpx (int x, int y);
[7316]145    void setRelCoorSoft2D (const Vector2D& relCoordSoft, float bias = 1.0);
146    void setRelCoorSoft2D (float x, float y, float bias = 1.0);
[5089]147    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
[5081]148    /** @returns the relative position */
[7316]149    inline const Vector2D& getRelCoor2D () const { return this->prevRelCoordinate; };
[5113]150    /** @returns the Relative Coordinate Destination */
[7316]151    inline const Vector2D& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; };
152    const Vector2D& getRelCoor2Dpx() const;
153    void setAbsCoor2D (const Vector2D& absCoord);
154    void setAbsCoor2D (float x, float y);
[5089]155    void setAbsCoor2Dpx (int x, int y);
[7316]156    void setAbsCoorSoft2D (const Vector2D& absCoordSoft, float bias = 1.0);
157    void setAbsCoorSoft2D (float x, float y, float bias = 1.0);
[5081]158    /** @returns the absolute position */
[7316]159    inline const Vector2D& getAbsCoor2D () const { return this->absCoordinate; };
160    const Vector2D& getAbsCoor2Dpx () const;
[5089]161
[7316]162    void shiftCoor2D (const Vector2D& shift);
[5089]163    void shiftCoor2Dpx (int x, int y);
[5081]164
165    void setRelDir2D (float relDir);
166    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
167    /** @returns the relative Direction */
168    inline float getRelDir2D () const { return this->prevRelDirection; };
[5113]169    /** @returns the Relative Directional Destination */
170    inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; };
[5081]171    void setAbsDir2D (float absDir);
[5414]172    void setAbsDirSoft2D (float absDirSoft, float bias = 1.0);
[5081]173    /** @returns the absolute Direction */
174    inline float getAbsDir2D () const { return this->absDirection; };
[5083]175    void shiftDir2D (float shiftDir);
[5081]176
177    /** @returns the Speed of the Node */
178    inline float getSpeed() const { return 0; };
179    /** @returns the Velocity of the Node */
[7316]180    inline const Vector2D& getVelocity() const { return this->velocity; };
[5081]181
182
[5403]183    void addChild2D (Element2D* child);
[7221]184    void addChild2D (const std::string& childName);
[5081]185    void removeChild2D (Element2D* child);
186    void remove2D();
187
[6299]188    /** @param parent the new parent of this Element2D */
189    void setParent2D (Element2D* parent) { parent->addChild2D(this); };
[7221]190    void setParent2D (const std::string& parentName);
[5081]191    /** @returns the parent of this Element2D */
[5397]192    inline Element2D* getParent2D () const { return this->parent; };
[5387]193    /** @returns the List of Children of this Element2D */
[5775]194    inline const std::list<Element2D*>& getChildren2D() const { return this->children; };
[5081]195
[5382]196    void setParentSoft2D(Element2D* parentNode, float bias = 1.0);
[7221]197    void setParentSoft2D(const std::string& parentName, float bias = 1.0);
[5081]198
[6299]199    void setParentMode2D (E2D_PARENT_MODE parentMode);
[7221]200    void setParentMode2D (const std::string& parentingMode);
[5081]201    /** @returns the Parenting mode of this node */
202    int getParentMode2D() const { return this->parentMode; };
203
[6299]204    // NULL_PARENT //
205    /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */
206    static Element2D* getNullElement()  { return (Element2D::nullElement != NULL)? Element2D::nullElement : Element2D::createNullElement(); };
207
208
[5081]209    void update2D (float dt);
210
[7052]211    void debug2D (unsigned int depth = 1, unsigned int level = 0) const;
[7332]212    void debug2DSC (unsigned int depth = 1, unsigned int level = 0) { this->debug2D(depth, level); }; // Wrapping Function for ShellCommand.
[5417]213    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,0,0), unsigned int level = 0) const;
[5081]214
215    // helper functions //
[7221]216    static const char* parentingModeToString2D(int parentingMode);
217    static E2D_PARENT_MODE stringToParentingMode2D(const std::string& parentingMode);
[5081]218
[5401]219    static const char* layer2DToChar(E2D_LAYER layer);
[7221]220    static E2D_LAYER charToLayer2D(const std::string& layer);
[5401]221
[7330]222    static bool layerSortPredicate(const Element2D* elem1, const Element2D* elem2);
223
[5081]224  private:
[6299]225    void eraseChild2D(Element2D* child);
[5081]226    /** tells the child that the parent's Coordinate has changed */
[6299]227    inline void parentCoorChanged2D () { this->bRelCoorChanged = true; }
[5081]228    /** tells the child that the parent's Direction has changed */
[6299]229    inline void parentDirChanged2D () { this->bRelDirChanged = true; }
[5081]230    /** @returns the last calculated coordinate */
[7316]231    inline Vector2D getLastAbsCoor2D() { return this->lastAbsCoordinate; }
[5081]232
[6299]233    void reparent2D();
234    static Element2D* createNullElement();
235    bool checkIntegrity(const Element2D* checkParent) const;
[4840]236
[5401]237
[5378]238  private:
239    const PNode*            bindNode;           //!< a node over which to display this 2D-element
[5371]240    float                   sizeX;              //!< The size of the rendered item in x-direction
241    float                   sizeY;              //!< The size of the rendered Item in y-direction
[7031]242    Vector2D*               toSize;             //!< The Size to iterate to.
[5371]243
[5089]244    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
[4856]245
[6299]246    bool                    bVisible;            //!< If the given Element2D is visible.
247    bool                    bActive;             //!< If the given Element2D is active.
[5089]248    E2D_LAYER               layer;              //!< What layer this Element2D is on.
[4856]249
[5089]250    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
251    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
[5081]252
[7316]253    Vector2D                relCoordinate;      //!< coordinates relative to the parent
254    Vector2D                absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
[5089]255    float                   relDirection;       //!< direction relative to the parent
[5211]256    float                   absDirection;       //!< absolute diretion in the world ( from (0,0,1) )
[5081]257
[7316]258    Vector2D                prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
259    Vector2D                lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
[5089]260    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
[5081]261
[7316]262    Vector2D                velocity;           //!< Saves the velocity.
[5081]263
[7316]264    Vector2D*               toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
[5382]265    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
[5089]266    float                   bias;               //!< how fast to iterate to the given position (default is 1)
[5081]267
[5089]268    Element2D*              parent;             //!< a pointer to the parent node
[5775]269    std::list<Element2D*>   children;           //!< list of the children of this Element2D
[5081]270
[5089]271    unsigned int            parentMode;         //!< the mode of the binding
[1853]272
[6299]273    static Element2D*       nullElement;        //!< The top-most Element
[5082]274};
275
[4838]276#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.