Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5088 was 5088, checked in by bensch, 20 years ago

orxonox/trunk: initialize and delete toCoord/toDir in PNode and Element2D

File size: 15.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 "graphics_engine.h"
22#include "p_node.h"
23#include "load_param.h"
24#include "tinyxml.h"
25#include "class_list.h"
26#include "list.h"
27
28using namespace std;
29
30
31/**
32 * standard constructor
33 * @todo this constructor is not jet implemented - do it
34*/
35Element2D::Element2D (Element2D* parent)
36{
37  this->init(parent);
38}
39
40/**
41 * standard deconstructor
42*/
43Element2D::~Element2D ()
44{
45  // delete what has to be deleted here
46  Render2D::getInstance()->unregisterElement2D(this);
47
48  if (this->toCoordinate != NULL)
49    delete this->toCoordinate;
50  if (this->toDirection != NULL)
51    delete this->toDirection;
52}
53
54
55/**
56 * initializes a Element2D
57 */
58void Element2D::init(Element2D* parent)
59{
60  this->setClassID(CL_ELEMENT_2D, "Element2D");
61
62  this->setVisibility(true);
63  this->setActiveness(true);
64  this->setPosition2D(0,0);
65  this->setAlignment(E2D_ALIGN_NONE);
66  this->layer = E2D_TOP;
67
68  this->setParentMode2D(E2D_PARENT_ALL);
69  this->children = new tList<Element2D>;
70  this->bRelCoorChanged = true;
71  this->bRelDirChanged = true;
72  this->toCoordinate = NULL;
73  this->toDirection = NULL;
74  if (parent == NULL)
75    this->parent = NULL;
76//  else
77  //  this->setParent2D(parent);
78
79  Render2D::getInstance()->registerElement2D(this);
80}
81
82/**
83 * Loads the Parameters of an Element2D from...
84 * @param root The XML-element to load from
85 */
86void Element2D::loadParams(const TiXmlElement* root)
87{
88  LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment)
89      .describe("loads the alignment: (either: center, left, right or screen-center)");
90
91  LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer)
92      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
93
94  LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode)
95      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
96
97  LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D)
98      .describe("the _relative_ position (away from alignment) this 2d-element shows");
99
100  LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility)
101      .describe("if the Element is visible or not");
102}
103
104/**
105 * sets the alignment of the 2D-element in form of a String
106 * @param alignment the alignment @see loadParams
107*/
108void Element2D::setAlignment(const char* alignment)
109{
110  if (!strcmp(alignment, "center"))
111    this->setAlignment(E2D_ALIGN_CENTER);
112  else if (!strcmp(alignment, "left"))
113    this->setAlignment(E2D_ALIGN_LEFT);
114  else if (!strcmp(alignment, "right"))
115    this->setAlignment(E2D_ALIGN_RIGHT);
116  else if (!strcmp(alignment, "screen-center"))
117    this->setAlignment(E2D_ALIGN_SCREEN_CENTER);
118}
119
120
121/**
122 * moves a Element to another layer
123 * @param layer the Layer this is drawn on
124 */
125void Element2D::setLayer(E2D_LAYER layer)
126{
127  Render2D::getInstance()->moveToLayer(this, layer);
128  this->layer = layer;
129}
130
131/**
132 * sets the layer onto which this 2D-element is projected to.
133 * @param layer the layer @see loadParams
134 */
135void Element2D::setLayer(const char* layer)
136{
137  if (!strcmp(layer, "top"))
138    this->setLayer(E2D_TOP);
139  else if (!strcmp(layer, "medium"))
140    this->setLayer(E2D_MEDIUM);
141  else if (!strcmp(layer, "bottom"))
142    this->setLayer(E2D_BOTTOM);
143  else if (!strcmp(layer, "below-all"))
144    this->setLayer(E2D_BELOW_ALL);
145}
146
147
148/**
149 * sets a node, this 2D-Element should be shown upon
150 * @param bindNode the name of the Node (should be existing)
151 */
152void Element2D::setBindNode(const char* bindNode)
153{
154  const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE));
155  if (tmpBindNode != NULL)
156    this->bindNode = tmpBindNode;
157}
158
159/**
160 * this sets the position of the Element on the screen.
161 * Use this in the your tick function, if you want the element to be automatically positioned.
162 *
163 * @todo must be in update
164 */
165void Element2D::positioning()
166{
167  // setting the Position of this 2D-Element.
168  if (this->alignment == E2D_ALIGN_SCREEN_CENTER)
169  {
170    absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0];
171    absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1];
172    absPos2D.depth = 0;
173  }
174  else if (this->bindNode)
175  {
176    GLdouble projectPos[3];
177    gluProject(this->bindNode->getAbsCoor().x,
178               this->bindNode->getAbsCoor().y,
179               this->bindNode->getAbsCoor().z,
180               GraphicsEngine::modMat,
181               GraphicsEngine::projMat,
182               GraphicsEngine::viewPort,
183               projectPos,
184               projectPos+1,
185               projectPos+2);
186    absPos2D.x = projectPos[0] + this->relPos2D[0];
187    absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1];
188    absPos2D.depth = projectPos[2];
189  }
190  else
191  {
192    absPos2D.x = this->relPos2D[0];
193    absPos2D.y = this->relPos2D[1];
194    absPos2D.depth = 0;
195  }
196}
197
198
199void Element2D::setRelCoor2D (const Vector& relCoord)
200{
201  this->relCoordinate = relCoord;
202  this->bRelCoorChanged = true;
203}
204
205
206void Element2D::setRelCoor2D (float x, float y, float z)
207{
208  this->setAbsCoor2D(Vector(x,y,z));
209}
210
211void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias)
212{
213  if (likely(this->toCoordinate == NULL))
214    this->toCoordinate = new Vector();
215
216  *this->toCoordinate = relCoordSoft;
217  this->bias = bias;
218}
219
220void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias)
221{
222  this->setRelCoorSoft2D(Vector(x, y, depth), bias);
223}
224
225void Element2D::setAbsCoor2D (const Vector& absCoord)
226{
227  if( likely(this->parentMode & E2D_PARENT_MOVEMENT))
228  {
229    /* if you have set the absolute coordinates this overrides all other changes */
230    if (likely(this->parent != NULL))
231      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
232    else
233      this->relCoordinate = absCoord;
234  }
235  if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT)
236  {
237    if (likely(this->parent != NULL))
238      this->relCoordinate = absCoord - parent->getAbsCoor2D ();
239    else
240      this->relCoordinate = absCoord;
241  }
242
243  this->bRelCoorChanged = true;
244}
245
246void Element2D::setAbsCoor2D (float x, float y, float depth)
247{
248  this->setAbsCoor2D(Vector(x, y, depth));
249}
250
251void Element2D::shiftCoor2D (const Vector& shift)
252{
253  this->relCoordinate += shift;
254  this->bRelCoorChanged = true;
255
256}
257
258
259void Element2D::setRelDir2D (float relDir)
260{
261  this->relDirection = relDir;
262  this->bRelDirChanged = true;
263}
264
265void Element2D::setRelDirSoft2D(float relDirSoft, float bias)
266{
267  if (likely(this->toDirection == NULL))
268    this->toDirection = new float;
269
270  *this->toDirection = relDirSoft;
271  this->bias = bias;
272}
273
274void Element2D::setAbsDir2D (float absDir)
275{
276  if (likely(this->parent != NULL))
277    this->relDirection = absDir - this->parent->getAbsDir2D();
278  else
279    this->relDirection = absDir;
280
281  this->bRelDirChanged = true;
282}
283
284void Element2D::shiftDir2D (float shiftDir)
285{
286  this->relDirection = this->relDirection + shiftDir;
287  this->bRelDirChanged = true;
288}
289
290
291void Element2D::addChild2D (Element2D* child, int parentingMod)
292{
293  if( likely(child->parent != NULL))
294  {
295    PRINTF(4)("Element2D::addChild() - reparenting node: removing it and adding it again\n");
296    child->parent->children->remove(child);
297  }
298  child->parentMode = parentMode;
299  child->parent = this;
300  this->children->add(child);
301  child->parentCoorChanged();
302}
303
304void Element2D::addChild2D (const char* childName)
305{
306  Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D));
307  if (childNode != NULL)
308    this->addChild2D(childNode);
309}
310
311void Element2D::removeChild2D (Element2D* child)
312{
313  child->remove2D();
314  this->children->remove(child);
315  child->parent = NULL;
316}
317
318void Element2D::remove2D()
319{
320  tIterator<Element2D>* iterator = this->children->getIterator();
321  Element2D* pn = iterator->nextElement();
322
323  while( pn != NULL)
324  {
325    NullElement2D::getInstance()->addChild2D(pn, pn->getParentMode2D());
326    pn = iterator->nextElement();
327  }
328  delete iterator;
329  this->parent->children->remove(this);
330}
331
332
333void Element2D::setParent2D (Element2D* parent)
334{
335  parent->addChild2D(this);
336}
337
338void Element2D::setParent2D (const char* parentName)
339{
340  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
341  if (parentNode != NULL)
342    parentNode->addChild2D(this);
343
344}
345
346
347void Element2D::softReparent2D(Element2D* parentNode, float bias)
348{
349  if (this->parent == parentNode)
350    return;
351
352  if (likely(this->toCoordinate == NULL))
353  {
354    this->toCoordinate = new Vector();
355    *this->toCoordinate = this->getRelCoor2D();
356  }
357  if (likely(this->toDirection == NULL))
358  {
359    this->toDirection = new float;
360    *this->toDirection = this->getRelDir2D();
361  }
362  this->bias = bias;
363
364
365  Vector tmpV = this->getAbsCoor2D();
366  float tmpQ = this->getAbsDir2D();
367
368  parentNode->addChild2D(this);
369
370  if (this->parentMode & PNODE_ROTATE_MOVEMENT)
371    ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor()));
372  else
373    this->setRelCoor2D(tmpV - parentNode->getAbsCoor2D());
374
375  this->setRelDir2D(tmpQ - parentNode->getAbsDir2D());
376}
377
378void Element2D::softReparent2D(const char* parentName, float bias)
379{
380  Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D));
381  if (parentNode != NULL)
382    this->softReparent2D(parentNode, bias);
383}
384
385
386void Element2D::setParentMode2D (const char* parentingMode)
387{
388  this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode));
389}
390
391
392void Element2D::update2D (float dt)
393{
394  if( likely(this->parent != NULL))
395  {
396      // movement for nodes with smoothMove enabled
397    if (unlikely(this->toCoordinate != NULL))
398    {
399      Vector moveVect = (*this->toCoordinate - this->getRelCoor2D()) *dt*bias;
400
401      if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA))
402      {
403        this->shiftCoor2D(moveVect);
404      }
405      else
406      {
407        delete this->toCoordinate;
408        this->toCoordinate = NULL;
409        PRINTF(5)("SmoothMove of %s finished\n", this->getName());
410      }
411    }
412    if (unlikely(this->toDirection != NULL))
413    {
414      float rotFlot = (*this->toDirection - this->getRelDir2D()) *dt*bias;
415      if (likely(rotFlot >= .001))//PNODE_ITERATION_DELTA))
416      {
417        this->shiftDir2D(rotFlot);
418      }
419      else
420      {
421        delete this->toDirection;
422        this->toDirection = NULL;
423        PRINTF(5)("SmoothRotate of %s finished\n", this->getName());
424      }
425    }
426
427    // MAIN UPDATE /////////////////////////////////////
428    this->lastAbsCoordinate = this->absCoordinate;
429
430    PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
431
432
433    if( this->parentMode & PNODE_LOCAL_ROTATE && this->bRelDirChanged)
434    {
435      /* update the current absDirection - remember * means rotation around sth.*/
436      this->prevRelCoordinate = this->relCoordinate;
437      this->absDirection = this->relDirection + parent->getAbsDir2D();;
438    }
439
440    if(likely(this->parentMode & PNODE_MOVEMENT))
441    {
442      /* update the current absCoordinate */
443      this->prevRelCoordinate = this->relCoordinate;
444      this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
445    }
446    else if( this->parentMode & PNODE_ROTATE_MOVEMENT)
447    {
448      /* update the current absCoordinate */
449      this->prevRelCoordinate = this->relCoordinate;
450      this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
451      this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
452
453    }
454    /////////////////////////////////////////////////
455  }
456  else
457  {
458    PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z);
459    if (this->bRelCoorChanged)
460      this->absCoordinate = this->relCoordinate;
461    if (this->bRelDirChanged)
462      this->absDirection = this->getAbsDir2D() * this->relDirection;
463  }
464
465  if(this->children->getSize() > 0)
466  {
467    tIterator<Element2D>* iterator = this->children->getIterator();
468    Element2D* pn = iterator->nextElement();
469    while( pn != NULL)
470    {
471      /* if this node has changed, make sure, that all children are updated also */
472      if( likely(this->bRelCoorChanged))
473        pn->parentCoorChanged ();
474      if( likely(this->bRelDirChanged))
475        pn->parentDirChanged ();
476
477      pn->update2D(dt);
478          //pn = this->children->nextElement();
479      pn = iterator->nextElement();
480    }
481    delete iterator;
482  }
483  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
484  this->bRelCoorChanged = false;
485  this->bRelDirChanged = false;
486}
487
488
489void Element2D::debug (unsigned int depth, unsigned int level) const
490{
491  for (unsigned int i = 0; i < level; i++)
492    PRINT(0)(" |");
493  if (this->children->getSize() > 0)
494    PRINT(0)(" +");
495  else
496    PRINT(0)(" -");
497  PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n",
498  this->getClassName(),
499  this->getName(),
500  this->absCoordinate.x,
501  this->absCoordinate.y,
502  this->relCoordinate.x,
503  this->relCoordinate.y,
504  this->getAbsDir2D(),
505  Element2D::parentingModeToChar2D(parentMode));
506  if (depth >= 2 || depth == 0)
507  {
508    tIterator<Element2D>* iterator = this->children->getIterator();
509      //PNode* pn = this->children->enumerate ();
510    Element2D* pn = iterator->nextElement();
511    while( pn != NULL)
512    {
513      if (depth == 0)
514        pn->debug(0, level + 1);
515      else
516        pn->debug(depth - 1, level +1);
517      pn = iterator->nextElement();
518    }
519    delete iterator;
520  }
521}
522
523#include "color.h"
524
525void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const
526{
527
528}
529
530
531// helper functions //
532const char* Element2D::parentingModeToChar2D(int parentingMode)
533{
534  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
535    return "local-rotate";
536  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
537    return "rotate-movement";
538  else if (parentingMode == E2D_PARENT_MOVEMENT)
539    return "movement";
540  else if (parentingMode == E2D_PARENT_ALL)
541    return "all";
542  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
543    return "rotate-and-move";
544}
545
546E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode)
547{
548  if (!strcmp(parentingMode, "local-rotate"))
549    return (E2D_PARENT_LOCAL_ROTATE);
550  else  if (!strcmp(parentingMode, "rotate-movement"))
551    return (E2D_PARENT_ROTATE_MOVEMENT);
552  else  if (!strcmp(parentingMode, "movement"))
553    return (E2D_PARENT_MOVEMENT);
554  else  if (!strcmp(parentingMode, "all"))
555    return (E2D_PARENT_ALL);
556  else  if (!strcmp(parentingMode, "rotate-and-move"))
557    return (E2D_PARENT_ROTATE_AND_MOVE);
558}
559
560
561
562
563
564
565/**
566 * ticks the 2d-Element
567 * @param dt the time elapsed since the last tick
568 */
569void Element2D::tick(float dt)
570{
571  this->positioning();
572}
573
574
575
576
577
578
579
580NullElement2D* NullElement2D::singletonRef = 0;
581
582/**
583 *  creates the one and only NullElement2D
584 * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0))
585 */
586NullElement2D::NullElement2D ()
587{
588  this->setClassID(CL_NULL_PARENT, "NullElement2D");
589  this->setName("NullElement2D");
590
591  this->setParentMode2D(E2D_PARENT_ALL);
592  NullElement2D::singletonRef = this;
593}
594
595
596/**
597 *  standard deconstructor
598 */
599NullElement2D::~NullElement2D ()
600{
601  //delete singletonRef;
602  NullElement2D::singletonRef = NULL;
603}
Note: See TracBrowser for help on using the repository browser.