Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/render2D/element_2d.cc @ 5403

Last change on this file since 5403 was 5403, checked in by bensch, 19 years ago

orxonox/trunk:
implemented List-addAtIteratorPosition, to add any entity in the Sorted list at a designated position (for include-sorting)
include-sort the list of added Element2D's after their layer-depth

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