Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/overlays/hud/StoryModeHUD.cc @ 11929

Last change on this file since 11929 was 11783, checked in by landauf, 7 years ago

merged Presentation_HS17_merge back to trunk

  • Property svn:eol-style set to native
File size: 6.3 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#include "controllers/NewHumanController.h"
57
58#include "worldentities/NameableStaticEntity.h"
59
60namespace orxonox
61{
62    RegisterClass ( StoryModeHUD );
63
64    // Constructor of the StoryMode HUD
65    StoryModeHUD::StoryModeHUD(Context* context) : OrxonoxOverlay(context)
66    {
67        RegisterObject(StoryModeHUD);
68
69        this->textSize_ = 1.0;
70    }
71
72    // Destructor of the StoryMode HUD
73    StoryModeHUD::~StoryModeHUD()
74    {
75        for(Ogre::TextAreaOverlayElement* text : texts)
76            Ogre::OverlayManager::getSingleton().destroyOverlayElement(text);
77    }
78
79    // Functions of the StoryMode HUD
80
81    // XML Port for Level construction.
82    void StoryModeHUD::XMLPort(Element& xmlelement, XMLPort::Mode mode)
83    {
84        SUPER(StoryModeHUD, XMLPort, xmlelement, mode);
85
86        XMLPortParam(StoryModeHUD, "font", setFont, getFont, xmlelement, mode);
87        XMLPortParam(StoryModeHUD, "textSize", setTextSize, getTextSize, xmlelement, mode);
88    }
89
90    void StoryModeHUD::initialize(){
91        firstTick = false;
92        // Scales used for dimensions and text size
93        float xScale = this->getActualSize().x;
94        float yScale = this->getActualSize().y;
95
96        //Sets the Camera angle at 30 degrees above it, so levels can be seen better   
97        CameraManager::getInstance().getActiveCamera()->setOrientation(Vector3::UNIT_X, Degree(-30));
98
99        for(NameableStaticEntity* planet : ObjectList<NameableStaticEntity>()){
100
101
102            Ogre::TextAreaOverlayElement* text = static_cast<Ogre::TextAreaOverlayElement*>( Ogre::OverlayManager::getSingleton()
103                    .createOverlayElement("TextArea", "StoryModeHUD_navText_" + getUniqueNumberString()));
104
105            texts.push_back(text);
106            text->setDimensions(xScale, yScale);
107
108            //font name of the text needs to be set here, not in the xml setter function
109            text->setFontName(this->fontName_);
110            text->setCharHeight(this->textSize_ * yScale);
111
112            //set text
113            text->setCaption(planet->getLevelName());
114
115            text->hide();
116       
117            this->background_->addChild(text);
118        }
119    }
120
121    // Set the Font of this HUD.
122    void StoryModeHUD::setFont(const std::string& font)
123    {
124        const Ogre::ResourcePtr& fontPtr = Ogre::FontManager::getSingleton().getByName(font);
125        if (fontPtr.isNull())
126        {
127            this->fontName_ = "Monofur";
128            orxout(internal_warning) << "StoryModeHUD: Font '" << font << "' not found. Font has been set to Monofur." << endl;
129            return;
130        }
131        this->fontName_ = font;
132    }
133   
134    // Gets the Font of this HUD   
135    const std::string& StoryModeHUD::getFont() const
136    {
137        return this->fontName_;
138    }
139
140    // Set the size of the Text
141    void StoryModeHUD::setTextSize(float size)
142    {
143        if (size <= 0.0f)
144        {
145            this->textSize_ = 0.05f;
146            orxout(internal_warning) << "StoryModeHUD: Non positive font size not allowed. Font size has been set to 0.05" << endl;
147            return;
148        }
149        this->textSize_ = size;
150       
151    }
152
153    // returns the Size of the Text
154    float StoryModeHUD::getTextSize() const
155    {
156        return this->textSize_;
157    }
158
159    // Tick: this is the most important function. It's recalled every frame and makes sure things happen on the screen.
160    void StoryModeHUD::tick(float dt)
161    {
162        SUPER(StoryModeHUD, tick, dt);
163
164        if(firstTick)
165            this->initialize();
166
167        // cam is the pointer which represents your camera
168        Camera* cam = CameraManager::getInstance().getActiveCamera();
169        if (cam == nullptr)
170            return;
171
172        // camTransform is a Matrix, which converts 3D world of the game into 2D on your screen
173        const Matrix4& camTransform = cam->getOgreCamera()->getProjectionMatrix() * cam->getOgreCamera()->getViewMatrix();
174
175        size_t i = 0;
176        for(NameableStaticEntity* planet : ObjectList<NameableStaticEntity>()){
177            if (i >= texts.size()) {
178                break;
179            }
180            Ogre::TextAreaOverlayElement* text = texts[i];
181            i++;
182
183            // Transform to screen coordinates
184            Vector3 pos = camTransform * planet->getWorldPosition();
185
186            // If you fly passed the description, it gets out of sight
187            if (!(pos.z > 1.0)){
188                   
189                // Position text
190                text->setLeft((pos.x+1)/2); // The (0,0) Coordinate is in the upper left corner.
191                text->setTop((-pos.y+1)/2);  // With those two calculations we set the desired positions
192
193                // Make sure the overlays are shown
194                text->show();
195            }
196        }
197    }
198}
Note: See TracBrowser for help on using the repository browser.