Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

hmm… does not help

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