Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6818 was 6512, checked in by bensch, 19 years ago

orxonox/trunk: loadParams is now virtual.
ALL THE CLASSES HAVE TO CALL

SuperClass::loadParams(root);

isntead of:
static_cast<SuperClass*>(this)→loadParams(root);

which was quite stupid anyways

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