Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7174 was 7052, checked in by bensch, 19 years ago

less debug

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