4 * Created on: Nov 18, 2012
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"
26 using common::Inventory;
29 using geometry::Vector;
31 using graphics::Frame;
35 EquipMenu::EquipMenu(PartyMenu *parent, int 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);
51 void EquipMenu::OnEnterState(SDL_Surface *) {
52 equipmentMenu.SetInactive();
55 void EquipMenu::OnExitState(SDL_Surface *) {
59 void EquipMenu::OnResumeState(SDL_Surface *) {
63 void EquipMenu::OnPauseState(SDL_Surface *) {
68 void EquipMenu::OnResize(int width, int height) {
73 void EquipMenu::HandleEvents(const Input &input) {
74 if (actionMenu.IsActive()) {
75 if (input.JustPressed(Input::PAD_UP)) {
76 actionMenu.PreviousRow();
78 if (input.JustPressed(Input::PAD_DOWN)) {
81 if (input.JustPressed(Input::ACTION_A)) {
82 switch (actionMenu.Selected()) {
84 actionMenu.SetSelected();
85 equipmentMenu.SetActive();
87 case CHOICE_STRONGEST:
91 actionMenu.SetSelected();
92 equipmentMenu.SetActive();
94 case CHOICE_REMOVE_ALL:
98 actionMenu.SetSelected();
99 equipmentMenu.SetActive();
102 } else if (input.JustPressed(Input::ACTION_B)) {
105 } else if (equipmentMenu.IsActive()) {
106 if (input.JustPressed(Input::PAD_UP)) {
107 equipmentMenu.PreviousRow();
109 if (input.JustPressed(Input::PAD_DOWN)) {
110 equipmentMenu.NextRow();
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()) {
120 case CHOICE_STRONGEST:
121 case CHOICE_REMOVE_ALL:
122 // invalid state, recover
123 equipmentMenu.SetInactive();
124 actionMenu.SetActive();
137 void EquipMenu::UpdateWorld(float deltaT) {
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);
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);
164 int EquipMenu::Width() const {
165 return parent->Width();
168 int EquipMenu::Height() const {
169 return parent->Height();
172 void EquipMenu::RenderStatus(SDL_Surface *screen, const Vector<int> &offset) const {
173 parent->GetHeroStatus(cursor).Render(screen, offset);
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());
180 Vector<int> position(offset);
181 RenderStatsLine(parent->Res().atpLabel, stats.Attack(), screen, position);
183 position += lineBreak;
184 RenderStatsLine(parent->Res().dfpLabel, stats.Defense(), screen, position);
186 position += lineBreak;
187 RenderStatsLine(parent->Res().strLabel, stats.Strength(), screen, position);
189 position += lineBreak;
190 RenderStatsLine(parent->Res().aglLabel, stats.Agility(), screen, position);
192 position += lineBreak;
193 RenderStatsLine(parent->Res().intLabel, stats.Intelligence(), screen, position);
195 position += lineBreak;
196 RenderStatsLine(parent->Res().gutLabel, stats.Gut(), screen, position);
198 position += lineBreak;
199 RenderStatsLine(parent->Res().mgrLabel, stats.MagicResistance(), screen, position);
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);
206 font.DrawString(label, screen, position, 3);
207 font.DrawNumber(number, screen, position + numberOffset, 3);
210 void EquipMenu::RenderEquipmentMenu(SDL_Surface *screen, const Vector<int> &offset) const {
211 equipmentMenu.Draw(screen, offset);
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);
219 frame.Draw(screen, offset, 15 * font.CharWidth(), 10 * font.CharHeight());
220 actionMenu.Draw(screen, offset + menuOffset);
223 void EquipMenu::RenderInventoryMenu(SDL_Surface *screen, const Vector<int> &offset) const {
228 void EquipMenu::NextHero() {
229 cursor = (cursor + 1) % parent->Game().state->partySize;
233 void EquipMenu::PreviousHero() {
234 cursor = (cursor + parent->Game().state->partySize - 1) % parent->Game().state->partySize;
238 Hero &EquipMenu::GetHero() {
239 return *parent->Game().state->party[cursor];
242 const Hero &EquipMenu::GetHero() const {
243 return *parent->Game().state->party[cursor];
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());
254 equipmentMenu.Add(parent->Res().noEquipmentText, 0, false);
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));
270 void EquipMenu::RemoveItem() {
271 Inventory &inv(parent->Game().state->inventory);
272 Hero::EquipSlot slot = Hero::EquipSlot(equipmentMenu.SelectedIndex());
274 if (GetHero().Equipped(slot) && inv.Add(GetHero().Equipment(slot), 1)) {
275 GetHero().RemoveEquipment(slot);
281 void EquipMenu::DropItem() {
282 GetHero().RemoveEquipment(Hero::EquipSlot(equipmentMenu.SelectedIndex()));