Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Element2D uses Vector2D instead of Vector whenever possible

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