Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new style for resources (prework/movement)

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