Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fabienHS15/src/modules/overlays/hud/HUDWeaponMode.cc @ 10721

Last change on this file since 10721 was 10721, checked in by fvultier, 9 years ago
File size: 10.1 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 *   Author:
23 *      Yuning Chai
24 *      Felix Schulthess
25 *   Co-authors:
26 *      Reto Grieder
27 *      Wolfgang Roenninger
28 *
29 */
30
31#include "HUDWeaponMode.h"
32
33#include <OgreOverlayManager.h>
34#include <OgrePanelOverlayElement.h>
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "tools/TextureGenerator.h"
39#include "weaponsystem/WeaponMode.h"
40#include "weaponsystem/Munition.h"
41#include "util/Convert.h"
42
43namespace orxonox
44{
45    RegisterClass(HUDWeaponMode);
46
47    HUDWeaponMode::HUDWeaponMode(Context* context) : OrxonoxOverlay(context)
48    {
49        RegisterObject(HUDWeaponMode);
50
51        weaponIndex_ = 0;
52        weaponModeIndex_ = 0;
53
54        overlayElementIcon_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
55        overlayElementIcon_->setPosition(0.0f,0.0f);
56        overlayElementIcon_->setDimensions(1.0f,1.0f);
57        this->background_->addChild(overlayElementIcon_);
58
59        overlayElementReplenish_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
60        overlayElementReplenish_->setPosition(0.0f,0.0f);
61        overlayElementReplenish_->setDimensions(1.0f,1.0f);
62        this->background_->addChild(overlayElementReplenish_);
63
64        overlayElementMunition_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
65        overlayElementMunition_->setPosition(0.0f,0.0f);
66        overlayElementMunition_->setDimensions(1.0f,1.0f);
67        this->background_->addChild(overlayElementMunition_);
68
69        overlayElementState_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeaponMode" + getUniqueNumberString()));
70        overlayElementState_->setPosition(0.0f,0.0f);
71        overlayElementState_->setDimensions(1.0f,1.0f);
72        this->background_->addChild(overlayElementState_);
73
74        overlayElementIcon_->show();
75        overlayElementReplenish_->show();
76        overlayElementMunition_->show();
77        overlayElementState_->show();
78
79        // Create two text overlays
80        this->textOverlayLeft_ = new OverlayText(this->getContext());
81        assert(this->textOverlayLeft_.get());
82        this->textOverlayLeft_->setCaption("???");
83        textOverlayLeft_->setPickPoint(Vector2(0.0f,0.0f));
84        textOverlayLeft_->setVisible(true);
85        textOverlayLeft_->setAlignment(OverlayText::Center);
86        textOverlayLeft_->setTextSize(0.02f);
87        textOverlayLeft_->setColour(ColourValue(0.21,0.70,0.21,1.0));
88        textOverlayLeft_->setAspectCorrection(false);
89       
90        this->textOverlayRight_ = new OverlayText(this->getContext());
91        assert(this->textOverlayLeft_.get());
92        this->textOverlayRight_->setCaption("???");
93        textOverlayRight_->setPickPoint(Vector2(0.0f,0.0f));
94        textOverlayRight_->setVisible(true);
95        textOverlayRight_->setAlignment(OverlayText::Center);
96        textOverlayRight_->setTextSize(0.02f);
97        textOverlayRight_->setColour(ColourValue(0.21,0.70,0.21,1.0));
98        textOverlayRight_->setAspectCorrection(false);
99
100        materialNameState_ = "Orxonox/WSHUD_Reloading";
101
102        overlayElementReplenish_->setMaterialName("Orxonox/WSHUD_Replenish");
103    }
104
105    HUDWeaponMode::~HUDWeaponMode()
106    {
107        if (this->isInitialized())
108        {
109            //this->textOverlay_->destroy();
110        }
111    }
112
113    void HUDWeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode)
114    {
115        SUPER(HUDWeaponMode, XMLPort, xmlelement, mode);
116    }
117
118    void HUDWeaponMode::tick(float dt)
119    {
120        SUPER(HUDWeaponMode, tick, dt);
121
122        if (this->owner_ && this->weaponMode_)
123        {
124            std::string lastMaterialNameState = materialNameState_;
125
126            if (weaponMode_->getReloading())
127            {
128                materialNameState_ = "Orxonox/WSHUD_Reloading";
129            }
130            else
131            {
132                materialNameState_ = "Orxonox/WSHUD_Ready";
133            }           
134
135            Munition* munition = this->weaponMode_->getMunition();
136
137            if (munition != NULL)
138            {
139                bool useSeparateMagazines = munition->getUseSeparateMagazines();
140                bool stackMunition = munition->getStackMunition();
141
142                if (!useSeparateMagazines && !stackMunition)
143                {
144                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
145                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
146                }
147                else if (!useSeparateMagazines && stackMunition)
148                {
149                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunition(weaponMode_)));
150                    this->textOverlayRight_->setCaption("");
151                }
152                else if (useSeparateMagazines && !stackMunition)
153                {
154                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
155                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
156                }
157
158                if (munition->getNumMunition(weaponMode_) == 0)
159                {
160                    materialNameState_ = "Orxonox/WSHUD_Empty";
161                }
162            }
163
164            if (munition->isA(Class(ReplenishingMunition)))
165            {
166                ReplenishingMunition* replenishingMunition = dynamic_cast<ReplenishingMunition*>(munition);
167
168                if (replenishingMunition->canAddMunition(1))
169                {
170                    float progress = 1.0f - replenishingMunition->getProgress();
171                    overlayElementReplenish_->setDimensions(1.0f,progress);                   
172                }
173                else
174                {
175                    overlayElementReplenish_->setDimensions(1.0f,1.0f);
176                }
177            }
178
179            if (materialNameState_ != lastMaterialNameState)
180            {
181                overlayElementState_->setMaterialName(materialNameState_);
182            }
183        }
184    }   
185
186    void HUDWeaponMode::positionChanged()
187    {
188        OrxonoxOverlay::positionChanged();
189
190        positionHUDChilds();
191    }
192
193    void HUDWeaponMode::sizeChanged()
194    {
195        OrxonoxOverlay::sizeChanged();
196
197        positionHUDChilds();
198    }
199
200    void HUDWeaponMode::changedOwner()
201    {
202        SUPER(HUDWeaponMode, changedOwner);
203
204        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
205    }
206
207    void HUDWeaponMode::changedOverlayGroup()
208    {
209        SUPER(HUDWeaponMode, changedOverlayGroup);
210
211        this->getOverlayGroup()->addElement(this->textOverlayLeft_.get());
212        this->getOverlayGroup()->addElement(this->textOverlayRight_.get());
213    }   
214
215    void HUDWeaponMode::changedVisibility()
216    {
217        SUPER(HUDWeaponMode, changedVisibility);
218
219        this->textOverlayLeft_->setVisible(this->isVisible());
220        this->textOverlayRight_->setVisible(this->isVisible());
221    }
222
223    void HUDWeaponMode::changedName()
224    {
225        SUPER(HUDWeaponMode, changedName);
226
227        //this->textOverlay_->setName(this->getName() + "text");
228    } 
229
230    void HUDWeaponMode::setWeaponMode(WeaponMode* weaponMode)
231    {
232        weaponMode_ = weaponMode;
233
234        if (!weaponMode_)
235        {
236            return;
237        }
238
239        std::string materialName = weaponMode_->getHUDImageString();
240
241        overlayElementIcon_->setMaterialName(materialName);
242
243        Munition* munition = this->weaponMode_->getMunition();
244
245        if (munition != NULL)
246        {
247            bool useSeparateMagazines = munition->getUseSeparateMagazines();
248            bool stackMunition = munition->getStackMunition();
249
250            if (!useSeparateMagazines && !stackMunition)
251            {
252                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionShare");
253            }
254            else if (!useSeparateMagazines && stackMunition)
255            {
256                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionStack");
257            }
258            else if (useSeparateMagazines && !stackMunition)
259            {
260                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionSeparate");
261            }
262        }
263    }
264
265    void HUDWeaponMode::positionHUDChilds()
266    {
267        Vector2 size = Vector2(getSize().x*45.0f/150.0f, getSize().y);
268        Vector2 offset1 = Vector2(getSize().x*82.5f/150.0f, 0.0f);
269        Vector2 offset2 = Vector2(getSize().x*127.5f/150.0f, 0.0f);
270
271        textOverlayLeft_->setPosition(getPosition() + offset1);
272        textOverlayRight_->setPosition(getPosition() + offset2);
273
274        textOverlayLeft_->setSize(size);
275        textOverlayRight_->setSize(size);
276
277        textOverlayLeft_->setTextSize(getSize().y);
278        textOverlayRight_->setTextSize(getSize().y);
279    }
280
281    void HUDWeaponMode::updateSize()
282    {
283        this->setSize(weaponModeHUDActualSize_);
284        updatePosition();
285    }
286
287    void HUDWeaponMode::updatePosition()
288    {
289        this->setPosition(Vector2(weaponModeHUDActualSize_.x*weaponIndex_,weaponModeHUDActualSize_.y*weaponModeIndex_));
290    }     
291}
Note: See TracBrowser for help on using the repository browser.