Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/lib/graphics/render2D/element_2d.cc @ 8513

Last change on this file since 8513 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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