]> git.localhorst.tv Git - l2e.git/blob - src/menu/EquipMenu.cpp
30cc1798a70c9b55dca4f20ea3fa1ba1a74c24b4
[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                                         break;
89                                 case CHOICE_REMOVE:
90                                         actionMenu.SetSelected();
91                                         equipmentMenu.SetActive();
92                                         break;
93                                 case CHOICE_REMOVE_ALL:
94                                         RemoveAllEquipment();
95                                         break;
96                                 case CHOICE_DROP:
97                                         actionMenu.SetSelected();
98                                         equipmentMenu.SetActive();
99                                         break;
100                         }
101                 } else if (input.JustPressed(Input::ACTION_B)) {
102                         Ctrl().PopState();
103                 }
104         }
105 }
106
107 void EquipMenu::UpdateWorld(float deltaT) {
108
109 }
110
111 void EquipMenu::Render(SDL_Surface *screen) {
112         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
113         Vector<int> shoulderNavOffset(
114                         5 * parent->Res().statusFont->CharWidth(),
115                         parent->Res().statusFont->CharHeight());
116         Vector<int> statsOffset(
117                         4 * parent->Res().statusFont->CharWidth(),
118                         8 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
119         Vector<int> equipOffset(
120                         17 * parent->Res().statusFont->CharWidth(),
121                         4 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
122         Vector<int> menuOffset(
123                         15 * parent->Res().statusFont->CharWidth(),
124                         17 * parent->Res().statusFont->CharHeight() - parent->Res().statusFont->CharHeight() / 8);
125
126         parent->RenderBackground(screen);
127         parent->Res().shoulderNav->Draw(screen, offset + shoulderNavOffset);
128         RenderStatus(screen, offset + parent->StatusOffset(0));
129         RenderStats(screen, offset + statsOffset);
130         RenderEquipmentMenu(screen, offset + equipOffset);
131         RenderActionMenu(screen, offset + menuOffset);
132 }
133
134 int EquipMenu::Width() const {
135         return parent->Width();
136 }
137
138 int EquipMenu::Height() const {
139         return parent->Height();
140 }
141
142 void EquipMenu::RenderStatus(SDL_Surface *screen, const Vector<int> &offset) const {
143         parent->GetHeroStatus(cursor).Render(screen, offset);
144 }
145
146 void EquipMenu::RenderStats(SDL_Surface *screen, const Vector<int> &offset) const {
147         const Stats &stats(GetHero().GetStats());
148         Vector<int> lineBreak(0, parent->Res().statusFont->CharHeight());
149
150         Vector<int> position(offset);
151         RenderStatsLine(parent->Res().atpLabel, stats.Attack(), screen, position);
152
153         position += lineBreak;
154         RenderStatsLine(parent->Res().dfpLabel, stats.Defense(), screen, position);
155
156         position += lineBreak;
157         RenderStatsLine(parent->Res().strLabel, stats.Strength(), screen, position);
158
159         position += lineBreak;
160         RenderStatsLine(parent->Res().aglLabel, stats.Agility(), screen, position);
161
162         position += lineBreak;
163         RenderStatsLine(parent->Res().intLabel, stats.Intelligence(), screen, position);
164
165         position += lineBreak;
166         RenderStatsLine(parent->Res().gutLabel, stats.Gut(), screen, position);
167
168         position += lineBreak;
169         RenderStatsLine(parent->Res().mgrLabel, stats.MagicResistance(), screen, position);
170 }
171
172 void EquipMenu::RenderStatsLine(const char *label, int number, SDL_Surface *screen, const Vector<int> &position) const {
173         const Font &font(*parent->Res().statusFont);
174         const Vector<int> numberOffset(4 * font.CharWidth(), 0);
175
176         font.DrawString(label, screen, position, 3);
177         font.DrawNumber(number, screen, position + numberOffset, 3);
178 }
179
180 void EquipMenu::RenderEquipmentMenu(SDL_Surface *screen, const Vector<int> &offset) const {
181         equipmentMenu.Draw(screen, offset);
182 }
183
184 void EquipMenu::RenderActionMenu(SDL_Surface *screen, const Vector<int> &offset) const {
185         const Font &font(*parent->Res().statusFont);
186         const Frame &frame(*parent->Res().statusFrame);
187         const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 2);
188
189         frame.Draw(screen, offset, 15 * font.CharWidth(), 10 * font.CharHeight());
190         actionMenu.Draw(screen, offset + menuOffset);
191 }
192
193 void EquipMenu::RenderInventoryMenu(SDL_Surface *screen, const Vector<int> &offset) const {
194
195 }
196
197
198 void EquipMenu::NextHero() {
199         cursor = (cursor + 1) % parent->Game().state->partySize;
200         LoadEquipment();
201 }
202
203 void EquipMenu::PreviousHero() {
204         cursor = (cursor + parent->Game().state->partySize - 1) % parent->Game().state->partySize;
205         LoadEquipment();
206 }
207
208 Hero &EquipMenu::GetHero() {
209         return *parent->Game().state->party[cursor];
210 }
211
212 const Hero &EquipMenu::GetHero() const {
213         return *parent->Game().state->party[cursor];
214 }
215
216
217 void EquipMenu::LoadEquipment() {
218         equipmentMenu.Clear();
219         if (GetHero().HasWeapon()) {
220                 equipmentMenu.Add(GetHero().Weapon()->Name(), GetHero().Weapon(), true, GetHero().Weapon()->MenuIcon());
221         } else {
222                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
223         }
224         if (GetHero().HasArmor()) {
225                 equipmentMenu.Add(GetHero().Armor()->Name(), GetHero().Armor(), true, GetHero().Armor()->MenuIcon());
226         } else {
227                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
228         }
229         if (GetHero().HasShield()) {
230                 equipmentMenu.Add(GetHero().Shield()->Name(), GetHero().Shield(), true, GetHero().Shield()->MenuIcon());
231         } else {
232                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
233         }
234         if (GetHero().HasHelmet()) {
235                 equipmentMenu.Add(GetHero().Helmet()->Name(), GetHero().Helmet(), true, GetHero().Helmet()->MenuIcon());
236         } else {
237                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
238         }
239         if (GetHero().HasRing()) {
240                 equipmentMenu.Add(GetHero().Ring()->Name(), GetHero().Ring(), true, GetHero().Ring()->MenuIcon());
241         } else {
242                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
243         }
244         if (GetHero().HasJewel()) {
245                 equipmentMenu.Add(GetHero().Jewel()->Name(), GetHero().Jewel(), true, GetHero().Jewel()->MenuIcon());
246         } else {
247                 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
248         }
249 }
250
251 void EquipMenu::RemoveAllEquipment() {
252         Inventory &inv(parent->Game().state->inventory);
253         if (GetHero().HasWeapon() && inv.Add(GetHero().Weapon(), 1)) {
254                 GetHero().RemoveWeapon();
255         }
256         if (GetHero().HasArmor() && inv.Add(GetHero().Armor(), 1)) {
257                 GetHero().RemoveArmor();
258         }
259         if (GetHero().HasShield() && inv.Add(GetHero().Shield(), 1)) {
260                 GetHero().RemoveShield();
261         }
262         if (GetHero().HasHelmet() && inv.Add(GetHero().Helmet(), 1)) {
263                 GetHero().RemoveHelmet();
264         }
265         if (GetHero().HasRing() && inv.Add(GetHero().Ring(), 1)) {
266                 GetHero().RemoveRing();
267         }
268         if (GetHero().HasJewel() && inv.Add(GetHero().Jewel(), 1)) {
269                 GetHero().RemoveJewel();
270         }
271         LoadEquipment();
272 }
273
274 }