[2369] | 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 "HUDHealthBar.h" |
---|
| 30 | |
---|
[3196] | 31 | #include "util/Convert.h" |
---|
[2369] | 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
[5735] | 34 | #include "worldentities/pawns/Pawn.h" |
---|
[2369] | 35 | #include "overlays/OverlayGroup.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[9667] | 39 | RegisterClass(HUDHealthBar); |
---|
[2369] | 40 | |
---|
[9667] | 41 | HUDHealthBar::HUDHealthBar(Context* context) : HUDBar(context) |
---|
[2369] | 42 | { |
---|
| 43 | RegisterObject(HUDHealthBar); |
---|
| 44 | |
---|
| 45 | this->owner_ = 0; |
---|
| 46 | this->bUseBarColour_ = false; |
---|
| 47 | |
---|
[9667] | 48 | this->textoverlay_ = new OverlayText(this->getContext()); |
---|
[2369] | 49 | |
---|
[6054] | 50 | assert(this->textoverlay_.get()); |
---|
[2369] | 51 | |
---|
| 52 | this->textoverlay_->setCaption(""); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | HUDHealthBar::~HUDHealthBar() |
---|
| 56 | { |
---|
| 57 | if (this->isInitialized()) |
---|
[6054] | 58 | { |
---|
[5929] | 59 | this->textoverlay_->destroy(); |
---|
[6054] | 60 | this->textoverlay_ = 0; |
---|
| 61 | } |
---|
[2369] | 62 | } |
---|
| 63 | |
---|
| 64 | void HUDHealthBar::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 65 | { |
---|
| 66 | SUPER(HUDHealthBar, XMLPort, xmlelement, mode); |
---|
| 67 | |
---|
| 68 | XMLPortParam(HUDHealthBar, "showtext", setTextVisible, getTextVisible, xmlelement, mode).defaultValues(true); |
---|
| 69 | XMLPortParam(HUDHealthBar, "textfont", setTextFont, getTextFont, xmlelement, mode).defaultValues("Monofur"); |
---|
| 70 | XMLPortParam(HUDHealthBar, "textusebarcolour", setTextUseBarColour, getTextUseBarColour, xmlelement, mode).defaultValues(false); |
---|
| 71 | XMLPortParam(HUDHealthBar, "textcolour", setTextColour, getTextColour, xmlelement, mode).defaultValues(ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
| 72 | XMLPortParam(HUDHealthBar, "textsize", setTextSize, getTextSize, xmlelement, mode).defaultValues(1.0f); |
---|
| 73 | XMLPortParam(HUDHealthBar, "textalign", setTextAlignmentString, getTextAlignmentString, xmlelement, mode).defaultValues("left"); |
---|
| 74 | XMLPortParam(HUDHealthBar, "textoffset", setTextOffset, getTextOffset, xmlelement, mode).defaultValues(Vector2::ZERO); |
---|
| 75 | XMLPortParam(HUDHealthBar, "textpickpoint", setTextPickPoint, getTextPickPoint, xmlelement, mode).defaultValues(Vector2::ZERO); |
---|
| 76 | XMLPortParam(HUDHealthBar, "textrotation", setTextRotation, getTextRotation, xmlelement, mode).defaultValues(0.0f); |
---|
| 77 | XMLPortParam(HUDHealthBar, "textcorrectaspect", setTextAspectCorrection, getTextAspectCorrection, xmlelement, mode).defaultValues(true); |
---|
| 78 | XMLPortParam(HUDHealthBar, "textspacewidth", setTextSpaceWidth, getTextSpaceWidth, xmlelement, mode).defaultValues(true); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void HUDHealthBar::tick(float dt) |
---|
| 82 | { |
---|
| 83 | SUPER(HUDHealthBar, tick, dt); |
---|
| 84 | |
---|
[10171] | 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
[2369] | 89 | if (this->owner_) |
---|
| 90 | { |
---|
| 91 | this->setValue(this->owner_->getHealth() / this->owner_->getInitialHealth()); |
---|
[3300] | 92 | this->textoverlay_->setCaption(multi_cast<std::string>(static_cast<int>(this->owner_->getHealth()))); |
---|
[2369] | 93 | } |
---|
[5929] | 94 | else |
---|
| 95 | { |
---|
| 96 | this->setValue(0); |
---|
| 97 | this->textoverlay_->setCaption("0"); |
---|
| 98 | } |
---|
[2369] | 99 | |
---|
| 100 | if (this->bUseBarColour_) |
---|
| 101 | this->textoverlay_->setColour(this->getCurrentBarColour()); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void HUDHealthBar::changedOwner() |
---|
| 105 | { |
---|
| 106 | SUPER(HUDHealthBar, changedOwner); |
---|
| 107 | |
---|
[3325] | 108 | this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); |
---|
[2369] | 109 | } |
---|
| 110 | |
---|
| 111 | void HUDHealthBar::changedOverlayGroup() |
---|
| 112 | { |
---|
| 113 | SUPER(HUDHealthBar, changedOverlayGroup); |
---|
| 114 | |
---|
[6054] | 115 | this->getOverlayGroup()->addElement(this->textoverlay_.get()); |
---|
[2369] | 116 | } |
---|
| 117 | |
---|
| 118 | void HUDHealthBar::changedVisibility() |
---|
| 119 | { |
---|
| 120 | SUPER(HUDHealthBar, changedVisibility); |
---|
| 121 | |
---|
| 122 | this->textoverlay_->setVisible(this->isVisible()); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void HUDHealthBar::changedName() |
---|
| 126 | { |
---|
| 127 | SUPER(HUDHealthBar, changedName); |
---|
| 128 | |
---|
| 129 | this->textoverlay_->setName(this->getName() + "text"); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void HUDHealthBar::setTextColour(const ColourValue& colour) |
---|
| 133 | { |
---|
| 134 | this->textColour_ = colour; |
---|
| 135 | if (!this->bUseBarColour_) |
---|
| 136 | this->textoverlay_->setColour(colour); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void HUDHealthBar::setTextUseBarColour(bool bUseBarColour) |
---|
| 140 | { |
---|
| 141 | this->bUseBarColour_ = bUseBarColour; |
---|
| 142 | if (!bUseBarColour) |
---|
| 143 | this->textoverlay_->setColour(this->textColour_); |
---|
| 144 | } |
---|
| 145 | } |
---|