/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * ... * */ #include "HUDChargeBar.h" #include "util/Convert.h" #include "core/CoreIncludes.h" #include "core/XMLPort.h" #include "worldentities/pawns/Pawn.h" #include "overlays/OverlayGroup.h" namespace orxonox { RegisterClass(HUDChargeBar); HUDChargeBar::HUDChargeBar(Context* context) : HUDBar(context) { RegisterObject(HUDChargeBar); this->owner_ = nullptr; this->bUseBarColour_ = false; this->textOffset_ = Vector2(0.0f, 0.0f); this->textScale_ = 1.0f; this->correspondingMode_ = nullptr; this->setIconPosition(Vector2(0.05f,0.5f)); this->setIconDimensions(Vector2(0.1f,0.5f)); this->textoverlay_ = new OverlayText(this->getContext()); assert(this->textoverlay_.get()); this->textoverlay_->setCaption(""); this->textoverlay_->setAspectCorrection(false); positionText(); } HUDChargeBar::~HUDChargeBar() { if (this->isInitialized()) { this->textoverlay_->destroy(); this->textoverlay_ = nullptr; } } void HUDChargeBar::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(HUDChargeBar, XMLPort, xmlelement, mode); XMLPortParam(HUDChargeBar, "showtext", setTextVisible, getTextVisible, xmlelement, mode).defaultValues(true); XMLPortParam(HUDChargeBar, "textfont", setTextFont, getTextFont, xmlelement, mode).defaultValues("Monofur"); XMLPortParam(HUDChargeBar, "textusebarcolour", setTextUseBarColour, getTextUseBarColour, xmlelement, mode).defaultValues(false); XMLPortParam(HUDChargeBar, "textcolour", setTextColour, getTextColour, xmlelement, mode).defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0)); XMLPortParam(HUDChargeBar, "textalign", setTextAlignmentString, getTextAlignmentString, xmlelement, mode).defaultValues("left"); XMLPortParam(HUDChargeBar, "textoffset", setTextOffset, getTextOffset, xmlelement, mode).defaultValues(Vector2::ZERO); XMLPortParam(HUDChargeBar, "textscale", setTextScale, getTextScale, xmlelement, mode).defaultValues(1.0f); XMLPortParam(HUDChargeBar, "textpickpoint", setTextPickPoint, getTextPickPoint, xmlelement, mode).defaultValues(Vector2::ZERO); XMLPortParam(HUDChargeBar, "textrotation", setTextRotation, getTextRotation, xmlelement, mode).defaultValues(0.0f); XMLPortParam(HUDChargeBar, "textspacewidth", setTextSpaceWidth, getTextSpaceWidth, xmlelement, mode).defaultValues(true); } void HUDChargeBar::tick(float dt) { SUPER(HUDChargeBar, tick, dt); if (this->owner_) { if(correspondingMode_ != nullptr){ this->setValue( correspondingMode_->getCharges() * 1.0f / correspondingMode_->getMaxCharges() ); this->textoverlay_->setCaption(multi_cast(static_cast(this->owner_->getHealth()))); } } else { this->setValue(0); this->textoverlay_->setCaption("0"); } if (this->bUseBarColour_) this->textoverlay_->setColour(this->getCurrentBarColour()); } void HUDChargeBar::changedOwner() { SUPER(HUDChargeBar, changedOwner); this->owner_ = orxonox_cast(this->getOwner()); if(this->owner_ == nullptr){ return; } const WeaponSystem* weaponsystem = owner_->getWeaponSystem(); if(weaponsystem == nullptr){ return; } const std::vector weaponpacklist = weaponsystem->getAllWeaponPacks(); for(WeaponPack* weaponpack : weaponpacklist){ const std::vector weaponlist = weaponpack->getAllWeapons(); for(Weapon* weapon : weaponlist){ const std::multimap weaponmodelist = weapon->getAllWeaponmodes(); for(std::multimap::const_iterator it = weaponmodelist.begin(); it != weaponmodelist.end(); ++it){ if(it->second->isChargeable()){ this->correspondingMode_ = it->second; return; } } } } } void HUDChargeBar::changedOverlayGroup() { SUPER(HUDChargeBar, changedOverlayGroup); this->getOverlayGroup()->addElement(this->textoverlay_.get()); } void HUDChargeBar::changedVisibility() { SUPER(HUDChargeBar, changedVisibility); this->textoverlay_->setVisible(this->isVisible()); } void HUDChargeBar::changedName() { SUPER(HUDChargeBar, changedName); this->textoverlay_->setName(this->getName() + "text"); } void HUDChargeBar::setTextColour(const ColourValue& colour) { this->textColour_ = colour; if (!this->bUseBarColour_) this->textoverlay_->setColour(colour); } void HUDChargeBar::setTextUseBarColour(bool bUseBarColour) { this->bUseBarColour_ = bUseBarColour; if (!bUseBarColour) this->textoverlay_->setColour(this->textColour_); } void HUDChargeBar::positionText() { this->textoverlay_->setPosition(this->getPosition() + this->textOffset_*this->getActualSize()); this->textoverlay_->setTextSize(this->getActualSize().y*this->textScale_); } void HUDChargeBar::positionChanged() { HUDBar::positionChanged(); positionText(); } void HUDChargeBar::sizeChanged() { HUDBar::sizeChanged(); positionText(); } }