4 * Created on: Oct 21, 2012
10 #include "ChangeHero.h"
11 #include "EquipMenu.h"
12 #include "InventoryMenu.h"
13 #include "Resources.h"
14 #include "SelectHero.h"
15 #include "SpellMenu.h"
16 #include "StatusMenu.h"
17 #include "../app/Application.h"
18 #include "../app/Input.h"
19 #include "../common/GameConfig.h"
20 #include "../common/GameState.h"
21 #include "../geometry/Vector.h"
22 #include "../graphics/Font.h"
23 #include "../graphics/Frame.h"
24 #include "../graphics/Texture.h"
27 using common::GameConfig;
28 using geometry::Vector;
32 PartyMenu::PartyMenu(GameConfig *game)
34 , mainMenu(*game->menuResources->mainMenuProperties) {
35 for (int i(0); i < 4; ++i) {
36 status[i].SetHero(game->state->party, i);
37 status[i].SetResources(game->menuResources);
39 statusPositions[0] = Vector<int>(0, 0);
40 statusPositions[1] = Vector<int>(status[0].Width(), 0);
41 statusPositions[2] = Vector<int>(0, status[0].Height());
42 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
44 mainMenu.Add(Res().mainMenuItemText, 0);
45 mainMenu.Add(Res().mainMenuStatusText, 4);
46 mainMenu.Add(Res().mainMenuSpellText, 1);
47 mainMenu.Add(Res().mainMenuChangeText, 5);
48 mainMenu.Add(Res().mainMenuCapsuleText, 2);
49 mainMenu.Add(Res().mainMenuConfigText, 6);
50 mainMenu.Add(Res().mainMenuEquipmentText, 3);
51 mainMenu.Add(Res().mainMenuScenarioText, 7);
54 PartyMenu::~PartyMenu() {
59 void PartyMenu::OnEnterState(SDL_Surface *) {
63 void PartyMenu::OnExitState(SDL_Surface *) {
67 void PartyMenu::OnResumeState(SDL_Surface *) {
71 void PartyMenu::OnPauseState(SDL_Surface *) {
76 void PartyMenu::OnResize(int width, int height) {
81 void PartyMenu::HandleEvents(const Input &input) {
82 if (input.JustPressed(Input::ACTION_B)) {
87 if (input.JustPressed(Input::PAD_UP)) {
88 mainMenu.PreviousRow();
89 } else if (input.JustPressed(Input::PAD_RIGHT)) {
91 } else if (input.JustPressed(Input::PAD_DOWN)) {
93 } else if (input.JustPressed(Input::PAD_LEFT)) {
94 mainMenu.PreviousItem();
97 if (input.JustPressed(Input::ACTION_A)) {
98 switch (mainMenu.Selected()) {
100 Ctrl().PushState(new InventoryMenu(this));
102 case MENU_ITEM_SPELL:
103 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
105 case MENU_ITEM_CAPSULE:
107 case MENU_ITEM_EQUIP:
108 Ctrl().PushState(new SelectHero(this, this, this, OnEquipSelect));
110 case MENU_ITEM_STATUS:
111 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
113 case MENU_ITEM_CHANGE:
114 Ctrl().PushState(new ChangeHero(this));
116 case MENU_ITEM_CONFIG:
118 case MENU_ITEM_SCENARIO:
126 void PartyMenu::UpdateWorld(float deltaT) {
130 void PartyMenu::Render(SDL_Surface *screen) {
131 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
133 RenderBackground(screen);
134 RenderHeros(screen, offset);
135 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
136 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
139 int PartyMenu::Width() const {
140 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
143 int PartyMenu::Height() const {
144 return 2 * Res().normalFont->CharHeight()
145 + 2 * status[0].Height()
146 + Res().normalFont->CharHeight()
147 + 8 * Res().normalFont->CharHeight()
148 + 5 * Res().normalFont->CharHeight()
149 + 2 * Res().normalFont->CharHeight();
152 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
153 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
156 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
157 for (int i(0); i < 4; ++i) {
158 status[i].Render(screen, offset + StatusOffset(i));
162 Vector<int> PartyMenu::StatusOffset(int index) const {
163 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
166 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
167 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
169 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
170 mainMenu.Draw(screen, offset + menuOffset);
173 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
174 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
176 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
177 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
179 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
180 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
182 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
183 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
185 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
186 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
187 if (game->state->time / 60 < 10) {
188 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
191 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
192 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
194 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
195 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
199 Resources &PartyMenu::Res() {
200 return *game->menuResources;
203 const Resources &PartyMenu::Res() const {
204 return *game->menuResources;
207 void PartyMenu::OnEquipSelect(void *ref, int index) {
208 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
209 self->Ctrl().ChangeState(
210 new EquipMenu(self, index));
213 void PartyMenu::OnSpellSelect(void *ref, int index) {
214 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
215 self->Ctrl().ChangeState(
216 new SpellMenu(self, index));
219 void PartyMenu::OnStatusSelect(void *ref, int index) {
220 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
221 self->Ctrl().ChangeState(
222 new StatusMenu(self, index));