Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS15/src/modules/overlays/hud/HUDWeaponMode.cc @ 11053

Last change on this file since 11053 was 11044, checked in by landauf, 9 years ago

fixed visibility of weapon hud:

  • propagate changedVisibility to all child overlays (necessary if the containing overlaygroup changes its visibility)
  • initialize the visibility of child overlays (necessary if the visibility of the weaponsystem is 'false' in the hud definition)
  • Property svn:eol-style set to native
File size: 10.1 KB
RevLine 
[10688]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:
[10724]23 *      Fabien Vultier
[10688]24 *   Co-authors:
[10724]25 *      ...
[10688]26 *
27 */
28
29#include "HUDWeaponMode.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "util/Convert.h"
[10724]34#include "core/class/Super.h"
[10688]35
36namespace orxonox
37{
38    RegisterClass(HUDWeaponMode);
39
40    HUDWeaponMode::HUDWeaponMode(Context* context) : OrxonoxOverlay(context)
41    {
42        RegisterObject(HUDWeaponMode);
43
[10721]44        weaponIndex_ = 0;
45        weaponModeIndex_ = 0;
46
[10688]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());
[10791]75        this->textOverlayLeft_->setCaption("?");
[10688]76        textOverlayLeft_->setPickPoint(Vector2(0.0f,0.0f));
77        textOverlayLeft_->setVisible(true);
[10721]78        textOverlayLeft_->setAlignment(OverlayText::Center);
[10688]79        textOverlayLeft_->setTextSize(0.02f);
80        textOverlayLeft_->setColour(ColourValue(0.21,0.70,0.21,1.0));
[10721]81        textOverlayLeft_->setAspectCorrection(false);
[10724]82        textOverlayLeft_->setZOrder(600);
[10688]83       
84        this->textOverlayRight_ = new OverlayText(this->getContext());
[10791]85        assert(this->textOverlayRight_.get());
86        this->textOverlayRight_->setCaption("?");
[10688]87        textOverlayRight_->setPickPoint(Vector2(0.0f,0.0f));
88        textOverlayRight_->setVisible(true);
[10721]89        textOverlayRight_->setAlignment(OverlayText::Center);
[10688]90        textOverlayRight_->setTextSize(0.02f);
91        textOverlayRight_->setColour(ColourValue(0.21,0.70,0.21,1.0));
[10721]92        textOverlayRight_->setAspectCorrection(false);
[10724]93        textOverlayRight_->setZOrder(600);
[10688]94
[10794]95        materialNameState_ = "";
[10688]96
97        overlayElementReplenish_->setMaterialName("Orxonox/WSHUD_Replenish");
98    }
99
100    HUDWeaponMode::~HUDWeaponMode()
101    {
102        if (this->isInitialized())
103        {
[10739]104            this->textOverlayLeft_->destroy();
105            this->textOverlayRight_->destroy();
[10688]106        }
107    }
108
[10721]109    void HUDWeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode)
110    {
111        SUPER(HUDWeaponMode, XMLPort, xmlelement, mode);
112    }
113
[10688]114    void HUDWeaponMode::tick(float dt)
115    {
116        SUPER(HUDWeaponMode, tick, dt);
117
118        if (this->owner_ && this->weaponMode_)
119        {
120            std::string lastMaterialNameState = materialNameState_;
121
122            if (weaponMode_->getReloading())
123            {
124                materialNameState_ = "Orxonox/WSHUD_Reloading";
125            }
126            else
127            {
128                materialNameState_ = "Orxonox/WSHUD_Ready";
129            }           
130
131            Munition* munition = this->weaponMode_->getMunition();
132
133            if (munition != NULL)
134            {
[10724]135                MunitionDeployment::Value deployment = munition->getMunitionDeployment();
[10688]136
[10724]137                if (deployment == MunitionDeployment::Share)
[10688]138                {
139                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
140                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
141                }
[10724]142                else if (deployment == MunitionDeployment::Stack)
[10688]143                {
144                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunition(weaponMode_)));
145                    this->textOverlayRight_->setCaption("");
146                }
[10724]147                else if (deployment == MunitionDeployment::Separate)
[10688]148                {
149                    this->textOverlayLeft_->setCaption(multi_cast<std::string>(munition->getNumMunitionInCurrentMagazine(weaponMode_)));
150                    this->textOverlayRight_->setCaption(multi_cast<std::string>(munition->getNumMagazines()));
151                }
152
153                if (munition->getNumMunition(weaponMode_) == 0)
154                {
155                    materialNameState_ = "Orxonox/WSHUD_Empty";
156                }
157
[10791]158                if (munition->isA(Class(ReplenishingMunition)))
159                {
160                    ReplenishingMunition* replenishingMunition = dynamic_cast<ReplenishingMunition*>(munition);
[10688]161
[10878]162                    if (replenishingMunition->canAddMagazines(1))
[10791]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                    }
[10688]171                }
172            }
173
174            if (materialNameState_ != lastMaterialNameState)
175            {
176                overlayElementState_->setMaterialName(materialNameState_);
177            }
178        }
179    }   
180
181    void HUDWeaponMode::positionChanged()
182    {
183        OrxonoxOverlay::positionChanged();
184
185        positionHUDChilds();
[10721]186    }
[10688]187
[10721]188    void HUDWeaponMode::sizeChanged()
189    {
190        OrxonoxOverlay::sizeChanged();
191
192        positionHUDChilds();
193    }
194
[10688]195    void HUDWeaponMode::changedOwner()
196    {
197        SUPER(HUDWeaponMode, changedOwner);
198
199        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
[10794]200
[10795]201        materialNameState_ = ""; // Needed to set the material in the tick after a change of the owner.
[10688]202    }
203
204    void HUDWeaponMode::changedOverlayGroup()
205    {
206        SUPER(HUDWeaponMode, changedOverlayGroup);
207
208        this->getOverlayGroup()->addElement(this->textOverlayLeft_.get());
209        this->getOverlayGroup()->addElement(this->textOverlayRight_.get());
210    }   
211
212    void HUDWeaponMode::changedVisibility()
213    {
214        SUPER(HUDWeaponMode, changedVisibility);
215
[10794]216        bool visible = this->isVisible();
217
218        this->textOverlayLeft_->setVisible(visible);
219        this->textOverlayRight_->setVisible(visible);
[10688]220    }
221
222    void HUDWeaponMode::changedName()
223    {
224        SUPER(HUDWeaponMode, changedName);
225
226        //this->textOverlay_->setName(this->getName() + "text");
227    } 
228
229    void HUDWeaponMode::setWeaponMode(WeaponMode* weaponMode)
230    {
231        weaponMode_ = weaponMode;
232
233        if (!weaponMode_)
234        {
235            return;
236        }
237
238        std::string materialName = weaponMode_->getHUDImageString();
239
240        overlayElementIcon_->setMaterialName(materialName);
241
242        Munition* munition = this->weaponMode_->getMunition();
243
244        if (munition != NULL)
245        {
[10724]246            MunitionDeployment::Value deployment = munition->getMunitionDeployment();
[10688]247
[10724]248            if (deployment == MunitionDeployment::Share)
[10688]249            {
250                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionShare");
251            }
[10724]252            else if (deployment == MunitionDeployment::Stack)
[10688]253            {
254                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionStack");
255            }
[10724]256            else if (deployment == MunitionDeployment::Separate)
[10688]257            {
258                overlayElementMunition_->setMaterialName("Orxonox/WSHUD_MunitionSeparate");
259            }
[10721]260        }
[10688]261    }
262
263    void HUDWeaponMode::positionHUDChilds()
264    {
[10721]265        Vector2 size = Vector2(getSize().x*45.0f/150.0f, getSize().y);
266        Vector2 offset1 = Vector2(getSize().x*82.5f/150.0f, 0.0f);
267        Vector2 offset2 = Vector2(getSize().x*127.5f/150.0f, 0.0f);
268
269        textOverlayLeft_->setPosition(getPosition() + offset1);
270        textOverlayRight_->setPosition(getPosition() + offset2);
271
272        textOverlayLeft_->setSize(size);
273        textOverlayRight_->setSize(size);
274
275        textOverlayLeft_->setTextSize(getSize().y);
276        textOverlayRight_->setTextSize(getSize().y);
277    }
278
279    void HUDWeaponMode::updateSize()
280    {
281        this->setSize(weaponModeHUDActualSize_);
282        updatePosition();
283    }
284
285    void HUDWeaponMode::updatePosition()
286    {
[10739]287        this->setPosition(Vector2(weaponModeHUDActualSize_.x*weaponIndex_,weaponModeHUDActualSize_.y*weaponModeIndex_) + this->positionOffset_);
[10721]288    }     
[11044]289}
Note: See TracBrowser for help on using the repository browser.