/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "element_2d.h" #include "render_2d.h" #include "graphics_engine.h" #include "p_node.h" #include "load_param.h" #include "tinyxml.h" #include "class_list.h" using namespace std; /** * standard constructor * @todo this constructor is not jet implemented - do it */ Element2D::Element2D () { this->init(); } /** * standard deconstructor */ Element2D::~Element2D () { // delete what has to be deleted here Render2D::getInstance()->unregisterElement2D(this); } /** * initializes a Element2D */ void Element2D::init() { this->setClassID(CL_ELEMENT_2D, "Element2D"); this->setVisibility(true); this->setActiveness(true); this->setPosition2D(0,0); this->setAlignment(E2D_ALIGN_NONE); this->layer = E2D_TOP; Render2D::getInstance()->registerElement2D(this); } /** * Loads the Parameters of an Element2D from... * @param root The XML-element to load from */ void Element2D::loadParams(const TiXmlElement* root) { LoadParam(root, "alignment", this, &Element2D::setAlignment) .describe("loads the alignment: (either: center, left, right or screen-center)"); LoadParam(root, "layer", this, &Element2D::setLayer) .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); LoadParam(root, "bind-node", this, &Element2D::setBindNode) .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); LoadParam(root, "2d-position", this, &Element2D::setPosition2D) .describe("the _relative_ position (away from alignment) this 2d-element shows"); LoadParam(root, "visibility", this, &Element2D::setVisibility) .describe("if the Element is visible or not"); } /** * sets the alignment of the 2D-element in form of a String * @param alignment the alignment @see loadParams */ void Element2D::setAlignment(const char* alignment) { if (!strcmp(alignment, "center")) this->setAlignment(E2D_ALIGN_CENTER); else if (!strcmp(alignment, "left")) this->setAlignment(E2D_ALIGN_LEFT); else if (!strcmp(alignment, "right")) this->setAlignment(E2D_ALIGN_RIGHT); else if (!strcmp(alignment, "screen-center")) this->setAlignment(E2D_ALIGN_SCREEN_CENTER); } /** * moves a Element to another layer * @param layer the Layer this is drawn on */ void Element2D::setLayer(E2D_LAYER layer) { Render2D::getInstance()->moveToLayer(this, layer); this->layer = layer; } /** * sets the layer onto which this 2D-element is projected to. * @param layer the layer @see loadParams */ void Element2D::setLayer(const char* layer) { if (!strcmp(layer, "top")) this->setLayer(E2D_TOP); else if (!strcmp(layer, "medium")) this->setLayer(E2D_MEDIUM); else if (!strcmp(layer, "bottom")) this->setLayer(E2D_BOTTOM); else if (!strcmp(layer, "below-all")) this->setLayer(E2D_BELOW_ALL); } /** * sets a node, this 2D-Element should be shown upon * @param bindNode the name of the Node (should be existing) */ void Element2D::setBindNode(const char* bindNode) { const PNode* tmpBindNode = dynamic_cast(ClassList::getObject(bindNode, CL_PARENT_NODE)); if (tmpBindNode != NULL) this->bindNode = tmpBindNode; } /** * this sets the position of the Element on the screen. * Use this in the your tick function, if you want the element to be automatically positioned. */ void Element2D::positioning() { // setting the Position of this 2D-Element. if (this->alignment == E2D_ALIGN_SCREEN_CENTER) { absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0]; absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1]; absPos2D.depth = 0; } else if (this->bindNode) { GLdouble projectPos[3]; gluProject(this->bindNode->getAbsCoor().x, this->bindNode->getAbsCoor().y, this->bindNode->getAbsCoor().z, GraphicsEngine::modMat, GraphicsEngine::projMat, GraphicsEngine::viewPort, projectPos, projectPos+1, projectPos+2); absPos2D.x = projectPos[0] + this->relPos2D[0]; absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1]; absPos2D.depth = projectPos[2]; } else { absPos2D.x = this->relPos2D[0]; absPos2D.y = this->relPos2D[1]; absPos2D.depth = 0; } } /** * ticks the 2d-Element * @param dt the time elapsed since the last tick */ void Element2D::tick(float dt) { this->positioning(); }