Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more performant Element2D'drawer

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