Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Sorting in ShellCommand

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