| 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 "HUDWeaponSystem.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/XMLPort.h" | 
|---|
| 33 | #include "weaponsystem/WeaponSystem.h" | 
|---|
| 34 | #include "weaponsystem/WeaponPack.h" | 
|---|
| 35 | #include "weaponsystem/Weapon.h" | 
|---|
| 36 | #include "util/Convert.h" | 
|---|
| 37 | #include "core/class/Super.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 | RegisterClass(HUDWeaponSystem); | 
|---|
| 42 |  | 
|---|
| 43 | HUDWeaponSystem::HUDWeaponSystem(Context* context) : OrxonoxOverlay(context) | 
|---|
| 44 | { | 
|---|
| 45 | RegisterObject(HUDWeaponSystem); | 
|---|
| 46 |  | 
|---|
| 47 | weaponModeHUDSize_ = Vector2(0.0f,0.0f); | 
|---|
| 48 | weaponModeHUDActualSize_ = Vector2(0.0f,0.0f); | 
|---|
| 49 |  | 
|---|
| 50 | weapons_.clear(); | 
|---|
| 51 | hudWeapons_.clear(); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | HUDWeaponSystem::~HUDWeaponSystem() | 
|---|
| 55 | { | 
|---|
| 56 | if (this->isInitialized()) | 
|---|
| 57 | { | 
|---|
| 58 | destroyHUDChilds(); | 
|---|
| 59 | } | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void HUDWeaponSystem::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 63 | { | 
|---|
| 64 | SUPER(HUDWeaponSystem, XMLPort, xmlelement, mode); | 
|---|
| 65 |  | 
|---|
| 66 | XMLPortParam(HUDWeaponSystem, "weaponModeHUDSize", setWeaponModeHUDSize, getWeaponModeHUDSize, xmlelement, mode); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void HUDWeaponSystem::positionChanged() | 
|---|
| 70 | { | 
|---|
| 71 | OrxonoxOverlay::positionChanged(); | 
|---|
| 72 |  | 
|---|
| 73 | positionHUDChilds(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void HUDWeaponSystem::sizeChanged() | 
|---|
| 77 | { | 
|---|
| 78 | OrxonoxOverlay::sizeChanged(); | 
|---|
| 79 |  | 
|---|
| 80 | weaponModeHUDActualSize_ = this->getActualSize(); | 
|---|
| 81 |  | 
|---|
| 82 | positionHUDChilds(); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void HUDWeaponSystem::changedOwner() | 
|---|
| 86 | { | 
|---|
| 87 | SUPER(HUDWeaponSystem, changedOwner); | 
|---|
| 88 |  | 
|---|
| 89 | this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); | 
|---|
| 90 |  | 
|---|
| 91 | updateWeaponList(); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void HUDWeaponSystem::changedOverlayGroup() | 
|---|
| 95 | { | 
|---|
| 96 | SUPER(HUDWeaponSystem, changedOverlayGroup); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void HUDWeaponSystem::changedVisibility() | 
|---|
| 100 | { | 
|---|
| 101 | SUPER(HUDWeaponSystem, changedVisibility); | 
|---|
| 102 |  | 
|---|
| 103 | bool visible = this->isVisible(); | 
|---|
| 104 |  | 
|---|
| 105 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) | 
|---|
| 106 | { | 
|---|
| 107 | (*it)->changedVisibility(); //inform all Child Overlays that our visibility has changed | 
|---|
| 108 | (*it)->setVisible(visible); | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void HUDWeaponSystem::changedName() | 
|---|
| 113 | { | 
|---|
| 114 | SUPER(HUDWeaponSystem, changedName); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void HUDWeaponSystem::updateWeaponList() | 
|---|
| 118 | { | 
|---|
| 119 | if (owner_ == nullptr) | 
|---|
| 120 | { | 
|---|
| 121 | return; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | weapons_.clear(); | 
|---|
| 125 |  | 
|---|
| 126 | destroyHUDChilds(); | 
|---|
| 127 |  | 
|---|
| 128 | if (owner_->getWeaponSystem()) | 
|---|
| 129 | { | 
|---|
| 130 | const std::vector<WeaponPack*>& weaponPacks = owner_->getWeaponSystem()->getAllWeaponPacks(); | 
|---|
| 131 |  | 
|---|
| 132 | for (std::vector<WeaponPack*>::const_iterator itPacks = weaponPacks.begin(); itPacks != weaponPacks.end(); ++itPacks) | 
|---|
| 133 | { | 
|---|
| 134 | const std::vector<Weapon*>& weapons = (*itPacks)->getAllWeapons(); | 
|---|
| 135 |  | 
|---|
| 136 | for (std::vector<Weapon*>::const_iterator itWeapons = weapons.begin(); itWeapons != weapons.end(); ++itWeapons) | 
|---|
| 137 | { | 
|---|
| 138 | this->weapons_.push_back(*itWeapons); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | createHUDChilds(); | 
|---|
| 143 | positionHUDChilds(); | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | void HUDWeaponSystem::createHUDChilds() | 
|---|
| 148 | { | 
|---|
| 149 | int positionIndex = 0; | 
|---|
| 150 |  | 
|---|
| 151 | for (std::vector<WeakPtr<Weapon> >::iterator it = weapons_.begin(); it != weapons_.end(); ++it) | 
|---|
| 152 | { | 
|---|
| 153 | HUDWeapon* hudWeapon = new HUDWeapon(this->getContext()); | 
|---|
| 154 | hudWeapon->setOwner(owner_); | 
|---|
| 155 | hudWeapon->setOverlayGroup(this->getOverlayGroup()); | 
|---|
| 156 | hudWeapon->setVisible(this->isVisible()); | 
|---|
| 157 | hudWeapon->setWeapon(*it); | 
|---|
| 158 | hudWeapon->setAspectCorrection(false); | 
|---|
| 159 | hudWeapon->setPickPoint(Vector2(0.0f,0.0f)); | 
|---|
| 160 |  | 
|---|
| 161 | hudWeapons_.push_back(hudWeapon); | 
|---|
| 162 |  | 
|---|
| 163 | ++ positionIndex; | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | void HUDWeaponSystem::positionHUDChilds() | 
|---|
| 168 | { | 
|---|
| 169 | int positionIndex = 0; | 
|---|
| 170 | Vector2 offset = this->getPosition(); | 
|---|
| 171 |  | 
|---|
| 172 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) | 
|---|
| 173 | { | 
|---|
| 174 | (*it)->setPositionOffset(offset); | 
|---|
| 175 | (*it)->setWeaponIndex(positionIndex); | 
|---|
| 176 | (*it)->setWeaponModeHUDActualSize(this->weaponModeHUDActualSize_); | 
|---|
| 177 |  | 
|---|
| 178 | ++ positionIndex; | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | void HUDWeaponSystem::destroyHUDChilds() | 
|---|
| 183 | { | 
|---|
| 184 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) | 
|---|
| 185 | { | 
|---|
| 186 | (*it)->destroy(); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | hudWeapons_.clear(); | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|