Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Layering system works, now i have to do the check on reparenting, so no lower layers are children of high-layer-parents

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