Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/graphics/render2D/element_2d.cc @ 5624

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

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

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