Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: renamed class LoadParam to CLoadParam and Macro LoadParamNew to LoadParam

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