Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more stl in Shell

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