[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[4838] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[4839] | 18 | #include "element_2d.h" |
---|
[4840] | 19 | #include "render_2d.h" |
---|
[1853] | 20 | |
---|
[4843] | 21 | #include "graphics_engine.h" |
---|
| 22 | #include "p_node.h" |
---|
[4858] | 23 | #include "load_param.h" |
---|
| 24 | #include "tinyxml.h" |
---|
| 25 | #include "class_list.h" |
---|
[4843] | 26 | |
---|
[1856] | 27 | using namespace std; |
---|
[1853] | 28 | |
---|
[1856] | 29 | |
---|
[3245] | 30 | /** |
---|
[4838] | 31 | * standard constructor |
---|
| 32 | * @todo this constructor is not jet implemented - do it |
---|
[3245] | 33 | */ |
---|
[4838] | 34 | Element2D::Element2D () |
---|
[3365] | 35 | { |
---|
[4847] | 36 | this->init(); |
---|
[3365] | 37 | } |
---|
[1853] | 38 | |
---|
[3245] | 39 | /** |
---|
[4838] | 40 | * standard deconstructor |
---|
[3245] | 41 | */ |
---|
[4838] | 42 | Element2D::~Element2D () |
---|
[3543] | 43 | { |
---|
| 44 | // delete what has to be deleted here |
---|
[4840] | 45 | Render2D::getInstance()->unregisterElement2D(this); |
---|
[3543] | 46 | } |
---|
[4843] | 47 | |
---|
| 48 | |
---|
[4858] | 49 | /** |
---|
| 50 | * initializes a Element2D |
---|
| 51 | */ |
---|
[4847] | 52 | void Element2D::init() |
---|
| 53 | { |
---|
| 54 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
---|
| 55 | |
---|
[4861] | 56 | this->setVisibility(true); |
---|
[4856] | 57 | this->setPosition2D(0,0); |
---|
[4861] | 58 | this->setAlignment(E2D_ALIGN_NONE); |
---|
[4862] | 59 | this->layer = E2D_TOP; |
---|
[4856] | 60 | |
---|
[4847] | 61 | Render2D::getInstance()->registerElement2D(this); |
---|
| 62 | } |
---|
| 63 | |
---|
[4843] | 64 | /** |
---|
[4858] | 65 | * Loads the Parameters of an Element2D from... |
---|
| 66 | * @param root The XML-element to load from |
---|
| 67 | */ |
---|
| 68 | void Element2D::loadParams(const TiXmlElement* root) |
---|
| 69 | { |
---|
| 70 | LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) |
---|
| 71 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
---|
| 72 | |
---|
| 73 | LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) |
---|
| 74 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
---|
| 75 | |
---|
| 76 | LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) |
---|
| 77 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
---|
| 78 | |
---|
| 79 | LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D) |
---|
| 80 | .describe("the _relative_ position (away from alignment) this 2d-element shows"); |
---|
| 81 | |
---|
[4860] | 82 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
---|
| 83 | .describe("if the Element is visible or not"); |
---|
[4858] | 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * sets the alignment of the 2D-element in form of a String |
---|
| 88 | * @param alignment the alignment @see loadParams |
---|
| 89 | */ |
---|
| 90 | void Element2D::setAlignment(const char* alignment) |
---|
| 91 | { |
---|
| 92 | if (!strcmp(alignment, "center")) |
---|
| 93 | this->setAlignment(E2D_ALIGN_CENTER); |
---|
| 94 | else if (!strcmp(alignment, "left")) |
---|
| 95 | this->setAlignment(E2D_ALIGN_LEFT); |
---|
| 96 | else if (!strcmp(alignment, "right")) |
---|
| 97 | this->setAlignment(E2D_ALIGN_RIGHT); |
---|
| 98 | else if (!strcmp(alignment, "screen-center")) |
---|
| 99 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
---|
| 100 | } |
---|
| 101 | |
---|
[4862] | 102 | |
---|
[4858] | 103 | /** |
---|
[4862] | 104 | * moves a Element to another layer |
---|
| 105 | * @param layer the Layer this is drawn on |
---|
| 106 | */ |
---|
| 107 | void Element2D::setLayer(E2D_LAYER layer) |
---|
| 108 | { |
---|
| 109 | Render2D::getInstance()->moveToLayer(this, layer); |
---|
| 110 | this->layer = layer; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
[4858] | 114 | * sets the layer onto which this 2D-element is projected to. |
---|
| 115 | * @param layer the layer @see loadParams |
---|
| 116 | */ |
---|
| 117 | void Element2D::setLayer(const char* layer) |
---|
| 118 | { |
---|
| 119 | if (!strcmp(layer, "top")) |
---|
| 120 | this->setLayer(E2D_TOP); |
---|
| 121 | else if (!strcmp(layer, "medium")) |
---|
| 122 | this->setLayer(E2D_MEDIUM); |
---|
| 123 | else if (!strcmp(layer, "bottom")) |
---|
| 124 | this->setLayer(E2D_BOTTOM); |
---|
| 125 | else if (!strcmp(layer, "below-all")) |
---|
| 126 | this->setLayer(E2D_BELOW_ALL); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * sets a node, this 2D-Element should be shown upon |
---|
| 132 | * @param bindNode the name of the Node (should be existing) |
---|
| 133 | */ |
---|
| 134 | void Element2D::setBindNode(const char* bindNode) |
---|
| 135 | { |
---|
| 136 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
---|
| 137 | if (tmpBindNode != NULL) |
---|
| 138 | this->bindNode = tmpBindNode; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
[4843] | 142 | * this sets the position of the Element on the screen. |
---|
[4847] | 143 | * Use this in the your tick function, if you want the element to be automatically positioned. |
---|
[4843] | 144 | */ |
---|
| 145 | void Element2D::positioning() |
---|
| 146 | { |
---|
[4856] | 147 | // setting the Position of this 2D-Element. |
---|
| 148 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER) |
---|
[4843] | 149 | { |
---|
[4856] | 150 | absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0]; |
---|
| 151 | absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1]; |
---|
| 152 | absPos2D.depth = 0; |
---|
| 153 | } |
---|
| 154 | else if (this->bindNode) |
---|
| 155 | { |
---|
[4843] | 156 | GLdouble projectPos[3]; |
---|
[4847] | 157 | gluProject(this->bindNode->getAbsCoor().x, |
---|
| 158 | this->bindNode->getAbsCoor().y, |
---|
| 159 | this->bindNode->getAbsCoor().z, |
---|
| 160 | GraphicsEngine::modMat, |
---|
| 161 | GraphicsEngine::projMat, |
---|
| 162 | GraphicsEngine::viewPort, |
---|
| 163 | projectPos, |
---|
| 164 | projectPos+1, |
---|
| 165 | projectPos+2); |
---|
| 166 | absPos2D.x = projectPos[0] + this->relPos2D[0]; |
---|
| 167 | absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1]; |
---|
| 168 | absPos2D.depth = projectPos[2]; |
---|
[4843] | 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
[4847] | 172 | absPos2D.x = this->relPos2D[0]; |
---|
| 173 | absPos2D.y = this->relPos2D[1]; |
---|
| 174 | absPos2D.depth = 0; |
---|
[4843] | 175 | } |
---|
[4847] | 176 | } |
---|
[4843] | 177 | |
---|
[4847] | 178 | /** |
---|
| 179 | * ticks the 2d-Element |
---|
| 180 | * @param dt the time elapsed since the last tick |
---|
| 181 | */ |
---|
| 182 | void Element2D::tick(float dt) |
---|
| 183 | { |
---|
| 184 | this->positioning(); |
---|
[4843] | 185 | } |
---|