Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D: PNode-style declaration

File size: 9.2 KB
Line 
1/*!
2 * @file element_2d.h
3 * Definition of the 2D elements rendered on top through the GraphicsEngine
4 * @todo reimplement it, so it looks just like PNode.
5*/
6
7#ifndef _ELEMENT_2D_H
8#define _ELEMENT_2D_H
9
10#include "base_object.h"
11#include "vector.h"
12
13// FORWARD DECLARATION
14class PNode;
15class TiXmlElement;
16template<class T> class tList;
17
18//!< An enumerator defining the Depth of a 2D-element.
19typedef enum
20{
21  E2D_BELOW_ALL                 =     1,        //!< Will be rendered below the 3D-scene. @todo make this work.
22  E2D_BOTTOM                    =     2,        //!< Will be rendered on the bottom Layer
23  E2D_MEDIUM                    =     4,        //!< Will be rendered on the medium Layer.
24  E2D_TOP                       =     8,        //!< Will be rendered on top of everything else
25
26  E2D_LAYER_COUNT               =     4         //!< The count of Layers.
27} E2D_LAYER;
28#define E2D_DEFAULT_LAYER       E2D_TOP
29#define E2D_ALL_LAYERS          E2D_TOP | E2D_MEDIUM | E2D_BOTTOM | E2D_BELOW_ALL
30
31typedef enum
32{
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
38} E2D_ALIGNMENT;
39
40typedef enum
41{
42  E2D_LOCAL_ROTATE          =   1,    //!< Rotates all the children around their centers.
43  E2D_ROTATE_MOVEMENT       =   2,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
44
45  E2D_MOVEMENT              =   4,    //!< Moves all children along with the parent.
46// special linkage modes
47  E2D_ALL                   =   3,    //!< Moves all children around the center of their parent, and also rotates their centers
48  E2D_ROTATE_AND_MOVE       =   5     //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent.
49} E2D_PARENT_MODE;
50#define     E2D_DEFAULT_PARENTING_MODE  E2D_ALL
51
52//! A Struct defining the Position of an Element in 2D-space
53struct Position2D
54{
55  float       x;                 //!< The x-coordinate.
56  float       y;                 //!< The y-coordinate.
57  float       depth;             //!< The distance from the viewing plane.
58};
59
60//! A class for ...
61class Element2D : virtual public BaseObject {
62
63  public:
64    Element2D();
65    virtual ~Element2D();
66
67    void init();
68    void loadParams(const TiXmlElement* root);
69
70    /** @param xCoord the xCoordinate @param yCoord the y-Coordinate. These coordinates are Relative */
71    inline void setPosition2D(int xCoord, int yCoord) { this->relPos2D[0] = xCoord; this->relPos2D[1] = yCoord; };
72    /** this returns the Absolute Position on the screen set by positioning in the tick-phase */
73    inline const Position2D& getPosition2D() { return this->absPos2D; };
74
75    /** @param alignment the new Alignment of the 2D-Element */
76    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
77    void setAlignment(const char* alignment);
78
79    void setLayer(E2D_LAYER layer);
80    void setLayer(const char* layer);
81    /** @returns the Layer this Element is drawn to */
82    inline E2D_LAYER getLayer() { return this->layer; };
83
84    /** @param visible true if the Element should be visible false otherwise (will not be rendered) */
85    inline void setVisibility(bool visible) { this->visible = visible; };
86    /** @param active true if the Element should be active, false otherwise (will not be ticked) */
87    inline void setActiveness(bool active) { this->active = active; };
88
89
90    /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */
91    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
92    void setBindNode(const char* bindNode);
93
94    /** @returns the visibility state */
95    inline bool isVisible() { return this->visible; };
96    /** @returns the active-State */
97    inline bool isActive() { return this->active; };
98
99
100    // LIKE PNODE
101  public:
102    void setRelCoor2D (const Vector& relCoord);
103    void setRelCoor2D (float x, float y, float dontCare);
104    void setRelCoorSoft2D(const Vector& relCoordSoft, float bias = 1.0);
105    void setRelCoorSoft2D(float x, float y, float dontCare, float bias = 1.0);
106    /** @returns the relative position */
107    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
108    void setAbsCoor2D (const Vector& absCoord);
109    void setAbsCoor2D (float x, float y, float depth);
110    /** @returns the absolute position */
111    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
112    void shiftCoor (const Vector& shift);
113
114    void setRelDir2D (float relDir);
115    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
116    /** @returns the relative Direction */
117    inline float getRelDir2D () const { return this->prevRelDirection; };
118    void setAbsDir2D (float absDir);
119    /** @returns the absolute Direction */
120    inline float getAbsDir2D () const { return this->absDirection; };
121    void shiftDir (float shiftDir);
122
123    /** @returns the Speed of the Node */
124    inline float getSpeed() const { return 0; };
125    /** @returns the Velocity of the Node */
126    inline const Vector& getVelocity() const { return this->velocity; };
127
128
129    void addChild2D (Element2D* child, int parentingMode = E2D_DEFAULT_PARENTING_MODE);
130    void addChild2D (const char* childName);
131    void removeChild2D (Element2D* child);
132    void remove2D();
133
134    void setParent2D (Element2D* parent);
135    void setParent2D (const char* parentName);
136    /** @returns the parent of this Element2D */
137    Element2D* getParent () const { return this->parent; };
138
139    void softReparent(PNode* parentNode, float bias = 1.0);
140    void softReparent(const char* parentName, float bias = 1.0);
141
142    /** @param parentMode sets the parentingMode of this Node */
143    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
144    void setParentMode2D (const char* parentingMode);
145    /** @returns the Parenting mode of this node */
146    int getParentMode2D() const { return this->parentMode; };
147
148    void update2D (float dt);
149
150    void debug (unsigned int depth = 1, unsigned int level = 0) const;
151    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
152
153    // helper functions //
154    static const char* parentingModeToChar(int parentingMode);
155    static E2D_PARENT_MODE charToParentingMode(const char* parentingMode);
156
157  private:
158    void init(Element2D* parent);
159    /** tells the child that the parent's Coordinate has changed */
160    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
161    /** tells the child that the parent's Direction has changed */
162    inline void parentDirChanged () { this->bRelDirChanged = true; }
163    /** @returns the last calculated coordinate */
164    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
165
166  public:
167    virtual void tick(float dt);
168    virtual void draw() const = NULL;
169
170  protected:
171    void positioning();
172
173  protected:
174    const PNode*            bindNode;         //!< a node over which to display this 2D-element
175    int                     relPos2D[2];      //!< X-coord, Y-Coord (relative to the Coordinates of the alignment if given.)
176    Position2D              absPos2D;         //!< The absolute position of the 2D-Element.
177
178    E2D_ALIGNMENT           alignment;        //!< How the Element is aligned around its Position
179
180  private:
181    bool                    visible;          //!< If the given Element2D is visible.
182    bool                    active;           //!< If the given Element2D is active.
183    E2D_LAYER               layer;            //!< What layer this Element2D is on.
184
185
186
187
188
189  private:
190    bool              bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
191    bool              bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
192
193    Vector            relCoordinate;      //!< coordinates relative to the parent
194    Vector            absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
195    float             relDirection;       //!< direction relative to the parent
196    float             absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
197
198    Vector            prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
199    Vector            lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
200    float             prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
201//  Quaternion        lastAbsDirection;
202
203    Vector            velocity;           //!< Saves the velocity.
204
205    Vector*           toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
206    float*            toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
207    float             bias;               //!< how fast to iterate to the given position (default is 1)
208
209    Element2D*        parent;             //!< a pointer to the parent node
210    tList<Element2D>* children;           //!< list of the children of this Element2D
211
212    unsigned int      parentMode;         //!< the mode of the binding
213};
214
215
216#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.