1 | /*! |
---|
2 | * @file element_2d.h |
---|
3 | * Definition of the 2D elements rendered on top of all other stuff. |
---|
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 "vector2D.h" |
---|
13 | #include <list> |
---|
14 | |
---|
15 | // FORWARD DECLARATION |
---|
16 | class PNode; |
---|
17 | class TiXmlElement; |
---|
18 | |
---|
19 | //!< An enumerator defining the Depth of a 2D-element. |
---|
20 | typedef enum |
---|
21 | { |
---|
22 | E2D_LAYER_BELOW_ALL = 0, //!< Will be rendered below the 3D-scene. @todo make this work. |
---|
23 | E2D_LAYER_BOTTOM = 1, //!< Will be rendered on the bottom Layer |
---|
24 | E2D_LAYER_MEDIUM = 2, //!< Will be rendered on the medium Layer. |
---|
25 | E2D_LAYER_TOP = 3, //!< Will be rendered on top of everything else |
---|
26 | E2D_LAYER_ABOVE_ALL = 4, //!< Will be rendered above everything else. |
---|
27 | |
---|
28 | E2D_LAYER_COUNT = 5, //!< The count of Layers. |
---|
29 | } E2D_LAYER; |
---|
30 | #define E2D_DEFAULT_LAYER E2D_LAYER_MEDIUM |
---|
31 | #define E2D_LAYER_ALL 5 |
---|
32 | |
---|
33 | typedef 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 | |
---|
42 | typedef enum |
---|
43 | { |
---|
44 | E2D_PARENT_NONE = 0x0000, |
---|
45 | E2D_PARENT_LOCAL_ROTATE = 0x0001, //!< Rotates all the children around their centers. |
---|
46 | E2D_PARENT_ROTATE_MOVEMENT = 0x0002, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
---|
47 | |
---|
48 | E2D_PARENT_MOVEMENT = 0x0004, //!< Moves all children along with the parent. |
---|
49 | // special linkage modes |
---|
50 | E2D_PARENT_ALL = 0x0003, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
51 | E2D_PARENT_ROTATE_AND_MOVE = 0x0005, //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent. |
---|
52 | |
---|
53 | |
---|
54 | // REPARENTING |
---|
55 | E2D_REPARENT_TO_NULL = 0x0010, //!< Reparents to the Null, if the Parent is Removed. Meaning the Node wont have a parent anymore. |
---|
56 | E2D_REPARENT_TO_PARENTS_PARENT = 0x0020, //!< Reparents the Node to the parents (old) parent it the parent gets removed. |
---|
57 | ///////////////////////////////////////////// // ELSE: Reparents to the NullParent. |
---|
58 | E2D_REPARENT_DELETE_CHILDREN = 0x0040, //!< Deletes the Children of the node when This Node is Removed. (Use with care). |
---|
59 | /// FIXME |
---|
60 | E2D_REPARENT_KEEP_POSITION = 0x0080, //!< Tries to keep the Position if the Node is reparented. |
---|
61 | |
---|
62 | |
---|
63 | // DELETION |
---|
64 | E2D_PROHIBIT_CHILD_DELETE = 0x0100, //!< Prohibits the Children from being deleted if this Node gets deleted. |
---|
65 | E2D_PROHIBIT_DELETE_WITH_PARENT = 0x0200, //!< Prohibits the Node to be deleted if the Parent is. Child will be reparented according to the Repaenting-Rules |
---|
66 | E2D_REPARENT_CHILDREN_ON_REMOVE = 0x0400, //!< Reparents the Children of the Node if the Node gets Removed. |
---|
67 | E2D_REPARENT_ON_PARENTS_REMOVE = 0x0800, //!< The Node gets Reparented if its Parent gets removed. Child will be reparented according to the Reparenting-Rules. |
---|
68 | |
---|
69 | // VISIBILITY/ACTIVITY |
---|
70 | E2D_HIDE_CHILDREN_IF_HIDDEN = 0x1000, //!< Prohibits the Children from being drawn if this node isn't visible. (used for Draw)) |
---|
71 | E2D_HIDE_IF_PARENT_HIDDEN = 0x2000, //!< Prohibits the node from being drawn if the Parent is invisible. |
---|
72 | E2D_UPDATE_CHILDREN_IF_INACTIVE = 0x4000, //!< Updates the Children of this Node even if the Parent is Inactive (note if this's parent is inactive children won't be updated.) |
---|
73 | E2D_STATIC_NODE = 0x8000, //!< Used for nodes that do not have any moving children, and that do not move. |
---|
74 | |
---|
75 | } E2D_PARENT_MODE; |
---|
76 | #define E2D_PARENT_MODE_DEFAULT E2D_PARENT_ALL | \ |
---|
77 | E2D_REPARENT_KEEP_POSITION |
---|
78 | |
---|
79 | //! A class for 2D-elements |
---|
80 | /** |
---|
81 | * this class mainly tries to imitate PNode in its functionality, but on a 2D-level |
---|
82 | * it extends PNode in the Sense of the tick-ability/draw-alility layering (and size) |
---|
83 | * |
---|
84 | * Layering: Layers are sorted into the Tree. e.g: |
---|
85 | * the roor is in the lowest Layer, the leaves in the highest (of each branche) |
---|
86 | * the first child of each node is in the lowest layer of all children, the last in the highest |
---|
87 | * -> the tree is sorted on insertion of a new Child: @see Element2D::addChild2D() |
---|
88 | */ |
---|
89 | class Element2D : virtual public BaseObject { |
---|
90 | |
---|
91 | public: |
---|
92 | Element2D(Element2D* parent = Element2D::getNullElement(), E2D_LAYER layer = E2D_DEFAULT_LAYER, short nodeFlags = E2D_PARENT_MODE_DEFAULT); |
---|
93 | virtual ~Element2D(); |
---|
94 | |
---|
95 | virtual void loadParams(const TiXmlElement* root); |
---|
96 | |
---|
97 | // ACTIVATION // |
---|
98 | inline void activate2D() { this->bActive = this->bRelCoorChanged = this->bRelDirChanged = true; }; |
---|
99 | inline void deactivate2D() { this->bActive = false; }; |
---|
100 | inline bool get2DActiveState() { return this->bActive; }; |
---|
101 | |
---|
102 | // ALIGNMENT // |
---|
103 | /** @param alignment the new Alignment of the 2D-Element */ |
---|
104 | inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; }; |
---|
105 | void setAlignment(const std::string& alignment); |
---|
106 | inline E2D_ALIGNMENT getAlignment() const { return this->alignment; }; |
---|
107 | |
---|
108 | // LAYERING // |
---|
109 | void setLayer(E2D_LAYER layer); |
---|
110 | void setLayer(const std::string& layer); |
---|
111 | /** @returns the Layer this Element is drawn to */ |
---|
112 | inline E2D_LAYER getLayer() const { return this->layer; }; |
---|
113 | |
---|
114 | // VISIBILITY // |
---|
115 | /** @param visible true if the Element should be visible false otherwise (will not be rendered) */ |
---|
116 | inline void setVisibility(bool visible) { this->bVisible = visible; }; |
---|
117 | /** @returns the visibility state */ |
---|
118 | inline bool isVisible() const { return (this->bVisible && this->bCurrentlyVisible); }; |
---|
119 | |
---|
120 | |
---|
121 | // POSITIONAL (E2D-specials) // |
---|
122 | /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */ |
---|
123 | void setBindNode(const PNode* bindNode); |
---|
124 | void setBindNode(const std::string& bindNode); |
---|
125 | inline const PNode* getBindNode() const { return this->bindNode; }; |
---|
126 | |
---|
127 | inline void setSize2D(float x, float y) { this->size = Vector2D(x, y); }; |
---|
128 | inline void setSize2D(const Vector2D& size) { this->size = size; }; |
---|
129 | inline const Vector2D& getSize2D() const { return this->size; }; |
---|
130 | void setSizeSoft2D(float x, float y, float bias = 1.0); |
---|
131 | inline void setSizeX2D(float x) { this->size.x = x; }; |
---|
132 | inline void setSizeY2D(float y) { this->size.y = y; }; |
---|
133 | inline float getSizeX2D() const { return this->size.x; }; |
---|
134 | inline float getSizeY2D() const { return this->size.y; }; |
---|
135 | |
---|
136 | public: |
---|
137 | virtual void tick(float dt) {}; |
---|
138 | virtual void draw() const {}; |
---|
139 | void tick2D(float dt); |
---|
140 | void draw2D(E2D_LAYER from, E2D_LAYER to) const; |
---|
141 | |
---|
142 | // LIKE PNODE |
---|
143 | public: |
---|
144 | void setRelCoor2D (const Vector2D& relCoord); |
---|
145 | void setRelCoorX2D(float x); |
---|
146 | void setRelCoorY2D(float y); |
---|
147 | void setRelCoor2D (float x, float y); |
---|
148 | void setRelCoor2Dpx (int x, int y); |
---|
149 | void setRelCoorSoft2D (const Vector2D& relCoordSoft, float bias = 1.0); |
---|
150 | void setRelCoorSoft2D (float x, float y, float bias = 1.0); |
---|
151 | void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0); |
---|
152 | /** @returns the relative position */ |
---|
153 | inline const Vector2D& getRelCoor2D () const { return this->prevRelCoordinate; }; |
---|
154 | /** @returns the Relative Coordinate Destination */ |
---|
155 | inline const Vector2D& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
156 | const Vector2D& getRelCoor2Dpx() const; |
---|
157 | void setAbsCoor2D (const Vector2D& absCoord); |
---|
158 | void setAbsCoor2D (float x, float y); |
---|
159 | void setAbsCoorX2D(float x); |
---|
160 | void setAbsCoorY2D(float y); |
---|
161 | void setAbsCoor2Dpx (int x, int y); |
---|
162 | void setAbsCoorSoft2D (const Vector2D& absCoordSoft, float bias = 1.0); |
---|
163 | void setAbsCoorSoft2D (float x, float y, float bias = 1.0); |
---|
164 | /** @returns the absolute position */ |
---|
165 | inline const Vector2D& getAbsCoor2D () const { return this->absCoordinate; }; |
---|
166 | const Vector2D& getAbsCoor2Dpx () const; |
---|
167 | |
---|
168 | void shiftCoor2D (const Vector2D& shift); |
---|
169 | void shiftCoor2Dpx (int x, int y); |
---|
170 | |
---|
171 | void setRelDir2D (float relDir); |
---|
172 | void setRelDirSoft2D(float relDirSoft, float bias = 1.0); |
---|
173 | /** @returns the relative Direction */ |
---|
174 | inline float getRelDir2D () const { return this->prevRelDirection; }; |
---|
175 | /** @returns the Relative Directional Destination */ |
---|
176 | inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
177 | void setAbsDir2D (float absDir); |
---|
178 | void setAbsDirSoft2D (float absDirSoft, float bias = 1.0); |
---|
179 | /** @returns the absolute Direction */ |
---|
180 | inline float getAbsDir2D () const { return this->absDirection; }; |
---|
181 | void shiftDir2D (float shiftDir); |
---|
182 | |
---|
183 | /** @returns the Speed of the Node */ |
---|
184 | inline float getSpeed() const { return 0; }; |
---|
185 | /** @returns the Velocity of the Node */ |
---|
186 | inline const Vector2D& getVelocity() const { return this->velocity; }; |
---|
187 | |
---|
188 | |
---|
189 | void addChild2D (Element2D* child); |
---|
190 | void addChild2D (const std::string& childName); |
---|
191 | void removeChild2D (Element2D* child); |
---|
192 | void remove2D(); |
---|
193 | |
---|
194 | /** @param parent the new parent of this Element2D */ |
---|
195 | void setParent2D (Element2D* parent) { parent->addChild2D(this); }; |
---|
196 | void setParent2D (const std::string& parentName); |
---|
197 | /** @returns the parent of this Element2D */ |
---|
198 | inline Element2D* getParent2D () const { return this->parent; }; |
---|
199 | /** @returns the List of Children of this Element2D */ |
---|
200 | inline const std::list<Element2D*>& getChildren2D() const { return this->children; }; |
---|
201 | |
---|
202 | void setParentSoft2D(Element2D* parentNode, float bias = 1.0); |
---|
203 | void setParentSoft2D(const std::string& parentName, float bias = 1.0); |
---|
204 | |
---|
205 | void setParentMode2D (E2D_PARENT_MODE parentMode); |
---|
206 | void setParentMode2D (const std::string& parentingMode); |
---|
207 | /** @returns the Parenting mode of this node */ |
---|
208 | int getParentMode2D() const { return this->parentMode; }; |
---|
209 | |
---|
210 | // NULL_PARENT // |
---|
211 | /** @returns the NullParent, the (main) ROOT of the PNode Tree. If it does not yet exist, it will be created. */ |
---|
212 | static Element2D* getNullElement() { return (Element2D::nullElement != NULL)? Element2D::nullElement : Element2D::createNullElement(); }; |
---|
213 | |
---|
214 | |
---|
215 | void update2D (float dt); |
---|
216 | |
---|
217 | void debug2D (unsigned int depth = 1, unsigned int level = 0) const; |
---|
218 | void debug2DSC (unsigned int depth = 1, unsigned int level = 0) { this->debug2D(depth, level); }; // Wrapping Function for ShellCommand. |
---|
219 | void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,0,0), unsigned int level = 0) const; |
---|
220 | |
---|
221 | // helper functions // |
---|
222 | static const char* parentingModeToString2D(int parentingMode); |
---|
223 | static E2D_PARENT_MODE stringToParentingMode2D(const std::string& parentingMode); |
---|
224 | |
---|
225 | static const char* layer2DToChar(E2D_LAYER layer); |
---|
226 | static E2D_LAYER charToLayer2D(const std::string& layer); |
---|
227 | |
---|
228 | static bool layerSortPredicate(const Element2D* elem1, const Element2D* elem2); |
---|
229 | |
---|
230 | private: |
---|
231 | void eraseChild2D(Element2D* child); |
---|
232 | /** tells the child that the parent's Coordinate has changed */ |
---|
233 | inline void parentCoorChanged2D () { this->bRelCoorChanged = true; } |
---|
234 | /** tells the child that the parent's Direction has changed */ |
---|
235 | inline void parentDirChanged2D () { this->bRelDirChanged = true; } |
---|
236 | /** @returns the last calculated coordinate */ |
---|
237 | inline Vector2D getLastAbsCoor2D() { return this->lastAbsCoordinate; } |
---|
238 | |
---|
239 | void reparent2D(); |
---|
240 | static Element2D* createNullElement(); |
---|
241 | bool checkIntegrity(const Element2D* checkParent) const; |
---|
242 | |
---|
243 | |
---|
244 | private: |
---|
245 | const PNode* bindNode; //!< a node over which to display this 2D-element |
---|
246 | Vector2D size; //!< The size of the rendered item |
---|
247 | Vector2D* toSize; //!< The Size to iterate to. |
---|
248 | |
---|
249 | E2D_ALIGNMENT alignment; //!< How the Element is aligned around its Position |
---|
250 | |
---|
251 | bool bVisible; //!< If the given Element2D is visible. |
---|
252 | bool bCurrentlyVisible; //!< Evaluated in the TICK process, to see if the Element is Currently visible. |
---|
253 | bool bActive; //!< If the given Element2D is active. |
---|
254 | E2D_LAYER layer; //!< What layer this Element2D is on. |
---|
255 | |
---|
256 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
257 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
258 | |
---|
259 | Vector2D relCoordinate; //!< coordinates relative to the parent |
---|
260 | Vector2D absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
261 | float relDirection; //!< direction relative to the parent |
---|
262 | float absDirection; //!< absolute diretion in the world ( from (0,0,1) ) |
---|
263 | |
---|
264 | Vector2D prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
265 | Vector2D lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
266 | float prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
267 | |
---|
268 | Vector2D velocity; //!< Saves the velocity. |
---|
269 | |
---|
270 | Vector2D* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
271 | float* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
272 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
273 | |
---|
274 | Element2D* parent; //!< a pointer to the parent node |
---|
275 | std::list<Element2D*> children; //!< list of the children of this Element2D |
---|
276 | |
---|
277 | unsigned int parentMode; //!< the mode of the binding |
---|
278 | |
---|
279 | static Element2D* nullElement; //!< The top-most Element |
---|
280 | }; |
---|
281 | |
---|
282 | #endif /* _ELEMENT_2D_H */ |
---|