Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: some GL-properties for the Textures

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