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