Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7029 was 7029, checked in by bensch, 18 years ago

orxonox/trunk: element2d's should be cleanly deleted at the end now :)

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