Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: stl::list in Element2D

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