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