Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/graphics/render2D/element_2d.h @ 5380

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

orxonox/trunk: some 2d-adaptions

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