| [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: | 
|---|
| [10721] | 23 | *      Fabien Vultier | 
|---|
| [10688] | 24 | *   Co-authors: | 
|---|
| [10721] | 25 | *      ... | 
|---|
| [10688] | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "HUDWeapon.h" | 
|---|
|  | 30 |  | 
|---|
|  | 31 | #include "core/CoreIncludes.h" | 
|---|
|  | 32 | #include "core/XMLPort.h" | 
|---|
|  | 33 | #include "util/Convert.h" | 
|---|
|  | 34 | #include "core/class/Super.h" | 
|---|
|  | 35 |  | 
|---|
|  | 36 | namespace orxonox | 
|---|
|  | 37 | { | 
|---|
|  | 38 | RegisterClass(HUDWeapon); | 
|---|
|  | 39 |  | 
|---|
|  | 40 | HUDWeapon::HUDWeapon(Context* context) : OrxonoxOverlay(context) | 
|---|
|  | 41 | { | 
|---|
|  | 42 | RegisterObject(HUDWeapon); | 
|---|
|  | 43 |  | 
|---|
| [10721] | 44 | weaponModeHUDActualSize_ = Vector2(0.0f,0.0f); | 
|---|
|  | 45 |  | 
|---|
|  | 46 | weaponModes_ = NULL; | 
|---|
|  | 47 | weaponIndex_ = 0; | 
|---|
|  | 48 | hudWeaponModes_.clear(); | 
|---|
|  | 49 |  | 
|---|
| [10688] | 50 | overlayElement_ = static_cast<Ogre::PanelOverlayElement* >(Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", "HUDWeapon" + getUniqueNumberString())); | 
|---|
|  | 51 | overlayElement_->setMaterialName("Orxonox/WSHUD_Weapon"); | 
|---|
|  | 52 | overlayElement_->setPosition(0.0f,0.0f); | 
|---|
|  | 53 | overlayElement_->setDimensions(1.0f,1.0f); | 
|---|
|  | 54 | this->background_->addChild(overlayElement_); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | HUDWeapon::~HUDWeapon() | 
|---|
|  | 58 | { | 
|---|
|  | 59 | if (this->isInitialized()) | 
|---|
|  | 60 | { | 
|---|
|  | 61 | destroyHUDChilds(); | 
|---|
|  | 62 | } | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 | void HUDWeapon::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
|  | 66 | { | 
|---|
|  | 67 | SUPER(HUDWeapon, XMLPort, xmlelement, mode); | 
|---|
|  | 68 |  | 
|---|
|  | 69 | /*XMLPortParam(HUDWeapons, "sensitivity", setRadarSensitivity, getRadarSensitivity, xmlelement, mode); | 
|---|
|  | 70 | XMLPortParam(HUDWeapons, "halfDotSizeDistance", setHalfDotSizeDistance, getHalfDotSizeDistance, xmlelement, mode);*/ | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 | void HUDWeapon::tick(float dt) | 
|---|
|  | 74 | { | 
|---|
|  | 75 | SUPER(HUDWeapon, tick, dt); | 
|---|
| [10721] | 76 |  | 
|---|
|  | 77 | if (!weapon_) | 
|---|
|  | 78 | { | 
|---|
|  | 79 | // TODO: destroy this HUD id the Weapon does no more exist. (Wehen the weak pointer is NULL) | 
|---|
|  | 80 | } | 
|---|
| [10688] | 81 | } | 
|---|
|  | 82 |  | 
|---|
|  | 83 | void HUDWeapon::positionChanged() | 
|---|
|  | 84 | { | 
|---|
|  | 85 | OrxonoxOverlay::positionChanged(); | 
|---|
|  | 86 |  | 
|---|
|  | 87 | positionHUDChilds(); | 
|---|
| [10721] | 88 | } | 
|---|
| [10688] | 89 |  | 
|---|
| [10721] | 90 | void HUDWeapon::sizeChanged() | 
|---|
|  | 91 | { | 
|---|
|  | 92 | OrxonoxOverlay::sizeChanged(); | 
|---|
|  | 93 |  | 
|---|
|  | 94 | positionHUDChilds(); | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [10688] | 97 | void HUDWeapon::changedOwner() | 
|---|
|  | 98 | { | 
|---|
|  | 99 | SUPER(HUDWeapon, changedOwner); | 
|---|
|  | 100 |  | 
|---|
|  | 101 | this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); | 
|---|
|  | 102 |  | 
|---|
|  | 103 | updateWeaponModeList(); | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | void HUDWeapon::changedOverlayGroup() | 
|---|
|  | 107 | { | 
|---|
|  | 108 | SUPER(HUDWeapon, changedOverlayGroup); | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | void HUDWeapon::changedVisibility() | 
|---|
|  | 112 | { | 
|---|
|  | 113 | SUPER(HUDWeapon, changedVisibility); | 
|---|
| [10794] | 114 |  | 
|---|
|  | 115 | bool visible = this->isVisible(); | 
|---|
|  | 116 |  | 
|---|
|  | 117 | for (std::vector<WeakPtr<HUDWeaponMode> >::iterator it = hudWeaponModes_.begin(); it != hudWeaponModes_.end(); ++it) | 
|---|
|  | 118 | { | 
|---|
| [11044] | 119 | (*it)->changedVisibility(); //inform all Child Overlays that our visibility has changed | 
|---|
| [10794] | 120 | (*it)->setVisible(visible); | 
|---|
|  | 121 | } | 
|---|
| [10688] | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | void HUDWeapon::changedName() | 
|---|
|  | 125 | { | 
|---|
|  | 126 | SUPER(HUDWeapon, changedName); | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
|  | 129 | void HUDWeapon::setWeapon(Weapon* weapon) | 
|---|
|  | 130 | { | 
|---|
|  | 131 | weapon_ = weapon; | 
|---|
|  | 132 |  | 
|---|
|  | 133 | if (!weapon_) | 
|---|
|  | 134 | { | 
|---|
|  | 135 | return; | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | updateWeaponModeList(); | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 | void HUDWeapon::updateWeaponModeList() | 
|---|
|  | 142 | { | 
|---|
| [11054] | 143 | if (owner_ == nullptr || weapon_ == nullptr) | 
|---|
| [10688] | 144 | { | 
|---|
|  | 145 | return; | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|
|  | 148 | destroyHUDChilds(); | 
|---|
|  | 149 |  | 
|---|
|  | 150 | weaponModes_ = weapon_->getAllWeaponmodes(); | 
|---|
|  | 151 |  | 
|---|
| [10721] | 152 | updateSize(); | 
|---|
| [10688] | 153 | createHUDChilds(); | 
|---|
| [10721] | 154 | positionHUDChilds(); | 
|---|
| [10688] | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 | void HUDWeapon::createHUDChilds() | 
|---|
|  | 158 | { | 
|---|
| [10721] | 159 | if (weaponModes_ == NULL) | 
|---|
|  | 160 | { | 
|---|
|  | 161 | return; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | int positionIndex = 0; | 
|---|
|  | 165 |  | 
|---|
| [10688] | 166 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = weaponModes_->begin(); it != weaponModes_->end(); ++it) | 
|---|
|  | 167 | { | 
|---|
|  | 168 | HUDWeaponMode* hudWeaponMode = new HUDWeaponMode(this->getContext()); | 
|---|
|  | 169 | hudWeaponMode->setOwner(owner_); | 
|---|
| [10721] | 170 | hudWeaponMode->setOverlayGroup(this->getOverlayGroup()); | 
|---|
| [11044] | 171 | hudWeaponMode->setVisible(this->isVisible()); | 
|---|
| [10688] | 172 | hudWeaponMode->setWeaponMode(it->second); | 
|---|
| [10721] | 173 | hudWeaponMode->setWeaponIndex(this->weaponIndex_); | 
|---|
|  | 174 | hudWeaponMode->setAspectCorrection(false); | 
|---|
|  | 175 | hudWeaponMode->setPickPoint(Vector2(0.0f,0.0f)); | 
|---|
|  | 176 |  | 
|---|
| [10688] | 177 | hudWeaponModes_.push_back(hudWeaponMode); | 
|---|
| [10721] | 178 |  | 
|---|
|  | 179 | ++ positionIndex; | 
|---|
|  | 180 | } | 
|---|
| [10688] | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | void HUDWeapon::positionHUDChilds() | 
|---|
|  | 184 | { | 
|---|
|  | 185 | int positionIndex = 0; | 
|---|
|  | 186 |  | 
|---|
|  | 187 | for (std::vector<WeakPtr<HUDWeaponMode> >::iterator it = hudWeaponModes_.begin(); it != hudWeaponModes_.end(); ++it) | 
|---|
|  | 188 | { | 
|---|
| [10739] | 189 | (*it)->setPositionOffset(this->positionOffset_); | 
|---|
| [10721] | 190 | (*it)->setWeaponModeIndex(positionIndex); | 
|---|
|  | 191 | (*it)->setWeaponIndex(this->weaponIndex_); | 
|---|
|  | 192 | (*it)->setWeaponModeHUDActualSize(this->weaponModeHUDActualSize_); | 
|---|
| [10688] | 193 |  | 
|---|
| [10721] | 194 | ++ positionIndex; | 
|---|
| [10688] | 195 | } | 
|---|
|  | 196 | } | 
|---|
|  | 197 |  | 
|---|
|  | 198 | void HUDWeapon::destroyHUDChilds() | 
|---|
|  | 199 | { | 
|---|
|  | 200 | for (std::vector<WeakPtr<HUDWeaponMode> >::iterator it = hudWeaponModes_.begin(); it != hudWeaponModes_.end(); ++it) | 
|---|
|  | 201 | { | 
|---|
|  | 202 | (*it)->destroy(); | 
|---|
|  | 203 | } | 
|---|
|  | 204 |  | 
|---|
|  | 205 | hudWeaponModes_.clear(); | 
|---|
|  | 206 | } | 
|---|
| [10721] | 207 |  | 
|---|
|  | 208 | void HUDWeapon::updateSize() | 
|---|
|  | 209 | { | 
|---|
|  | 210 | if (weaponModes_ != NULL) | 
|---|
|  | 211 | { | 
|---|
|  | 212 | this->setSize(Vector2(weaponModeHUDActualSize_.x,weaponModeHUDActualSize_.y*weaponModes_->size())); | 
|---|
|  | 213 | updatePosition(); | 
|---|
|  | 214 | } | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
|  | 217 | void HUDWeapon::updatePosition() | 
|---|
|  | 218 | { | 
|---|
|  | 219 | if (weaponModes_ != NULL) | 
|---|
|  | 220 | { | 
|---|
| [10739] | 221 | this->setPosition(Vector2(weaponModeHUDActualSize_.x*weaponIndex_,0.0f) + this->positionOffset_); | 
|---|
| [10721] | 222 | } | 
|---|
|  | 223 | } | 
|---|
| [10688] | 224 | } | 
|---|