Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ini-parser in own subdir now (also moved tiXml-lib to lib/parser

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