Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/render2D/element_2d.cc @ 7895

Last change on this file since 7895 was 7727, checked in by bensch, 19 years ago

trunk: some GL-properties for the Textures

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