Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7278 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

File size: 33.2 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[4838]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[4839]18#include "element_2d.h"
[4840]19#include "render_2d.h"
[1853]20
[6142]21#include <algorithm>
22
[5775]23#include "p_node.h"
24
[4843]25#include "graphics_engine.h"
[7193]26#include "util/loading/load_param.h"
[4858]27#include "class_list.h"
[5775]28
[5285]29#include "color.h"
[4843]30
[1856]31using namespace std;
[1853]32
[5285]33/**
[6295]34 * @brief standard constructor
[5285]35 * @param parent the parent to set for this Element2D
36 *
37 * NullElement2D needs this constructor with parameter NULL to initialize
38 * itself. Otherwise it would result in an endless Loop.
39 */
[6299]40Element2D::Element2D (Element2D* parent, E2D_LAYER layer, short nodeFlags)
[3365]41{
[6299]42  this->setClassID(CL_ELEMENT_2D, "Element2D");
43
44  this->setVisibility(true);
45  this->activate2D();
46  this->setAlignment(E2D_ALIGN_NONE);
47  this->bindNode = NULL;
48
49  this->parentMode = nodeFlags;
50  this->parent = NULL;
51  this->absDirection = 0.0;
52  this->relDirection = 0.0;
53  this->bRelCoorChanged = true;
54  this->bRelDirChanged = true;
55  this->toCoordinate = NULL;
56  this->toDirection = NULL;
[7031]57  this->toSize = NULL;
[6299]58  this->setSize2D(1, 1);
59
60
[5402]61  this->layer = layer;
[6299]62  if (parent != NULL)
63    parent->addChild2D(this);
[3365]64}
[1853]65
[6299]66
[3245]67/**
[6299]68 * @brief the mighty NullElement
69 * TopMost Node of them all.
70 */
71Element2D* Element2D::nullElement = NULL;
72
73
74/**
[6295]75 * @brief standard deconstructor
[5285]76 *
77 * There are two general ways to delete an Element2D
78 * 1. delete instance;
79 *   -> result
80 *    delete this Node and all its children and children's children...
81 *    (danger if you still want the instance!!)
82 *
83 * 2. instance->remove2D(); delete instance;
84 *   -> result:
[5401]85 *    moves its children to the NullElement2D
[5285]86 *    then deletes the Element.
87 */
[4838]88Element2D::~Element2D ()
[3543]89{
[6299]90  // remove the Element2D, delete it's children (if required).
91  std::list<Element2D*>::iterator tmp = this->children.begin();
92  std::list<Element2D*>::iterator deleteNode;
93  while(!this->children.empty())
94    while (tmp != this->children.end())
95    {
96      deleteNode = tmp;
97      tmp++;
98//      printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName());
99      if ((this->parentMode & E2D_PROHIBIT_CHILD_DELETE) ||
100          ((*deleteNode)->parentMode & E2D_PROHIBIT_DELETE_WITH_PARENT))
101      {
102        if (this == Element2D::nullElement && (*deleteNode)->parentMode & E2D_REPARENT_TO_NULL)
103          delete (*deleteNode);
104        else
105          (*deleteNode)->reparent2D();
106      }
107      else
108        delete (*deleteNode);
109    }
110
[5285]111  if (this->parent != NULL)
112  {
[6299]113    this->parent->eraseChild2D(this);
[5285]114    this->parent = NULL;
115  }
[5089]116
[5285]117  // remove all other allocated memory.
[5088]118  if (this->toCoordinate != NULL)
119    delete this->toCoordinate;
120  if (this->toDirection != NULL)
121    delete this->toDirection;
[6299]122
123  if (this == Element2D::nullElement)
124    Element2D::nullElement = NULL;
[3543]125}
[4843]126
127
[4858]128/**
[6295]129 * @brief Loads the Parameters of an Element2D from...
[4858]130 * @param root The XML-element to load from
131 */
132void Element2D::loadParams(const TiXmlElement* root)
133{
[6512]134  BaseObject::loadParams(root);
135
[5402]136  // ELEMENT2D-native settings.
[5671]137  LoadParam(root, "alignment", this, Element2D, setAlignment)
[4858]138      .describe("loads the alignment: (either: center, left, right or screen-center)");
139
[5671]140  LoadParam(root, "layer", this, Element2D, setLayer)
[4858]141      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
142
[5671]143  LoadParam(root, "bind-node", this, Element2D, setBindNode)
[7199]144      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
[4858]145
[5671]146  LoadParam(root, "visibility", this, Element2D, setVisibility)
[4860]147      .describe("if the Element is visible or not");
[5089]148
149
[5402]150// PNode-style:
[6878]151  LoadParam(root, "rel-coor-2d", this, Element2D, setRelCoor2D)
[5091]152      .describe("Sets The relative position of the Node to its parent.");
153
[6878]154  LoadParam(root, "abs-coor-2d", this, Element2D, setAbsCoor2D)
[5091]155      .describe("Sets The absolute Position of the Node.");
156
[6878]157  LoadParam(root, "rel-dir-2d", this, Element2D, setRelDir2D)
[5091]158      .describe("Sets The relative rotation of the Node to its parent.");
159
[6878]160  LoadParam(root, "abs-dir-2d", this, Element2D, setAbsDir2D)
[5091]161      .describe("Sets The absolute rotation of the Node.");
162
[5671]163  LoadParam(root, "parent", this, Element2D, setParent2D)
[5091]164      .describe("the Name of the Parent of this Element2D");
165
[5671]166  LoadParam(root, "parent-mode", this, Element2D, setParentMode2D)
[5091]167      .describe("the mode to connect this node to its parent ()");
168
169  // cycling properties
[6295]170  LOAD_PARAM_START_CYCLE(root, element);
[5091]171  {
[6299]172    LoadParam_CYCLE(element, "child", this, Element2D, addChild2D)
[6295]173        .describe("adds a new Child to the current Node.");
[5091]174  }
[6295]175  LOAD_PARAM_END_CYCLE(element);
[4858]176}
177
178/**
179 * sets the alignment of the 2D-element in form of a String
180 * @param alignment the alignment @see loadParams
181*/
[7221]182void Element2D::setAlignment(const std::string& alignment)
[4858]183{
[7221]184  if (alignment == "center")
[4858]185    this->setAlignment(E2D_ALIGN_CENTER);
[7221]186  else if (alignment == "left")
[4858]187    this->setAlignment(E2D_ALIGN_LEFT);
[7221]188  else if (alignment == "right")
[4858]189    this->setAlignment(E2D_ALIGN_RIGHT);
[7221]190  else if (alignment == "screen-center")
[4858]191    this->setAlignment(E2D_ALIGN_SCREEN_CENTER);
192}
193
[4862]194
[4858]195/**
[4862]196 * moves a Element to another layer
197 * @param layer the Layer this is drawn on
198 */
199void Element2D::setLayer(E2D_LAYER layer)
200{
[5402]201  if (this->parent != NULL && this->parent->getLayer() > layer)
202  {
[5403]203    PRINTF(2)("Unable to set %s to layer %s, because it's parent(%s) is of higher layer %s\n",
204              this->getName(),
205              Element2D::layer2DToChar(layer),
206              this->parent->getName(),
207              Element2D::layer2DToChar(this->parent->getLayer()));
[5402]208    layer = this->parent->getLayer();
209  }
[4862]210  this->layer = layer;
211}
212
213/**
[4858]214 * sets the layer onto which this 2D-element is projected to.
[7221]215 * @param layer the layer @see loadParams @see Element2D::charToLayer2D(const std::string& layer)
[4858]216 */
[7221]217void Element2D::setLayer(const std::string& layer)
[4858]218{
[5401]219  this->setLayer(Element2D::charToLayer2D(layer));
[4858]220}
221
[7031]222void Element2D::setSizeSoft2D(float x, float y, float bias)
223{
224  if (likely(this->toSize == NULL))
225    this->toSize = new Vector2D();
[4858]226
[7031]227  *this->toSize = Vector2D(x,y);;
228  this->bias = bias;
229}
230
231
232
[4858]233/**
[6299]234 * @brief sets a node, this 2D-Element should be shown upon
[4858]235 * @param bindNode the name of the Node (should be existing)
236 */
[7221]237void Element2D::setBindNode(const std::string& bindNode)
[4858]238{
239  const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE));
240  if (tmpBindNode != NULL)
241    this->bindNode = tmpBindNode;
242}
243
[5091]244/**
245 * sets the relative coordinate of the Element2D to its parent
246 * @param relCoord the relative coordinate to the parent
247 */
[5081]248void Element2D::setRelCoor2D (const Vector& relCoord)
249{
[5113]250  if (this->toCoordinate!= NULL)
251  {
252    delete this->toCoordinate;
253    this->toCoordinate = NULL;
254  }
[5082]255  this->relCoordinate = relCoord;
256  this->bRelCoorChanged = true;
[5081]257}
258
[5091]259/**
260 * sets the relative coordinate of the Element2D to its Parent
261 * @param x the x coordinate
262 * @param y the y coordinate
263 * @param z the z coordinate
264 */
[5081]265void Element2D::setRelCoor2D (float x, float y, float z)
266{
[5113]267  this->setRelCoor2D(Vector(x,y,z));
[5081]268}
269
[5091]270/**
271 * sets the Relative coordinate to the parent in Pixels
272 * @param x the relCoord X
273 * @param y the relCoord Y
274 */
[5089]275void Element2D::setRelCoor2Dpx (int x, int y)
276{
[5090]277  this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
278                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
[5089]279                     0
280                           ));
281}
282
[5091]283/**
284 * sets a new relative position smoothely
285 * @param relCoordSoft the new Position to iterate to
286 * @param bias how fast to iterate to this position
287 */
[5081]288void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias)
289{
[5082]290  if (likely(this->toCoordinate == NULL))
291    this->toCoordinate = new Vector();
292
293  *this->toCoordinate = relCoordSoft;
294  this->bias = bias;
[5081]295}
296
[5091]297/**
298 * sets a new relative position smoothely
299 * @param x the new x-coordinate in Pixels of the Position to iterate to
300 * @param y the new y-coordinate in Pixels of the Position to iterate to
301 * @param bias how fast to iterate to this position
302 */
[5089]303void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias)
304{
[5090]305  this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
306                         (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
[5089]307                         0),
308                         bias);
309}
310
[5091]311/**
312 * set relative coordinates smoothely
313 * @param x x-relative coordinates to its parent
314 * @param y y-relative coordinates to its parent
315 * @param z z-relative coordinates to its parent
316 * @see  void PNode::setRelCoorSoft (const Vector&, float)
317 */
[5082]318void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias)
[5081]319{
[5082]320  this->setRelCoorSoft2D(Vector(x, y, depth), bias);
[5081]321}
322
[5091]323/**
324 * @param absCoord set absolute coordinate
325 */
[5081]326void Element2D::setAbsCoor2D (const Vector& absCoord)
327{
[5113]328  if (this->toCoordinate!= NULL)
329  {
330    delete this->toCoordinate;
331    this->toCoordinate = NULL;
332  }
333
[5082]334  if( likely(this->parentMode & E2D_PARENT_MOVEMENT))
335  {
336    /* if you have set the absolute coordinates this overrides all other changes */
337    if (likely(this->parent != NULL))
338      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
339    else
340      this->relCoordinate = absCoord;
341  }
342  if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT)
343  {
344    if (likely(this->parent != NULL))
345      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
346    else
347      this->relCoordinate = absCoord;
348  }
349
350  this->bRelCoorChanged = true;
[5081]351}
352
[5091]353/**
354 * @param x x-coordinate.
355 * @param y y-coordinate.
356 * @param z z-coordinate.
357 * @see void PNode::setAbsCoor (const Vector& absCoord)
358 */
[5081]359void Element2D::setAbsCoor2D (float x, float y, float depth)
360{
[5082]361  this->setAbsCoor2D(Vector(x, y, depth));
[5081]362}
363
[5091]364/**
365 * @param x x-coordinate in Pixels
366 * @param y y-coordinate in Pixels
367 * @see void PNode::setAbsCoor (const Vector& absCoord)
368 */
[5089]369void Element2D::setAbsCoor2Dpx (int x, int y)
370{
[5090]371  this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
372                     (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
[5089]373                     0
374                           ));
375}
376
[5091]377/**
[5414]378 * @param absCoordSoft set absolute coordinate
379 * @param bias how fast to iterato to the new Coordinate
380 */
381void Element2D::setAbsCoorSoft2D (const Vector& absCoordSoft, float bias)
382{
383  if (this->toCoordinate == NULL)
384    this->toCoordinate = new Vector();
385
386  if( likely(this->parentMode & E2D_PARENT_MOVEMENT))
387  {
388    /* if you have set the absolute coordinates this overrides all other changes */
389    if (likely(this->parent != NULL))
390      *this->toCoordinate = absCoordSoft - parent->getAbsCoor2D ();
391    else
392      *this->toCoordinate = absCoordSoft;
393  }
394  if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT)
395  {
396    if (likely(this->parent != NULL))
397      *this->toCoordinate = absCoordSoft - parent->getAbsCoor2D ();
398    else
399      *this->toCoordinate = absCoordSoft;
400  }
401
402  this->bias = bias;
403}
404
405/**
406 * @param x x-coordinate.
407 * @param y y-coordinate.
408 * @param z z-coordinate.
409 * @see void PNode::setAbsCoor (const Vector& absCoord)
410 */
411void Element2D::setAbsCoorSoft2D (float x, float y, float depth, float bias)
412{
413  this->setAbsCoorSoft2D(Vector(x, y, depth), bias);
414}
415
416/**
[5091]417 *  shift coordinate ralative
418 * @param shift shift vector
419 *
420 * This simply adds the shift-Vector to the relative Coordinate
421 */
[5083]422void Element2D::shiftCoor2D (const Vector& shift)
[5081]423{
[5082]424  this->relCoordinate += shift;
425  this->bRelCoorChanged = true;
426
[5081]427}
428
[5091]429/**
430 * shifts in PixelSpace
431 * @param x the pixels to shift in X
432 * @param y the pixels to shift in Y
433 */
[5089]434void Element2D::shiftCoor2Dpx (int x, int y)
435{
[5090]436  this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(),
437                    (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(),
[5089]438                     0));
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/**
519 *  adds a child and makes this node to a parent
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/**
582 * removes a child from the node
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  {
675    this->toCoordinate = new Vector();
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
686  Vector tmpV = this->getAbsCoor2D();
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/**
702 * does the reparenting in a very smooth way
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
[6299]731
[5091]732/**
[6299]733 * @returns the NullElement (and if needed (most probably) creates it)
734 */
735Element2D* Element2D::createNullElement()
736{
737  if (likely(Element2D::nullElement == NULL))
738  {
739    Element2D::nullElement = new Element2D(NULL, E2D_LAYER_BELOW_ALL, E2D_PARENT_MODE_DEFAULT | E2D_REPARENT_TO_NULL);
740    Element2D::nullElement->setName("NullElement");
741  }
742  return Element2D::nullElement;
743}
744
745
746/**
747 * !! PRIVATE FUNCTION
748 * @brief checks the upward integrity (e.g if Element2D is somewhere up the Node tree.)
749 * @param checkParent the Parent to check.
750 * @returns true if the integrity-check succeeds, false otherwise.
751 *
752 * If there is a second occurence of checkParent before NULL, then a loop could get
753 * into the Tree, and we do not want this.
754 */
755bool Element2D::checkIntegrity(const Element2D* checkParent) const
756{
757  const Element2D* parent = this;
758  while ( (parent = parent->getParent2D()) != NULL)
759    if (unlikely(parent == checkParent))
760      return false;
761  return true;
762}
763
764
765/**
[5091]766 *  updates the absCoordinate/absDirection
767 * @param dt The time passed since the last update
[5081]768
[5091]769   this is used to go through the parent-tree to update all the absolute coordinates
770   and directions. this update should be done by the engine, so you don't have to
771   worry, normaly...
772 */
[5081]773void Element2D::update2D (float dt)
774{
[5089]775  // setting the Position of this 2D-Element.
[5083]776  if( likely(this->parent != NULL))
777  {
778      // movement for nodes with smoothMove enabled
779    if (unlikely(this->toCoordinate != NULL))
780    {
[5376]781      Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias;
[5082]782
[5083]783      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
784      {
785        this->shiftCoor2D(moveVect);
786      }
787      else
788      {
[5377]789        Vector tmp = *this->toCoordinate;
790        this->setRelCoor2D(tmp);
[5083]791        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
792      }
793    }
794    if (unlikely(this->toDirection != NULL))
795    {
[5376]796      float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias;
797      if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA))
[5083]798      {
799        this->shiftDir2D(rotFlot);
800      }
801      else
802      {
[5377]803        float tmp = *this->toDirection;
804        this->setRelDir2D(tmp);
[5083]805        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
806      }
807    }
[7031]808    if (unlikely(this->toSize != NULL))
809    {
810      Vector2D shiftSize = (*this->toSize - Vector2D(this->sizeX, this->sizeY)) *fabsf(dt)*bias;
811      if (likely((shiftSize).len() >= .001))//PNODE_ITERATION_DELTA))
812      {
813        this->sizeX += shiftSize.x;
814        this->sizeY += shiftSize.y;
815      }
816      else
817      {
818        delete this->toSize;
819        this->toSize = NULL;
820        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
821      }
822    }
[5083]823
824    // MAIN UPDATE /////////////////////////////////////
825    this->lastAbsCoordinate = this->absCoordinate;
826
[5084]827    PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[5083]828
829
[5118]830    if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged)
[5083]831    {
832      /* update the current absDirection - remember * means rotation around sth.*/
[5090]833      this->prevRelDirection = this->relDirection;
[5083]834      this->absDirection = this->relDirection + parent->getAbsDir2D();;
835    }
836
[5089]837
[5397]838    if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged))
[5083]839    {
840      this->prevRelCoordinate = this->relCoordinate;
[5089]841      this->absCoordinate.x = .5 + this->relCoordinate.x;
842      this->absCoordinate.y = .5 + this->relCoordinate.y;
843      this->absCoordinate.z = 0.0;
[5083]844    }
[5397]845    else if (unlikely(this->bindNode != NULL))
[5083]846    {
[6778]847      GLdouble projectPos[3] = {0.0, 0.0, 0.0};
[5089]848      gluProject(this->bindNode->getAbsCoor().x,
849                 this->bindNode->getAbsCoor().y,
850                 this->bindNode->getAbsCoor().z,
851                 GraphicsEngine::modMat,
852                 GraphicsEngine::projMat,
853                 GraphicsEngine::viewPort,
854                 projectPos,
855                 projectPos+1,
856                 projectPos+2);
[6778]857//       printf("%s::%s  == %f %f %f :: %f %f\n", this->getClassName(), this->getName(),
858//              this->bindNode->getAbsCoor().x,
859//              this->bindNode->getAbsCoor().y,
860//              this->bindNode->getAbsCoor().z,
861//              projectPos[0],
862//              projectPos[1]);
863
[5396]864      this->prevRelCoordinate.x = this->absCoordinate.x = projectPos[0] /* /(float)GraphicsEngine::getInstance()->getResolutionX() */ + this->relCoordinate.x;
865      this->prevRelCoordinate.y = this->absCoordinate.y = (float)GraphicsEngine::getInstance()->getResolutionY() -  projectPos[1] + this->relCoordinate.y;
866      this->prevRelCoordinate.z = this->absCoordinate.z = projectPos[2] + this->relCoordinate.z;
867      this->bRelCoorChanged = true;
[5089]868    }
869    else
870    {
871      if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged))
872      {
873        /* update the current absCoordinate */
874        this->prevRelCoordinate = this->relCoordinate;
875        this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
876      }
877      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
878      {
879        /* update the current absCoordinate */
880        this->prevRelCoordinate = this->relCoordinate;
[5090]881        float sine = sin(this->parent->getAbsDir2D());
882        float cose = cos(this->parent->getAbsDir2D());
883//        this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine;
884//        this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine;
885
[5089]886        this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
887        this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
[5083]888
[5089]889      }
[5083]890    }
891    /////////////////////////////////////////////////
892  }
893  else
894  {
[5084]895    PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
[5083]896    if (this->bRelCoorChanged)
[5118]897    {
898      this->prevRelCoordinate = this->relCoordinate;
[5083]899      this->absCoordinate = this->relCoordinate;
[5118]900    }
[5083]901    if (this->bRelDirChanged)
[5118]902    {
903      this->prevRelDirection = this->relDirection;
[5376]904      this->absDirection = this->getAbsDir2D() + this->relDirection;
[5118]905    }
[5083]906  }
907
[5089]908
909  // UPDATE CHILDREN
[6299]910  if(!this->children.empty() || this->parentMode & E2D_UPDATE_CHILDREN_IF_INACTIVE)
[5083]911  {
[5775]912    list<Element2D*>::iterator child;
913    for (child = this->children.begin(); child != this->children.end(); child++)
[5083]914    {
915      /* if this node has changed, make sure, that all children are updated also */
916      if( likely(this->bRelCoorChanged))
[6299]917        (*child)->parentCoorChanged2D();
[5083]918      if( likely(this->bRelDirChanged))
[6299]919        (*child)->parentDirChanged2D();
[5083]920
[5775]921      (*child)->update2D(dt);
[5083]922    }
923  }
[5089]924
925  // FINISHING PROCESS
[5083]926  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
927  this->bRelCoorChanged = false;
928  this->bRelDirChanged = false;
[5081]929}
930
[6299]931
[5091]932/**
933 *  displays some information about this pNode
934 * @param depth The deph into which to debug the children of this Element2D to.
935 * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...)
936 * @param level The n-th level of the Node we draw (this is internal and only for nice output)
937 */
[7052]938void Element2D::debug2D (unsigned int depth, unsigned int level) const
[5081]939{
[5082]940  for (unsigned int i = 0; i < level; i++)
941    PRINT(0)(" |");
[5775]942  if (this->children.size() > 0)
[5082]943    PRINT(0)(" +");
944  else
945    PRINT(0)(" -");
[5401]946  PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s, layer:%s\n",
947            this->getClassName(),
948            this->getName(),
949            this->absCoordinate.x,
950            this->absCoordinate.y,
951            this->relCoordinate.x,
952            this->relCoordinate.y,
953            this->getAbsDir2D(),
[7221]954            Element2D::parentingModeToString2D(parentMode),
[5401]955            Element2D::layer2DToChar(this->layer));
[5402]956
[5082]957  if (depth >= 2 || depth == 0)
958  {
[5775]959    list<Element2D*>::const_iterator child;
960    for (child = this->children.begin(); child != this->children.end(); child++)
[5082]961    {
962      if (depth == 0)
[7052]963        (*child)->debug2D(0, level + 1);
[5082]964      else
[7052]965        (*child)->debug2D(depth - 1, level +1);
[5082]966    }
967  }
[5081]968}
969
[5285]970/**
971 * ticks the 2d-Element
972 * @param dt the time elapsed since the last tick
[5401]973 *
974 * the element only gets tickt, if it is active.
975 * Be aware, that this walks through the entire Element2D-tree,
976 * searching for Elements to be ticked.
[5285]977 */
[5401]978void Element2D::tick2D(float dt)
[5285]979{
[6299]980  if (this->bActive)
[5401]981    this->tick(dt);
[5775]982  if (this->children.size() > 0)
[5401]983  {
[5775]984    list<Element2D*>::iterator child;
985    for (child = this->children.begin(); child != this->children.end(); child++)
986      (*child)->tick2D(dt);
[5401]987  }
988}
[5082]989
[5401]990/**
991 * draws all the Elements from this element2D downwards
[5404]992 * @param layer the maximal Layer to draw. @see E2D_LAYER
[5401]993 */
[5404]994void Element2D::draw2D(short layer) const
[5401]995{
[6299]996  if (this->bVisible)
[5401]997    this->draw();
[5775]998  if (this->children.size() > 0)
[5401]999  {
[5775]1000    list<Element2D*>::const_iterator child;
1001    for (child = this->children.begin(); child != this->children.end(); child++)
[5401]1002      if (likely(layer >= this->layer))
[5775]1003        (*child)->draw2D(layer);
[5401]1004  }
[5285]1005}
1006
[5091]1007/**
1008 * displays the Element2D at its position with its rotation as a Plane.
1009 */
[5417]1010void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const
[5081]1011{
[5417]1012  if (level == 0)
1013  {
1014    glPushAttrib(GL_ENABLE_BIT);
1015    glMatrixMode(GL_MODELVIEW);
[5082]1016
[5417]1017    glDisable(GL_LIGHTING);
1018    glDisable(GL_BLEND);
1019    glDisable(GL_TEXTURE_2D);
1020  }
1021
1022  glPushMatrix();
1023  /* translate */
1024  /* rotate */
1025  glColor3f(color.x, color.y, color.z);
1026
1027  glTranslatef (this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
1028  glRotatef(this->getAbsDir2D(), 0,0,1);
1029  glBegin(GL_LINE_LOOP);
[5418]1030  glVertex2f(0, 0);
1031  glVertex2f(0, +this->getSizeY2D());
[5417]1032  glVertex2f(+this->getSizeX2D(), +this->getSizeY2D());
[5418]1033  glVertex2f(+this->getSizeX2D(), 0);
[5417]1034  glEnd();
1035
1036
1037  glPopMatrix();
1038  if (depth >= 2 || depth == 0)
1039  {
1040    Vector childColor =  Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0));
[5775]1041    list<Element2D*>::const_iterator child;
1042    for (child = this->children.begin(); child != this->children.end(); child++)
[5417]1043    {
1044      // drawing the Dependency graph
[6299]1045      if (this != Element2D::getNullElement())
[5417]1046      {
1047        glBegin(GL_LINES);
1048        glColor3f(color.x, color.y, color.z);
1049        glVertex3f(this->getAbsCoor2D ().x,
1050                   this->getAbsCoor2D ().y,
1051                   0);
1052        glColor3f(childColor.x, childColor.y, childColor.z);
[5775]1053        glVertex3f((*child)->getAbsCoor2D ().x,
1054                   (*child)->getAbsCoor2D ().y,
[5417]1055                   0);
1056        glEnd();
1057      }
1058      if (depth == 0)
[5775]1059        (*child)->debugDraw2D(0, size, childColor, level+1);
[5417]1060      else
[5775]1061        (*child)->debugDraw2D(depth - 1, size, childColor, level +1);
[5417]1062    }
1063  }
1064  if (level == 0)
1065    glPopAttrib();
1066
[5081]1067}
1068
1069
1070// helper functions //
[5091]1071/**
1072 * converts a parentingMode into a string that is the name of it
1073 * @param parentingMode the ParentingMode to convert
1074 * @return the converted string
1075 */
[7221]1076const char* Element2D::parentingModeToString2D(int parentingMode)
[5081]1077{
[5082]1078  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
1079    return "local-rotate";
1080  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
1081    return "rotate-movement";
1082  else if (parentingMode == E2D_PARENT_MOVEMENT)
1083    return "movement";
1084  else if (parentingMode == E2D_PARENT_ALL)
1085    return "all";
1086  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
1087    return "rotate-and-move";
[5081]1088}
1089
[5091]1090/**
1091 * converts a parenting-mode-string into a int
1092 * @param parentingMode the string naming the parentingMode
1093 * @return the int corresponding to the named parentingMode
1094 */
[7221]1095E2D_PARENT_MODE Element2D::stringToParentingMode2D(const std::string& parentingMode)
[5081]1096{
[7221]1097  if (parentingMode == "local-rotate")
[5082]1098    return (E2D_PARENT_LOCAL_ROTATE);
[7221]1099  else  if (parentingMode == "rotate-movement")
[5082]1100    return (E2D_PARENT_ROTATE_MOVEMENT);
[7221]1101  else  if (parentingMode == "movement")
[5082]1102    return (E2D_PARENT_MOVEMENT);
[7221]1103  else  if (parentingMode == "all")
[5082]1104    return (E2D_PARENT_ALL);
[7221]1105  else  if (parentingMode == "rotate-and-move")
[5082]1106    return (E2D_PARENT_ROTATE_AND_MOVE);
[5081]1107}
1108
[5401]1109/**
1110 * converts a layer into its corresponding string
1111 * @param layer the layer to get the name-String of.
1112 * @returns the Name of the Layer (on error the default-layer-string is returned)
1113 */
1114const char* Element2D::layer2DToChar(E2D_LAYER layer)
1115{
1116  switch(layer)
1117  {
1118    case E2D_LAYER_TOP:
1119      return "top";
1120      break;
1121    case E2D_LAYER_MEDIUM:
1122      return "medium";
1123      break;
1124    case E2D_LAYER_BOTTOM:
1125      return "bottom";
1126      break;
1127    case E2D_LAYER_BELOW_ALL:
1128      return "below-all";
1129      break;
1130    default:
1131      return layer2DToChar(E2D_DEFAULT_LAYER);
1132      break;
1133  }
1134}
[5081]1135
[5401]1136/**
1137 * converts a String holding a actual Layer
1138 * @param layer the String to convert into a Layer2D
1139 * @returns the E2D_LAYER on success, E2D_DEFAULT_LAYER on error.
1140 */
[7221]1141E2D_LAYER Element2D::charToLayer2D(const std::string& layer)
[5401]1142{
[7221]1143  if (layer =="top")
[5401]1144    return (E2D_LAYER_TOP);
[7221]1145  else  if (layer == "medium")
[5401]1146    return (E2D_LAYER_MEDIUM);
[7221]1147  else  if (layer == "bottom")
[5401]1148    return (E2D_LAYER_BOTTOM);
[7221]1149  else  if (layer == "below-all")
[5401]1150    return (E2D_LAYER_BELOW_ALL);
1151  else
1152    return (E2D_DEFAULT_LAYER);
1153}
Note: See TracBrowser for help on using the repository browser.