Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: promised checking should be working now.

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