Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7644 was 7342, checked in by bensch, 19 years ago

orxonox/trunk: ShellBuffer and ShellInput had the segfault-capability for years, now they have not anymore :)

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