Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new GUI-functionality

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