Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Layer Sorting works as expected

File size: 33.8 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"
32SHELL_COMMAND(debug, Element2D, debug2DSC);
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    // Setting the New Parent.
536    child->parent = this;
537    if (likely(this != NULL))
538    {
539      // Layers of Children that are smaller than this(parents) Layer will be updated, and pushed to the front.
540      if (unlikely(this->layer > child->getLayer()))
541      {
542        PRINTF(2)("Layer '%s' of Child(%s::%s) lower than parents(%s::%s) layer '%s'. updating...\n",
543                  Element2D::layer2DToChar(child->getLayer()),child->getClassName(), child->getName(),
544                  this->getClassName(), this->getName(), Element2D::layer2DToChar(this->layer));
545        child->layer = this->layer;
546        this->children.push_front(child);
547      }
548      else
549      {
550        // Inserting the Element at the right Layer depth.
551        list<Element2D*>::iterator elem;
552        for (elem = this->children.begin(); elem != this->children.end(); elem++)
553        {
554          if ((*elem)->layer <= child->layer)
555          {
556            this->children.insert(elem, child);
557            break;
558          }
559        }
560        // if we are at the Last child push it back.
561        if (elem == this->children.end())
562          this->children.push_back(child);
563      }
564    }
565    else
566    {
567      PRINTF(1)("Tried to reparent2D to own child '%s::%s' to '%s::%s'.\n",
568                this->getClassName(), this->getName(), child->getClassName(), child->getName());
569      child->parent = NULL;
570    }
571  }
572  child->parentCoorChanged2D();
573}
574
575/**
576 * @see Element2D::addChild(Element2D* child);
577 * @param childName the name of the child to add to this PNode
578 */
579void Element2D::addChild2D (const std::string& childName)
580{
581  Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D));
582  if (childNode != NULL)
583    this->addChild2D(childNode);
584}
585
586/**
587 * @brief removes a child from the node
588 * @param child the child to remove from this Node..
589 *
590 * Children from nodes will not be lost, they are referenced to NullPointer
591 */
592void Element2D::removeChild2D (Element2D* child)
593{
594  if (child != NULL)
595    child->remove2D();
596}
597
598/**
599 * !! PRIVATE FUNCTION
600 * @brief reparents an Element2D (happens on Parents Node delete or remove and Flags are set.)
601 */
602void Element2D::reparent2D()
603{
604  if (this->parentMode & E2D_REPARENT_TO_NULL)
605    this->setParent2D((Element2D*)NULL);
606  else if (this->parentMode & E2D_REPARENT_TO_PARENTS_PARENT && this->parent != NULL)
607    this->setParent2D(this->parent->getParent2D());
608  else
609    this->setParent2D(Element2D::getNullElement());
610}
611
612
613/**
614 * @param child the child to be erased from this Nodes List
615 */
616void Element2D::eraseChild2D(Element2D* child)
617{
618  assert (this != NULL && child != NULL);
619  std::list<Element2D*>::iterator childIT = std::find(this->children.begin(), this->children.end(), child);
620  this->children.erase(childIT);
621}
622
623
624
625/**
626 * remove this Element from the tree and adds all children to NullElement2D
627 *
628 * afterwards this Node is free, and can be reattached, or deleted freely.
629 */
630void Element2D::remove2D()
631{
632  list<Element2D*>::iterator child = this->children.begin();
633  list<Element2D*>::iterator reparenter;
634  while (child != this->children.end())
635  {
636    reparenter = child;
637    child++;
638    if (this->parentMode & E2D_REPARENT_CHILDREN_ON_REMOVE ||
639        (*reparenter)->parentMode & E2D_REPARENT_ON_PARENTS_REMOVE)
640    {
641      printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName());
642      (*reparenter)->reparent2D();
643      printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent2D()->getClassName(),(*reparenter)->getParent2D()->getName());
644    }
645  }
646  if (this->parent != NULL)
647  {
648    this->parent->eraseChild2D(this);
649    this->parent = NULL;
650  }
651}
652
653
654/**
655 * @see Element2D::setParent(Element2D* parent);
656 * @param parentName the name of the Parent to set to this Element2D
657 */
658void Element2D::setParent2D (const std::string& parentName)
659{
660  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
661  if (parentNode != NULL)
662    parentNode->addChild2D(this);
663  else
664    PRINTF(2)("Not Found Element2D's (%s::%s) new Parent by Name: %s\n",
665              this->getClassName(), this->getName(), parentName.c_str());
666}
667
668/**
669 * does the reparenting in a very smooth way
670 * @param parentNode the new Node to connect this node to.
671 * @param bias the speed to iterate to this new Positions
672 */
673void Element2D::setParentSoft2D(Element2D* parentNode, float bias)
674{
675  if (this->parent == parentNode)
676    return;
677
678  if (likely(this->toCoordinate == NULL))
679  {
680    this->toCoordinate = new Vector2D();
681    *this->toCoordinate = this->getRelCoor2D();
682  }
683  if (likely(this->toDirection == NULL))
684  {
685    this->toDirection = new float;
686    *this->toDirection = this->getRelDir2D();
687  }
688  this->bias = bias;
689
690
691  Vector2D tmpV = this->getAbsCoor2D();
692  float tmpQ = this->getAbsDir2D();
693
694  parentNode->addChild2D(this);
695
696  if (this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) //! @todo implement this.
697    ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()));
698  else
699    this->relCoordinate = (tmpV - parentNode->getAbsCoor2D());
700  this->bRelCoorChanged = true;
701
702  this->relDirection = (tmpQ - parentNode->getAbsDir2D());
703  this->bRelDirChanged = true;
704}
705
706/**
707 * @brief does the reparenting in a very smooth way
708 * @param parentName the name of the Parent to reconnect to
709 * @param bias the speed to iterate to this new Positions
710 */
711void Element2D::setParentSoft2D(const std::string& parentName, float bias)
712{
713  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
714  if (parentNode != NULL)
715    this->setParentSoft2D(parentNode, bias);
716}
717
718/**
719 * @param parentMode sets the parentingMode of this Node
720 */
721void Element2D::setParentMode2D(E2D_PARENT_MODE parentMode)
722{
723  this->parentMode = ((this->parentMode & 0xfff0) | parentMode);
724}
725
726
727/**
728 * @brief sets the mode of this parent manually
729 * @param parentMode a String representing this parentingMode
730 */
731void Element2D::setParentMode2D (const std::string& parentingMode)
732{
733  this->setParentMode2D(Element2D::stringToParentingMode2D(parentingMode));
734}
735
736/**
737 * @brief checks if elem1 is in a deeper layer as elem2
738 * @param elem1 the first Element2D
739 * @param elem2 the second Element2D
740 * @returns true if elem1->layer < elem2->layer
741 */
742bool Element2D::layerSortPredicate(const Element2D* elem1, const Element2D* elem2)
743{
744  return elem1->layer < elem2->layer;
745}
746
747
748/**
749 * @returns the NullElement (and if needed (most probably) creates it)
750 */
751Element2D* Element2D::createNullElement()
752{
753  if (likely(Element2D::nullElement == NULL))
754  {
755    Element2D::nullElement = new Element2D(NULL, E2D_LAYER_BELOW_ALL, E2D_PARENT_MODE_DEFAULT | E2D_REPARENT_TO_NULL);
756    Element2D::nullElement->setName("NullElement");
757  }
758  return Element2D::nullElement;
759}
760
761
762/**
763 * !! PRIVATE FUNCTION
764 * @brief checks the upward integrity (e.g if Element2D is somewhere up the Node tree.)
765 * @param checkParent the Parent to check.
766 * @returns true if the integrity-check succeeds, false otherwise.
767 *
768 * If there is a second occurence of checkParent before NULL, then a loop could get
769 * into the Tree, and we do not want this.
770 */
771bool Element2D::checkIntegrity(const Element2D* checkParent) const
772{
773  const Element2D* parent = this;
774  while ( (parent = parent->getParent2D()) != NULL)
775    if (unlikely(parent == checkParent))
776      return false;
777  return true;
778}
779
780
781/**
782 * @brief updates the absCoordinate/absDirection
783 * @param dt The time passed since the last update
784
785   this is used to go through the parent-tree to update all the absolute coordinates
786   and directions. this update should be done by the engine, so you don't have to
787   worry, normaly...
788 */
789void Element2D::update2D (float dt)
790{
791  // setting the Position of this 2D-Element.
792  if( likely(this->parent != NULL))
793  {
794    // movement for nodes with smoothMove enabled
795    if (unlikely(this->toCoordinate != NULL))
796    {
797      Vector2D moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
798
799      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
800      {
801        this->shiftCoor2D(moveVect);
802      }
803      else
804      {
805        Vector2D tmp = *this->toCoordinate;
806        this->setRelCoor2D(tmp);
807        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
808      }
809    }
810    if (unlikely(this->toDirection != NULL))
811    {
812      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
813      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
814      {
815        this->shiftDir2D(rotFlot);
816      }
817      else
818      {
819        float tmp = *this->toDirection;
820        this->setRelDir2D(tmp);
821        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
822      }
823    }
824    if (unlikely(this->toSize != NULL))
825    {
826      Vector2D shiftSize = (*this->toSize - Vector2D(this->sizeX, this->sizeY)) *fabsf(dt)*bias;
827      if (likely((shiftSize).len() >= .001))//PNODE_ITERATION_DELTA))
828      {
829        this->sizeX += shiftSize.x;
830        this->sizeY += shiftSize.y;
831      }
832      else
833      {
834        delete this->toSize;
835        this->toSize = NULL;
836        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
837      }
838    }
839
840    // MAIN UPDATE /////////////////////////////////////
841    this->lastAbsCoordinate = this->absCoordinate;
842
843    PRINTF(5)("Element2D::update - %s - (%f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y);
844
845
846    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
847    {
848      /* update the current absDirection - remember * means rotation around sth.*/
849      this->prevRelDirection = this->relDirection;
850      this->absDirection = this->relDirection + parent->getAbsDir2D();;
851    }
852
853
854    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
855    {
856      this->prevRelCoordinate = this->relCoordinate;
857      this->absCoordinate.x = .5 + this->relCoordinate.x;
858      this->absCoordinate.y = .5 + this->relCoordinate.y;
859    }
860    else if (unlikely(this->bindNode != NULL))
861    {
862      GLdouble projectPos[3] = {0.0, 0.0, 0.0};
863      gluProject(this->bindNode->getAbsCoor().x,
864                 this->bindNode->getAbsCoor().y,
865                 this->bindNode->getAbsCoor().z,
866                 GraphicsEngine::modMat,
867                 GraphicsEngine::projMat,
868                 GraphicsEngine::viewPort,
869                 projectPos,
870                 projectPos+1,
871                 projectPos+2);
872      //       printf("%s::%s  == %f %f %f :: %f %f\n", this->getClassName(), this->getName(),
873      //              this->bindNode->getAbsCoor().x,
874      //              this->bindNode->getAbsCoor().y,
875      //              this->bindNode->getAbsCoor().z,
876      //              projectPos[0],
877      //              projectPos[1]);
878
879      this->prevRelCoordinate.x = this->absCoordinate.x = projectPos[0] /* /(float)GraphicsEngine::getInstance()->getResolutionX() */ + this->relCoordinate.x;
880      this->prevRelCoordinate.y = this->absCoordinate.y = (float)GraphicsEngine::getInstance()->getResolutionY() -  projectPos[1] + this->relCoordinate.y;
881      this->bRelCoorChanged = true;
882    }
883    else
884    {
885      if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged))
886      {
887        /* update the current absCoordinate */
888        this->prevRelCoordinate = this->relCoordinate;
889        this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
890      }
891      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
892      {
893        /* update the current absCoordinate */
894        this->prevRelCoordinate = this->relCoordinate;
895        float sine = sin(this->parent->getAbsDir2D());
896        float cose = cos(this->parent->getAbsDir2D());
897        //        this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine;
898        //        this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine;
899
900        this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
901        this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
902
903      }
904    }
905    /////////////////////////////////////////////////
906  }
907  else
908  {
909    PRINTF(5)("Element2D::update - (%f, %f)\n", this->absCoordinate.x, this->absCoordinate.y);
910    if (this->bRelCoorChanged)
911    {
912      this->prevRelCoordinate = this->relCoordinate;
913      this->absCoordinate = this->relCoordinate;
914    }
915    if (this->bRelDirChanged)
916    {
917      this->prevRelDirection = this->relDirection;
918      this->absDirection = this->getAbsDir2D() + this->relDirection;
919    }
920  }
921
922
923  // UPDATE CHILDREN
924  if(!this->children.empty() || this->parentMode & E2D_UPDATE_CHILDREN_IF_INACTIVE)
925  {
926    list<Element2D*>::iterator child;
927    for (child = this->children.begin(); child != this->children.end(); child++)
928    {
929      /* if this node has changed, make sure, that all children are updated also */
930      if( likely(this->bRelCoorChanged))
931        (*child)->parentCoorChanged2D();
932      if( likely(this->bRelDirChanged))
933        (*child)->parentDirChanged2D();
934
935      (*child)->update2D(dt);
936    }
937  }
938
939  // FINISHING PROCESS
940  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
941  this->bRelCoorChanged = false;
942  this->bRelDirChanged = false;
943}
944
945
946/**
947 *  displays some information about this pNode
948 * @param depth The deph into which to debug the children of this Element2D to.
949 * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...)
950 * @param level The n-th level of the Node we draw (this is internal and only for nice output)
951 */
952void Element2D::debug2D (unsigned int depth, unsigned int level) const
953{
954  for (unsigned int i = 0; i < level; i++)
955    PRINT(0)(" |");
956  if (this->children.size() > 0)
957    PRINT(0)(" +");
958  else
959    PRINT(0)(" -");
960  PRINT(0)("E2D(%s::%s);AC:(%0.2f, %0.2f);RC:(%0.2f, %0.2f);AD(%0.2f)->%s;Layer:(%s)\n",
961           this->getClassName(),
962           this->getName(),
963           this->absCoordinate.x,
964           this->absCoordinate.y,
965           this->relCoordinate.x,
966           this->relCoordinate.y,
967           this->getAbsDir2D(),
968           Element2D::parentingModeToString2D(parentMode),
969           Element2D::layer2DToChar(this->layer));
970
971  if (depth >= 2 || depth == 0)
972  {
973    list<Element2D*>::const_iterator child;
974    for (child = this->children.begin(); child != this->children.end(); child++)
975    {
976      if (depth == 0)
977        (*child)->debug2D(0, level + 1);
978      else
979        (*child)->debug2D(depth - 1, level +1);
980    }
981  }
982}
983
984/**
985 * ticks the 2d-Element
986 * @param dt the time elapsed since the last tick
987 *
988 * the element only gets tickt, if it is active.
989 * Be aware, that this walks through the entire Element2D-tree,
990 * searching for Elements to be ticked.
991 */
992void Element2D::tick2D(float dt)
993{
994  if (this->bActive)
995    this->tick(dt);
996  if (this->children.size() > 0)
997  {
998    list<Element2D*>::iterator child;
999    for (child = this->children.begin(); child != this->children.end(); child++)
1000      (*child)->tick2D(dt);
1001  }
1002}
1003
1004/**
1005 * draws all the Elements from this element2D downwards
1006 * @param layer the maximal Layer to draw. @see E2D_LAYER
1007 */
1008void Element2D::draw2D(short layer) const
1009{
1010  if (this->bVisible)
1011    this->draw();
1012  if (this->children.size() > 0)
1013  {
1014    list<Element2D*>::const_iterator child;
1015    for (child = this->children.begin(); child != this->children.end(); child++)
1016      if (likely(layer >= this->layer))
1017        (*child)->draw2D(layer);
1018  }
1019}
1020
1021/**
1022 * @brief displays the Element2D at its position with its rotation as a Plane.
1023 */
1024void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
1025{
1026  if (level == 0)
1027  {
1028    glPushAttrib(GL_ENABLE_BIT);
1029    glMatrixMode(GL_MODELVIEW);
1030
1031    glDisable(GL_LIGHTING);
1032    glDisable(GL_BLEND);
1033    glDisable(GL_TEXTURE_2D);
1034  }
1035
1036  glPushMatrix();
1037  /* translate */
1038  /* rotate */
1039  glColor3f(color.x, color.y, color.z);
1040
1041  glTranslatef (this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
1042  glRotatef(this->getAbsDir2D(), 0,0,1);
1043  glBegin(GL_LINE_LOOP);
1044  glVertex2f(0, 0);
1045  glVertex2f(0, +this->getSizeY2D());
1046  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
1047  glVertex2f(+this->getSizeX2D(), 0);
1048  glEnd();
1049
1050
1051  glPopMatrix();
1052  if (depth >= 2 || depth == 0)
1053  {
1054    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
1055    list<Element2D*>::const_iterator child;
1056    for (child = this->children.begin(); child != this->children.end(); child++)
1057    {
1058      // drawing the Dependency graph
1059      if (this != Element2D::getNullElement())
1060      {
1061        glBegin(GL_LINES);
1062        glColor3f(color.x, color.y, color.z);
1063        glVertex3f(this->getAbsCoor2D ().x,
1064                   this->getAbsCoor2D ().y,
1065                   0);
1066        glColor3f(childColor.x, childColor.y, childColor.z);
1067        glVertex3f((*child)->getAbsCoor2D ().x,
1068                   (*child)->getAbsCoor2D ().y,
1069                   0);
1070        glEnd();
1071      }
1072      if (depth == 0)
1073        (*child)->debugDraw2D(0, size, childColor, level+1);
1074      else
1075        (*child)->debugDraw2D(depth - 1, size, childColor, level +1);
1076    }
1077  }
1078  if (level == 0)
1079    glPopAttrib();
1080
1081}
1082
1083
1084// helper functions //
1085/**
1086 * @brief converts a parentingMode into a string that is the name of it
1087 * @param parentingMode the ParentingMode to convert
1088 * @return the converted string
1089 */
1090const char* Element2D::parentingModeToString2D(int parentingMode)
1091{
1092  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
1093    return "local-rotate";
1094  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
1095    return "rotate-movement";
1096  else if (parentingMode == E2D_PARENT_MOVEMENT)
1097    return "movement";
1098  else if (parentingMode == E2D_PARENT_ALL)
1099    return "all";
1100  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
1101    return "rotate-and-move";
1102}
1103
1104/**
1105 * @brief converts a parenting-mode-string into a int
1106 * @param parentingMode the string naming the parentingMode
1107 * @return the int corresponding to the named parentingMode
1108 */
1109E2D_PARENT_MODE Element2D::stringToParentingMode2D(const std::string& parentingMode)
1110{
1111  if (parentingMode == "local-rotate")
1112    return (E2D_PARENT_LOCAL_ROTATE);
1113  else  if (parentingMode == "rotate-movement")
1114    return (E2D_PARENT_ROTATE_MOVEMENT);
1115  else  if (parentingMode == "movement")
1116    return (E2D_PARENT_MOVEMENT);
1117  else  if (parentingMode == "all")
1118    return (E2D_PARENT_ALL);
1119  else  if (parentingMode == "rotate-and-move")
1120    return (E2D_PARENT_ROTATE_AND_MOVE);
1121}
1122
1123/**
1124 * @brief converts a layer into its corresponding string
1125 * @param layer the layer to get the name-String of.
1126 * @returns the Name of the Layer (on error the default-layer-string is returned)
1127 */
1128const char* Element2D::layer2DToChar(E2D_LAYER layer)
1129{
1130  switch(layer)
1131  {
1132    case E2D_LAYER_ABOVE_ALL:
1133      return "above-all";
1134    case E2D_LAYER_TOP:
1135      return "top";
1136    case E2D_LAYER_MEDIUM:
1137      return "medium";
1138    case E2D_LAYER_BOTTOM:
1139      return "bottom";
1140    case E2D_LAYER_BELOW_ALL:
1141      return "below-all";
1142    default:
1143      assert (false);
1144      return layer2DToChar(E2D_DEFAULT_LAYER);
1145  }
1146}
1147
1148/**
1149 * @brief converts a String holding a actual Layer
1150 * @param layer the String to convert into a Layer2D
1151 * @returns the E2D_LAYER on success, E2D_DEFAULT_LAYER on error.
1152 */
1153E2D_LAYER Element2D::charToLayer2D(const std::string& layer)
1154{
1155  if (layer == "above-all")
1156    return (E2D_LAYER_ABOVE_ALL);
1157  if (layer == "top")
1158    return (E2D_LAYER_TOP);
1159  else  if (layer == "medium")
1160    return (E2D_LAYER_MEDIUM);
1161  else  if (layer == "bottom")
1162    return (E2D_LAYER_BOTTOM);
1163  else  if (layer == "below-all")
1164    return (E2D_LAYER_BELOW_ALL);
1165  else
1166    return (E2D_DEFAULT_LAYER);
1167}
Note: See TracBrowser for help on using the repository browser.