Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd_merge/src/lib/graphics/render2D/element_2d.cc @ 6461

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

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

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