Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7900 was 7871, checked in by bensch, 19 years ago

orxonox/trunk: Element2D is better updated (after PNode :)

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