Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

compiles

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