]> git.localhorst.tv Git - l2e.git/blob - src/menu/EquipMenu.cpp
implemented equipment removing and dropping
[l2e.git] / src / menu / EquipMenu.cpp
1 /*
2  * EquipMenu.cpp
3  *
4  *  Created on: Nov 18, 2012
5  *      Author: holy
6  */
7
8 #include "EquipMenu.h"
9
10 #include "HeroStatus.h"
11 #include "PartyMenu.h"
12 #include "Resources.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/GameConfig.h"
16 #include "../common/GameState.h"
17 #include "../common/Hero.h"
18 #include "../common/Inventory.h"
19 #include "../common/Item.h"
20 #include "../common/Stats.h"
21 #include "../graphics/Font.h"
22 #include "../graphics/Frame.h"
23
24 using app::Input;
25 using common::Hero;
26 using common::Inventory;
27 using common::Item;
28 using common::Stats;
29 using geometry::Vector;
30 using graphics::Font;
31 using graphics::Frame;
32
33 namespace menu {
34
35 EquipMenu::EquipMenu(PartyMenu *parent, int cursor)
36 : parent(parent)
37 , cursor(cursor)
38 , actionMenu(*parent->Res().equipmentActionMenuProperties)
39 , equipmentMenu(*parent->Res().equipmentMenuProperties)
40 , inventoryMenu(*parent->Res().inventoryMenuProperties) {
41         actionMenu.Add(parent->Res().equipMenuEquipLabel, CHOICE_EQUIP);
42         actionMenu.Add(parent->Res().equipMenuStrongestLabel, CHOICE_STRONGEST);
43         actionMenu.Add(parent->Res().equipMenuRemoveLabel, CHOICE_REMOVE);
44         actionMenu.Add(parent->Res().equipMenuRemoveAllLabel, CHOICE_REMOVE_ALL);
45         actionMenu.Add(parent->Res().equipMenuDropLabel, CHOICE_DROP);
46
47         LoadEquipment();
48 }
49
50
51 void EquipMenu::OnEnterState(SDL_Surface *) {
52         equipmentMenu.SetInactive();
53 }
54
55 void EquipMenu::OnExitState(SDL_Surface *) {
56
57 }
58
59 void EquipMenu::OnResumeState(SDL_Surface *) {
60
61 }
62
63 void EquipMenu::OnPauseState(SDL_Surface *) {
64
65 }
66
67
68 void EquipMenu::OnResize(int width, int height) {
69
70 }
71
72
73 void EquipMenu::HandleEvents(const Input &input) {
74         if (actionMenu.IsActive()) {
75                 if (input.JustPressed(Input::PAD_UP)) {
76                         actionMenu.PreviousRow();
77                 }
78                 if (input.JustPressed(Input::PAD_DOWN)) {
79                         actionMenu.NextRow();
80                 }
81                 if (input.JustPressed(Input::ACTION_A)) {
82                         switch (actionMenu.Selected()) {
83                                 case CHOICE_EQUIP:
84                                         actionMenu.SetSelected();
85                                         equipmentMenu.SetActive();
86                                         break;
87                                 case CHOICE_STRONGEST:
88                                         // TODO
89                                         break;
90                                 case CHOICE_REMOVE:
91                                         actionMenu.SetSelected();
92                                         equipmentMenu.SetActive();
93                                         break;
94                                 case CHOICE_REMOVE_ALL:
95                                         RemoveAllEquipment();
96                                         break;
97                                 case CHOICE_DROP:
98                                         actionMenu.SetSelected();
99                                         equipmentMenu.SetActive();
100                                         break;
101                         }
102                 } else if (input.JustPressed(Input::ACTION_B)) {
103                         Ctrl().PopState();
104                 }
105         } else if (equipmentMenu.IsActive()) {
106                 if (input.JustPressed(Input::PAD_UP)) {
107                         equipmentMenu.PreviousRow();
108                 }
109                 if (input.JustPressed(Input::PAD_DOWN)) {
110                         equipmentMenu.NextRow();
111                 }
112                 if (input.JustPressed(Input::ACTION_B)) {
113                         equipmentMenu.SetInactive();
114                         actionMenu.SetActive();
115                 } else if (input.JustPressed(Input::ACTION_A)) {
116                         switch (actionMenu.Selected()) {
117                                 case CHOICE_EQUIP:
118                                         // TODO
119                                         break;
120                                 case CHOICE_STRONGEST:
121                                 case CHOICE_REMOVE_ALL:
122                                         // invalid state, recover
123                                         equipmentMenu.SetInactive();
124                                         actionMenu.SetActive();
125                                         break;
126                                 case CHOICE_REMOVE:
127                                         RemoveItem();
128                                         break;
129                                 case CHOICE_DROP:
130                                         DropItem();
131                                         break;
132                         }
133                 }
134         }
135 }
136
137 void EquipMenu::UpdateWorld(float deltaT) {
138
139 }
140
141 void EquipMenu::Render(SDL_Surface *screen) {
142         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
143         Vector<int> shoulderNavOffset(
144                         5 * parent->Res().statusFont->CharWidth(),
145                         parent->Res().statusFont->CharHeight());
146         Vector<int> statsOffset(
147                         4 * parent->Res().statusFont->CharWidth(),
148                         8 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
149         Vector<int> equipOffset(
150                         17 * parent->Res().statusFont->CharWidth(),
151                         4 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
152         Vector<int> menuOffset(
153                         15 * parent->Res().statusFont->CharWidth(),
154                         17 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
155
156         parent->RenderBackground(screen);
157         parent->Res().shoulderNav->Draw(screen, offset + shoulderNavOffset);
158         RenderStatus(screen, offset + parent->StatusOffset(0));
159         RenderStats(screen, offset + statsOffset);
160         RenderEquipmentMenu(screen, offset + equipOffset);
161         RenderActionMenu(screen, offset + menuOffset);
162 }
163
164 int EquipMenu::Width() const {
165         return parent->Width();
166 }
167
168 int EquipMenu::Height() const {
169         return parent->Height();
170 }
171
172 void EquipMenu::RenderStatus(SDL_Surface *screen, const Vector<int> &offset) const {
173         parent->GetHeroStatus(cursor).Render(screen, offset);
174 }
175
176 void EquipMenu::RenderStats(SDL_Surface *screen, const Vector<int> &offset) const {
177         const Stats &stats(GetHero().GetStats());
178         Vector<int> lineBreak(0, parent->Res().statusFont->CharHeight());
179
180         Vector<int> position(offset);
181         RenderStatsLine(parent->Res().atpLabel, stats.Attack(), screen, position);
182
183         position += lineBreak;
184         RenderStatsLine(parent->Res().dfpLabel, stats.Defense(), screen, position);
185
186         position += lineBreak;
187         RenderStatsLine(parent->Res().strLabel, stats.Strength(), screen, position);
188
189         position += lineBreak;
190         RenderStatsLine(parent->Res().aglLabel, stats.Agility(), screen, position);
191
192         position += lineBreak;
193         RenderStatsLine(parent->Res().intLabel, stats.Intelligence(), screen, position);
194
195         position += lineBreak;
196         RenderStatsLine(parent->Res().gutLabel, stats.Gut(), screen, position);
197
198         position += lineBreak;
199         RenderStatsLine(parent->Res().mgrLabel, stats.MagicResistance(), screen, position);
200 }
201
202 void EquipMenu::RenderStatsLine(const char *label, int number, SDL_Surface *screen, const Vector<int> &position) const {
203         const Font &font(*parent->Res().statusFont);
204         const Vector<int> numberOffset(4 * font.CharWidth(), 0);
205
206         font.DrawString(label, screen, position, 3);
207         font.DrawNumber(number, screen, position + numberOffset, 3);
208 }
209
210 void EquipMenu::RenderEquipmentMenu(SDL_Surface *screen, const Vector<int> &offset) const {
211         equipmentMenu.Draw(screen, offset);
212 }
213
214 void EquipMenu::RenderActionMenu(SDL_Surface *screen, const Vector<int> &offset) const {
215         const Font &font(*parent->Res().statusFont);
216         const Frame &frame(*parent->Res().statusFrame);
217         const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 2);
218
219         frame.Draw(screen, offset, 15 * font.CharWidth(), 10 * font.CharHeight());
220         actionMenu.Draw(screen, offset + menuOffset);
221 }
222
223 void EquipMenu::RenderInventoryMenu(SDL_Surface *screen, const Vector<int> &offset) const {
224
225 }
226
227
228 void EquipMenu::NextHero() {
229         cursor = (cursor + 1) % parent->Game().state->partySize;
230         LoadEquipment();
231 }
232
233 void EquipMenu::PreviousHero() {
234         cursor = (cursor + parent->Game().state->partySize - 1) % parent->Game().state->partySize;
235         LoadEquipment();
236 }
237
238 Hero &EquipMenu::GetHero() {
239         return *parent->Game().state->party[cursor];
240 }
241
242 const Hero &EquipMenu::GetHero() const {
243         return *parent->Game().state->party[cursor];
244 }
245
246
247 void EquipMenu::LoadEquipment() {
248         equipmentMenu.Clear();
249         if (GetHero().HasWeapon()) {
250                 equipmentMenu.Add(GetHero().Weapon()->Name(), GetHero().Weapon(), true, GetHero().Weapon()->MenuIcon());
251         } else {
252                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
253         }
254         if (GetHero().HasArmor()) {
255                 equipmentMenu.Add(GetHero().Armor()->Name(), GetHero().Armor(), true, GetHero().Armor()->MenuIcon());
256         } else {
257                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
258         }
259         if (GetHero().HasShield()) {
260                 equipmentMenu.Add(GetHero().Shield()->Name(), GetHero().Shield(), true, GetHero().Shield()->MenuIcon());
261         } else {
262                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
263         }
264         if (GetHero().HasHelmet()) {
265                 equipmentMenu.Add(GetHero().Helmet()->Name(), GetHero().Helmet(), true, GetHero().Helmet()->MenuIcon());
266         } else {
267                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
268         }
269         if (GetHero().HasRing()) {
270                 equipmentMenu.Add(GetHero().Ring()->Name(), GetHero().Ring(), true, GetHero().Ring()->MenuIcon());
271         } else {
272                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
273         }
274         if (GetHero().HasJewel()) {
275                 equipmentMenu.Add(GetHero().Jewel()->Name(), GetHero().Jewel(), true, GetHero().Jewel()->MenuIcon());
276         } else {
277                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
278         }
279 }
280
281 void EquipMenu::RemoveAllEquipment() {
282         Inventory &inv(parent->Game().state->inventory);
283         if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
284                 GetHero().RemoveWeapon();
285         }
286         if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
287                 GetHero().RemoveArmor();
288         }
289         if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
290                 GetHero().RemoveShield();
291         }
292         if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
293                 GetHero().RemoveHelmet();
294         }
295         if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
296                 GetHero().RemoveRing();
297         }
298         if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
299                 GetHero().RemoveJewel();
300         }
301         LoadEquipment();
302 }
303
304 void EquipMenu::RemoveItem() {
305         Inventory &inv(parent->Game().state->inventory);
306         switch (equipmentMenu.SelectedIndex()) {
307                 case 0:
308                         if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
309                                 GetHero().RemoveWeapon();
310                         }
311                         break;
312                 case 1:
313                         if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
314                                 GetHero().RemoveArmor();
315                         }
316                         break;
317                 case 2:
318                         if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
319                                 GetHero().RemoveShield();
320                         }
321                         break;
322                 case 3:
323                         if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
324                                 GetHero().RemoveHelmet();
325                         }
326                         break;
327                 case 4:
328                         if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
329                                 GetHero().RemoveRing();
330                         }
331                         break;
332                 case 5:
333                         if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
334                                 GetHero().RemoveJewel();
335                         }
336                         break;
337         }
338         LoadEquipment();
339 }
340
341 void EquipMenu::DropItem() {
342         switch (equipmentMenu.SelectedIndex()) {
343                 case 0:
344                         GetHero().RemoveWeapon();
345                         break;
346                 case 1:
347                         GetHero().RemoveArmor();
348                         break;
349                 case 2:
350                         GetHero().RemoveShield();
351                         break;
352                 case 3:
353                         GetHero().RemoveHelmet();
354                         break;
355                 case 4:
356                         GetHero().RemoveRing();
357                         break;
358                 case 5:
359                         GetHero().RemoveJewel();
360                         break;
361         }
362         LoadEquipment();
363 }
364
365 }