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