Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10724 was 10724, checked in by fvultier, 9 years ago

The weapon system HUD rescales properly if the window size changes.

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