1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "element_2d.h" |
---|
19 | #include "render_2d.h" |
---|
20 | |
---|
21 | #include "graphics_engine.h" |
---|
22 | #include "p_node.h" |
---|
23 | #include "load_param.h" |
---|
24 | #include "tinyxml.h" |
---|
25 | #include "class_list.h" |
---|
26 | #include "list.h" |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | Element2D::Element2D() |
---|
31 | { |
---|
32 | this->init(); |
---|
33 | this->setParent2D(NullElement2D::getInstance()); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | * @todo this constructor is not jet implemented - do it |
---|
39 | */ |
---|
40 | Element2D::Element2D (Element2D* parent) |
---|
41 | { |
---|
42 | this->init(); |
---|
43 | |
---|
44 | if (this->parent != NULL) |
---|
45 | this->setParent2D(parent); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * standard deconstructor |
---|
50 | */ |
---|
51 | Element2D::~Element2D () |
---|
52 | { |
---|
53 | // delete what has to be deleted here |
---|
54 | Render2D::getInstance()->unregisterElement2D(this); |
---|
55 | |
---|
56 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
57 | Element2D* pn = iterator->nextElement(); |
---|
58 | while( pn != NULL) |
---|
59 | { |
---|
60 | delete pn; |
---|
61 | pn = iterator->nextElement(); |
---|
62 | } |
---|
63 | delete iterator; |
---|
64 | /* this deletes all children in the list */ |
---|
65 | delete this->children; |
---|
66 | if (this->parent) |
---|
67 | this->parent->removeChild2D(this); |
---|
68 | |
---|
69 | if (this->toCoordinate != NULL) |
---|
70 | delete this->toCoordinate; |
---|
71 | if (this->toDirection != NULL) |
---|
72 | delete this->toDirection; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | * initializes a Element2D |
---|
78 | */ |
---|
79 | void Element2D::init() |
---|
80 | { |
---|
81 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
---|
82 | |
---|
83 | this->setVisibility(true); |
---|
84 | this->setActiveness(true); |
---|
85 | this->setAlignment(E2D_ALIGN_NONE); |
---|
86 | this->layer = E2D_TOP; |
---|
87 | this->bindNode = NULL; |
---|
88 | |
---|
89 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
90 | this->parent = NULL; |
---|
91 | this->children = new tList<Element2D>; |
---|
92 | this->relDirection = 0.0; |
---|
93 | this->bRelCoorChanged = true; |
---|
94 | this->bRelDirChanged = true; |
---|
95 | this->toCoordinate = NULL; |
---|
96 | this->toDirection = NULL; |
---|
97 | // else |
---|
98 | // this->setParent2D(parent); |
---|
99 | |
---|
100 | Render2D::getInstance()->registerElement2D(this); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Loads the Parameters of an Element2D from... |
---|
105 | * @param root The XML-element to load from |
---|
106 | */ |
---|
107 | void Element2D::loadParams(const TiXmlElement* root) |
---|
108 | { |
---|
109 | LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) |
---|
110 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
---|
111 | |
---|
112 | LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) |
---|
113 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
---|
114 | |
---|
115 | LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) |
---|
116 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
---|
117 | |
---|
118 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
---|
119 | .describe("if the Element is visible or not"); |
---|
120 | |
---|
121 | |
---|
122 | // PNode-style: |
---|
123 | LoadParam<Element2D>(root, "rel-coor", this, &Element2D::setRelCoor2D) |
---|
124 | .describe("Sets The relative position of the Node to its parent."); |
---|
125 | |
---|
126 | LoadParam<Element2D>(root, "abs-coor", this, &Element2D::setAbsCoor2D) |
---|
127 | .describe("Sets The absolute Position of the Node."); |
---|
128 | |
---|
129 | LoadParam<Element2D>(root, "rel-dir", this, &Element2D::setRelDir2D) |
---|
130 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
131 | |
---|
132 | LoadParam<Element2D>(root, "abs-dir", this, &Element2D::setAbsDir2D) |
---|
133 | .describe("Sets The absolute rotation of the Node."); |
---|
134 | |
---|
135 | LoadParam<Element2D>(root, "parent", this, &Element2D::setParent2D) |
---|
136 | .describe("the Name of the Parent of this Element2D"); |
---|
137 | |
---|
138 | LoadParam<Element2D>(root, "parent-mode", this, &Element2D::setParentMode2D) |
---|
139 | .describe("the mode to connect this node to its parent ()"); |
---|
140 | |
---|
141 | // cycling properties |
---|
142 | if (root != NULL) |
---|
143 | { |
---|
144 | const TiXmlElement* element = root->FirstChildElement(); |
---|
145 | while (element != NULL) |
---|
146 | { |
---|
147 | LoadParam<Element2D>(root, "parent", this, &Element2D::addChild2D, true) |
---|
148 | .describe("adds a new Child to the current Node."); |
---|
149 | |
---|
150 | element = element->NextSiblingElement(); |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * sets the alignment of the 2D-element in form of a String |
---|
157 | * @param alignment the alignment @see loadParams |
---|
158 | */ |
---|
159 | void Element2D::setAlignment(const char* alignment) |
---|
160 | { |
---|
161 | if (!strcmp(alignment, "center")) |
---|
162 | this->setAlignment(E2D_ALIGN_CENTER); |
---|
163 | else if (!strcmp(alignment, "left")) |
---|
164 | this->setAlignment(E2D_ALIGN_LEFT); |
---|
165 | else if (!strcmp(alignment, "right")) |
---|
166 | this->setAlignment(E2D_ALIGN_RIGHT); |
---|
167 | else if (!strcmp(alignment, "screen-center")) |
---|
168 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | * moves a Element to another layer |
---|
174 | * @param layer the Layer this is drawn on |
---|
175 | */ |
---|
176 | void Element2D::setLayer(E2D_LAYER layer) |
---|
177 | { |
---|
178 | Render2D::getInstance()->moveToLayer(this, layer); |
---|
179 | this->layer = layer; |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * sets the layer onto which this 2D-element is projected to. |
---|
184 | * @param layer the layer @see loadParams |
---|
185 | */ |
---|
186 | void Element2D::setLayer(const char* layer) |
---|
187 | { |
---|
188 | if (!strcmp(layer, "top")) |
---|
189 | this->setLayer(E2D_TOP); |
---|
190 | else if (!strcmp(layer, "medium")) |
---|
191 | this->setLayer(E2D_MEDIUM); |
---|
192 | else if (!strcmp(layer, "bottom")) |
---|
193 | this->setLayer(E2D_BOTTOM); |
---|
194 | else if (!strcmp(layer, "below-all")) |
---|
195 | this->setLayer(E2D_BELOW_ALL); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /** |
---|
200 | * sets a node, this 2D-Element should be shown upon |
---|
201 | * @param bindNode the name of the Node (should be existing) |
---|
202 | */ |
---|
203 | void Element2D::setBindNode(const char* bindNode) |
---|
204 | { |
---|
205 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
---|
206 | if (tmpBindNode != NULL) |
---|
207 | this->bindNode = tmpBindNode; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * sets the relative coordinate of the Element2D to its parent |
---|
212 | * @param relCoord the relative coordinate to the parent |
---|
213 | */ |
---|
214 | void Element2D::setRelCoor2D (const Vector& relCoord) |
---|
215 | { |
---|
216 | this->relCoordinate = relCoord; |
---|
217 | this->bRelCoorChanged = true; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | /** |
---|
222 | * sets the relative coordinate of the Element2D to its Parent |
---|
223 | * @param x the x coordinate |
---|
224 | * @param y the y coordinate |
---|
225 | * @param z the z coordinate |
---|
226 | */ |
---|
227 | void Element2D::setRelCoor2D (float x, float y, float z) |
---|
228 | { |
---|
229 | this->setAbsCoor2D(Vector(x,y,z)); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * sets the Relative coordinate to the parent in Pixels |
---|
234 | * @param x the relCoord X |
---|
235 | * @param y the relCoord Y |
---|
236 | */ |
---|
237 | void Element2D::setRelCoor2Dpx (int x, int y) |
---|
238 | { |
---|
239 | this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
240 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
241 | 0 |
---|
242 | )); |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * sets a new relative position smoothely |
---|
247 | * @param relCoordSoft the new Position to iterate to |
---|
248 | * @param bias how fast to iterate to this position |
---|
249 | */ |
---|
250 | void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias) |
---|
251 | { |
---|
252 | if (likely(this->toCoordinate == NULL)) |
---|
253 | this->toCoordinate = new Vector(); |
---|
254 | |
---|
255 | *this->toCoordinate = relCoordSoft; |
---|
256 | this->bias = bias; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * sets a new relative position smoothely |
---|
261 | * @param x the new x-coordinate in Pixels of the Position to iterate to |
---|
262 | * @param y the new y-coordinate in Pixels of the Position to iterate to |
---|
263 | * @param bias how fast to iterate to this position |
---|
264 | */ |
---|
265 | void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias) |
---|
266 | { |
---|
267 | this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
268 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
269 | 0), |
---|
270 | bias); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * set relative coordinates smoothely |
---|
275 | * @param x x-relative coordinates to its parent |
---|
276 | * @param y y-relative coordinates to its parent |
---|
277 | * @param z z-relative coordinates to its parent |
---|
278 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
279 | */ |
---|
280 | void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias) |
---|
281 | { |
---|
282 | this->setRelCoorSoft2D(Vector(x, y, depth), bias); |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * @param absCoord set absolute coordinate |
---|
287 | */ |
---|
288 | void Element2D::setAbsCoor2D (const Vector& absCoord) |
---|
289 | { |
---|
290 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
---|
291 | { |
---|
292 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
293 | if (likely(this->parent != NULL)) |
---|
294 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
295 | else |
---|
296 | this->relCoordinate = absCoord; |
---|
297 | } |
---|
298 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
---|
299 | { |
---|
300 | if (likely(this->parent != NULL)) |
---|
301 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
302 | else |
---|
303 | this->relCoordinate = absCoord; |
---|
304 | } |
---|
305 | |
---|
306 | this->bRelCoorChanged = true; |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * @param x x-coordinate. |
---|
311 | * @param y y-coordinate. |
---|
312 | * @param z z-coordinate. |
---|
313 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
314 | */ |
---|
315 | void Element2D::setAbsCoor2D (float x, float y, float depth) |
---|
316 | { |
---|
317 | this->setAbsCoor2D(Vector(x, y, depth)); |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * @param x x-coordinate in Pixels |
---|
322 | * @param y y-coordinate in Pixels |
---|
323 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
324 | */ |
---|
325 | void Element2D::setAbsCoor2Dpx (int x, int y) |
---|
326 | { |
---|
327 | this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
328 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
329 | 0 |
---|
330 | )); |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * shift coordinate ralative |
---|
335 | * @param shift shift vector |
---|
336 | * |
---|
337 | * This simply adds the shift-Vector to the relative Coordinate |
---|
338 | */ |
---|
339 | void Element2D::shiftCoor2D (const Vector& shift) |
---|
340 | { |
---|
341 | this->relCoordinate += shift; |
---|
342 | this->bRelCoorChanged = true; |
---|
343 | |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * shifts in PixelSpace |
---|
348 | * @param x the pixels to shift in X |
---|
349 | * @param y the pixels to shift in Y |
---|
350 | */ |
---|
351 | void Element2D::shiftCoor2Dpx (int x, int y) |
---|
352 | { |
---|
353 | this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
354 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
355 | 0)); |
---|
356 | } |
---|
357 | |
---|
358 | /** |
---|
359 | * set relative direction |
---|
360 | * @param relDir to its parent |
---|
361 | */ |
---|
362 | void Element2D::setRelDir2D (float relDir) |
---|
363 | { |
---|
364 | this->relDirection = relDir; |
---|
365 | this->bRelDirChanged = true; |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
370 | * @param relDirSoft the direction to iterate to smoothely. |
---|
371 | * @param bias how fast to iterate to the new Direction |
---|
372 | */ |
---|
373 | void Element2D::setRelDirSoft2D(float relDirSoft, float bias) |
---|
374 | { |
---|
375 | if (likely(this->toDirection == NULL)) |
---|
376 | this->toDirection = new float; |
---|
377 | |
---|
378 | *this->toDirection = relDirSoft; |
---|
379 | this->bias = bias; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * sets the absolute direction |
---|
384 | * @param absDir absolute coordinates |
---|
385 | */ |
---|
386 | void Element2D::setAbsDir2D (float absDir) |
---|
387 | { |
---|
388 | if (likely(this->parent != NULL)) |
---|
389 | this->relDirection = absDir - this->parent->getAbsDir2D(); |
---|
390 | else |
---|
391 | this->relDirection = absDir; |
---|
392 | |
---|
393 | this->bRelDirChanged = true; |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * shift Direction |
---|
398 | * @param shift the direction around which to shift. |
---|
399 | */ |
---|
400 | void Element2D::shiftDir2D (float shiftDir) |
---|
401 | { |
---|
402 | this->relDirection = this->relDirection + shiftDir; |
---|
403 | this->bRelDirChanged = true; |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * adds a child and makes this node to a parent |
---|
408 | * @param child child reference |
---|
409 | * @param parentMode on which changes the child should also change ist state |
---|
410 | * |
---|
411 | * use this to add a child to this node. |
---|
412 | */ |
---|
413 | void Element2D::addChild2D (Element2D* child, int parentingMod) |
---|
414 | { |
---|
415 | if( likely(child->parent != NULL)) |
---|
416 | { |
---|
417 | PRINTF(4)("Element2D::addChild() - reparenting node: removing it and adding it again\n"); |
---|
418 | child->parent->children->remove(child); |
---|
419 | } |
---|
420 | child->parentMode = parentMode; |
---|
421 | child->parent = this; |
---|
422 | this->children->add(child); |
---|
423 | child->parentCoorChanged(); |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * @see Element2D::addChild(Element2D* child); |
---|
428 | * @param childName the name of the child to add to this PNode |
---|
429 | */ |
---|
430 | void Element2D::addChild2D (const char* childName) |
---|
431 | { |
---|
432 | Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D)); |
---|
433 | if (childNode != NULL) |
---|
434 | this->addChild2D(childNode); |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * removes a child from the node |
---|
439 | * @param child the child to remove from this Node.. |
---|
440 | * |
---|
441 | * Children from nodes will not be lost, they are referenced to NullPointer |
---|
442 | */ |
---|
443 | void Element2D::removeChild2D (Element2D* child) |
---|
444 | { |
---|
445 | child->remove2D(); |
---|
446 | this->children->remove(child); |
---|
447 | child->parent = NULL; |
---|
448 | } |
---|
449 | |
---|
450 | /** |
---|
451 | * remove this pnode from the tree and adds all following to NullParent |
---|
452 | * |
---|
453 | * this can be the case, if an entity in the world is being destroyed. |
---|
454 | */ |
---|
455 | void Element2D::remove2D() |
---|
456 | { |
---|
457 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
458 | Element2D* pn = iterator->nextElement(); |
---|
459 | |
---|
460 | while( pn != NULL) |
---|
461 | { |
---|
462 | NullElement2D::getInstance()->addChild2D(pn, pn->getParentMode2D()); |
---|
463 | pn = iterator->nextElement(); |
---|
464 | } |
---|
465 | delete iterator; |
---|
466 | this->parent->children->remove(this); |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | * sets the parent of this Element2D |
---|
471 | * @param parent the Parent to set |
---|
472 | */ |
---|
473 | void Element2D::setParent2D (Element2D* parent) |
---|
474 | { |
---|
475 | parent->addChild2D(this); |
---|
476 | } |
---|
477 | |
---|
478 | /** |
---|
479 | * @see Element2D::setParent(Element2D* parent); |
---|
480 | * @param parentName the name of the Parent to set to this Element2D |
---|
481 | */ |
---|
482 | void Element2D::setParent2D (const char* parentName) |
---|
483 | { |
---|
484 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
485 | if (parentNode != NULL) |
---|
486 | parentNode->addChild2D(this); |
---|
487 | |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * does the reparenting in a very smooth way |
---|
492 | * @param parentNode the new Node to connect this node to. |
---|
493 | * @param bias the speed to iterate to this new Positions |
---|
494 | */ |
---|
495 | void Element2D::softReparent2D(Element2D* parentNode, float bias) |
---|
496 | { |
---|
497 | if (this->parent == parentNode) |
---|
498 | return; |
---|
499 | |
---|
500 | if (likely(this->toCoordinate == NULL)) |
---|
501 | { |
---|
502 | this->toCoordinate = new Vector(); |
---|
503 | *this->toCoordinate = this->getRelCoor2D(); |
---|
504 | } |
---|
505 | if (likely(this->toDirection == NULL)) |
---|
506 | { |
---|
507 | this->toDirection = new float; |
---|
508 | *this->toDirection = this->getRelDir2D(); |
---|
509 | } |
---|
510 | this->bias = bias; |
---|
511 | |
---|
512 | |
---|
513 | Vector tmpV = this->getAbsCoor2D(); |
---|
514 | float tmpQ = this->getAbsDir2D(); |
---|
515 | |
---|
516 | parentNode->addChild2D(this); |
---|
517 | |
---|
518 | if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
---|
519 | ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
---|
520 | else |
---|
521 | this->setRelCoor2D(tmpV - parentNode->getAbsCoor2D()); |
---|
522 | |
---|
523 | this->setRelDir2D(tmpQ - parentNode->getAbsDir2D()); |
---|
524 | } |
---|
525 | |
---|
526 | /** |
---|
527 | * does the reparenting in a very smooth way |
---|
528 | * @param parentName the name of the Parent to reconnect to |
---|
529 | * @param bias the speed to iterate to this new Positions |
---|
530 | */ |
---|
531 | void Element2D::softReparent2D(const char* parentName, float bias) |
---|
532 | { |
---|
533 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
534 | if (parentNode != NULL) |
---|
535 | this->softReparent2D(parentNode, bias); |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * sets the mode of this parent manually |
---|
540 | * @param parentMode a String representing this parentingMode |
---|
541 | */ |
---|
542 | void Element2D::setParentMode2D (const char* parentingMode) |
---|
543 | { |
---|
544 | this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode)); |
---|
545 | } |
---|
546 | |
---|
547 | /** |
---|
548 | * updates the absCoordinate/absDirection |
---|
549 | * @param dt The time passed since the last update |
---|
550 | |
---|
551 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
552 | and directions. this update should be done by the engine, so you don't have to |
---|
553 | worry, normaly... |
---|
554 | */ |
---|
555 | void Element2D::update2D (float dt) |
---|
556 | { |
---|
557 | // setting the Position of this 2D-Element. |
---|
558 | if( likely(this->parent != NULL)) |
---|
559 | { |
---|
560 | // movement for nodes with smoothMove enabled |
---|
561 | if (unlikely(this->toCoordinate != NULL)) |
---|
562 | { |
---|
563 | Vector moveVect = (*this->toCoordinate - this->getRelCoor2D()) *dt*bias; |
---|
564 | |
---|
565 | if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA)) |
---|
566 | { |
---|
567 | this->shiftCoor2D(moveVect); |
---|
568 | } |
---|
569 | else |
---|
570 | { |
---|
571 | delete this->toCoordinate; |
---|
572 | this->toCoordinate = NULL; |
---|
573 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
574 | } |
---|
575 | } |
---|
576 | if (unlikely(this->toDirection != NULL)) |
---|
577 | { |
---|
578 | float rotFlot = (*this->toDirection - this->getRelDir2D()) *dt*bias; |
---|
579 | if (likely(rotFlot >= .001))//PNODE_ITERATION_DELTA)) |
---|
580 | { |
---|
581 | this->shiftDir2D(rotFlot); |
---|
582 | } |
---|
583 | else |
---|
584 | { |
---|
585 | delete this->toDirection; |
---|
586 | this->toDirection = NULL; |
---|
587 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | // MAIN UPDATE ///////////////////////////////////// |
---|
592 | this->lastAbsCoordinate = this->absCoordinate; |
---|
593 | |
---|
594 | PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
595 | |
---|
596 | |
---|
597 | if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged) |
---|
598 | { |
---|
599 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
600 | this->prevRelDirection = this->relDirection; |
---|
601 | this->absDirection = this->relDirection + parent->getAbsDir2D();; |
---|
602 | } |
---|
603 | |
---|
604 | |
---|
605 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged) |
---|
606 | { |
---|
607 | this->prevRelCoordinate = this->relCoordinate; |
---|
608 | this->absCoordinate.x = .5 + this->relCoordinate.x; |
---|
609 | this->absCoordinate.y = .5 + this->relCoordinate.y; |
---|
610 | this->absCoordinate.z = 0.0; |
---|
611 | } |
---|
612 | else if (this->bindNode) |
---|
613 | { |
---|
614 | GLdouble projectPos[3]; |
---|
615 | gluProject(this->bindNode->getAbsCoor().x, |
---|
616 | this->bindNode->getAbsCoor().y, |
---|
617 | this->bindNode->getAbsCoor().z, |
---|
618 | GraphicsEngine::modMat, |
---|
619 | GraphicsEngine::projMat, |
---|
620 | GraphicsEngine::viewPort, |
---|
621 | projectPos, |
---|
622 | projectPos+1, |
---|
623 | projectPos+2); |
---|
624 | this->absCoordinate.x = projectPos[0]/(float)GraphicsEngine::getInstance()->getResolutionX() + this->relCoordinate.x; |
---|
625 | this->absCoordinate.y = projectPos[1]/(float)GraphicsEngine::getInstance()->getResolutionY() + this->relCoordinate.y; |
---|
626 | this->absCoordinate.z = projectPos[2] + this->relCoordinate.z; |
---|
627 | } |
---|
628 | else |
---|
629 | { |
---|
630 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
631 | { |
---|
632 | /* update the current absCoordinate */ |
---|
633 | this->prevRelCoordinate = this->relCoordinate; |
---|
634 | this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate; |
---|
635 | } |
---|
636 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
637 | { |
---|
638 | /* update the current absCoordinate */ |
---|
639 | this->prevRelCoordinate = this->relCoordinate; |
---|
640 | float sine = sin(this->parent->getAbsDir2D()); |
---|
641 | float cose = cos(this->parent->getAbsDir2D()); |
---|
642 | // this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine; |
---|
643 | // this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine; |
---|
644 | |
---|
645 | this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D())); |
---|
646 | this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D())); |
---|
647 | |
---|
648 | } |
---|
649 | } |
---|
650 | ///////////////////////////////////////////////// |
---|
651 | } |
---|
652 | else |
---|
653 | { |
---|
654 | PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
655 | if (this->bRelCoorChanged) |
---|
656 | this->absCoordinate = this->relCoordinate; |
---|
657 | if (this->bRelDirChanged) |
---|
658 | this->absDirection = this->getAbsDir2D() * this->relDirection; |
---|
659 | } |
---|
660 | |
---|
661 | |
---|
662 | // UPDATE CHILDREN |
---|
663 | if(this->children->getSize() > 0) |
---|
664 | { |
---|
665 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
666 | Element2D* pn = iterator->nextElement(); |
---|
667 | while( pn != NULL) |
---|
668 | { |
---|
669 | /* if this node has changed, make sure, that all children are updated also */ |
---|
670 | if( likely(this->bRelCoorChanged)) |
---|
671 | pn->parentCoorChanged (); |
---|
672 | if( likely(this->bRelDirChanged)) |
---|
673 | pn->parentDirChanged (); |
---|
674 | |
---|
675 | pn->update2D(dt); |
---|
676 | //pn = this->children->nextElement(); |
---|
677 | pn = iterator->nextElement(); |
---|
678 | } |
---|
679 | delete iterator; |
---|
680 | } |
---|
681 | |
---|
682 | // FINISHING PROCESS |
---|
683 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
684 | this->bRelCoorChanged = false; |
---|
685 | this->bRelDirChanged = false; |
---|
686 | } |
---|
687 | |
---|
688 | /** |
---|
689 | * displays some information about this pNode |
---|
690 | * @param depth The deph into which to debug the children of this Element2D to. |
---|
691 | * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...) |
---|
692 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
---|
693 | */ |
---|
694 | void Element2D::debug (unsigned int depth, unsigned int level) const |
---|
695 | { |
---|
696 | for (unsigned int i = 0; i < level; i++) |
---|
697 | PRINT(0)(" |"); |
---|
698 | if (this->children->getSize() > 0) |
---|
699 | PRINT(0)(" +"); |
---|
700 | else |
---|
701 | PRINT(0)(" -"); |
---|
702 | PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n", |
---|
703 | this->getClassName(), |
---|
704 | this->getName(), |
---|
705 | this->absCoordinate.x, |
---|
706 | this->absCoordinate.y, |
---|
707 | this->relCoordinate.x, |
---|
708 | this->relCoordinate.y, |
---|
709 | this->getAbsDir2D(), |
---|
710 | Element2D::parentingModeToChar2D(parentMode)); |
---|
711 | if (depth >= 2 || depth == 0) |
---|
712 | { |
---|
713 | tIterator<Element2D>* iterator = this->children->getIterator(); |
---|
714 | //PNode* pn = this->children->enumerate (); |
---|
715 | Element2D* pn = iterator->nextElement(); |
---|
716 | while( pn != NULL) |
---|
717 | { |
---|
718 | if (depth == 0) |
---|
719 | pn->debug(0, level + 1); |
---|
720 | else |
---|
721 | pn->debug(depth - 1, level +1); |
---|
722 | pn = iterator->nextElement(); |
---|
723 | } |
---|
724 | delete iterator; |
---|
725 | } |
---|
726 | } |
---|
727 | |
---|
728 | #include "color.h" |
---|
729 | |
---|
730 | /** |
---|
731 | * displays the Element2D at its position with its rotation as a Plane. |
---|
732 | */ |
---|
733 | void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const |
---|
734 | { |
---|
735 | |
---|
736 | } |
---|
737 | |
---|
738 | |
---|
739 | // helper functions // |
---|
740 | /** |
---|
741 | * converts a parentingMode into a string that is the name of it |
---|
742 | * @param parentingMode the ParentingMode to convert |
---|
743 | * @return the converted string |
---|
744 | */ |
---|
745 | const char* Element2D::parentingModeToChar2D(int parentingMode) |
---|
746 | { |
---|
747 | if (parentingMode == E2D_PARENT_LOCAL_ROTATE) |
---|
748 | return "local-rotate"; |
---|
749 | else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT) |
---|
750 | return "rotate-movement"; |
---|
751 | else if (parentingMode == E2D_PARENT_MOVEMENT) |
---|
752 | return "movement"; |
---|
753 | else if (parentingMode == E2D_PARENT_ALL) |
---|
754 | return "all"; |
---|
755 | else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE) |
---|
756 | return "rotate-and-move"; |
---|
757 | } |
---|
758 | |
---|
759 | /** |
---|
760 | * converts a parenting-mode-string into a int |
---|
761 | * @param parentingMode the string naming the parentingMode |
---|
762 | * @return the int corresponding to the named parentingMode |
---|
763 | */ |
---|
764 | E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode) |
---|
765 | { |
---|
766 | if (!strcmp(parentingMode, "local-rotate")) |
---|
767 | return (E2D_PARENT_LOCAL_ROTATE); |
---|
768 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
769 | return (E2D_PARENT_ROTATE_MOVEMENT); |
---|
770 | else if (!strcmp(parentingMode, "movement")) |
---|
771 | return (E2D_PARENT_MOVEMENT); |
---|
772 | else if (!strcmp(parentingMode, "all")) |
---|
773 | return (E2D_PARENT_ALL); |
---|
774 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
775 | return (E2D_PARENT_ROTATE_AND_MOVE); |
---|
776 | } |
---|
777 | |
---|
778 | |
---|
779 | |
---|
780 | |
---|
781 | |
---|
782 | |
---|
783 | /** |
---|
784 | * ticks the 2d-Element |
---|
785 | * @param dt the time elapsed since the last tick |
---|
786 | */ |
---|
787 | void Element2D::tick(float dt) |
---|
788 | { |
---|
789 | |
---|
790 | } |
---|
791 | |
---|
792 | |
---|
793 | |
---|
794 | |
---|
795 | |
---|
796 | |
---|
797 | |
---|
798 | NullElement2D* NullElement2D::singletonRef = 0; |
---|
799 | |
---|
800 | /** |
---|
801 | * creates the one and only NullElement2D |
---|
802 | * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0)) |
---|
803 | */ |
---|
804 | NullElement2D::NullElement2D () : Element2D(NULL) |
---|
805 | { |
---|
806 | this->setClassID(CL_NULL_PARENT, "NullElement2D"); |
---|
807 | this->setName("NullElement2D"); |
---|
808 | |
---|
809 | this->setParentMode2D(E2D_PARENT_ALL); |
---|
810 | NullElement2D::singletonRef = this; |
---|
811 | } |
---|
812 | |
---|
813 | |
---|
814 | /** |
---|
815 | * standard deconstructor |
---|
816 | */ |
---|
817 | NullElement2D::~NullElement2D () |
---|
818 | { |
---|
819 | //delete singletonRef; |
---|
820 | NullElement2D::singletonRef = NULL; |
---|
821 | } |
---|