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