Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: rebuild Element2D. Now it is more like PNode

File size: 9.7 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_LOCAL_ROTATE       =   1,    //!< Rotates all the children around their centers.
45  E2D_PARENT_ROTATE_MOVEMENT    =   2,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
46
47  E2D_PARENT_MOVEMENT           =   4,    //!< Moves all children along with the parent.
48// special linkage modes
49  E2D_PARENT_ALL                =   3,    //!< Moves all children around the center of their parent, and also rotates their centers
50  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.
51} E2D_PARENT_MODE;
52#define     E2D_DEFAULT_PARENTING_MODE  E2D_PARENT_ALL
53
54//! A Struct defining the Position of an Element in 2D-space
55struct Position2D
56{
57  float       x;                 //!< The x-coordinate.
58  float       y;                 //!< The y-coordinate.
59  float       depth;             //!< The distance from the viewing plane.
60};
61
62//! A class for ...
63class Element2D : virtual public BaseObject {
64
65  public:
66    Element2D();
67    Element2D(Element2D* parent);
68    virtual ~Element2D();
69
70    void init();
71    void loadParams(const TiXmlElement* root);
72
73    /** @param alignment the new Alignment of the 2D-Element */
74    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
75    void setAlignment(const char* alignment);
76    inline E2D_ALIGNMENT getAlignment() const { return this->alignment; };
77
78    void setLayer(E2D_LAYER layer);
79    void setLayer(const char* layer);
80    /** @returns the Layer this Element is drawn to */
81    inline E2D_LAYER getLayer() { return this->layer; };
82
83    /** @param visible true if the Element should be visible false otherwise (will not be rendered) */
84    inline void setVisibility(bool visible) { this->visible = visible; };
85    /** @param active true if the Element should be active, false otherwise (will not be ticked) */
86    inline void setActiveness(bool active) { this->active = active; };
87
88
89    /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */
90    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
91    void setBindNode(const char* bindNode);
92    inline const PNode* getBindNode() const { return this->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 = 1.0);
104    void setRelCoor2Dpx (int x, int y);
105    void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0);
106    void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0);
107    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
108    /** @returns the relative position */
109    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
110    const Vector& getRelCoor2Dpx() const;
111    void setAbsCoor2D (const Vector& absCoord);
112    void setAbsCoor2D (float x, float y, float depth = 1.0);
113    void setAbsCoor2Dpx (int x, int y);
114    /** @returns the absolute position */
115    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
116    const Vector& getAbsCoor2Dpx () const;
117
118    void shiftCoor2D (const Vector& shift);
119    void shiftCoor2Dpx (int x, int y);
120
121    void setRelDir2D (float relDir);
122    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
123    /** @returns the relative Direction */
124    inline float getRelDir2D () const { return this->prevRelDirection; };
125    void setAbsDir2D (float absDir);
126    /** @returns the absolute Direction */
127    inline float getAbsDir2D () const { return this->absDirection; };
128    void shiftDir2D (float shiftDir);
129
130    /** @returns the Speed of the Node */
131    inline float getSpeed() const { return 0; };
132    /** @returns the Velocity of the Node */
133    inline const Vector& getVelocity() const { return this->velocity; };
134
135
136    void addChild2D (Element2D* child, int parentingMode = E2D_DEFAULT_PARENTING_MODE);
137    void addChild2D (const char* childName);
138    void removeChild2D (Element2D* child);
139    void remove2D();
140
141    void setParent2D (Element2D* parent);
142    void setParent2D (const char* parentName);
143    /** @returns the parent of this Element2D */
144    Element2D* getParent () const { return this->parent; };
145
146    void softReparent2D(Element2D* parentNode, float bias = 1.0);
147    void softReparent2D(const char* parentName, float bias = 1.0);
148
149    /** @param parentMode sets the parentingMode of this Node */
150    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
151    void setParentMode2D (const char* parentingMode);
152    /** @returns the Parenting mode of this node */
153    int getParentMode2D() const { return this->parentMode; };
154
155    void update2D (float dt);
156
157    void debug (unsigned int depth = 1, unsigned int level = 0) const;
158    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
159
160    // helper functions //
161    static const char* parentingModeToChar2D(int parentingMode);
162    static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode);
163
164  private:
165    /** tells the child that the parent's Coordinate has changed */
166    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
167    /** tells the child that the parent's Direction has changed */
168    inline void parentDirChanged () { this->bRelDirChanged = true; }
169    /** @returns the last calculated coordinate */
170    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
171
172  public:
173    virtual void tick(float dt);
174    virtual void draw() const = NULL;
175
176  private:
177    const PNode*            bindNode;           //!< a node over which to display this 2D-element
178
179    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
180
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    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
186    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
187
188    Vector                  relCoordinate;      //!< coordinates relative to the parent
189    Vector                  absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
190    float                   relDirection;       //!< direction relative to the parent
191    float                   absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
192
193    Vector                  prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
194    Vector                  lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
195    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
196//  float                   lastAbsDirection;
197
198    Vector                  velocity;           //!< Saves the velocity.
199
200    Vector*                 toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
201    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
202    float                   bias;               //!< how fast to iterate to the given position (default is 1)
203
204    Element2D*              parent;             //!< a pointer to the parent node
205    tList<Element2D>*       children;           //!< list of the children of this Element2D
206
207    unsigned int            parentMode;         //!< the mode of the binding
208};
209
210//! The top joint of all Element2D's every Element2D is somehow connected to this one.
211class NullElement2D : public Element2D {
212
213  public:
214    /** @returns a Pointer to the only object of this Class */
215    inline static NullElement2D* getInstance() { if (!singletonRef) singletonRef = new NullElement2D();  return singletonRef; };
216    virtual ~NullElement2D ();
217
218  private:
219    NullElement2D ();
220    virtual void draw() const {};
221
222  private:
223    static NullElement2D* singletonRef;        //!< A reference to the NullElement2D
224
225};
226
227#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.