Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/render2D/element_2d.cc @ 7875

Last change on this file since 7875 was 7874, checked in by bensch, 19 years ago

fixed the Shell

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