Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: promised checking should be working now.

File size: 11.0 KB
Line 
1/*!
2 * @file element_2d.h
3 * Definition of the 2D elements rendered on top through the GraphicsEngine
4 */
5
6#ifndef _ELEMENT_2D_H
7#define _ELEMENT_2D_H
8
9#include "base_object.h"
10
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_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
25
26  E2D_LAYER_COUNT               =     4,         //!< The count of Layers.
27
28  E2D_LAYER_EXTERN              =    -1,         //!< In a Layers handeled externally.
29
30  E2D_LAYER_ALL                 =     5,
31} E2D_LAYER;
32#define E2D_DEFAULT_LAYER       E2D_LAYER_MEDIUM
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, E2D_LAYER layer = E2D_DEFAULT_LAYER);
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() const { 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  public:
109    virtual void tick(float dt) {};
110    virtual void draw() const = 0;
111    void tick2D(float dt);
112    void draw2D(E2D_LAYER layer) const;
113
114    // LIKE PNODE
115  public:
116    void setRelCoor2D (const Vector& relCoord);
117    void setRelCoor2D (float x, float y, float dontCare = 1.0);
118    void setRelCoor2Dpx (int x, int y);
119    void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0);
120    void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0);
121    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
122    /** @returns the relative position */
123    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
124    /** @returns the Relative Coordinate Destination */
125    inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; };
126    const Vector& getRelCoor2Dpx() const;
127    void setAbsCoor2D (const Vector& absCoord);
128    void setAbsCoor2D (float x, float y, float depth = 1.0);
129    void setAbsCoor2Dpx (int x, int y);
130    /** @returns the absolute position */
131    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
132    const Vector& getAbsCoor2Dpx () const;
133
134    void shiftCoor2D (const Vector& shift);
135    void shiftCoor2Dpx (int x, int y);
136
137    void setRelDir2D (float relDir);
138    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
139    /** @returns the relative Direction */
140    inline float getRelDir2D () const { return this->prevRelDirection; };
141    /** @returns the Relative Directional Destination */
142    inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; };
143    void setAbsDir2D (float absDir);
144    /** @returns the absolute Direction */
145    inline float getAbsDir2D () const { return this->absDirection; };
146    void shiftDir2D (float shiftDir);
147
148    /** @returns the Speed of the Node */
149    inline float getSpeed() const { return 0; };
150    /** @returns the Velocity of the Node */
151    inline const Vector& getVelocity() const { return this->velocity; };
152
153
154    void addChild2D (Element2D* child, int parentingMode = E2D_PARENT_NONE);
155    void addChild2D (const char* childName);
156    void removeChild2D (Element2D* child);
157    void remove2D();
158
159    void setParent2D (Element2D* parent);
160    void setParent2D (const char* parentName);
161    /** @returns the parent of this Element2D */
162    inline Element2D* getParent2D () const { return this->parent; };
163    /** @returns the List of Children of this Element2D */
164    inline const tList<Element2D>* getChildren2D() const { return this->children; };
165
166    void setParentSoft2D(Element2D* parentNode, float bias = 1.0);
167    void setParentSoft2D(const char* parentName, float bias = 1.0);
168
169    /** @param parentMode sets the parentingMode of this Node */
170    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
171    void setParentMode2D (const char* parentingMode);
172    /** @returns the Parenting mode of this node */
173    int getParentMode2D() const { return this->parentMode; };
174
175    void update2D (float dt);
176
177    void debug (unsigned int depth = 1, unsigned int level = 0) const;
178    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
179
180    // helper functions //
181    static const char* parentingModeToChar2D(int parentingMode);
182    static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode);
183
184    static const char* layer2DToChar(E2D_LAYER layer);
185    static E2D_LAYER charToLayer2D(const char* layer);
186
187  private:
188    /** tells the child that the parent's Coordinate has changed */
189    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
190    /** tells the child that the parent's Direction has changed */
191    inline void parentDirChanged () { this->bRelDirChanged = true; }
192    /** @returns the last calculated coordinate */
193    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
194
195
196
197  private:
198    const PNode*            bindNode;           //!< a node over which to display this 2D-element
199    float                   sizeX;              //!< The size of the rendered item in x-direction
200    float                   sizeY;              //!< The size of the rendered Item in y-direction
201
202    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
203
204    bool                    visible;            //!< If the given Element2D is visible.
205    bool                    active;             //!< If the given Element2D is active.
206    E2D_LAYER               layer;              //!< What layer this Element2D is on.
207
208    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
209    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
210
211    Vector                  relCoordinate;      //!< coordinates relative to the parent
212    Vector                  absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
213    float                   relDirection;       //!< direction relative to the parent
214    float                   absDirection;       //!< absolute diretion in the world ( from (0,0,1) )
215
216    Vector                  prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
217    Vector                  lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
218    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
219
220    Vector                  velocity;           //!< Saves the velocity.
221
222    Vector*                 toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft)
223    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft)
224    float                   bias;               //!< how fast to iterate to the given position (default is 1)
225
226    Element2D*              parent;             //!< a pointer to the parent node
227    tList<Element2D>*       children;           //!< list of the children of this Element2D
228
229    unsigned int            parentMode;         //!< the mode of the binding
230};
231
232//! The top joint of all Element2D's every Element2D is somehow connected to this one.
233class NullElement2D : public Element2D {
234
235  public:
236    /** @returns a Pointer to the only object of this Class */
237    inline static NullElement2D* getInstance() { if (!NullElement2D::singletonRef) NullElement2D::singletonRef = new NullElement2D();  return NullElement2D::singletonRef; };
238    inline static bool isInstanciated() { return (NullElement2D::singletonRef != NULL)?true:false; };
239    virtual ~NullElement2D ();
240
241  private:
242    NullElement2D ();
243    virtual void draw() const {};
244
245  private:
246    static NullElement2D* singletonRef;        //!< A reference to the NullElement2D
247
248};
249
250#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.