Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Sorting in ShellCommand

File size: 33.4 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
31//#include "shell_command.h"
32//SHELL_COMMAND(debug, Element2D, debug2D);
33
34using namespace std;
35
36/**
37 * @brief standard constructor
38 * @param parent the parent to set for this Element2D
39 *
40 * NullElement2D needs this constructor with parameter NULL to initialize
41 * itself. Otherwise it would result in an endless Loop.
42 */
43Element2D::Element2D (Element2D* parent, E2D_LAYER layer, short nodeFlags)
44{
45  this->setClassID(CL_ELEMENT_2D, "Element2D");
46
47  this->setVisibility(true);
48  this->activate2D();
49  this->setAlignment(E2D_ALIGN_NONE);
50  this->bindNode = NULL;
51
52  this->parentMode = nodeFlags;
53  this->parent = NULL;
54  this->absDirection = 0.0;
55  this->relDirection = 0.0;
56  this->bRelCoorChanged = true;
57  this->bRelDirChanged = true;
58  this->toCoordinate = NULL;
59  this->toDirection = NULL;
60  this->toSize = NULL;
61  this->setSize2D(1, 1);
62
63
64  this->layer = layer;
65  if (parent != NULL)
66    parent->addChild2D(this);
67}
68
69
70/**
71 * @brief the mighty NullElement
72 * TopMost Node of them all.
73 */
74Element2D* Element2D::nullElement = NULL;
75
76
77/**
78 * @brief standard deconstructor
79 *
80 * There are two general ways to delete an Element2D
81 * 1. delete instance;
82 *   -> result
83 *    delete this Node and all its children and children's children...
84 *    (danger if you still want the instance!!)
85 *
86 * 2. instance->remove2D(); delete instance;
87 *   -> result:
88 *    moves its children to the NullElement2D
89 *    then deletes the Element.
90 */
91Element2D::~Element2D ()
92{
93  // remove the Element2D, delete it's children (if required).
94  std::list<Element2D*>::iterator tmp = this->children.begin();
95  std::list<Element2D*>::iterator deleteNode;
96  while(!this->children.empty())
97    while (tmp != this->children.end())
98    {
99      deleteNode = tmp;
100      tmp++;
101//      printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
102      if ((this->parentMode & E2D_PROHIBIT_CHILD_DELETE) ||
103          ((*deleteNode)->parentMode & E2D_PROHIBIT_DELETE_WITH_PARENT))
104      {
105        if (this == Element2D::nullElement && (*deleteNode)->parentMode & E2D_REPARENT_TO_NULL)
106          delete (*deleteNode);
107        else
108          (*deleteNode)->reparent2D();
109      }
110      else
111        delete (*deleteNode);
112    }
113
114  if (this->parent != NULL)
115  {
116    this->parent->eraseChild2D(this);
117    this->parent = NULL;
118  }
119
120  // remove all other allocated memory.
121  if (this->toCoordinate != NULL)
122    delete this->toCoordinate;
123  if (this->toDirection != NULL)
124    delete this->toDirection;
125
126  if (this == Element2D::nullElement)
127    Element2D::nullElement = NULL;
128}
129
130
131/**
132 * @brief Loads the Parameters of an Element2D from...
133 * @param root The XML-element to load from
134 */
135void Element2D::loadParams(const TiXmlElement* root)
136{
137  BaseObject::loadParams(root);
138
139  // ELEMENT2D-native settings.
140  LoadParam(root, "alignment", this, Element2D, setAlignment)
141      .describe("loads the alignment: (either: center, left, right or screen-center)");
142
143  LoadParam(root, "layer", this, Element2D, setLayer)
144      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
145
146  LoadParam(root, "bind-node", this, Element2D, setBindNode)
147      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
148
149  LoadParam(root, "visibility", this, Element2D, setVisibility)
150      .describe("if the Element is visible or not");
151
152
153// PNode-style:
154  LoadParam(root, "rel-coor-2d", this, Element2D, setRelCoor2D)
155      .describe("Sets The relative position of the Node to its parent.");
156
157  LoadParam(root, "abs-coor-2d", this, Element2D, setAbsCoor2D)
158      .describe("Sets The absolute Position of the Node.");
159
160  LoadParam(root, "rel-dir-2d", this, Element2D, setRelDir2D)
161      .describe("Sets The relative rotation of the Node to its parent.");
162
163  LoadParam(root, "abs-dir-2d", this, Element2D, setAbsDir2D)
164      .describe("Sets The absolute rotation of the Node.");
165
166  LoadParam(root, "parent", this, Element2D, setParent2D)
167      .describe("the Name of the Parent of this Element2D");
168
169  LoadParam(root, "parent-mode", this, Element2D, setParentMode2D)
170      .describe("the mode to connect this node to its parent ()");
171
172  // cycling properties
173  LOAD_PARAM_START_CYCLE(root, element);
174  {
175    LoadParam_CYCLE(element, "child", this, Element2D, addChild2D)
176        .describe("adds a new Child to the current Node.");
177  }
178  LOAD_PARAM_END_CYCLE(element);
179}
180
181/**
182 * sets the alignment of the 2D-element in form of a String
183 * @param alignment the alignment @see loadParams
184*/
185void Element2D::setAlignment(const std::string& alignment)
186{
187  if (alignment == "center")
188    this->setAlignment(E2D_ALIGN_CENTER);
189  else if (alignment == "left")
190    this->setAlignment(E2D_ALIGN_LEFT);
191  else if (alignment == "right")
192    this->setAlignment(E2D_ALIGN_RIGHT);
193  else if (alignment == "screen-center")
194    this->setAlignment(E2D_ALIGN_SCREEN_CENTER);
195}
196
197
198/**
199 * moves a Element to another layer
200 * @param layer the Layer this is drawn on
201 */
202void Element2D::setLayer(E2D_LAYER layer)
203{
204  if (this->parent != NULL && this->parent->getLayer() > layer)
205  {
206    PRINTF(2)("Unable to set %s to layer %s, because it's parent(%s) is of higher layer %s\n",
207              this->getName(),
208              Element2D::layer2DToChar(layer),
209              this->parent->getName(),
210              Element2D::layer2DToChar(this->parent->getLayer()));
211    layer = this->parent->getLayer();
212  }
213  this->layer = layer;
214
215
216  if (this->parent != NULL)
217    this->parent->children.sort(layerSortPredicate);
218}
219
220/**
221 * sets the layer onto which this 2D-element is projected to.
222 * @param layer the layer @see loadParams @see Element2D::charToLayer2D(const std::string& layer)
223 */
224void Element2D::setLayer(const std::string& layer)
225{
226  this->setLayer(Element2D::charToLayer2D(layer));
227}
228
229void Element2D::setSizeSoft2D(float x, float y, float bias)
230{
231  if (likely(this->toSize == NULL))
232    this->toSize = new Vector2D();
233
234  *this->toSize = Vector2D(x,y);;
235  this->bias = bias;
236}
237
238
239
240/**
241 * @brief sets a node, this 2D-Element should be shown upon
242 * @param bindNode the name of the Node (should be existing)
243 */
244void Element2D::setBindNode(const std::string& bindNode)
245{
246  const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE));
247  if (tmpBindNode != NULL)
248    this->bindNode = tmpBindNode;
249}
250
251/**
252 * sets the relative coordinate of the Element2D to its parent
253 * @param relCoord the relative coordinate to the parent
254 */
255void Element2D::setRelCoor2D (const Vector2D& relCoord)
256{
257  if (this->toCoordinate!= NULL)
258  {
259    delete this->toCoordinate;
260    this->toCoordinate = NULL;
261  }
262  this->relCoordinate = relCoord;
263  this->bRelCoorChanged = true;
264}
265
266/**
267 * sets the relative coordinate of the Element2D to its Parent
268 * @param x the x coordinate
269 * @param y the y coordinate
270 */
271void Element2D::setRelCoor2D (float x, float y)
272{
273  this->setRelCoor2D(Vector2D(x,y));
274}
275
276/**
277 * sets the Relative coordinate to the parent in Pixels
278 * @param x the relCoord X
279 * @param y the relCoord Y
280 */
281void Element2D::setRelCoor2Dpx (int x, int y)
282{
283  this->setRelCoor2D(Vector2D((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
284                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY()));
285}
286
287/**
288 * sets a new relative position smoothely
289 * @param relCoordSoft the new Position to iterate to
290 * @param bias how fast to iterate to this position
291 */
292void Element2D::setRelCoorSoft2D(const Vector2D& relCoordSoft, float bias)
293{
294  if (likely(this->toCoordinate == NULL))
295    this->toCoordinate = new Vector2D();
296
297  *this->toCoordinate = relCoordSoft;
298  this->bias = bias;
299}
300
301/**
302 * sets a new relative position smoothely
303 * @param x the new x-coordinate in Pixels of the Position to iterate to
304 * @param y the new y-coordinate in Pixels of the Position to iterate to
305 * @param bias how fast to iterate to this position
306 */
307void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias)
308{
309  this->setRelCoorSoft2D(Vector2D((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
310                         (float)y/(float)GraphicsEngine::getInstance()->getResolutionY()),
311                         bias);
312}
313
314/**
315 * set relative coordinates smoothely
316 * @param x x-relative coordinates to its parent
317 * @param y y-relative coordinates to its parent
318 * @param z z-relative coordinates to its parent
319 * @see  void PNode::setRelCoorSoft (const Vector2D&, float)
320 */
321void Element2D::setRelCoorSoft2D(float x, float y, float bias)
322{
323  this->setRelCoorSoft2D(Vector2D(x, y), bias);
324}
325
326/**
327 * @param absCoord set absolute coordinate
328 */
329void Element2D::setAbsCoor2D (const Vector2D& absCoord)
330{
331  if (this->toCoordinate!= NULL)
332  {
333    delete this->toCoordinate;
334    this->toCoordinate = NULL;
335  }
336
337  if( likely(this->parentMode & E2D_PARENT_MOVEMENT))
338  {
339    /* if you have set the absolute coordinates this overrides all other changes */
340    if (likely(this->parent != NULL))
341      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
342    else
343      this->relCoordinate = absCoord;
344  }
345  if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT)
346  {
347    if (likely(this->parent != NULL))
348      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
349    else
350      this->relCoordinate = absCoord;
351  }
352
353  this->bRelCoorChanged = true;
354}
355
356/**
357 * @param x x-coordinate.
358 * @param y y-coordinate.
359 * @param z z-coordinate.
360 * @see void PNode::setAbsCoor (const Vector2D& absCoord)
361 */
362void Element2D::setAbsCoor2D (float x, float y)
363{
364  this->setAbsCoor2D(Vector2D(x, y));
365}
366
367/**
368 * @param x x-coordinate in Pixels
369 * @param y y-coordinate in Pixels
370 * @see void PNode::setAbsCoor (const Vector2D& absCoord)
371 */
372void Element2D::setAbsCoor2Dpx (int x, int y)
373{
374  this->setAbsCoor2D(Vector2D((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
375                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY()));
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 Vector2D& absCoordSoft, float bias)
383{
384  if (this->toCoordinate == NULL)
385    this->toCoordinate = new Vector2D();
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 Vector2D& absCoord)
411 */
412void Element2D::setAbsCoorSoft2D (float x, float y, float bias)
413{
414  this->setAbsCoorSoft2D(Vector2D(x, y), bias);
415}
416
417/**
418 *  shift coordinate ralative
419 * @param shift shift vector
420 *
421 * This simply adds the shift-Vector2D to the relative Coordinate
422 */
423void Element2D::shiftCoor2D (const Vector2D& 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(Vector2D((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
438                    (float)y/(float)GraphicsEngine::getInstance()->getResolutionY()));
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 * @brief 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 * @brief 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 Vector2D();
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  Vector2D 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 * @brief 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 * @brief checks if elem1 is in a deeper layer as elem2
733 * @param elem1 the first Element2D
734 * @param elem2 the second Element2D
735 * @returns true if elem1->layer < elem2->layer
736 */
737bool Element2D::layerSortPredicate(const Element2D* elem1, const Element2D* elem2)
738{
739  return elem1->layer < elem2->layer;
740}
741
742
743/**
744 * @returns the NullElement (and if needed (most probably) creates it)
745 */
746Element2D* Element2D::createNullElement()
747{
748  if (likely(Element2D::nullElement == NULL))
749  {
750    Element2D::nullElement = new Element2D(NULL, E2D_LAYER_BELOW_ALL, E2D_PARENT_MODE_DEFAULT | E2D_REPARENT_TO_NULL);
751    Element2D::nullElement->setName("NullElement");
752  }
753  return Element2D::nullElement;
754}
755
756
757/**
758 * !! PRIVATE FUNCTION
759 * @brief checks the upward integrity (e.g if Element2D is somewhere up the Node tree.)
760 * @param checkParent the Parent to check.
761 * @returns true if the integrity-check succeeds, false otherwise.
762 *
763 * If there is a second occurence of checkParent before NULL, then a loop could get
764 * into the Tree, and we do not want this.
765 */
766bool Element2D::checkIntegrity(const Element2D* checkParent) const
767{
768  const Element2D* parent = this;
769  while ( (parent = parent->getParent2D()) != NULL)
770    if (unlikely(parent == checkParent))
771      return false;
772  return true;
773}
774
775
776/**
777 * @brief updates the absCoordinate/absDirection
778 * @param dt The time passed since the last update
779
780   this is used to go through the parent-tree to update all the absolute coordinates
781   and directions. this update should be done by the engine, so you don't have to
782   worry, normaly...
783 */
784void Element2D::update2D (float dt)
785{
786  // setting the Position of this 2D-Element.
787  if( likely(this->parent != NULL))
788  {
789      // movement for nodes with smoothMove enabled
790    if (unlikely(this->toCoordinate != NULL))
791    {
792      Vector2D moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
793
794      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
795      {
796        this->shiftCoor2D(moveVect);
797      }
798      else
799      {
800        Vector2D tmp = *this->toCoordinate;
801        this->setRelCoor2D(tmp);
802        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
803      }
804    }
805    if (unlikely(this->toDirection != NULL))
806    {
807      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
808      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
809      {
810        this->shiftDir2D(rotFlot);
811      }
812      else
813      {
814        float tmp = *this->toDirection;
815        this->setRelDir2D(tmp);
816        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
817      }
818    }
819    if (unlikely(this->toSize != NULL))
820    {
821      Vector2D shiftSize = (*this->toSize - Vector2D(this->sizeX, this->sizeY)) *fabsf(dt)*bias;
822      if (likely((shiftSize).len() >= .001))//PNODE_ITERATION_DELTA))
823      {
824        this->sizeX += shiftSize.x;
825        this->sizeY += shiftSize.y;
826      }
827      else
828      {
829        delete this->toSize;
830        this->toSize = NULL;
831        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
832      }
833    }
834
835    // MAIN UPDATE /////////////////////////////////////
836    this->lastAbsCoordinate = this->absCoordinate;
837
838    PRINTF(5)("Element2D::update - %s - (%f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y);
839
840
841    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
842    {
843      /* update the current absDirection - remember * means rotation around sth.*/
844      this->prevRelDirection = this->relDirection;
845      this->absDirection = this->relDirection + parent->getAbsDir2D();;
846    }
847
848
849    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
850    {
851      this->prevRelCoordinate = this->relCoordinate;
852      this->absCoordinate.x = .5 + this->relCoordinate.x;
853      this->absCoordinate.y = .5 + this->relCoordinate.y;
854    }
855    else if (unlikely(this->bindNode != NULL))
856    {
857      GLdouble projectPos[3] = {0.0, 0.0, 0.0};
858      gluProject(this->bindNode->getAbsCoor().x,
859                 this->bindNode->getAbsCoor().y,
860                 this->bindNode->getAbsCoor().z,
861                 GraphicsEngine::modMat,
862                 GraphicsEngine::projMat,
863                 GraphicsEngine::viewPort,
864                 projectPos,
865                 projectPos+1,
866                 projectPos+2);
867//       printf("%s::%s  == %f %f %f :: %f %f\n", this->getClassName(), this->getName(),
868//              this->bindNode->getAbsCoor().x,
869//              this->bindNode->getAbsCoor().y,
870//              this->bindNode->getAbsCoor().z,
871//              projectPos[0],
872//              projectPos[1]);
873
874      this->prevRelCoordinate.x = this->absCoordinate.x = projectPos[0] /* /(float)GraphicsEngine::getInstance()->getResolutionX() */ + this->relCoordinate.x;
875      this->prevRelCoordinate.y = this->absCoordinate.y = (float)GraphicsEngine::getInstance()->getResolutionY() -  projectPos[1] + this->relCoordinate.y;
876      this->bRelCoorChanged = true;
877    }
878    else
879    {
880      if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged))
881      {
882        /* update the current absCoordinate */
883        this->prevRelCoordinate = this->relCoordinate;
884        this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
885      }
886      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
887      {
888        /* update the current absCoordinate */
889        this->prevRelCoordinate = this->relCoordinate;
890        float sine = sin(this->parent->getAbsDir2D());
891        float cose = cos(this->parent->getAbsDir2D());
892//        this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine;
893//        this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine;
894
895        this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
896        this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
897
898      }
899    }
900    /////////////////////////////////////////////////
901  }
902  else
903  {
904    PRINTF(5)("Element2D::update - (%f, %f)\n", this->absCoordinate.x, this->absCoordinate.y);
905    if (this->bRelCoorChanged)
906    {
907      this->prevRelCoordinate = this->relCoordinate;
908      this->absCoordinate = this->relCoordinate;
909    }
910    if (this->bRelDirChanged)
911    {
912      this->prevRelDirection = this->relDirection;
913      this->absDirection = this->getAbsDir2D() + this->relDirection;
914    }
915  }
916
917
918  // UPDATE CHILDREN
919  if(!this->children.empty() || this->parentMode & E2D_UPDATE_CHILDREN_IF_INACTIVE)
920  {
921    list<Element2D*>::iterator child;
922    for (child = this->children.begin(); child != this->children.end(); child++)
923    {
924      /* if this node has changed, make sure, that all children are updated also */
925      if( likely(this->bRelCoorChanged))
926        (*child)->parentCoorChanged2D();
927      if( likely(this->bRelDirChanged))
928        (*child)->parentDirChanged2D();
929
930      (*child)->update2D(dt);
931    }
932  }
933
934  // FINISHING PROCESS
935  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
936  this->bRelCoorChanged = false;
937  this->bRelDirChanged = false;
938}
939
940
941/**
942 *  displays some information about this pNode
943 * @param depth The deph into which to debug the children of this Element2D to.
944 * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...)
945 * @param level The n-th level of the Node we draw (this is internal and only for nice output)
946 */
947void Element2D::debug2D (unsigned int depth, unsigned int level) const
948{
949  for (unsigned int i = 0; i < level; i++)
950    PRINT(0)(" |");
951  if (this->children.size() > 0)
952    PRINT(0)(" +");
953  else
954    PRINT(0)(" -");
955  PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s, layer:%s\n",
956            this->getClassName(),
957            this->getName(),
958            this->absCoordinate.x,
959            this->absCoordinate.y,
960            this->relCoordinate.x,
961            this->relCoordinate.y,
962            this->getAbsDir2D(),
963            Element2D::parentingModeToString2D(parentMode),
964            Element2D::layer2DToChar(this->layer));
965
966  if (depth >= 2 || depth == 0)
967  {
968    list<Element2D*>::const_iterator child;
969    for (child = this->children.begin(); child != this->children.end(); child++)
970    {
971      if (depth == 0)
972        (*child)->debug2D(0, level + 1);
973      else
974        (*child)->debug2D(depth - 1, level +1);
975    }
976  }
977}
978
979/**
980 * ticks the 2d-Element
981 * @param dt the time elapsed since the last tick
982 *
983 * the element only gets tickt, if it is active.
984 * Be aware, that this walks through the entire Element2D-tree,
985 * searching for Elements to be ticked.
986 */
987void Element2D::tick2D(float dt)
988{
989  if (this->bActive)
990    this->tick(dt);
991  if (this->children.size() > 0)
992  {
993    list<Element2D*>::iterator child;
994    for (child = this->children.begin(); child != this->children.end(); child++)
995      (*child)->tick2D(dt);
996  }
997}
998
999/**
1000 * draws all the Elements from this element2D downwards
1001 * @param layer the maximal Layer to draw. @see E2D_LAYER
1002 */
1003void Element2D::draw2D(short layer) const
1004{
1005  if (this->bVisible)
1006    this->draw();
1007  if (this->children.size() > 0)
1008  {
1009    list<Element2D*>::const_iterator child;
1010    for (child = this->children.begin(); child != this->children.end(); child++)
1011      if (likely(layer >= this->layer))
1012        (*child)->draw2D(layer);
1013  }
1014}
1015
1016/**
1017 * @brief displays the Element2D at its position with its rotation as a Plane.
1018 */
1019void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
1020{
1021  if (level == 0)
1022  {
1023    glPushAttrib(GL_ENABLE_BIT);
1024    glMatrixMode(GL_MODELVIEW);
1025
1026    glDisable(GL_LIGHTING);
1027    glDisable(GL_BLEND);
1028    glDisable(GL_TEXTURE_2D);
1029  }
1030
1031  glPushMatrix();
1032  /* translate */
1033  /* rotate */
1034  glColor3f(color.x, color.y, color.z);
1035
1036  glTranslatef (this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
1037  glRotatef(this->getAbsDir2D(), 0,0,1);
1038  glBegin(GL_LINE_LOOP);
1039  glVertex2f(0, 0);
1040  glVertex2f(0, +this->getSizeY2D());
1041  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
1042  glVertex2f(+this->getSizeX2D(), 0);
1043  glEnd();
1044
1045
1046  glPopMatrix();
1047  if (depth >= 2 || depth == 0)
1048  {
1049    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
1050    list<Element2D*>::const_iterator child;
1051    for (child = this->children.begin(); child != this->children.end(); child++)
1052    {
1053      // drawing the Dependency graph
1054      if (this != Element2D::getNullElement())
1055      {
1056        glBegin(GL_LINES);
1057        glColor3f(color.x, color.y, color.z);
1058        glVertex3f(this->getAbsCoor2D ().x,
1059                   this->getAbsCoor2D ().y,
1060                   0);
1061        glColor3f(childColor.x, childColor.y, childColor.z);
1062        glVertex3f((*child)->getAbsCoor2D ().x,
1063                   (*child)->getAbsCoor2D ().y,
1064                   0);
1065        glEnd();
1066      }
1067      if (depth == 0)
1068        (*child)->debugDraw2D(0, size, childColor, level+1);
1069      else
1070        (*child)->debugDraw2D(depth - 1, size, childColor, level +1);
1071    }
1072  }
1073  if (level == 0)
1074    glPopAttrib();
1075
1076}
1077
1078
1079// helper functions //
1080/**
1081 * @brief converts a parentingMode into a string that is the name of it
1082 * @param parentingMode the ParentingMode to convert
1083 * @return the converted string
1084 */
1085const char* Element2D::parentingModeToString2D(int parentingMode)
1086{
1087  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
1088    return "local-rotate";
1089  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
1090    return "rotate-movement";
1091  else if (parentingMode == E2D_PARENT_MOVEMENT)
1092    return "movement";
1093  else if (parentingMode == E2D_PARENT_ALL)
1094    return "all";
1095  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
1096    return "rotate-and-move";
1097}
1098
1099/**
1100 * @brief converts a parenting-mode-string into a int
1101 * @param parentingMode the string naming the parentingMode
1102 * @return the int corresponding to the named parentingMode
1103 */
1104E2D_PARENT_MODE Element2D::stringToParentingMode2D(const std::string& parentingMode)
1105{
1106  if (parentingMode == "local-rotate")
1107    return (E2D_PARENT_LOCAL_ROTATE);
1108  else  if (parentingMode == "rotate-movement")
1109    return (E2D_PARENT_ROTATE_MOVEMENT);
1110  else  if (parentingMode == "movement")
1111    return (E2D_PARENT_MOVEMENT);
1112  else  if (parentingMode == "all")
1113    return (E2D_PARENT_ALL);
1114  else  if (parentingMode == "rotate-and-move")
1115    return (E2D_PARENT_ROTATE_AND_MOVE);
1116}
1117
1118/**
1119 * @brief converts a layer into its corresponding string
1120 * @param layer the layer to get the name-String of.
1121 * @returns the Name of the Layer (on error the default-layer-string is returned)
1122 */
1123const char* Element2D::layer2DToChar(E2D_LAYER layer)
1124{
1125  switch(layer)
1126  {
1127    case E2D_LAYER_ABOVE_ALL:
1128      return "above-all";
1129    case E2D_LAYER_TOP:
1130      return "top";
1131    case E2D_LAYER_MEDIUM:
1132      return "medium";
1133    case E2D_LAYER_BOTTOM:
1134      return "bottom";
1135    case E2D_LAYER_BELOW_ALL:
1136      return "below-all";
1137    default:
1138      assert (false);
1139      return layer2DToChar(E2D_DEFAULT_LAYER);
1140  }
1141}
1142
1143/**
1144 * @brief converts a String holding a actual Layer
1145 * @param layer the String to convert into a Layer2D
1146 * @returns the E2D_LAYER on success, E2D_DEFAULT_LAYER on error.
1147 */
1148E2D_LAYER Element2D::charToLayer2D(const std::string& layer)
1149{
1150  if (layer == "above-all")
1151    return (E2D_LAYER_ABOVE_ALL);
1152  if (layer == "top")
1153    return (E2D_LAYER_TOP);
1154  else  if (layer == "medium")
1155    return (E2D_LAYER_MEDIUM);
1156  else  if (layer == "bottom")
1157    return (E2D_LAYER_BOTTOM);
1158  else  if (layer == "below-all")
1159    return (E2D_LAYER_BELOW_ALL);
1160  else
1161    return (E2D_DEFAULT_LAYER);
1162}
Note: See TracBrowser for help on using the repository browser.