Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sagerjFS16/src/modules/overlays/hud/HUDChargeBar.cc @ 11185

Last change on this file since 11185 was 11185, checked in by sagerj, 8 years ago

fix the charging during reloadtime, the hudchargebar working so far…

File size: 6.7 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "HUDChargeBar.h"
30
31#include "util/Convert.h"
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "worldentities/pawns/Pawn.h"
35#include "overlays/OverlayGroup.h"
36
37
38namespace orxonox
39{
40    RegisterClass(HUDChargeBar);
41
42    HUDChargeBar::HUDChargeBar(Context* context) : HUDBar(context)
43    {
44        RegisterObject(HUDChargeBar);
45
46        this->owner_ = nullptr;
47        this->bUseBarColour_ = false;
48        this->textOffset_ = Vector2(0.0f, 0.0f);
49        this->textScale_ = 1.0f;
50        this->correspondingMode_ = nullptr;
51
52        this->setIconPosition(Vector2(0.05f,0.5f));
53        this->setIconDimensions(Vector2(0.1f,0.5f));
54
55        this->textoverlay_ = new OverlayText(this->getContext());
56
57        assert(this->textoverlay_.get());
58
59        this->textoverlay_->setCaption("");
60        this->textoverlay_->setAspectCorrection(false);
61
62        positionText();
63    }
64
65    HUDChargeBar::~HUDChargeBar()
66    {
67        if (this->isInitialized())
68        {
69            this->textoverlay_->destroy();
70            this->textoverlay_ = nullptr;
71        }
72    }
73
74    void HUDChargeBar::XMLPort(Element& xmlelement, XMLPort::Mode mode)
75    {
76        SUPER(HUDChargeBar, XMLPort, xmlelement, mode);
77
78        XMLPortParam(HUDChargeBar, "showtext",          setTextVisible,          getTextVisible,          xmlelement, mode).defaultValues(true);
79        XMLPortParam(HUDChargeBar, "textfont",          setTextFont,             getTextFont,             xmlelement, mode).defaultValues("Monofur");
80        XMLPortParam(HUDChargeBar, "textusebarcolour",  setTextUseBarColour,     getTextUseBarColour,     xmlelement, mode).defaultValues(false);
81        XMLPortParam(HUDChargeBar, "textcolour",        setTextColour,           getTextColour,           xmlelement, mode).defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0));
82        XMLPortParam(HUDChargeBar, "textalign",         setTextAlignmentString,  getTextAlignmentString,  xmlelement, mode).defaultValues("left");
83        XMLPortParam(HUDChargeBar, "textoffset",        setTextOffset,           getTextOffset,           xmlelement, mode).defaultValues(Vector2::ZERO);
84        XMLPortParam(HUDChargeBar, "textscale",         setTextScale,            getTextScale,            xmlelement, mode).defaultValues(1.0f);
85        XMLPortParam(HUDChargeBar, "textpickpoint",     setTextPickPoint,        getTextPickPoint,        xmlelement, mode).defaultValues(Vector2::ZERO);
86        XMLPortParam(HUDChargeBar, "textrotation",      setTextRotation,         getTextRotation,         xmlelement, mode).defaultValues(0.0f);
87        XMLPortParam(HUDChargeBar, "textspacewidth",    setTextSpaceWidth,       getTextSpaceWidth,       xmlelement, mode).defaultValues(true);
88    }
89
90    void HUDChargeBar::tick(float dt)
91    {
92        SUPER(HUDChargeBar, tick, dt);
93
94        if (this->owner_)
95        {
96            if(correspondingMode_ != nullptr){
97                this->setValue( correspondingMode_->getCharges() * 1.0f / correspondingMode_->getMaxCharges() );
98                this->textoverlay_->setCaption(multi_cast<std::string>(static_cast<int>(this->owner_->getHealth())));
99            }
100
101           
102        }
103        else
104        {
105            this->setValue(0);
106            this->textoverlay_->setCaption("0");
107        }
108
109        if (this->bUseBarColour_)
110            this->textoverlay_->setColour(this->getCurrentBarColour());
111
112
113    }
114
115    void HUDChargeBar::changedOwner()
116    {
117        SUPER(HUDChargeBar, changedOwner);
118
119        this->owner_ = orxonox_cast<Pawn*>(this->getOwner());
120        if(this->owner_ == nullptr){
121            return;
122        }
123
124        const WeaponSystem* weaponsystem = owner_->getWeaponSystem();
125        if(weaponsystem == nullptr){
126            return;
127        }
128
129        const std::vector<WeaponPack*> weaponpacklist = weaponsystem->getAllWeaponPacks();
130        for(WeaponPack* weaponpack : weaponpacklist){
131            const std::vector<Weapon*> weaponlist = weaponpack->getAllWeapons();
132            for(Weapon* weapon : weaponlist){
133                const std::multimap<unsigned int, WeaponMode*> weaponmodelist = weapon->getAllWeaponmodes();
134                for(std::multimap<unsigned int, WeaponMode*>::const_iterator it = weaponmodelist.begin(); it != weaponmodelist.end(); ++it){
135                    if(it->second->isChargeable()){
136                        this->correspondingMode_ = it->second;
137                        return;
138                    }
139                }
140            }
141        }
142    }
143
144    void HUDChargeBar::changedOverlayGroup()
145    {
146        SUPER(HUDChargeBar, changedOverlayGroup);
147
148        this->getOverlayGroup()->addElement(this->textoverlay_.get());
149    }
150
151    void HUDChargeBar::changedVisibility()
152    {
153        SUPER(HUDChargeBar, changedVisibility);
154
155        this->textoverlay_->setVisible(this->isVisible());
156    }
157
158    void HUDChargeBar::changedName()
159    {
160        SUPER(HUDChargeBar, changedName);
161
162        this->textoverlay_->setName(this->getName() + "text");
163    }
164
165    void HUDChargeBar::setTextColour(const ColourValue& colour)
166    {
167        this->textColour_ = colour;
168        if (!this->bUseBarColour_)
169            this->textoverlay_->setColour(colour);
170    }
171
172    void HUDChargeBar::setTextUseBarColour(bool bUseBarColour)
173    {
174        this->bUseBarColour_ = bUseBarColour;
175        if (!bUseBarColour)
176            this->textoverlay_->setColour(this->textColour_);
177    }
178
179    void HUDChargeBar::positionText()
180    {
181        this->textoverlay_->setPosition(this->getPosition() + this->textOffset_*this->getActualSize());
182        this->textoverlay_->setTextSize(this->getActualSize().y*this->textScale_);
183    }
184
185    void HUDChargeBar::positionChanged()
186    {
187        HUDBar::positionChanged();
188        positionText();
189    }
190
191    void HUDChargeBar::sizeChanged()
192    {
193        HUDBar::sizeChanged();
194        positionText();
195    }   
196}
Note: See TracBrowser for help on using the repository browser.