Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

File size: 12.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_BELOW_ALL                 =     1,        //!< Will be rendered below the 3D-scene. @todo make this work.
22  E2D_BOTTOM                    =     2,        //!< Will be rendered on the bottom Layer
23  E2D_MEDIUM                    =     4,        //!< Will be rendered on the medium Layer.
24  E2D_TOP                       =     8,        //!< Will be rendered on top of everything else
25
26  E2D_LAYER_COUNT               =     4         //!< The count of Layers.
27} E2D_LAYER;
28#define E2D_DEFAULT_LAYER       E2D_TOP
29#define E2D_ALL_LAYERS          E2D_TOP | E2D_MEDIUM | E2D_BOTTOM | E2D_BELOW_ALL
30
31typedef enum
32{
33  E2D_ALIGN_NONE                =     0,
34  E2D_ALIGN_LEFT                =     1,
35  E2D_ALIGN_RIGHT               =     2,
36  E2D_ALIGN_CENTER              =     4,
37  E2D_ALIGN_SCREEN_CENTER       =     8,
38} E2D_ALIGNMENT;
39
40typedef enum
41{
42  E2D_PARENT_NONE               =   0,
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();
66    Element2D(Element2D* parent);
67    virtual ~Element2D();
68
69    void init();
70    void loadParams(const TiXmlElement* root);
71
72    /** @param alignment the new Alignment of the 2D-Element */
73    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
74    void setAlignment(const char* alignment);
75    inline E2D_ALIGNMENT getAlignment() const { return this->alignment; };
76
77    void setLayer(E2D_LAYER layer);
78    void setLayer(const char* layer);
79    /** @returns the Layer this Element is drawn to */
80    inline E2D_LAYER getLayer() { return this->layer; };
81
82    /** @param visible true if the Element should be visible false otherwise (will not be rendered) */
83    inline void setVisibility(bool visible) { this->visible = visible; };
84    /** @param active true if the Element should be active, false otherwise (will not be ticked) */
85    inline void setActiveness(bool active) { this->active = active; };
86
87
88    /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */
89    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
90    void setBindNode(const char* bindNode);
91    inline const PNode* getBindNode() const { return this->bindNode; };
92
93    /** @returns the visibility state */
94    inline bool isVisible() { return this->visible; };
95    /** @returns the active-State */
96    inline bool isActive() { return this->active; };
97
98    /////////////
99    // SCALING //
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    // PNODE-STYLE-behavioral
113  public:
114    /////////////////
115    // COORDINATES //
116    /////////////////
117    // RELATIVE //
118    void setRelCoor2D (const Vector& relCoord);
119    /** @param x x-coordinate.  @param y y-coordinate. @param z z-coordinate. @see void PNode::setRelCoor2D (const Vector& absCoord) */
120    void setRelCoor2D (float x, float y, float dontCare = 1.0) { this->setRelCoor2D(Vector(x, y, dontCare)); };
121    void setRelCoor2Dpx (int x, int y);
122    inline void setRelCoor2Dpx (const Vector& relCoordpx) { this->setRelCoor2D(relCoordpx.x, relCoordpx.y); };
123    void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0);
124    /** set relative coordinates smoothely @param x x-relative coordinates to its parent
125     * @param y y-relative coordinates to its parent @param z z-relative coordinates to its parent
126     * @see  void PNode::setRelCoorSoft (const Vector&, float) */
127    void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0) { this->setRelCoorSoft2D(Vector(x,y, dontCare), bias); };
128    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
129    /** @returns the relative position */
130    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
131    /** @returns the Relative Coordinate Destination */
132    inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; };
133    Vector getRelCoor2Dpx() const;
134    // ABSOLUTE //
135    void setAbsCoor2D (const Vector& absCoord);
136    /** @param x x-coordinate.  @param y y-coordinate. @param z z-coordinate. @see void PNode::setAbsCoor2D (const Vector& absCoord) */
137    void setAbsCoor2D (float x, float y, float depth = 1.0) { this->setAbsCoor2D(Vector(x, y, depth)); };
138    void setAbsCoor2Dpx (int x, int y);
139    /** @returns the absolute position */
140    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
141    Vector getAbsCoor2Dpx () const;
142
143    void shiftCoor2D (const Vector& shift);
144    void shiftCoor2Dpx (int x, int y);
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    // DIRECTIONS //
153    ////////////////
154    // RELATIVE //
155    void setRelDir2D (float relDir);
156    void setRelDirSoft2D(float relDirSoft, float bias = 1.0);
157    /** @returns the relative Direction */
158    inline float getRelDir2D () const { return this->prevRelDirection; };
159    /** @returns the Relative Directional Destination */
160    inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; };
161    // ABSOLUTE //
162    void setAbsDir2D (float absDir);
163    /** @returns the absolute Direction */
164    inline float getAbsDir2D () const { return this->absDirection; };
165    void shiftDir2D (float shiftDir);
166    ///////////////
167    // PARENTING //
168    ///////////////
169    void addChild2D (Element2D* child, int parentingMode = E2D_PARENT_NONE);
170    void addChild2D (const char* childName);
171    void removeChild2D (Element2D* child);
172    void remove2D();
173
174    void setParent2D (Element2D* parent);
175    void setParent2D (const char* parentName);
176    /** @returns the parent of this Element2D */
177    Element2D* getParent () const { return this->parent; };
178
179    void softReparent2D(Element2D* parentNode, float bias = 1.0);
180    void softReparent2D(const char* parentName, float bias = 1.0);
181
182    /** @param parentMode sets the parentingMode of this Node */
183    void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; };
184    void setParentMode2D (const char* parentingMode);
185    /** @returns the Parenting mode of this node */
186    int getParentMode2D() const { return this->parentMode; };
187
188
189    //////////////
190    // HANDLING //
191    //////////////
192    void update2D (float dt);
193
194    void debug (unsigned int depth = 1, unsigned int level = 0) const;
195    void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const;
196
197    // helper functions //
198    static const char* parentingModeToChar2D(int parentingMode);
199    static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode);
200
201  private:
202    /** tells the child that the parent's Coordinate has changed */
203    inline void parentCoorChanged () { this->bRelCoorChanged = true; }
204    /** tells the child that the parent's Direction has changed */
205    inline void parentDirChanged () { this->bRelDirChanged = true; }
206    /** @returns the last calculated coordinate */
207    inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
208
209  public:
210    virtual void tick(float dt);
211    virtual void draw() const = 0;
212
213  private:
214    const PNode*            bindNode;           //!< a node over which to display this 2D-element
215    bool                    coordinateType;     //!< true if type is [0-1],[0-1]-style, false if type is [0-ResolutionX][0-ResolutionY]-style.
216
217    float                   sizeX;              //!< The size of the rendered item in x-direction
218    float                   sizeY;              //!< The size of the rendered Item in y-direction
219
220    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
221
222    bool                    visible;            //!< If the given Element2D is visible.
223    bool                    active;             //!< If the given Element2D is active.
224    E2D_LAYER               layer;              //!< What layer this Element2D is on.
225
226    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
227    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
228
229    Vector                  relCoordinate;      //!< coordinates relative to the parent
230    Vector                  absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
231    float                   relDirection;       //!< direction relative to the parent
232    float                   absDirection;       //!< absolute diretion in the world ( from (0,0,1) )
233
234    Vector                  prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
235    Vector                  lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
236    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
237
238    Vector                  velocity;           //!< Saves the velocity.
239
240    Vector*                 toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
241    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
242    float                   bias;               //!< how fast to iterate to the given position (default is 1)
243
244    Element2D*              parent;             //!< a pointer to the parent node
245    tList<Element2D>*       children;           //!< list of the children of this Element2D
246
247    unsigned int            parentMode;         //!< the mode of the binding
248};
249
250//! The top joint of all Element2D's every Element2D is somehow connected to this one.
251class NullElement2D : public Element2D {
252
253  public:
254    /** @returns a Pointer to the only object of this Class */
255    inline static NullElement2D* getInstance() { if (!NullElement2D::singletonRef) NullElement2D::singletonRef = new NullElement2D();  return NullElement2D::singletonRef; };
256    inline static bool isInstanciated() { return (NullElement2D::singletonRef != NULL)?true:false; };
257    virtual ~NullElement2D ();
258
259  private:
260    NullElement2D ();
261    virtual void draw() const {};
262
263  private:
264    static NullElement2D* singletonRef;        //!< A reference to the NullElement2D
265
266};
267
268#endif /* _ELEMENT_2D_H */
Note: See TracBrowser for help on using the repository browser.