Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6472 was 6307, checked in by bensch, 19 years ago

orxonox/trunk: PNode fix: PNODE_MOVEMENT didn't work, because the mask was bork

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