Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: made bool's loadable

File size: 4.6 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
27using namespace std;
28
29
30/**
31 * standard constructor
32 * @todo this constructor is not jet implemented - do it
33*/
34Element2D::Element2D ()
35{
36  this->init();
37}
38
39/**
40 * standard deconstructor
41*/
42Element2D::~Element2D ()
43{
44  // delete what has to be deleted here
45  Render2D::getInstance()->unregisterElement2D(this);
46}
47
48
49/**
50 * initializes a Element2D
51 */
52void Element2D::init()
53{
54  this->setClassID(CL_ELEMENT_2D, "Element2D");
55
56  this->setPosition2D(0,0);
57  this->setAlignment(E2D_ALIGN_CENTER);
58
59  Render2D::getInstance()->registerElement2D(this);
60}
61
62/**
63 * Loads the Parameters of an Element2D from...
64 * @param root The XML-element to load from
65 */
66void Element2D::loadParams(const TiXmlElement* root)
67{
68  LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment)
69      .describe("loads the alignment: (either: center, left, right or screen-center)");
70
71  LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer)
72      .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)");
73
74  LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode)
75      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
76
77  LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D)
78      .describe("the _relative_ position (away from alignment) this 2d-element shows");
79
80  LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility)
81      .describe("if the Element is visible or not");
82}
83
84/**
85 * sets the alignment of the 2D-element in form of a String
86 * @param alignment the alignment @see loadParams
87*/
88void Element2D::setAlignment(const char* alignment)
89{
90  if (!strcmp(alignment, "center"))
91    this->setAlignment(E2D_ALIGN_CENTER);
92  else if (!strcmp(alignment, "left"))
93    this->setAlignment(E2D_ALIGN_LEFT);
94  else if (!strcmp(alignment, "right"))
95    this->setAlignment(E2D_ALIGN_RIGHT);
96  else if (!strcmp(alignment, "screen-center"))
97    this->setAlignment(E2D_ALIGN_SCREEN_CENTER);
98}
99
100/**
101 * sets the layer onto which this 2D-element is projected to.
102 * @param layer the layer @see loadParams
103 */
104void Element2D::setLayer(const char* layer)
105{
106  if (!strcmp(layer, "top"))
107    this->setLayer(E2D_TOP);
108  else if (!strcmp(layer, "medium"))
109    this->setLayer(E2D_MEDIUM);
110  else if (!strcmp(layer, "bottom"))
111    this->setLayer(E2D_BOTTOM);
112  else if (!strcmp(layer, "below-all"))
113    this->setLayer(E2D_BELOW_ALL);
114}
115
116
117/**
118 * sets a node, this 2D-Element should be shown upon
119 * @param bindNode the name of the Node (should be existing)
120 */
121void Element2D::setBindNode(const char* bindNode)
122{
123  const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE));
124  if (tmpBindNode != NULL)
125    this->bindNode = tmpBindNode;
126}
127
128/**
129 * this sets the position of the Element on the screen.
130 * Use this in the your tick function, if you want the element to be automatically positioned.
131 */
132void Element2D::positioning()
133{
134
135  // setting the Position of this 2D-Element.
136  if (this->alignment == E2D_ALIGN_SCREEN_CENTER)
137  {
138    absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0];
139    absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1];
140    absPos2D.depth = 0;
141  }
142  else if (this->bindNode)
143  {
144    GLdouble projectPos[3];
145    gluProject(this->bindNode->getAbsCoor().x,
146               this->bindNode->getAbsCoor().y,
147               this->bindNode->getAbsCoor().z,
148               GraphicsEngine::modMat,
149               GraphicsEngine::projMat,
150               GraphicsEngine::viewPort,
151               projectPos,
152               projectPos+1,
153               projectPos+2);
154    absPos2D.x = projectPos[0] + this->relPos2D[0];
155    absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1];
156    absPos2D.depth = projectPos[2];
157  }
158  else
159  {
160    absPos2D.x = this->relPos2D[0];
161    absPos2D.y = this->relPos2D[1];
162    absPos2D.depth = 0;
163  }
164}
165
166/**
167 * ticks the 2d-Element
168 * @param dt the time elapsed since the last tick
169 */
170void Element2D::tick(float dt)
171{
172  this->positioning();
173}
Note: See TracBrowser for help on using the repository browser.