Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Text offers its Size to Element2D

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