Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5251 was 5251, checked in by bensch, 20 years ago

orxonox/trunk: saver change between fonts in Shell

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