Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7479 was 7342, checked in by bensch, 19 years ago

orxonox/trunk: ShellBuffer and ShellInput had the segfault-capability for years, now they have not anymore :)

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