Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Layering system works, now i have to do the check on reparenting, so no lower layers are children of high-layer-parents

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