Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new GUI-functionality

File size: 30.0 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 <algorithm>
22
23#include "p_node.h"
24
25#include "graphics_engine.h"
26#include "load_param.h"
27#include "class_list.h"
28
29#include "color.h"
30
31using namespace std;
32
33/**
34 * standard constructor
35 */
36Element2D::Element2D()
37{
38  this->init();
39  this->setParent2D(NullElement2D::getInstance());
40}
41
42/**
43 * @brief standard constructor
44 * @param parent the parent to set for this Element2D
45 *
46 * NullElement2D needs this constructor with parameter NULL to initialize
47 * itself. Otherwise it would result in an endless Loop.
48 */
49Element2D::Element2D (Element2D* parent, E2D_LAYER layer)
50{
51  this->init();
52  this->layer = layer;
53  // check Parenting, and if ok parent the stuff
54  if (this->parent != NULL)
55    this->setParent2D(parent);
56  else if (NullElement2D::isInstanciated())
57    this->setParent2D(NullElement2D::getInstance());
58}
59
60/**
61 * @brief standard deconstructor
62 *
63 * There are two general ways to delete an Element2D
64 * 1. delete instance;
65 *   -> result
66 *    delete this Node and all its children and children's children...
67 *    (danger if you still want the instance!!)
68 *
69 * 2. instance->remove2D(); delete instance;
70 *   -> result:
71 *    moves its children to the NullElement2D
72 *    then deletes the Element.
73 */
74Element2D::~Element2D ()
75{
76  // remove the Node, delete it's children.
77  while (this->children.size() > 0)
78  {
79    Element2D* child = this->children.front();
80    this->children.pop_front();
81    delete child;
82  }
83  if (this->parent != NULL)
84  {
85    this->parent->eraseChild(this);
86    this->parent = NULL;
87  }
88
89  // remove all other allocated memory.
90  if (this->toCoordinate != NULL)
91    delete this->toCoordinate;
92  if (this->toDirection != NULL)
93    delete this->toDirection;
94}
95
96
97/**
98 * @brief initializes a Element2D
99 */
100void Element2D::init()
101{
102  this->setClassID(CL_ELEMENT_2D, "Element2D");
103
104  this->setVisibility(true);
105  this->setActiveness(true);
106  this->setAlignment(E2D_ALIGN_NONE);
107  this->layer = E2D_DEFAULT_LAYER;
108  this->bindNode = NULL;
109
110  this->setParentMode2D(E2D_PARENT_ALL);
111  this->parent = NULL;
112  this->absDirection = 0.0;
113  this->relDirection = 0.0;
114  this->bRelCoorChanged = true;
115  this->bRelDirChanged = true;
116  this->toCoordinate = NULL;
117  this->toDirection = NULL;
118  this->setSize2D(1,1);
119}
120
121/**
122 * @brief Loads the Parameters of an Element2D from...
123 * @param root The XML-element to load from
124 */
125void Element2D::loadParams(const TiXmlElement* root)
126{
127  // ELEMENT2D-native settings.
128  LoadParam(root, "alignment", this, Element2D, setAlignment)
129      .describe("loads the alignment: (either: center, left, right or screen-center)");
130
131  LoadParam(root, "layer", this, Element2D, setLayer)
132      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
133
134  LoadParam(root, "bind-node", this, Element2D, setBindNode)
135      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
136
137  LoadParam(root, "visibility", this, Element2D, setVisibility)
138      .describe("if the Element is visible or not");
139
140
141// PNode-style:
142  LoadParam(root, "rel-coor", this, Element2D, setRelCoor2D)
143      .describe("Sets The relative position of the Node to its parent.");
144
145  LoadParam(root, "abs-coor", this, Element2D, setAbsCoor2D)
146      .describe("Sets The absolute Position of the Node.");
147
148  LoadParam(root, "rel-dir", this, Element2D, setRelDir2D)
149      .describe("Sets The relative rotation of the Node to its parent.");
150
151  LoadParam(root, "abs-dir", this, Element2D, setAbsDir2D)
152      .describe("Sets The absolute rotation of the Node.");
153
154  LoadParam(root, "parent", this, Element2D, setParent2D)
155      .describe("the Name of the Parent of this Element2D");
156
157  LoadParam(root, "parent-mode", this, Element2D, setParentMode2D)
158      .describe("the mode to connect this node to its parent ()");
159
160  // cycling properties
161  LOAD_PARAM_START_CYCLE(root, element);
162  {
163    LoadParam_CYCLE(element, "parent", this, Element2D, addChild2D)
164        .describe("adds a new Child to the current Node.");
165  }
166  LOAD_PARAM_END_CYCLE(element);
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  assert(child != NULL);
510  if( likely(child->parent != NULL))
511  {
512    PRINTF(5)("Element2D::addChild() - reparenting node: removing it and adding it again\n");
513    child->parent->eraseChild(child);
514  }
515  child->parent = this;
516  if (likely(this != NULL))
517  {
518    // ELEMENT SORTING TO LAYERS  //
519    unsigned int childCount = this->children.size();
520
521    list<Element2D*>::iterator elem;
522    for (elem = this->children.begin(); elem != this->children.end(); elem++)
523    {
524      if ((*elem)->layer < child->layer)
525      {
526        this->children.insert(elem, child);
527        break;
528      }
529    }
530
531    if (childCount == this->children.size())
532      this->children.push_back(child);
533    ////////////////////////////////
534    if (unlikely(this->layer > child->getLayer()))
535    {
536      PRINTF(2)("Layer '%s' of Child(%s) lower than parents(%s) layer '%s'. updating...\n",
537      Element2D::layer2DToChar(child->getLayer()),child->getName(), this->getName(), Element2D::layer2DToChar(this->layer));
538      child->setLayer(this->layer);
539    }
540  }
541  child->parentCoorChanged();
542}
543
544/**
545 * @see Element2D::addChild(Element2D* child);
546 * @param childName the name of the child to add to this PNode
547 */
548void Element2D::addChild2D (const char* childName)
549{
550  Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D));
551  if (childNode != NULL)
552    this->addChild2D(childNode);
553}
554
555/**
556 * removes a child from the node
557 * @param child the child to remove from this Node..
558 *
559 * Children from nodes will not be lost, they are referenced to NullPointer
560 */
561void Element2D::removeChild2D (Element2D* child)
562{
563  if (child != NULL)
564    child->remove2D();
565}
566
567/**
568 * remove this Element from the tree and adds all children to NullElement2D
569 *
570 * afterwards this Node is free, and can be reattached, or deleted freely.
571 */
572void Element2D::remove2D()
573{
574  list<Element2D*>::iterator child;
575  for (child = this->children.begin(); child != this->children.end(); child++)
576    NullElement2D::getInstance()->addChild2D(*child);
577
578  this->children.clear();
579
580  if (this->parent != NULL)
581  {
582    this->parent->eraseChild(this);
583    this->parent = NULL;
584  }
585}
586
587/**
588 * sets the parent of this Element2D
589 * @param parent the Parent to set
590 */
591void Element2D::setParent2D (Element2D* parent)
592{
593  parent->addChild2D(this);
594}
595
596/**
597 * @see Element2D::setParent(Element2D* parent);
598 * @param parentName the name of the Parent to set to this Element2D
599 */
600void Element2D::setParent2D (const char* parentName)
601{
602  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
603  if (parentNode != NULL)
604    parentNode->addChild2D(this);
605
606}
607
608/**
609 * does the reparenting in a very smooth way
610 * @param parentNode the new Node to connect this node to.
611 * @param bias the speed to iterate to this new Positions
612 */
613void Element2D::setParentSoft2D(Element2D* parentNode, float bias)
614{
615  if (this->parent == parentNode)
616    return;
617
618  if (likely(this->toCoordinate == NULL))
619  {
620    this->toCoordinate = new Vector();
621    *this->toCoordinate = this->getRelCoor2D();
622  }
623  if (likely(this->toDirection == NULL))
624  {
625    this->toDirection = new float;
626    *this->toDirection = this->getRelDir2D();
627  }
628  this->bias = bias;
629
630
631  Vector tmpV = this->getAbsCoor2D();
632  float tmpQ = this->getAbsDir2D();
633
634  parentNode->addChild2D(this);
635
636  if (this->parentMode & PNODE_ROTATE_MOVEMENT) //! @todo implement this.
637    ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()));
638  else
639    this->relCoordinate = (tmpV - parentNode->getAbsCoor2D());
640  this->bRelCoorChanged = true;
641
642  this->relDirection = (tmpQ - parentNode->getAbsDir2D());
643  this->bRelDirChanged = true;
644}
645
646/**
647 * does the reparenting in a very smooth way
648 * @param parentName the name of the Parent to reconnect to
649 * @param bias the speed to iterate to this new Positions
650 */
651void Element2D::setParentSoft2D(const char* parentName, float bias)
652{
653  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
654  if (parentNode != NULL)
655    this->setParentSoft2D(parentNode, bias);
656}
657
658/** @param child the child to be erased from this Nodes List */
659void Element2D::eraseChild(Element2D* child)
660{
661  assert (this != NULL && child != NULL);
662  std::list<Element2D*>::iterator childIT = std::find(this->children.begin(), this->children.end(), child);
663  this->children.erase(childIT);
664}
665
666/**
667 * @brief sets the mode of this parent manually
668 * @param parentMode a String representing this parentingMode
669 */
670void Element2D::setParentMode2D (const char* parentingMode)
671{
672  this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode));
673}
674
675/**
676 *  updates the absCoordinate/absDirection
677 * @param dt The time passed since the last update
678
679   this is used to go through the parent-tree to update all the absolute coordinates
680   and directions. this update should be done by the engine, so you don't have to
681   worry, normaly...
682 */
683void Element2D::update2D (float dt)
684{
685  // setting the Position of this 2D-Element.
686  if( likely(this->parent != NULL))
687  {
688      // movement for nodes with smoothMove enabled
689    if (unlikely(this->toCoordinate != NULL))
690    {
691      Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
692
693      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
694      {
695        this->shiftCoor2D(moveVect);
696      }
697      else
698      {
699        Vector tmp = *this->toCoordinate;
700        this->setRelCoor2D(tmp);
701        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
702      }
703    }
704    if (unlikely(this->toDirection != NULL))
705    {
706      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
707      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
708      {
709        this->shiftDir2D(rotFlot);
710      }
711      else
712      {
713        float tmp = *this->toDirection;
714        this->setRelDir2D(tmp);
715        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
716      }
717    }
718
719    // MAIN UPDATE /////////////////////////////////////
720    this->lastAbsCoordinate = this->absCoordinate;
721
722    PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
723
724
725    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
726    {
727      /* update the current absDirection - remember * means rotation around sth.*/
728      this->prevRelDirection = this->relDirection;
729      this->absDirection = this->relDirection + parent->getAbsDir2D();;
730    }
731
732
733    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
734    {
735      this->prevRelCoordinate = this->relCoordinate;
736      this->absCoordinate.x = .5 + this->relCoordinate.x;
737      this->absCoordinate.y = .5 + this->relCoordinate.y;
738      this->absCoordinate.z = 0.0;
739    }
740    else if (unlikely(this->bindNode != NULL))
741    {
742      GLdouble projectPos[3] = {0, 0, 0};
743      gluProject(this->bindNode->getAbsCoor().x,
744                 this->bindNode->getAbsCoor().y,
745                 this->bindNode->getAbsCoor().z,
746                 GraphicsEngine::modMat,
747                 GraphicsEngine::projMat,
748                 GraphicsEngine::viewPort,
749                 projectPos,
750                 projectPos+1,
751                 projectPos+2);
752      this->prevRelCoordinate.x = this->absCoordinate.x = projectPos[0] /* /(float)GraphicsEngine::getInstance()->getResolutionX() */ + this->relCoordinate.x;
753      this->prevRelCoordinate.y = this->absCoordinate.y = (float)GraphicsEngine::getInstance()->getResolutionY() -  projectPos[1] + this->relCoordinate.y;
754      this->prevRelCoordinate.z = this->absCoordinate.z = projectPos[2] + this->relCoordinate.z;
755      this->bRelCoorChanged = true;
756    }
757    else
758    {
759      if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged))
760      {
761        /* update the current absCoordinate */
762        this->prevRelCoordinate = this->relCoordinate;
763        this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
764      }
765      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
766      {
767        /* update the current absCoordinate */
768        this->prevRelCoordinate = this->relCoordinate;
769        float sine = sin(this->parent->getAbsDir2D());
770        float cose = cos(this->parent->getAbsDir2D());
771//        this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine;
772//        this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine;
773
774        this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
775        this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
776
777      }
778    }
779    /////////////////////////////////////////////////
780  }
781  else
782  {
783    PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
784    if (this->bRelCoorChanged)
785    {
786      this->prevRelCoordinate = this->relCoordinate;
787      this->absCoordinate = this->relCoordinate;
788    }
789    if (this->bRelDirChanged)
790    {
791      this->prevRelDirection = this->relDirection;
792      this->absDirection = this->getAbsDir2D() + this->relDirection;
793    }
794  }
795
796
797  // UPDATE CHILDREN
798  if(this->children.size() > 0)
799  {
800    list<Element2D*>::iterator child;
801    for (child = this->children.begin(); child != this->children.end(); child++)
802    {
803      /* if this node has changed, make sure, that all children are updated also */
804      if( likely(this->bRelCoorChanged))
805        (*child)->parentCoorChanged ();
806      if( likely(this->bRelDirChanged))
807        (*child)->parentDirChanged ();
808
809      (*child)->update2D(dt);
810    }
811  }
812
813  // FINISHING PROCESS
814  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
815  this->bRelCoorChanged = false;
816  this->bRelDirChanged = false;
817}
818
819/**
820 *  displays some information about this pNode
821 * @param depth The deph into which to debug the children of this Element2D to.
822 * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...)
823 * @param level The n-th level of the Node we draw (this is internal and only for nice output)
824 */
825void Element2D::debug (unsigned int depth, unsigned int level) const
826{
827  for (unsigned int i = 0; i < level; i++)
828    PRINT(0)(" |");
829  if (this->children.size() > 0)
830    PRINT(0)(" +");
831  else
832    PRINT(0)(" -");
833  PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s, layer:%s\n",
834            this->getClassName(),
835            this->getName(),
836            this->absCoordinate.x,
837            this->absCoordinate.y,
838            this->relCoordinate.x,
839            this->relCoordinate.y,
840            this->getAbsDir2D(),
841            Element2D::parentingModeToChar2D(parentMode),
842            Element2D::layer2DToChar(this->layer));
843
844  if (depth >= 2 || depth == 0)
845  {
846    list<Element2D*>::const_iterator child;
847    for (child = this->children.begin(); child != this->children.end(); child++)
848    {
849      if (depth == 0)
850        (*child)->debug(0, level + 1);
851      else
852        (*child)->debug(depth - 1, level +1);
853    }
854  }
855}
856
857/**
858 * ticks the 2d-Element
859 * @param dt the time elapsed since the last tick
860 *
861 * the element only gets tickt, if it is active.
862 * Be aware, that this walks through the entire Element2D-tree,
863 * searching for Elements to be ticked.
864 */
865void Element2D::tick2D(float dt)
866{
867  if (this->active)
868    this->tick(dt);
869  if (this->children.size() > 0)
870  {
871    list<Element2D*>::iterator child;
872    for (child = this->children.begin(); child != this->children.end(); child++)
873      (*child)->tick2D(dt);
874  }
875}
876
877/**
878 * draws all the Elements from this element2D downwards
879 * @param layer the maximal Layer to draw. @see E2D_LAYER
880 */
881void Element2D::draw2D(short layer) const
882{
883  if (this->visible)
884    this->draw();
885  if (this->children.size() > 0)
886  {
887    list<Element2D*>::const_iterator child;
888    for (child = this->children.begin(); child != this->children.end(); child++)
889      if (likely(layer >= this->layer))
890        (*child)->draw2D(layer);
891  }
892}
893
894/**
895 * displays the Element2D at its position with its rotation as a Plane.
896 */
897void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
898{
899  if (level == 0)
900  {
901    glPushAttrib(GL_ENABLE_BIT);
902    glMatrixMode(GL_MODELVIEW);
903
904    glDisable(GL_LIGHTING);
905    glDisable(GL_BLEND);
906    glDisable(GL_TEXTURE_2D);
907  }
908
909  glPushMatrix();
910  /* translate */
911  /* rotate */
912  glColor3f(color.x, color.y, color.z);
913
914  glTranslatef (this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
915  glRotatef(this->getAbsDir2D(), 0,0,1);
916  glBegin(GL_LINE_LOOP);
917  glVertex2f(0, 0);
918  glVertex2f(0, +this->getSizeY2D());
919  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
920  glVertex2f(+this->getSizeX2D(), 0);
921  glEnd();
922
923
924  glPopMatrix();
925  if (depth >= 2 || depth == 0)
926  {
927    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
928    list<Element2D*>::const_iterator child;
929    for (child = this->children.begin(); child != this->children.end(); child++)
930    {
931      // drawing the Dependency graph
932      if (this != NullElement2D::getInstance())
933      {
934        glBegin(GL_LINES);
935        glColor3f(color.x, color.y, color.z);
936        glVertex3f(this->getAbsCoor2D ().x,
937                   this->getAbsCoor2D ().y,
938                   0);
939        glColor3f(childColor.x, childColor.y, childColor.z);
940        glVertex3f((*child)->getAbsCoor2D ().x,
941                   (*child)->getAbsCoor2D ().y,
942                   0);
943        glEnd();
944      }
945      if (depth == 0)
946        (*child)->debugDraw2D(0, size, childColor, level+1);
947      else
948        (*child)->debugDraw2D(depth - 1, size, childColor, level +1);
949    }
950  }
951  if (level == 0)
952    glPopAttrib();
953
954}
955
956
957// helper functions //
958/**
959 * converts a parentingMode into a string that is the name of it
960 * @param parentingMode the ParentingMode to convert
961 * @return the converted string
962 */
963const char* Element2D::parentingModeToChar2D(int parentingMode)
964{
965  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
966    return "local-rotate";
967  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
968    return "rotate-movement";
969  else if (parentingMode == E2D_PARENT_MOVEMENT)
970    return "movement";
971  else if (parentingMode == E2D_PARENT_ALL)
972    return "all";
973  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
974    return "rotate-and-move";
975}
976
977/**
978 * converts a parenting-mode-string into a int
979 * @param parentingMode the string naming the parentingMode
980 * @return the int corresponding to the named parentingMode
981 */
982E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode)
983{
984  if (!strcmp(parentingMode, "local-rotate"))
985    return (E2D_PARENT_LOCAL_ROTATE);
986  else  if (!strcmp(parentingMode, "rotate-movement"))
987    return (E2D_PARENT_ROTATE_MOVEMENT);
988  else  if (!strcmp(parentingMode, "movement"))
989    return (E2D_PARENT_MOVEMENT);
990  else  if (!strcmp(parentingMode, "all"))
991    return (E2D_PARENT_ALL);
992  else  if (!strcmp(parentingMode, "rotate-and-move"))
993    return (E2D_PARENT_ROTATE_AND_MOVE);
994}
995
996/**
997 * converts a layer into its corresponding string
998 * @param layer the layer to get the name-String of.
999 * @returns the Name of the Layer (on error the default-layer-string is returned)
1000 */
1001const char* Element2D::layer2DToChar(E2D_LAYER layer)
1002{
1003  switch(layer)
1004  {
1005    case E2D_LAYER_TOP:
1006      return "top";
1007      break;
1008    case E2D_LAYER_MEDIUM:
1009      return "medium";
1010      break;
1011    case E2D_LAYER_BOTTOM:
1012      return "bottom";
1013      break;
1014    case E2D_LAYER_BELOW_ALL:
1015      return "below-all";
1016      break;
1017    default:
1018      return layer2DToChar(E2D_DEFAULT_LAYER);
1019      break;
1020  }
1021}
1022
1023/**
1024 * converts a String holding a actual Layer
1025 * @param layer the String to convert into a Layer2D
1026 * @returns the E2D_LAYER on success, E2D_DEFAULT_LAYER on error.
1027 */
1028E2D_LAYER Element2D::charToLayer2D(const char* layer)
1029{
1030  if (!strcmp(layer, "top"))
1031    return (E2D_LAYER_TOP);
1032  else  if (!strcmp(layer, "medium"))
1033    return (E2D_LAYER_MEDIUM);
1034  else  if (!strcmp(layer, "bottom"))
1035    return (E2D_LAYER_BOTTOM);
1036  else  if (!strcmp(layer, "below-all"))
1037    return (E2D_LAYER_BELOW_ALL);
1038  else
1039    return (E2D_DEFAULT_LAYER);
1040}
1041
1042
1043
1044
1045///////////////////
1046// NullElement2D //
1047///////////////////
1048NullElement2D* NullElement2D::singletonRef = 0;
1049
1050/**
1051 * @brief creates the one and only NullElement2D
1052 */
1053NullElement2D::NullElement2D () : Element2D(NULL, E2D_LAYER_BELOW_ALL)
1054{
1055  this->setClassID(CL_NULL_ELEMENT_2D, "NullElement2D");
1056  this->setName("NullElement2D");
1057
1058  this->setParentMode2D(E2D_PARENT_ALL);
1059  NullElement2D::singletonRef = this;
1060}
1061
1062
1063/**
1064 *  standard deconstructor
1065 */
1066NullElement2D::~NullElement2D ()
1067{
1068  NullElement2D::singletonRef = NULL;
1069}
Note: See TracBrowser for help on using the repository browser.