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 | #include <list> |
---|
13 | |
---|
14 | // FORWARD DECLARATION |
---|
15 | class PNode; |
---|
16 | class TiXmlElement; |
---|
17 | |
---|
18 | //!< An enumerator defining the Depth of a 2D-element. |
---|
19 | typedef enum |
---|
20 | { |
---|
21 | E2D_LAYER_BELOW_ALL = 0, //!< Will be rendered below the 3D-scene. @todo make this work. |
---|
22 | E2D_LAYER_BOTTOM = 1, //!< Will be rendered on the bottom Layer |
---|
23 | E2D_LAYER_MEDIUM = 2, //!< Will be rendered on the medium Layer. |
---|
24 | E2D_LAYER_TOP = 3, //!< 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_LAYER_MEDIUM |
---|
29 | #define E2D_LAYER_ALL 4 |
---|
30 | |
---|
31 | typedef 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 | |
---|
40 | typedef 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 |
---|
54 | struct 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 2D-elements |
---|
62 | /** |
---|
63 | * this class mainly tries to imitate PNode in its functionality, but on a 2D-level |
---|
64 | * it extends PNode in the Sense of the tick-ability/draw-alility layering (and size) |
---|
65 | * |
---|
66 | * Layering: Layers are sorted into the Tree. e.g: |
---|
67 | * the roor is in the lowest Layer, the leaves in the highest (of each branche) |
---|
68 | * the first child of each node is in the lowest layer of all children, the last in the highest |
---|
69 | * -> the tree is sorted on insertion of a new Child: @see Element2D::addChild2D() |
---|
70 | */ |
---|
71 | class Element2D : virtual public BaseObject { |
---|
72 | |
---|
73 | public: |
---|
74 | Element2D(); |
---|
75 | Element2D(Element2D* parent, E2D_LAYER layer = E2D_DEFAULT_LAYER); |
---|
76 | virtual ~Element2D(); |
---|
77 | |
---|
78 | void init(); |
---|
79 | void loadParams(const TiXmlElement* root); |
---|
80 | |
---|
81 | /** @param alignment the new Alignment of the 2D-Element */ |
---|
82 | inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; }; |
---|
83 | void setAlignment(const char* alignment); |
---|
84 | inline E2D_ALIGNMENT getAlignment() const { return this->alignment; }; |
---|
85 | |
---|
86 | void setLayer(E2D_LAYER layer); |
---|
87 | void setLayer(const char* layer); |
---|
88 | /** @returns the Layer this Element is drawn to */ |
---|
89 | inline E2D_LAYER getLayer() const { return this->layer; }; |
---|
90 | |
---|
91 | /** @param visible true if the Element should be visible false otherwise (will not be rendered) */ |
---|
92 | inline void setVisibility(bool visible) { this->visible = visible; }; |
---|
93 | /** @param active true if the Element should be active, false otherwise (will not be ticked) */ |
---|
94 | inline void setActiveness(bool active) { this->active = active; }; |
---|
95 | |
---|
96 | |
---|
97 | /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */ |
---|
98 | inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; }; |
---|
99 | void setBindNode(const char* bindNode); |
---|
100 | inline const PNode* getBindNode() const { return this->bindNode; }; |
---|
101 | |
---|
102 | /** @returns the visibility state */ |
---|
103 | inline bool isVisible() { return this->visible; }; |
---|
104 | /** @returns the active-State */ |
---|
105 | inline bool isActive() { return this->active; }; |
---|
106 | |
---|
107 | |
---|
108 | inline void setSize2D(float x, float y) { this->sizeX = x, this->sizeY = y; }; |
---|
109 | inline void setSizeX2D(float x) { this->sizeX = x; }; |
---|
110 | inline void setSizeY2D(float y) { this->sizeY = y; }; |
---|
111 | inline float getSizeX2D() const { return this->sizeX; }; |
---|
112 | inline float getSizeY2D() const { return this->sizeY; }; |
---|
113 | |
---|
114 | public: |
---|
115 | virtual void tick(float dt) {}; |
---|
116 | virtual void draw() const = 0; |
---|
117 | void tick2D(float dt); |
---|
118 | void draw2D(short layer) const; |
---|
119 | |
---|
120 | // LIKE PNODE |
---|
121 | public: |
---|
122 | void setRelCoor2D (const Vector& relCoord); |
---|
123 | void setRelCoor2D (float x, float y, float dontCare = 1.0); |
---|
124 | void setRelCoor2Dpx (int x, int y); |
---|
125 | void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0); |
---|
126 | void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0); |
---|
127 | void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0); |
---|
128 | /** @returns the relative position */ |
---|
129 | inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; }; |
---|
130 | /** @returns the Relative Coordinate Destination */ |
---|
131 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
132 | const Vector& getRelCoor2Dpx() const; |
---|
133 | void setAbsCoor2D (const Vector& absCoord); |
---|
134 | void setAbsCoor2D (float x, float y, float depth = 1.0); |
---|
135 | void setAbsCoor2Dpx (int x, int y); |
---|
136 | void setAbsCoorSoft2D (const Vector& absCoordSoft, float bias = 1.0); |
---|
137 | void setAbsCoorSoft2D (float x, float y, float depth = 1.0, float bias = 1.0); |
---|
138 | /** @returns the absolute position */ |
---|
139 | inline const Vector& getAbsCoor2D () const { return this->absCoordinate; }; |
---|
140 | const Vector& getAbsCoor2Dpx () const; |
---|
141 | |
---|
142 | void shiftCoor2D (const Vector& shift); |
---|
143 | void shiftCoor2Dpx (int x, int y); |
---|
144 | |
---|
145 | void setRelDir2D (float relDir); |
---|
146 | void setRelDirSoft2D(float relDirSoft, float bias = 1.0); |
---|
147 | /** @returns the relative Direction */ |
---|
148 | inline float getRelDir2D () const { return this->prevRelDirection; }; |
---|
149 | /** @returns the Relative Directional Destination */ |
---|
150 | inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
151 | void setAbsDir2D (float absDir); |
---|
152 | void setAbsDirSoft2D (float absDirSoft, float bias = 1.0); |
---|
153 | /** @returns the absolute Direction */ |
---|
154 | inline float getAbsDir2D () const { return this->absDirection; }; |
---|
155 | void shiftDir2D (float shiftDir); |
---|
156 | |
---|
157 | /** @returns the Speed of the Node */ |
---|
158 | inline float getSpeed() const { return 0; }; |
---|
159 | /** @returns the Velocity of the Node */ |
---|
160 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
161 | |
---|
162 | |
---|
163 | void addChild2D (Element2D* child); |
---|
164 | void addChild2D (const char* childName); |
---|
165 | void removeChild2D (Element2D* child); |
---|
166 | void remove2D(); |
---|
167 | |
---|
168 | void setParent2D (Element2D* parent); |
---|
169 | void setParent2D (const char* parentName); |
---|
170 | /** @returns the parent of this Element2D */ |
---|
171 | inline Element2D* getParent2D () const { return this->parent; }; |
---|
172 | /** @returns the List of Children of this Element2D */ |
---|
173 | inline const std::list<Element2D*>& getChildren2D() const { return this->children; }; |
---|
174 | |
---|
175 | void setParentSoft2D(Element2D* parentNode, float bias = 1.0); |
---|
176 | void setParentSoft2D(const char* parentName, float bias = 1.0); |
---|
177 | |
---|
178 | /** @param parentMode sets the parentingMode of this Node */ |
---|
179 | void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
180 | void setParentMode2D (const char* parentingMode); |
---|
181 | /** @returns the Parenting mode of this node */ |
---|
182 | int getParentMode2D() const { return this->parentMode; }; |
---|
183 | |
---|
184 | void update2D (float dt); |
---|
185 | |
---|
186 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
187 | void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,0,0), unsigned int level = 0) const; |
---|
188 | |
---|
189 | // helper functions // |
---|
190 | static const char* parentingModeToChar2D(int parentingMode); |
---|
191 | static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode); |
---|
192 | |
---|
193 | static const char* layer2DToChar(E2D_LAYER layer); |
---|
194 | static E2D_LAYER charToLayer2D(const char* layer); |
---|
195 | |
---|
196 | private: |
---|
197 | /** tells the child that the parent's Coordinate has changed */ |
---|
198 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
199 | /** tells the child that the parent's Direction has changed */ |
---|
200 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
201 | /** @returns the last calculated coordinate */ |
---|
202 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | private: |
---|
207 | const PNode* bindNode; //!< a node over which to display this 2D-element |
---|
208 | float sizeX; //!< The size of the rendered item in x-direction |
---|
209 | float sizeY; //!< The size of the rendered Item in y-direction |
---|
210 | |
---|
211 | E2D_ALIGNMENT alignment; //!< How the Element is aligned around its Position |
---|
212 | |
---|
213 | bool visible; //!< If the given Element2D is visible. |
---|
214 | bool active; //!< If the given Element2D is active. |
---|
215 | E2D_LAYER layer; //!< What layer this Element2D is on. |
---|
216 | |
---|
217 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
218 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
219 | |
---|
220 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
221 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
222 | float relDirection; //!< direction relative to the parent |
---|
223 | float absDirection; //!< absolute diretion in the world ( from (0,0,1) ) |
---|
224 | |
---|
225 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
226 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
227 | float prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
228 | |
---|
229 | Vector velocity; //!< Saves the velocity. |
---|
230 | |
---|
231 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
232 | float* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
233 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
234 | |
---|
235 | Element2D* parent; //!< a pointer to the parent node |
---|
236 | std::list<Element2D*> children; //!< list of the children of this Element2D |
---|
237 | |
---|
238 | unsigned int parentMode; //!< the mode of the binding |
---|
239 | }; |
---|
240 | |
---|
241 | //! The top joint of all Element2D's every Element2D is somehow connected to this one. |
---|
242 | class NullElement2D : public Element2D { |
---|
243 | |
---|
244 | public: |
---|
245 | /** @returns a Pointer to the only object of this Class */ |
---|
246 | inline static NullElement2D* getInstance() { if (!NullElement2D::singletonRef) NullElement2D::singletonRef = new NullElement2D(); return NullElement2D::singletonRef; }; |
---|
247 | inline static bool isInstanciated() { return (NullElement2D::singletonRef != NULL)?true:false; }; |
---|
248 | virtual ~NullElement2D (); |
---|
249 | |
---|
250 | private: |
---|
251 | NullElement2D (); |
---|
252 | virtual void draw() const {}; |
---|
253 | |
---|
254 | private: |
---|
255 | static NullElement2D* singletonRef; //!< A reference to the NullElement2D |
---|
256 | |
---|
257 | }; |
---|
258 | |
---|
259 | #endif /* _ELEMENT_2D_H */ |
---|