Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/graphics/render2D/element_2d.cc @ 7341

Last change on this file since 7341 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
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 "graphics_engine.h"
22#include "p_node.h"
23#include "load_param.h"
24#include "tinyxml.h"
25#include "class_list.h"
26#include "list.h"
27#include "color.h"
28
29using namespace std;
30
31/**
32 * standard constructor
33 */
34Element2D::Element2D()
35{
36  this->init();
37  this->setParent2D(NullElement2D::getInstance());
38}
39
40/**
41 * standard constructor
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 */
47Element2D::Element2D (Element2D* parent, E2D_LAYER layer)
48{
49  this->init();
50  this->layer = layer;
51  // check Parenting, and if ok parent the stuff
52  if (this->parent != NULL)
53    this->setParent2D(parent);
54  else if (NullElement2D::isInstanciated())
55    this->setParent2D(NullElement2D::getInstance());
56}
57
58/**
59 * standard deconstructor
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:
69 *    moves its children to the NullElement2D
70 *    then deletes the Element.
71 */
72Element2D::~Element2D ()
73{
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)
79  {
80    delete child;
81    child = iterator->nextElement();
82  }
83  delete iterator;
84
85  if (this->parent != NULL)
86  {
87    this->parent->children->remove(this);
88    this->parent = NULL;
89  }
90  delete this->children;
91
92  // remove all other allocated memory.
93  if (this->toCoordinate != NULL)
94    delete this->toCoordinate;
95  if (this->toDirection != NULL)
96    delete this->toDirection;
97}
98
99
100/**
101 * initializes a Element2D
102 */
103void Element2D::init()
104{
105  this->setClassID(CL_ELEMENT_2D, "Element2D");
106
107  this->setVisibility(true);
108  this->setActiveness(true);
109  this->setAlignment(E2D_ALIGN_NONE);
110  this->layer = E2D_DEFAULT_LAYER;
111  this->bindNode = NULL;
112
113  this->setParentMode2D(E2D_PARENT_ALL);
114  this->parent = NULL;
115  this->children = new tList<Element2D>;
116  this->absDirection = 0.0;
117  this->relDirection = 0.0;
118  this->bRelCoorChanged = true;
119  this->bRelDirChanged = true;
120  this->toCoordinate = NULL;
121  this->toDirection = NULL;
122  this->setSize2D(1,1);
123}
124
125/**
126 * Loads the Parameters of an Element2D from...
127 * @param root The XML-element to load from
128 */
129void Element2D::loadParams(const TiXmlElement* root)
130{
131  // ELEMENT2D-native settings.
132  LoadParam(root, "alignment", this, Element2D, setAlignment)
133      .describe("loads the alignment: (either: center, left, right or screen-center)");
134
135  LoadParam(root, "layer", this, Element2D, setLayer)
136      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
137
138  LoadParam(root, "bind-node", this, Element2D, setBindNode)
139      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
140
141  LoadParam(root, "visibility", this, Element2D, setVisibility)
142      .describe("if the Element is visible or not");
143
144
145// PNode-style:
146  LoadParam(root, "rel-coor", this, Element2D, setRelCoor2D)
147      .describe("Sets The relative position of the Node to its parent.");
148
149  LoadParam(root, "abs-coor", this, Element2D, setAbsCoor2D)
150      .describe("Sets The absolute Position of the Node.");
151
152  LoadParam(root, "rel-dir", this, Element2D, setRelDir2D)
153      .describe("Sets The relative rotation of the Node to its parent.");
154
155  LoadParam(root, "abs-dir", this, Element2D, setAbsDir2D)
156      .describe("Sets The absolute rotation of the Node.");
157
158  LoadParam(root, "parent", this, Element2D, setParent2D)
159      .describe("the Name of the Parent of this Element2D");
160
161  LoadParam(root, "parent-mode", this, Element2D, setParentMode2D)
162      .describe("the mode to connect this node to its parent ()");
163
164  // cycling properties
165  if (root != NULL)
166  {
167    LOAD_PARAM_START_CYCLE(root, element);
168    {
169      LoadParam_CYCLE(element, "parent", this, Element2D, addChild2D)
170          .describe("adds a new Child to the current Node.");
171    }
172    LOAD_PARAM_END_CYCLE(element);
173  }
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
192
193/**
194 * moves a Element to another layer
195 * @param layer the Layer this is drawn on
196 */
197void Element2D::setLayer(E2D_LAYER layer)
198{
199  if (this->parent != NULL && this->parent->getLayer() > layer)
200  {
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()));
206    layer = this->parent->getLayer();
207  }
208  this->layer = layer;
209}
210
211/**
212 * sets the layer onto which this 2D-element is projected to.
213 * @param layer the layer @see loadParams @see Element2D::charToLayer2D(const char* layer)
214 */
215void Element2D::setLayer(const char* layer)
216{
217  this->setLayer(Element2D::charToLayer2D(layer));
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
232/**
233 * sets the relative coordinate of the Element2D to its parent
234 * @param relCoord the relative coordinate to the parent
235 */
236void Element2D::setRelCoor2D (const Vector& relCoord)
237{
238  if (this->toCoordinate!= NULL)
239  {
240    delete this->toCoordinate;
241    this->toCoordinate = NULL;
242  }
243  this->relCoordinate = relCoord;
244  this->bRelCoorChanged = true;
245}
246
247
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 */
254void Element2D::setRelCoor2D (float x, float y, float z)
255{
256  this->setRelCoor2D(Vector(x,y,z));
257}
258
259/**
260 * sets the Relative coordinate to the parent in Pixels
261 * @param x the relCoord X
262 * @param y the relCoord Y
263 */
264void Element2D::setRelCoor2Dpx (int x, int y)
265{
266  this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
267                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
268                     0
269                           ));
270}
271
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 */
277void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias)
278{
279  if (likely(this->toCoordinate == NULL))
280    this->toCoordinate = new Vector();
281
282  *this->toCoordinate = relCoordSoft;
283  this->bias = bias;
284}
285
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 */
292void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias)
293{
294  this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
295                         (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
296                         0),
297                         bias);
298}
299
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 */
307void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias)
308{
309  this->setRelCoorSoft2D(Vector(x, y, depth), bias);
310}
311
312/**
313 * @param absCoord set absolute coordinate
314 */
315void Element2D::setAbsCoor2D (const Vector& absCoord)
316{
317  if (this->toCoordinate!= NULL)
318  {
319    delete this->toCoordinate;
320    this->toCoordinate = NULL;
321  }
322
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;
340}
341
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 */
348void Element2D::setAbsCoor2D (float x, float y, float depth)
349{
350  this->setAbsCoor2D(Vector(x, y, depth));
351}
352
353/**
354 * @param x x-coordinate in Pixels
355 * @param y y-coordinate in Pixels
356 * @see void PNode::setAbsCoor (const Vector& absCoord)
357 */
358void Element2D::setAbsCoor2Dpx (int x, int y)
359{
360  this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
361                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
362                     0
363                           ));
364}
365
366/**
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/**
406 *  shift coordinate ralative
407 * @param shift shift vector
408 *
409 * This simply adds the shift-Vector to the relative Coordinate
410 */
411void Element2D::shiftCoor2D (const Vector& shift)
412{
413  this->relCoordinate += shift;
414  this->bRelCoorChanged = true;
415
416}
417
418/**
419 * shifts in PixelSpace
420 * @param x the pixels to shift in X
421 * @param y the pixels to shift in Y
422 */
423void Element2D::shiftCoor2Dpx (int x, int y)
424{
425  this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
426                    (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
427                     0));
428}
429
430/**
431 *  set relative direction
432 * @param relDir to its parent
433 */
434void Element2D::setRelDir2D (float relDir)
435{
436  if (this->toDirection!= NULL)
437  {
438    delete this->toDirection;
439    this->toDirection = NULL;
440  }
441
442  this->relDirection = relDir;
443  this->bRelDirChanged = true;
444}
445
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 */
451void Element2D::setRelDirSoft2D(float relDirSoft, float bias)
452{
453  if (likely(this->toDirection == NULL))
454    this->toDirection = new float;
455
456  *this->toDirection = relDirSoft;
457  this->bias = bias;
458}
459
460/**
461 *  sets the absolute direction
462 * @param absDir absolute coordinates
463 */
464void Element2D::setAbsDir2D (float absDir)
465{
466  if (this->toDirection!= NULL)
467  {
468    delete this->toDirection;
469    this->toDirection = NULL;
470  }
471
472  if (likely(this->parent != NULL))
473    this->relDirection = absDir - this->parent->getAbsDir2D();
474  else
475    this->relDirection = absDir;
476
477  this->bRelDirChanged = true;
478}
479
480/**
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/**
498 * shift Direction
499 * @param shift the direction around which to shift.
500 */
501void Element2D::shiftDir2D (float shiftDir)
502{
503  this->relDirection = this->relDirection + shiftDir;
504  this->bRelDirChanged = true;
505}
506
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 */
514void Element2D::addChild2D (Element2D* child)
515{
516  if( likely(child->parent != NULL))
517  {
518    PRINTF(5)("Element2D::addChild() - reparenting node: removing it and adding it again\n");
519    child->parent->children->remove(child);
520  }
521  child->parent = this;
522  if (likely(this != NULL))
523  {
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    ////////////////////////////////
541    if (unlikely(this->layer > child->getLayer()))
542    {
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));
545      child->setLayer(this->layer);
546    }
547  }
548  child->parentCoorChanged();
549}
550
551/**
552 * @see Element2D::addChild(Element2D* child);
553 * @param childName the name of the child to add to this PNode
554 */
555void Element2D::addChild2D (const char* childName)
556{
557  Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D));
558  if (childNode != NULL)
559    this->addChild2D(childNode);
560}
561
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 */
568void Element2D::removeChild2D (Element2D* child)
569{
570  if (child != NULL)
571    child->remove2D();
572}
573
574/**
575 * remove this Element from the tree and adds all children to NullElement2D
576 *
577 * afterwards this Node is free, and can be reattached, or deleted freely.
578 */
579void Element2D::remove2D()
580{
581  tIterator<Element2D>* iterator = this->children->getIterator();
582  Element2D* pn = iterator->firstElement();
583
584  while( pn != NULL)
585  {
586    NullElement2D::getInstance()->addChild2D(pn);
587    pn = iterator->nextElement();
588  }
589  delete iterator;
590
591  delete this->children;
592  this->children = new tList<Element2D>;
593
594  if (this->parent != NULL)
595  {
596    this->parent->children->remove(this);
597    this->parent = NULL;
598  }
599}
600
601/**
602 * sets the parent of this Element2D
603 * @param parent the Parent to set
604 */
605void Element2D::setParent2D (Element2D* parent)
606{
607  parent->addChild2D(this);
608}
609
610/**
611 * @see Element2D::setParent(Element2D* parent);
612 * @param parentName the name of the Parent to set to this Element2D
613 */
614void Element2D::setParent2D (const char* parentName)
615{
616  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
617  if (parentNode != NULL)
618    parentNode->addChild2D(this);
619
620}
621
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 */
627void Element2D::setParentSoft2D(Element2D* parentNode, float bias)
628{
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
650  if (this->parentMode & PNODE_ROTATE_MOVEMENT) //! @todo implement this.
651    ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()));
652  else
653    this->relCoordinate = (tmpV - parentNode->getAbsCoor2D());
654  this->bRelCoorChanged = true;
655
656  this->relDirection = (tmpQ - parentNode->getAbsDir2D());
657  this->bRelDirChanged = true;
658}
659
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 */
665void Element2D::setParentSoft2D(const char* parentName, float bias)
666{
667  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
668  if (parentNode != NULL)
669    this->setParentSoft2D(parentNode, bias);
670}
671
672/**
673 *  sets the mode of this parent manually
674 * @param parentMode a String representing this parentingMode
675 */
676void Element2D::setParentMode2D (const char* parentingMode)
677{
678  this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode));
679}
680
681/**
682 *  updates the absCoordinate/absDirection
683 * @param dt The time passed since the last update
684
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 */
689void Element2D::update2D (float dt)
690{
691  // setting the Position of this 2D-Element.
692  if( likely(this->parent != NULL))
693  {
694      // movement for nodes with smoothMove enabled
695    if (unlikely(this->toCoordinate != NULL))
696    {
697      Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
698
699      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
700      {
701        this->shiftCoor2D(moveVect);
702      }
703      else
704      {
705        Vector tmp = *this->toCoordinate;
706        this->setRelCoor2D(tmp);
707        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
708      }
709    }
710    if (unlikely(this->toDirection != NULL))
711    {
712      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
713      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
714      {
715        this->shiftDir2D(rotFlot);
716      }
717      else
718      {
719        float tmp = *this->toDirection;
720        this->setRelDir2D(tmp);
721        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
722      }
723    }
724
725    // MAIN UPDATE /////////////////////////////////////
726    this->lastAbsCoordinate = this->absCoordinate;
727
728    PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
729
730
731    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
732    {
733      /* update the current absDirection - remember * means rotation around sth.*/
734      this->prevRelDirection = this->relDirection;
735      this->absDirection = this->relDirection + parent->getAbsDir2D();;
736    }
737
738
739    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
740    {
741      this->prevRelCoordinate = this->relCoordinate;
742      this->absCoordinate.x = .5 + this->relCoordinate.x;
743      this->absCoordinate.y = .5 + this->relCoordinate.y;
744      this->absCoordinate.z = 0.0;
745    }
746    else if (unlikely(this->bindNode != NULL))
747    {
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);
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;
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;
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
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()));
782
783      }
784    }
785    /////////////////////////////////////////////////
786  }
787  else
788  {
789    PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
790    if (this->bRelCoorChanged)
791    {
792      this->prevRelCoordinate = this->relCoordinate;
793      this->absCoordinate = this->relCoordinate;
794    }
795    if (this->bRelDirChanged)
796    {
797      this->prevRelDirection = this->relDirection;
798      this->absDirection = this->getAbsDir2D() + this->relDirection;
799    }
800  }
801
802
803  // UPDATE CHILDREN
804  if(this->children->getSize() > 0)
805  {
806    tIterator<Element2D>* iterator = this->children->getIterator();
807    Element2D* pn = iterator->firstElement();
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  }
821
822  // FINISHING PROCESS
823  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
824  this->bRelCoorChanged = false;
825  this->bRelDirChanged = false;
826}
827
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 */
834void Element2D::debug (unsigned int depth, unsigned int level) const
835{
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)(" -");
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));
852
853  if (depth >= 2 || depth == 0)
854  {
855    tIterator<Element2D>* iterator = this->children->getIterator();
856    Element2D* pn = iterator->firstElement();
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  }
867}
868
869/**
870 * ticks the 2d-Element
871 * @param dt the time elapsed since the last tick
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.
876 */
877void Element2D::tick2D(float dt)
878{
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}
893
894/**
895 * draws all the Elements from this element2D downwards
896 * @param layer the maximal Layer to draw. @see E2D_LAYER
897 */
898void Element2D::draw2D(short layer) const
899{
900  if (this->visible)
901    this->draw();
902  if (this->children->getSize() > 0)
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  }
914}
915
916/**
917 * displays the Element2D at its position with its rotation as a Plane.
918 */
919void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
920{
921  if (level == 0)
922  {
923    glPushAttrib(GL_ENABLE_BIT);
924    glMatrixMode(GL_MODELVIEW);
925
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);
939  glVertex2f(0, 0);
940  glVertex2f(0, +this->getSizeY2D());
941  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
942  glVertex2f(+this->getSizeX2D(), 0);
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
979}
980
981
982// helper functions //
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 */
988const char* Element2D::parentingModeToChar2D(int parentingMode)
989{
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";
1000}
1001
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 */
1007E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode)
1008{
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);
1019}
1020
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}
1047
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}
1066
1067
1068
1069
1070///////////////////
1071// NullElement2D //
1072///////////////////
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 */
1079NullElement2D::NullElement2D () : Element2D(NULL, E2D_LAYER_BELOW_ALL)
1080{
1081  this->setClassID(CL_NULL_ELEMENT_2D, "NullElement2D");
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.