Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10724 was 10724, checked in by fvultier, 9 years ago

The weapon system HUD rescales properly if the window size changes.

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