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