Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/render2D/element_2d.cc @ 9715

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

renamed newclassid to classid and newobjectlist to objectlist

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