Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/overlays/hud/HUDWeaponMode.cc @ 12394

Last change on this file since 12394 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

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