Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented PNode-functionality in Element2D,
update and debugDraw still missing

File size: 12.1 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::shiftCoor (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::shiftDir (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
381}
382
383
384void Element2D::debug (unsigned int depth, unsigned int level) const
385{
386  for (unsigned int i = 0; i < level; i++)
387    PRINT(0)(" |");
388  if (this->children->getSize() > 0)
389    PRINT(0)(" +");
390  else
391    PRINT(0)(" -");
392  PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n",
393  this->getClassName(),
394  this->getName(),
395  this->absCoordinate.x,
396  this->absCoordinate.y,
397  this->relCoordinate.x,
398  this->relCoordinate.y,
399  this->getAbsDir2D(),
400  Element2D::parentingModeToChar2D(parentMode));
401  if (depth >= 2 || depth == 0)
402  {
403    tIterator<Element2D>* iterator = this->children->getIterator();
404      //PNode* pn = this->children->enumerate ();
405    Element2D* pn = iterator->nextElement();
406    while( pn != NULL)
407    {
408      if (depth == 0)
409        pn->debug(0, level + 1);
410      else
411        pn->debug(depth - 1, level +1);
412      pn = iterator->nextElement();
413    }
414    delete iterator;
415  }
416}
417
418#include "color.h"
419
420void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const
421{
422
423}
424
425
426// helper functions //
427const char* Element2D::parentingModeToChar2D(int parentingMode)
428{
429  if (parentingMode == E2D_PARENT_LOCAL_ROTATE)
430    return "local-rotate";
431  else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT)
432    return "rotate-movement";
433  else if (parentingMode == E2D_PARENT_MOVEMENT)
434    return "movement";
435  else if (parentingMode == E2D_PARENT_ALL)
436    return "all";
437  else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE)
438    return "rotate-and-move";
439}
440
441E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode)
442{
443  if (!strcmp(parentingMode, "local-rotate"))
444    return (E2D_PARENT_LOCAL_ROTATE);
445  else  if (!strcmp(parentingMode, "rotate-movement"))
446    return (E2D_PARENT_ROTATE_MOVEMENT);
447  else  if (!strcmp(parentingMode, "movement"))
448    return (E2D_PARENT_MOVEMENT);
449  else  if (!strcmp(parentingMode, "all"))
450    return (E2D_PARENT_ALL);
451  else  if (!strcmp(parentingMode, "rotate-and-move"))
452    return (E2D_PARENT_ROTATE_AND_MOVE);
453}
454
455
456
457
458
459
460/**
461 * ticks the 2d-Element
462 * @param dt the time elapsed since the last tick
463 */
464void Element2D::tick(float dt)
465{
466  this->positioning();
467}
468
469
470
471
472
473
474
475NullElement2D* NullElement2D::singletonRef = 0;
476
477/**
478 *  creates the one and only NullElement2D
479 * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0))
480 */
481NullElement2D::NullElement2D ()
482{
483  this->setClassID(CL_NULL_PARENT, "NullElement2D");
484  this->setName("NullElement2D");
485
486  this->setParentMode2D(E2D_PARENT_ALL);
487  NullElement2D::singletonRef = this;
488}
489
490
491/**
492 *  standard deconstructor
493 */
494NullElement2D::~NullElement2D ()
495{
496  //delete singletonRef;
497  NullElement2D::singletonRef = NULL;
498}
Note: See TracBrowser for help on using the repository browser.