Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk:
implemented List-addAtIteratorPosition, to add any entity in the Sorted list at a designated position (for include-sorting)
include-sort the list of added Element2D's after their layer-depth

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