Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: nicer quit-modi
TextEngine is now deleted by GraphicsEngine
trying to fix errors in the Element2D deletion

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