Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better mouse capture now

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