Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/CampaignMap_HS17/src/modules/overlays/hud/StoryModeHUD.cc @ 11584

Last change on this file since 11584 was 11584, checked in by boltn, 6 years ago

Planeten koennen nun mittels objectlist beschrieben werden. todo: textart und groesse mittels xml einstellbar machen

File size: 5.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   
23 *   This HUD is used for the implementation of the campaign map.
24 *   So far it can only be used to give names to an object planet.
25 *
26 *   Author:
27 *      Nikola Bolt
28 *   Co-authors:
29 *      Claudio Fanconi
30 */
31
32
33// Header file of this cc file
34#include "StoryModeHUD.h"
35
36// Ogre stuff
37#include <OgreCamera.h>
38#include <OgreFontManager.h>
39#include <OgreOverlayManager.h>
40#include <OgreTextAreaOverlayElement.h>
41#include <OgrePanelOverlayElement.h>
42
43// Other stuff
44#include <typeinfo>
45
46#include "util/Convert.h"
47#include "core/command/ConsoleCommandIncludes.h"
48#include "core/CoreIncludes.h"
49#include "core/XMLPort.h"
50#include "CameraManager.h"
51#include "graphics/Camera.h"
52#include "worldentities/pawns/Pawn.h"
53#include "worldentities/WorldEntity.h"
54#include "core/config/ConfigValueIncludes.h"
55#include "tools/TextureGenerator.h"
56
57#include "worldentities/StoryModePlanet.h"
58
59namespace orxonox
60{
61    RegisterClass ( StoryModeHUD );
62
63    // Constructor of the StoryMode HUD
64    StoryModeHUD::StoryModeHUD(Context* context) : OrxonoxOverlay(context)
65    {
66        RegisterObject(StoryModeHUD);
67        this->initialize();
68    }
69
70    // Destructor of the StoryMode HUD
71    StoryModeHUD::~StoryModeHUD()
72    {
73        for(Ogre::TextAreaOverlayElement* text : texts)
74          delete text;
75    }
76
77    // Functions of the StoryMode HUD
78
79    // XML Port for Level construction.
80    void StoryModeHUD::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        SUPER(StoryModeHUD, XMLPort, xmlelement, mode);
83
84        XMLPortParam(StoryModeHUD, "font", setFont, getFont, xmlelement, mode);
85        XMLPortParam(StoryModeHUD, "textSize", setTextSize, getTextSize, xmlelement, mode);
86    }
87
88    void StoryModeHUD::initialize(){
89        // Scales used for dimensions and text size
90        float xScale = this->getActualSize().x;
91        float yScale = this->getActualSize().y;
92
93       
94        for(unsigned int i = 0; i < ObjectList<StoryModePlanet>().size(); i++){
95
96            Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton()
97                    .createOverlayElement("TextArea", "StoryModeHUD_navText_" + getUniqueNumberString()));
98
99            texts.push_back(text);
100            //text->setFontName(this->fontName_);
101            //text->setCharHeight(this->textSize_ * yScale);
102            texts[i]->setDimensions(xScale, yScale);
103
104            texts[i]->hide();
105       
106            this->background_->addChild(texts[i]);
107        }
108        //this->setFont("Monofur");
109        //this->setTextSize("0.5f");
110    }
111
112    // Set the Font size of the Text.
113    void StoryModeHUD::setFont(const std::string& font)
114    {
115        const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font);
116        if (fontPtr.isNull())
117        {
118            orxout(internal_warning) << "StoryModeHUD: Font '" << font << "' not found" << endl;
119            return;
120        }
121        this->fontName_ = font;
122        for(Ogre::TextAreaOverlayElement* text : texts)
123            if(text != nullptr)
124                text->setFontName(this->fontName_);
125    }
126   
127    // Gets the Font of the Text   
128    const std::string& StoryModeHUD::getFont() const
129    {
130        return this->fontName_;
131    }
132
133    // Set the size of the Text
134    void StoryModeHUD::setTextSize(float size)
135    {
136        if (size <= 0.0f)
137        {
138            orxout(internal_warning) << "StoryModeHUD: Negative font size not allowed" << endl;
139            return;
140        }
141        this->textSize_ = size;
142       
143    }
144
145    // returns the Size of the Text
146    float StoryModeHUD::getTextSize() const
147    {
148        return this->textSize_;
149    }
150
151    // Tick: this is the most important function. It's recalled every frame and makes sure things happen on the screen.
152    void StoryModeHUD::tick(float dt)
153    {
154        SUPER(StoryModeHUD, tick, dt);
155
156        // cam is the pointer which represents your camera
157        Camera* cam = CameraManager::getInstance().getActiveCamera();
158        if (cam == nullptr)
159            return;
160
161        // camTransform is a Matrix, which converts 3D world of the game into 2D on your screen
162        const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
163
164        int i = 0;
165        for(StoryModePlanet* planet : ObjectList<StoryModePlanet>()){
166
167            //display name next to cursor
168            texts[i]->setCaption(planet->getLevelName());
169
170            // Transform to screen coordinates
171            Vector3 pos = camTransform * planet->getWorldPosition();
172
173            // If you fly passed the description, it gets out of sight
174            if (!(pos.z > 1.0)){
175                   
176                // Position text
177                texts[i]->setLeft((pos.x+1)/2); // The (0,0) Coordinate is in the upper left corner.
178                texts[i]->setTop((-pos.y+1)/2);  // With those two calculations we set the desired positions
179
180                // Make sure the overlays are shown
181                texts[i]->show();
182            }
183            i++;
184
185        }
186    }
187}
Note: See TracBrowser for help on using the repository browser.