Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Ok the Element2D-renderer works, and hides Elements, that are behind the Camera

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