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, 18 years ago

orxonox/trunk: stl::list in Element2D

File size: 29.8 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "element_2d.h"
19#include "render_2d.h"
20
21#include "p_node.h"
22
23#include "graphics_engine.h"
24#include "load_param.h"
25#include "tinyxml.h"
26#include "class_list.h"
27
28#include "color.h"
29
30using namespace std;
31
32/**
33 * standard constructor
34 */
35Element2D::Element2D()
36{
37  this->init();
38  this->setParent2D(NullElement2D::getInstance());
39}
40
41/**
42 * standard constructor
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 */
48Element2D::Element2D (Element2D* parent, E2D_LAYER layer)
49{
50  this->init();
51  this->layer = layer;
52  // check Parenting, and if ok parent the stuff
53  if (this->parent != NULL)
54    this->setParent2D(parent);
55  else if (NullElement2D::isInstanciated())
56    this->setParent2D(NullElement2D::getInstance());
57}
58
59/**
60 * standard deconstructor
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:
70 *    moves its children to the NullElement2D
71 *    then deletes the Element.
72 */
73Element2D::~Element2D ()
74{
75  // remove the Node, delete it's children.
76  list<Element2D*>::iterator child;
77  for (child = this->children.begin(); child != this->children.end(); child++)
78    delete *child;
79
80  if (this->parent != NULL)
81  {
82    this->parent->children.remove(this);
83    this->parent = NULL;
84  }
85
86  // remove all other allocated memory.
87  if (this->toCoordinate != NULL)
88    delete this->toCoordinate;
89  if (this->toDirection != NULL)
90    delete this->toDirection;
91}
92
93
94/**
95 * initializes a Element2D
96 */
97void Element2D::init()
98{
99  this->setClassID(CL_ELEMENT_2D, "Element2D");
100
101  this->setVisibility(true);
102  this->setActiveness(true);
103  this->setAlignment(E2D_ALIGN_NONE);
104  this->layer = E2D_DEFAULT_LAYER;
105  this->bindNode = NULL;
106
107  this->setParentMode2D(E2D_PARENT_ALL);
108  this->parent = NULL;
109  this->absDirection = 0.0;
110  this->relDirection = 0.0;
111  this->bRelCoorChanged = true;
112  this->bRelDirChanged = true;
113  this->toCoordinate = NULL;
114  this->toDirection = NULL;
115  this->setSize2D(1,1);
116}
117
118/**
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{
124  // ELEMENT2D-native settings.
125  LoadParam(root, "alignment", this, Element2D, setAlignment)
126      .describe("loads the alignment: (either: center, left, right or screen-center)");
127
128  LoadParam(root, "layer", this, Element2D, setLayer)
129      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
130
131  LoadParam(root, "bind-node", this, Element2D, setBindNode)
132      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
133
134  LoadParam(root, "visibility", this, Element2D, setVisibility)
135      .describe("if the Element is visible or not");
136
137
138// PNode-style:
139  LoadParam(root, "rel-coor", this, Element2D, setRelCoor2D)
140      .describe("Sets The relative position of the Node to its parent.");
141
142  LoadParam(root, "abs-coor", this, Element2D, setAbsCoor2D)
143      .describe("Sets The absolute Position of the Node.");
144
145  LoadParam(root, "rel-dir", this, Element2D, setRelDir2D)
146      .describe("Sets The relative rotation of the Node to its parent.");
147
148  LoadParam(root, "abs-dir", this, Element2D, setAbsDir2D)
149      .describe("Sets The absolute rotation of the Node.");
150
151  LoadParam(root, "parent", this, Element2D, setParent2D)
152      .describe("the Name of the Parent of this Element2D");
153
154  LoadParam(root, "parent-mode", this, Element2D, setParentMode2D)
155      .describe("the mode to connect this node to its parent ()");
156
157  // cycling properties
158  if (root != NULL)
159  {
160    LOAD_PARAM_START_CYCLE(root, element);
161    {
162      LoadParam_CYCLE(element, "parent", this, Element2D, addChild2D)
163          .describe("adds a new Child to the current Node.");
164    }
165    LOAD_PARAM_END_CYCLE(element);
166  }
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
185
186/**
187 * moves a Element to another layer
188 * @param layer the Layer this is drawn on
189 */
190void Element2D::setLayer(E2D_LAYER layer)
191{
192  if (this->parent != NULL && this->parent->getLayer() > layer)
193  {
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()));
199    layer = this->parent->getLayer();
200  }
201  this->layer = layer;
202}
203
204/**
205 * sets the layer onto which this 2D-element is projected to.
206 * @param layer the layer @see loadParams @see Element2D::charToLayer2D(const char* layer)
207 */
208void Element2D::setLayer(const char* layer)
209{
210  this->setLayer(Element2D::charToLayer2D(layer));
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
225/**
226 * sets the relative coordinate of the Element2D to its parent
227 * @param relCoord the relative coordinate to the parent
228 */
229void Element2D::setRelCoor2D (const Vector& relCoord)
230{
231  if (this->toCoordinate!= NULL)
232  {
233    delete this->toCoordinate;
234    this->toCoordinate = NULL;
235  }
236  this->relCoordinate = relCoord;
237  this->bRelCoorChanged = true;
238}
239
240
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 */
247void Element2D::setRelCoor2D (float x, float y, float z)
248{
249  this->setRelCoor2D(Vector(x,y,z));
250}
251
252/**
253 * sets the Relative coordinate to the parent in Pixels
254 * @param x the relCoord X
255 * @param y the relCoord Y
256 */
257void Element2D::setRelCoor2Dpx (int x, int y)
258{
259  this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
260                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
261                     0
262                           ));
263}
264
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 */
270void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias)
271{
272  if (likely(this->toCoordinate == NULL))
273    this->toCoordinate = new Vector();
274
275  *this->toCoordinate = relCoordSoft;
276  this->bias = bias;
277}
278
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 */
285void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias)
286{
287  this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
288                         (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
289                         0),
290                         bias);
291}
292
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 */
300void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias)
301{
302  this->setRelCoorSoft2D(Vector(x, y, depth), bias);
303}
304
305/**
306 * @param absCoord set absolute coordinate
307 */
308void Element2D::setAbsCoor2D (const Vector& absCoord)
309{
310  if (this->toCoordinate!= NULL)
311  {
312    delete this->toCoordinate;
313    this->toCoordinate = NULL;
314  }
315
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;
333}
334
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 */
341void Element2D::setAbsCoor2D (float x, float y, float depth)
342{
343  this->setAbsCoor2D(Vector(x, y, depth));
344}
345
346/**
347 * @param x x-coordinate in Pixels
348 * @param y y-coordinate in Pixels
349 * @see void PNode::setAbsCoor (const Vector& absCoord)
350 */
351void Element2D::setAbsCoor2Dpx (int x, int y)
352{
353  this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
354                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
355                     0
356                           ));
357}
358
359/**
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/**
399 *  shift coordinate ralative
400 * @param shift shift vector
401 *
402 * This simply adds the shift-Vector to the relative Coordinate
403 */
404void Element2D::shiftCoor2D (const Vector& shift)
405{
406  this->relCoordinate += shift;
407  this->bRelCoorChanged = true;
408
409}
410
411/**
412 * shifts in PixelSpace
413 * @param x the pixels to shift in X
414 * @param y the pixels to shift in Y
415 */
416void Element2D::shiftCoor2Dpx (int x, int y)
417{
418  this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
419                    (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
420                     0));
421}
422
423/**
424 *  set relative direction
425 * @param relDir to its parent
426 */
427void Element2D::setRelDir2D (float relDir)
428{
429  if (this->toDirection!= NULL)
430  {
431    delete this->toDirection;
432    this->toDirection = NULL;
433  }
434
435  this->relDirection = relDir;
436  this->bRelDirChanged = true;
437}
438
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 */
444void Element2D::setRelDirSoft2D(float relDirSoft, float bias)
445{
446  if (likely(this->toDirection == NULL))
447    this->toDirection = new float;
448
449  *this->toDirection = relDirSoft;
450  this->bias = bias;
451}
452
453/**
454 *  sets the absolute direction
455 * @param absDir absolute coordinates
456 */
457void Element2D::setAbsDir2D (float absDir)
458{
459  if (this->toDirection!= NULL)
460  {
461    delete this->toDirection;
462    this->toDirection = NULL;
463  }
464
465  if (likely(this->parent != NULL))
466    this->relDirection = absDir - this->parent->getAbsDir2D();
467  else
468    this->relDirection = absDir;
469
470  this->bRelDirChanged = true;
471}
472
473/**
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/**
491 * shift Direction
492 * @param shift the direction around which to shift.
493 */
494void Element2D::shiftDir2D (float shiftDir)
495{
496  this->relDirection = this->relDirection + shiftDir;
497  this->bRelDirChanged = true;
498}
499
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 */
507void Element2D::addChild2D (Element2D* child)
508{
509  if( likely(child->parent != NULL))
510  {
511    PRINTF(5)("Element2D::addChild() - reparenting node: removing it and adding it again\n");
512    child->parent->children.remove(child);
513  }
514  child->parent = this;
515  if (likely(this != NULL))
516  {
517    // ELEMENT SORTING TO LAYERS  //
518    unsigned int childCount = this->children.size();
519
520    list<Element2D*>::iterator elem;
521    for (elem = this->children.begin(); elem != this->children.end(); elem++)
522    {
523      if ((*elem)->layer < child->layer)
524      {
525        this->children.insert(elem, child);
526        break;
527      }
528    }
529
530    if (childCount == this->children.size())
531      this->children.push_back(child);
532    ////////////////////////////////
533    if (unlikely(this->layer > child->getLayer()))
534    {
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));
537      child->setLayer(this->layer);
538    }
539  }
540  child->parentCoorChanged();
541}
542
543/**
544 * @see Element2D::addChild(Element2D* child);
545 * @param childName the name of the child to add to this PNode
546 */
547void Element2D::addChild2D (const char* childName)
548{
549  Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D));
550  if (childNode != NULL)
551    this->addChild2D(childNode);
552}
553
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 */
560void Element2D::removeChild2D (Element2D* child)
561{
562  if (child != NULL)
563    child->remove2D();
564}
565
566/**
567 * remove this Element from the tree and adds all children to NullElement2D
568 *
569 * afterwards this Node is free, and can be reattached, or deleted freely.
570 */
571void Element2D::remove2D()
572{
573  list<Element2D*>::iterator child;
574  for (child = this->children.begin(); child != this->children.end(); child++)
575    NullElement2D::getInstance()->addChild2D(*child);
576
577  this->children.erase(this->children.begin(), this->children.end());
578
579  if (this->parent != NULL)
580  {
581    this->parent->children.remove(this);
582    this->parent = NULL;
583  }
584}
585
586/**
587 * sets the parent of this Element2D
588 * @param parent the Parent to set
589 */
590void Element2D::setParent2D (Element2D* parent)
591{
592  parent->addChild2D(this);
593}
594
595/**
596 * @see Element2D::setParent(Element2D* parent);
597 * @param parentName the name of the Parent to set to this Element2D
598 */
599void Element2D::setParent2D (const char* parentName)
600{
601  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
602  if (parentNode != NULL)
603    parentNode->addChild2D(this);
604
605}
606
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 */
612void Element2D::setParentSoft2D(Element2D* parentNode, float bias)
613{
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
635  if (this->parentMode & PNODE_ROTATE_MOVEMENT) //! @todo implement this.
636    ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()));
637  else
638    this->relCoordinate = (tmpV - parentNode->getAbsCoor2D());
639  this->bRelCoorChanged = true;
640
641  this->relDirection = (tmpQ - parentNode->getAbsDir2D());
642  this->bRelDirChanged = true;
643}
644
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 */
650void Element2D::setParentSoft2D(const char* parentName, float bias)
651{
652  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
653  if (parentNode != NULL)
654    this->setParentSoft2D(parentNode, bias);
655}
656
657/**
658 *  sets the mode of this parent manually
659 * @param parentMode a String representing this parentingMode
660 */
661void Element2D::setParentMode2D (const char* parentingMode)
662{
663  this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode));
664}
665
666/**
667 *  updates the absCoordinate/absDirection
668 * @param dt The time passed since the last update
669
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 */
674void Element2D::update2D (float dt)
675{
676  // setting the Position of this 2D-Element.
677  if( likely(this->parent != NULL))
678  {
679      // movement for nodes with smoothMove enabled
680    if (unlikely(this->toCoordinate != NULL))
681    {
682      Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
683
684      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
685      {
686        this->shiftCoor2D(moveVect);
687      }
688      else
689      {
690        Vector tmp = *this->toCoordinate;
691        this->setRelCoor2D(tmp);
692        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
693      }
694    }
695    if (unlikely(this->toDirection != NULL))
696    {
697      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
698      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
699      {
700        this->shiftDir2D(rotFlot);
701      }
702      else
703      {
704        float tmp = *this->toDirection;
705        this->setRelDir2D(tmp);
706        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
707      }
708    }
709
710    // MAIN UPDATE /////////////////////////////////////
711    this->lastAbsCoordinate = this->absCoordinate;
712
713    PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
714
715
716    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
717    {
718      /* update the current absDirection - remember * means rotation around sth.*/
719      this->prevRelDirection = this->relDirection;
720      this->absDirection = this->relDirection + parent->getAbsDir2D();;
721    }
722
723
724    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
725    {
726      this->prevRelCoordinate = this->relCoordinate;
727      this->absCoordinate.x = .5 + this->relCoordinate.x;
728      this->absCoordinate.y = .5 + this->relCoordinate.y;
729      this->absCoordinate.z = 0.0;
730    }
731    else if (unlikely(this->bindNode != NULL))
732    {
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);
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;
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;
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
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()));
767
768      }
769    }
770    /////////////////////////////////////////////////
771  }
772  else
773  {
774    PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
775    if (this->bRelCoorChanged)
776    {
777      this->prevRelCoordinate = this->relCoordinate;
778      this->absCoordinate = this->relCoordinate;
779    }
780    if (this->bRelDirChanged)
781    {
782      this->prevRelDirection = this->relDirection;
783      this->absDirection = this->getAbsDir2D() + this->relDirection;
784    }
785  }
786
787
788  // UPDATE CHILDREN
789  if(this->children.size() > 0)
790  {
791    list<Element2D*>::iterator child;
792    for (child = this->children.begin(); child != this->children.end(); child++)
793    {
794      /* if this node has changed, make sure, that all children are updated also */
795      if( likely(this->bRelCoorChanged))
796        (*child)->parentCoorChanged ();
797      if( likely(this->bRelDirChanged))
798        (*child)->parentDirChanged ();
799
800      (*child)->update2D(dt);
801    }
802  }
803
804  // FINISHING PROCESS
805  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
806  this->bRelCoorChanged = false;
807  this->bRelDirChanged = false;
808}
809
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 */
816void Element2D::debug (unsigned int depth, unsigned int level) const
817{
818  for (unsigned int i = 0; i < level; i++)
819    PRINT(0)(" |");
820  if (this->children.size() > 0)
821    PRINT(0)(" +");
822  else
823    PRINT(0)(" -");
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));
834
835  if (depth >= 2 || depth == 0)
836  {
837    list<Element2D*>::const_iterator child;
838    for (child = this->children.begin(); child != this->children.end(); child++)
839    {
840      if (depth == 0)
841        (*child)->debug(0, level + 1);
842      else
843        (*child)->debug(depth - 1, level +1);
844    }
845  }
846}
847
848/**
849 * ticks the 2d-Element
850 * @param dt the time elapsed since the last tick
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.
855 */
856void Element2D::tick2D(float dt)
857{
858  if (this->active)
859    this->tick(dt);
860  if (this->children.size() > 0)
861  {
862    list<Element2D*>::iterator child;
863    for (child = this->children.begin(); child != this->children.end(); child++)
864      (*child)->tick2D(dt);
865  }
866}
867
868/**
869 * draws all the Elements from this element2D downwards
870 * @param layer the maximal Layer to draw. @see E2D_LAYER
871 */
872void Element2D::draw2D(short layer) const
873{
874  if (this->visible)
875    this->draw();
876  if (this->children.size() > 0)
877  {
878    list<Element2D*>::const_iterator child;
879    for (child = this->children.begin(); child != this->children.end(); child++)
880      if (likely(layer >= this->layer))
881        (*child)->draw2D(layer);
882  }
883}
884
885/**
886 * displays the Element2D at its position with its rotation as a Plane.
887 */
888void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
889{
890  if (level == 0)
891  {
892    glPushAttrib(GL_ENABLE_BIT);
893    glMatrixMode(GL_MODELVIEW);
894
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);
908  glVertex2f(0, 0);
909  glVertex2f(0, +this->getSizeY2D());
910  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
911  glVertex2f(+this->getSizeX2D(), 0);
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));
919    list<Element2D*>::const_iterator child;
920    for (child = this->children.begin(); child != this->children.end(); child++)
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);
931        glVertex3f((*child)->getAbsCoor2D ().x,
932                   (*child)->getAbsCoor2D ().y,
933                   0);
934        glEnd();
935      }
936      if (depth == 0)
937        (*child)->debugDraw2D(0, size, childColor, level+1);
938      else
939        (*child)->debugDraw2D(depth - 1, size, childColor, level +1);
940    }
941  }
942  if (level == 0)
943    glPopAttrib();
944
945}
946
947
948// helper functions //
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 */
954const char* Element2D::parentingModeToChar2D(int parentingMode)
955{
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";
966}
967
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 */
973E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode)
974{
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);
985}
986
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}
1013
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}
1032
1033
1034
1035
1036///////////////////
1037// NullElement2D //
1038///////////////////
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 */
1045NullElement2D::NullElement2D () : Element2D(NULL, E2D_LAYER_BELOW_ALL)
1046{
1047  this->setClassID(CL_NULL_ELEMENT_2D, "NullElement2D");
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.