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