Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented the update function

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