Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: flush
i am going into the weekend now
@patrick: ther might be some strange behaviour, and i noticed, that the CD-engine is iterating more through the entities, than it should
I hope, this is just a minor bug, because it almost killed me in the process of developing
i do not exactly know if it is from the CD-engine, but it is producing different outputs when i start the game five times in a row

File size: 9.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#include "vector.h"
13
14// FORWARD DECLARATION
15class PNode;
16class TiXmlElement;
17template<class T> class tList;
18
19//!< An enumerator defining the Depth of a 2D-element.
20typedef enum
21{
22  E2D_BELOW_ALL                 =     1,        //!< Will be rendered below the 3D-scene. @todo make this work.
23  E2D_BOTTOM                    =     2,        //!< Will be rendered on the bottom Layer
24  E2D_MEDIUM                    =     4,        //!< Will be rendered on the medium Layer.
25  E2D_TOP                       =     8,        //!< Will be rendered on top of everything else
26
27  E2D_LAYER_COUNT               =     4         //!< The count of Layers.
28} E2D_LAYER;
29#define E2D_DEFAULT_LAYER       E2D_TOP
30#define E2D_ALL_LAYERS          E2D_TOP | E2D_MEDIUM | E2D_BOTTOM | E2D_BELOW_ALL
31
32typedef enum
33{
34  E2D_ALIGN_NONE                =     0,
35  E2D_ALIGN_LEFT                =     1,
36  E2D_ALIGN_RIGHT               =     2,
37  E2D_ALIGN_CENTER              =     4,
38  E2D_ALIGN_SCREEN_CENTER       =     8
39} E2D_ALIGNMENT;
40
41typedef enum
42{
43  E2D_PARENT_LOCAL_ROTATE       =   1,    //!< Rotates all the children around their centers.
44  E2D_PARENT_ROTATE_MOVEMENT    =   2,    //!< Moves all the children around the center of their parent, without the rotation around their own centers.
45
46  E2D_PARENT_MOVEMENT           =   4,    //!< Moves all children along with the parent.
47// special linkage modes
48  E2D_PARENT_ALL                =   3,    //!< Moves all children around the center of their parent, and also rotates their centers
49  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.
50} E2D_PARENT_MODE;
51#define     E2D_DEFAULT_PARENTING_MODE  E2D_PARENT_ALL
52
53//! A Struct defining the Position of an Element in 2D-space
54struct Position2D
55{
56  float       x;                 //!< The x-coordinate.
57  float       y;                 //!< The y-coordinate.
58  float       depth;             //!< The distance from the viewing plane.
59};
60
61//! A class for ...
62class Element2D : virtual public BaseObject {
63
64  public:
65    Element2D(Element2D* parent = NULL);
66    virtual ~Element2D();
67
68    void init(Element2D* parent = NULL);
69    void loadParams(const TiXmlElement* root);
70
71    /** @param xCoord the xCoordinate @param yCoord the y-Coordinate. These coordinates are Relative */
72    inline void setPosition2D(int xCoord, int yCoord) { this->relPos2D[0] = xCoord; this->relPos2D[1] = yCoord; };
73    /** this returns the Absolute Position on the screen set by positioning in the tick-phase */
74    inline const Position2D& getPosition2D() { return this->absPos2D; };
75
76    /** @param alignment the new Alignment of the 2D-Element */
77    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
78    void setAlignment(const char* 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
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    // LIKE PNODE
102  public:
103    void setRelCoor2D (const Vector& relCoord);
104    void setRelCoor2D (float x, float y, float dontCare);
105    void setRelCoorSoft2D(const Vector& relCoordSoft, float bias = 1.0);
106    void setRelCoorSoft2D(float x, float y, float dontCare, float bias = 1.0);
107    /** @returns the relative position */
108    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
109    void setAbsCoor2D (const Vector& absCoord);
110    void setAbsCoor2D (float x, float y, float depth);
111    /** @returns the absolute position */
112    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
113    void shiftCoor2D (const Vector& shift);
114
115    void setRelDir2D (float relDir);
116    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
117    /** @returns the relative Direction */
118    inline float getRelDir2D () const { return this->prevRelDirection; };
119    void setAbsDir2D (float absDir);
120    /** @returns the absolute Direction */
121    inline float getAbsDir2D () const { return this->absDirection; };
122    void shiftDir2D (float shiftDir);
123
124    /** @returns the Speed of the Node */
125    inline float getSpeed() const { return 0; };
126    /** @returns the Velocity of the Node */
127    inline const Vector& getVelocity() const { return this->velocity; };
128
129
130    void addChild2D (Element2D* child, int parentingMode = E2D_DEFAULT_PARENTING_MODE);
131    void addChild2D (const char* childName);
132    void removeChild2D (Element2D* child);
133    void remove2D();
134
135    void setParent2D (Element2D* parent);
136    void setParent2D (const char* parentName);
137    /** @returns the parent of this Element2D */
138    Element2D* getParent () const { return this->parent; };
139
140    void softReparent2D(Element2D* parentNode, float bias = 1.0);
141    void softReparent2D(const char* parentName, float bias = 1.0);
142
143    /** @param parentMode sets the parentingMode of this Node */
144    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
145    void setParentMode2D (const char* parentingMode);
146    /** @returns the Parenting mode of this node */
147    int getParentMode2D() const { return this->parentMode; };
148
149    void update2D (float dt);
150
151    void debug (unsigned int depth = 1, unsigned int level = 0) const;
152    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
153
154    // helper functions //
155    static const char* parentingModeToChar2D(int parentingMode);
156    static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode);
157
158  private:
159    /** tells the child that the parent's Coordinate has changed */
160    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
161    /** tells the child that the parent's Direction has changed */
162    inline void parentDirChanged () { this->bRelDirChanged = true; }
163    /** @returns the last calculated coordinate */
164    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
165
166  public:
167    virtual void tick(float dt);
168    virtual void draw() const = NULL;
169
170  protected:
171    void positioning();
172    //void Element2D(NullElement2D* nullElem);
173
174  protected:
175    const PNode*            bindNode;         //!< a node over which to display this 2D-element
176    int                     relPos2D[2];      //!< X-coord, Y-Coord (relative to the Coordinates of the alignment if given.)
177    Position2D              absPos2D;         //!< The absolute position of the 2D-Element.
178
179    E2D_ALIGNMENT           alignment;        //!< How the Element is aligned around its Position
180
181  private:
182    bool                    visible;          //!< If the given Element2D is visible.
183    bool                    active;           //!< If the given Element2D is active.
184    E2D_LAYER               layer;            //!< What layer this Element2D is on.
185
186
187  private:
188    bool              bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
189    bool              bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
190
191    Vector            relCoordinate;      //!< coordinates relative to the parent
192    Vector            absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
193    float             relDirection;       //!< direction relative to the parent
194    float             absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
195
196    Vector            prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
197    Vector            lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
198    float             prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
199//  Quaternion        lastAbsDirection;
200
201    Vector            velocity;           //!< Saves the velocity.
202
203    Vector*           toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
204    float*            toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
205    float             bias;               //!< how fast to iterate to the given position (default is 1)
206
207    Element2D*        parent;             //!< a pointer to the parent node
208    tList<Element2D>* children;           //!< list of the children of this Element2D
209
210    unsigned int      parentMode;         //!< the mode of the binding
211};
212
213//! The top joint of all Element2D's every Element2D is somehow connected to this one.
214class NullElement2D : public Element2D {
215
216  public:
217    /** @returns a Pointer to the only object of this Class */
218    inline static NullElement2D* getInstance() { if (!singletonRef) singletonRef = new NullElement2D();  return singletonRef; };
219    virtual ~NullElement2D ();
220
221  private:
222    NullElement2D ();
223    virtual void draw() const {};
224
225  private:
226    static NullElement2D* singletonRef;        //!< A reference to the NullElement2D
227
228};
229
230#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.