Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7318 was 7316, checked in by bensch, 20 years ago

orxonox/trunk: Element2D uses Vector2D instead of Vector whenever possible

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