| [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 |  | 
|---|
| [11795] | 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 |  | 
|---|
| [10688] | 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 |  | 
|---|
| [10721] | 52 |         weaponModeHUDActualSize_ = Vector2(0.0f,0.0f); | 
|---|
 | 53 |  | 
|---|
 | 54 |         weaponIndex_ = 0; | 
|---|
 | 55 |         hudWeaponModes_.clear(); | 
|---|
 | 56 |  | 
|---|
| [10688] | 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); | 
|---|
| [10721] | 83 |  | 
|---|
 | 84 |         if (!weapon_) | 
|---|
 | 85 |         { | 
|---|
| [11071] | 86 |             // TODO: destroy this HUD id the Weapon does no more exist. (Wehen the weak pointer is null) | 
|---|
| [10721] | 87 |         } | 
|---|
| [10688] | 88 |     }     | 
|---|
 | 89 |  | 
|---|
 | 90 |     void HUDWeapon::positionChanged() | 
|---|
 | 91 |     { | 
|---|
 | 92 |         OrxonoxOverlay::positionChanged(); | 
|---|
 | 93 |  | 
|---|
 | 94 |         positionHUDChilds(); | 
|---|
| [10721] | 95 |     } | 
|---|
| [10688] | 96 |  | 
|---|
| [10721] | 97 |     void HUDWeapon::sizeChanged() | 
|---|
 | 98 |     { | 
|---|
 | 99 |         OrxonoxOverlay::sizeChanged(); | 
|---|
 | 100 |  | 
|---|
 | 101 |         positionHUDChilds(); | 
|---|
 | 102 |     } | 
|---|
 | 103 |  | 
|---|
| [10688] | 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); | 
|---|
| [10794] | 121 |  | 
|---|
 | 122 |         bool visible = this->isVisible(); | 
|---|
 | 123 |  | 
|---|
| [11071] | 124 |         for (HUDWeaponMode* hudWeaponMode : hudWeaponModes_) | 
|---|
| [10794] | 125 |         { | 
|---|
| [11071] | 126 |             hudWeaponMode->changedVisibility(); //inform all Child Overlays that our visibility has changed | 
|---|
 | 127 |             hudWeaponMode->setVisible(visible); | 
|---|
| [10794] | 128 |         } | 
|---|
| [10688] | 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 |     { | 
|---|
| [11071] | 150 |         if (owner_ == nullptr || weapon_ == nullptr) | 
|---|
| [10688] | 151 |         { | 
|---|
 | 152 |             return; | 
|---|
 | 153 |         } | 
|---|
 | 154 |              | 
|---|
 | 155 |         destroyHUDChilds(); | 
|---|
 | 156 |  | 
|---|
| [10721] | 157 |         updateSize(); | 
|---|
| [10688] | 158 |         createHUDChilds(); | 
|---|
| [10721] | 159 |         positionHUDChilds(); | 
|---|
| [10688] | 160 |     }   | 
|---|
 | 161 |  | 
|---|
 | 162 |     void HUDWeapon::createHUDChilds() | 
|---|
 | 163 |     { | 
|---|
| [11071] | 164 |         if (weapon_ == nullptr) | 
|---|
| [10721] | 165 |         { | 
|---|
 | 166 |             return; | 
|---|
 | 167 |         } | 
|---|
 | 168 |  | 
|---|
 | 169 |         int positionIndex = 0; | 
|---|
 | 170 |  | 
|---|
| [11071] | 171 |         for (const auto& mapEntry : weapon_->getAllWeaponmodes()) | 
|---|
| [10688] | 172 |         { | 
|---|
 | 173 |             HUDWeaponMode* hudWeaponMode = new HUDWeaponMode(this->getContext()); | 
|---|
 | 174 |             hudWeaponMode->setOwner(owner_); | 
|---|
| [10721] | 175 |             hudWeaponMode->setOverlayGroup(this->getOverlayGroup()); | 
|---|
| [11044] | 176 |             hudWeaponMode->setVisible(this->isVisible()); | 
|---|
| [11071] | 177 |             hudWeaponMode->setWeaponMode(mapEntry.second); | 
|---|
| [10721] | 178 |             hudWeaponMode->setWeaponIndex(this->weaponIndex_);                     | 
|---|
 | 179 |             hudWeaponMode->setAspectCorrection(false); | 
|---|
 | 180 |             hudWeaponMode->setPickPoint(Vector2(0.0f,0.0f)); | 
|---|
 | 181 |  | 
|---|
| [10688] | 182 |             hudWeaponModes_.push_back(hudWeaponMode); | 
|---|
| [10721] | 183 |  | 
|---|
 | 184 |             ++ positionIndex; | 
|---|
 | 185 |         } | 
|---|
| [10688] | 186 |     }       | 
|---|
 | 187 |  | 
|---|
 | 188 |     void HUDWeapon::positionHUDChilds() | 
|---|
 | 189 |     { | 
|---|
 | 190 |         int positionIndex = 0; | 
|---|
 | 191 |  | 
|---|
| [11071] | 192 |         for (HUDWeaponMode* hudWeaponMode : hudWeaponModes_) | 
|---|
| [10688] | 193 |         { | 
|---|
| [11071] | 194 |             hudWeaponMode->setPositionOffset(this->positionOffset_); | 
|---|
 | 195 |             hudWeaponMode->setWeaponModeIndex(positionIndex); | 
|---|
 | 196 |             hudWeaponMode->setWeaponIndex(this->weaponIndex_); | 
|---|
 | 197 |             hudWeaponMode->setWeaponModeHUDActualSize(this->weaponModeHUDActualSize_); | 
|---|
| [10688] | 198 |  | 
|---|
| [10721] | 199 |             ++ positionIndex; | 
|---|
| [10688] | 200 |         } | 
|---|
 | 201 |     }   | 
|---|
 | 202 |  | 
|---|
 | 203 |     void HUDWeapon::destroyHUDChilds() | 
|---|
 | 204 |     { | 
|---|
| [11071] | 205 |         for (HUDWeaponMode* hudWeaponMode : hudWeaponModes_) | 
|---|
| [10688] | 206 |         { | 
|---|
| [11071] | 207 |             hudWeaponMode->destroy(); | 
|---|
| [10688] | 208 |         }  | 
|---|
 | 209 |  | 
|---|
 | 210 |         hudWeaponModes_.clear(); | 
|---|
 | 211 |     } | 
|---|
| [10721] | 212 |  | 
|---|
 | 213 |     void HUDWeapon::updateSize() | 
|---|
 | 214 |     { | 
|---|
| [11071] | 215 |         if (weapon_ != nullptr) | 
|---|
| [10721] | 216 |         { | 
|---|
| [11071] | 217 |             this->setSize(Vector2(weaponModeHUDActualSize_.x,weaponModeHUDActualSize_.y*weapon_->getAllWeaponmodes().size())); | 
|---|
| [10721] | 218 |             updatePosition(); | 
|---|
 | 219 |         }         | 
|---|
 | 220 |     } | 
|---|
 | 221 |  | 
|---|
 | 222 |     void HUDWeapon::updatePosition() | 
|---|
 | 223 |     { | 
|---|
| [11071] | 224 |         if (weapon_ != nullptr) | 
|---|
| [10721] | 225 |         { | 
|---|
| [10739] | 226 |             this->setPosition(Vector2(weaponModeHUDActualSize_.x*weaponIndex_,0.0f) + this->positionOffset_); | 
|---|
| [10721] | 227 |         }         | 
|---|
 | 228 |     }        | 
|---|
| [10688] | 229 | } | 
|---|