Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fabienHS15/src/modules/overlays/hud/HUDWeapon.cc @ 10721

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