| 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 | |
|---|
| 27 | using namespace std; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * standard constructor |
|---|
| 32 | * @todo this constructor is not jet implemented - do it |
|---|
| 33 | */ |
|---|
| 34 | Element2D::Element2D () |
|---|
| 35 | { |
|---|
| 36 | this->init(); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * standard deconstructor |
|---|
| 41 | */ |
|---|
| 42 | Element2D::~Element2D () |
|---|
| 43 | { |
|---|
| 44 | // delete what has to be deleted here |
|---|
| 45 | Render2D::getInstance()->unregisterElement2D(this); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * initializes a Element2D |
|---|
| 51 | */ |
|---|
| 52 | void Element2D::init() |
|---|
| 53 | { |
|---|
| 54 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
|---|
| 55 | |
|---|
| 56 | this->setVisibility(true); |
|---|
| 57 | this->setPosition2D(0,0); |
|---|
| 58 | this->setAlignment(E2D_ALIGN_NONE); |
|---|
| 59 | this->layer = E2D_TOP; |
|---|
| 60 | |
|---|
| 61 | Render2D::getInstance()->registerElement2D(this); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 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 | |
|---|
| 82 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
|---|
| 83 | .describe("if the Element is visible or not"); |
|---|
| 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 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 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 | /** |
|---|
| 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 | /** |
|---|
| 142 | * this sets the position of the Element on the screen. |
|---|
| 143 | * Use this in the your tick function, if you want the element to be automatically positioned. |
|---|
| 144 | */ |
|---|
| 145 | void Element2D::positioning() |
|---|
| 146 | { |
|---|
| 147 | // setting the Position of this 2D-Element. |
|---|
| 148 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER) |
|---|
| 149 | { |
|---|
| 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 | { |
|---|
| 156 | GLdouble projectPos[3]; |
|---|
| 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]; |
|---|
| 169 | } |
|---|
| 170 | else |
|---|
| 171 | { |
|---|
| 172 | absPos2D.x = this->relPos2D[0]; |
|---|
| 173 | absPos2D.y = this->relPos2D[1]; |
|---|
| 174 | absPos2D.depth = 0; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 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(); |
|---|
| 185 | } |
|---|