Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: PNode and Element2D can now return their children-lists as const tList<type>*

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