]> git.localhorst.tv Git - l2e.git/blob - src/menu/EquipMenu.cpp
df924b18a024ade156e66fdc6784c2184665b9df
[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         for (int i = 0; i < Hero::EQUIP_COUNT; ++i) {
250                 if (GetHero().Equipped(Hero::EquipSlot(i))) {
251                         const Item *item(GetHero().Equipment(Hero::EquipSlot(i)));
252                         equipmentMenu.Add(item->Name(), item, true, item->MenuIcon());
253                 } else {
254                         equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
255                 }
256         }
257 }
258
259 void EquipMenu::RemoveAllEquipment() {
260         Inventory &inv(parent->Game().state->inventory);
261         for (int i = 0; i < Hero::EQUIP_COUNT; ++i) {
262                 if (GetHero().Equipped(Hero::EquipSlot(i))
263                                 && inv.Add(GetHero().Equipment(Hero::EquipSlot(i)), 1)) {
264                         GetHero().RemoveEquipment(Hero::EquipSlot(i));
265                 }
266         }
267         LoadEquipment();
268 }
269
270 void EquipMenu::RemoveItem() {
271         Inventory &inv(parent->Game().state->inventory);
272         Hero::EquipSlot slot = Hero::EquipSlot(equipmentMenu.SelectedIndex());
273
274         if (GetHero().Equipped(slot) && inv.Add(GetHero().Equipment(slot), 1)) {
275                 GetHero().RemoveEquipment(slot);
276         }
277
278         LoadEquipment();
279 }
280
281 void EquipMenu::DropItem() {
282         GetHero().RemoveEquipment(Hero::EquipSlot(equipmentMenu.SelectedIndex()));
283         LoadEquipment();
284 }
285
286 }