Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Elemet2D-drawing better
prevent segfault in setParent with NULL as new Parent in Element2D and PNode

File size: 10.6 KB
Line 
1/*!
2 * @file element_2d.h
3 * Definition of the 2D elements rendered on top through the GraphicsEngine
4 *
5 * @todo reimplement it, so it looks just like PNode.
6*/
7
8#ifndef _ELEMENT_2D_H
9#define _ELEMENT_2D_H
10
11#include "base_object.h"
12
13#include "vector.h"
14
15// FORWARD DECLARATION
16class PNode;
17class TiXmlElement;
18template<class T> class tList;
19
20//!< An enumerator defining the Depth of a 2D-element.
21typedef enum
22{
23  E2D_BELOW_ALL,       //!< Will be rendered below the 3D-scene. @todo make this work.
24  E2D_BOTTOM,          //!< Will be rendered on the bottom Layer
25  E2D_MEDIUM,          //!< Will be rendered on the medium Layer.
26  E2D_TOP,             //!< Will be rendered on top of everything else
27
28  E2D_LAYER_COUNT,     //!< The count of Layers.
29
30  E2D_LAYER_ALL,
31} E2D_LAYER;
32#define E2D_DEFAULT_LAYER       E2D_TOP
33
34typedef enum
35{
36  E2D_ALIGN_NONE                =     0,
37  E2D_ALIGN_LEFT                =     1,
38  E2D_ALIGN_RIGHT               =     2,
39  E2D_ALIGN_CENTER              =     4,
40  E2D_ALIGN_SCREEN_CENTER       =     8
41} E2D_ALIGNMENT;
42
43typedef enum
44{
45  E2D_PARENT_NONE               =   0,
46  E2D_PARENT_LOCAL_ROTATE       =   1,    //!< Rotates all the children around their centers.
47  E2D_PARENT_ROTATE_MOVEMENT    =   2,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
48
49  E2D_PARENT_MOVEMENT           =   4,    //!< Moves all children along with the parent.
50// special linkage modes
51  E2D_PARENT_ALL                =   3,    //!< Moves all children around the center of their parent, and also rotates their centers
52  E2D_PARENT_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.
53} E2D_PARENT_MODE;
54#define     E2D_DEFAULT_PARENTING_MODE  E2D_PARENT_ALL
55
56//! A Struct defining the Position of an Element in 2D-space
57struct Position2D
58{
59  float       x;                 //!< The x-coordinate.
60  float       y;                 //!< The y-coordinate.
61  float       depth;             //!< The distance from the viewing plane.
62};
63
64//! A class for ...
65class Element2D : virtual public BaseObject {
66
67  public:
68    Element2D();
69    Element2D(Element2D* parent);
70    virtual ~Element2D();
71
72    void init();
73    void loadParams(const TiXmlElement* root);
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    inline E2D_ALIGNMENT getAlignment() const { return this->alignment; };
79
80    void setLayer(E2D_LAYER layer);
81    void setLayer(const char* layer);
82    /** @returns the Layer this Element is drawn to */
83    inline E2D_LAYER getLayer() { return this->layer; };
84
85    /** @param visible true if the Element should be visible false otherwise (will not be rendered) */
86    inline void setVisibility(bool visible) { this->visible = visible; };
87    /** @param active true if the Element should be active, false otherwise (will not be ticked) */
88    inline void setActiveness(bool active) { this->active = active; };
89
90
91    /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */
92    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
93    void setBindNode(const char* bindNode);
94    inline const PNode* getBindNode() const { return this->bindNode; };
95
96    /** @returns the visibility state */
97    inline bool isVisible() { return this->visible; };
98    /** @returns the active-State */
99    inline bool isActive() { return this->active; };
100
101
102    inline void setSize2D(float x, float y) { this->sizeX = x, this->sizeY = y; };
103    inline void setSizeX2D(float x) { this->sizeX = x; };
104    inline void setSizeY2D(float y) { this->sizeY = y; };
105    inline float getSizeX2D() const { return this->sizeX; };
106    inline float getSizeY2D() const { return this->sizeY; };
107
108    // LIKE PNODE
109  public:
110    void setRelCoor2D (const Vector& relCoord);
111    void setRelCoor2D (float x, float y, float dontCare = 1.0);
112    void setRelCoor2Dpx (int x, int y);
113    void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0);
114    void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0);
115    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
116    /** @returns the relative position */
117    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
118    /** @returns the Relative Coordinate Destination */
119    inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; };
120    const Vector& getRelCoor2Dpx() const;
121    void setAbsCoor2D (const Vector& absCoord);
122    void setAbsCoor2D (float x, float y, float depth = 1.0);
123    void setAbsCoor2Dpx (int x, int y);
124    /** @returns the absolute position */
125    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
126    const Vector& getAbsCoor2Dpx () const;
127
128    void shiftCoor2D (const Vector& shift);
129    void shiftCoor2Dpx (int x, int y);
130
131    void setRelDir2D (float relDir);
132    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
133    /** @returns the relative Direction */
134    inline float getRelDir2D () const { return this->prevRelDirection; };
135    /** @returns the Relative Directional Destination */
136    inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; };
137    void setAbsDir2D (float absDir);
138    /** @returns the absolute Direction */
139    inline float getAbsDir2D () const { return this->absDirection; };
140    void shiftDir2D (float shiftDir);
141
142    /** @returns the Speed of the Node */
143    inline float getSpeed() const { return 0; };
144    /** @returns the Velocity of the Node */
145    inline const Vector& getVelocity() const { return this->velocity; };
146
147
148    void addChild2D (Element2D* child, int parentingMode = E2D_PARENT_NONE);
149    void addChild2D (const char* childName);
150    void removeChild2D (Element2D* child);
151    void remove2D();
152
153    void setParent2D (Element2D* parent);
154    void setParent2D (const char* parentName);
155    /** @returns the parent of this Element2D */
156    inline Element2D* getParent2D () const { return this->parent; };
157    /** @returns the List of Children of this Element2D */
158    inline const tList<Element2D>* getChildren2D() const { return this->children; };
159
160    void setParentSoft2D(Element2D* parentNode, float bias = 1.0);
161    void setParentSoft2D(const char* parentName, float bias = 1.0);
162
163    /** @param parentMode sets the parentingMode of this Node */
164    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
165    void setParentMode2D (const char* parentingMode);
166    /** @returns the Parenting mode of this node */
167    int getParentMode2D() const { return this->parentMode; };
168
169    void update2D (float dt);
170
171    void debug (unsigned int depth = 1, unsigned int level = 0) const;
172    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
173
174    // helper functions //
175    static const char* parentingModeToChar2D(int parentingMode);
176    static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode);
177
178  private:
179    /** tells the child that the parent's Coordinate has changed */
180    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
181    /** tells the child that the parent's Direction has changed */
182    inline void parentDirChanged () { this->bRelDirChanged = true; }
183    /** @returns the last calculated coordinate */
184    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
185
186  public:
187    virtual void tick(float dt);
188    virtual void draw() const = 0;
189
190  private:
191    const PNode*            bindNode;           //!< a node over which to display this 2D-element
192    float                   sizeX;              //!< The size of the rendered item in x-direction
193    float                   sizeY;              //!< The size of the rendered Item in y-direction
194
195    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
196
197    bool                    visible;            //!< If the given Element2D is visible.
198    bool                    active;             //!< If the given Element2D is active.
199    E2D_LAYER               layer;              //!< What layer this Element2D is on.
200
201    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
202    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
203
204    Vector                  relCoordinate;      //!< coordinates relative to the parent
205    Vector                  absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
206    float                   relDirection;       //!< direction relative to the parent
207    float                   absDirection;       //!< absolute diretion in the world ( from (0,0,1) )
208
209    Vector                  prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
210    Vector                  lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
211    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
212
213    Vector                  velocity;           //!< Saves the velocity.
214
215    Vector*                 toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
216    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
217    float                   bias;               //!< how fast to iterate to the given position (default is 1)
218
219    Element2D*              parent;             //!< a pointer to the parent node
220    tList<Element2D>*       children;           //!< list of the children of this Element2D
221
222    unsigned int            parentMode;         //!< the mode of the binding
223};
224
225//! The top joint of all Element2D's every Element2D is somehow connected to this one.
226class NullElement2D : public Element2D {
227
228  public:
229    /** @returns a Pointer to the only object of this Class */
230    inline static NullElement2D* getInstance() { if (!NullElement2D::singletonRef) NullElement2D::singletonRef = new NullElement2D();  return NullElement2D::singletonRef; };
231    inline static bool isInstanciated() { return (NullElement2D::singletonRef != NULL)?true:false; };
232    virtual ~NullElement2D ();
233
234  private:
235    NullElement2D ();
236    virtual void draw() const {};
237
238  private:
239    static NullElement2D* singletonRef;        //!< A reference to the NullElement2D
240
241};
242
243#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.