4 * Created on: Oct 21, 2012
10 #include "ChangeHero.h"
11 #include "InventoryMenu.h"
12 #include "Resources.h"
13 #include "SelectHero.h"
14 #include "SpellMenu.h"
15 #include "StatusMenu.h"
16 #include "../app/Application.h"
17 #include "../app/Input.h"
18 #include "../common/GameConfig.h"
19 #include "../common/GameState.h"
20 #include "../geometry/Vector.h"
21 #include "../graphics/Font.h"
22 #include "../graphics/Frame.h"
23 #include "../graphics/Texture.h"
26 using common::GameConfig;
27 using geometry::Vector;
31 PartyMenu::PartyMenu(GameConfig *game)
33 , mainMenu(*game->menuResources->mainMenuProperties) {
34 for (int i(0); i < 4; ++i) {
35 status[i].SetHero(game->state->party, i);
36 status[i].SetResources(game->menuResources);
38 statusPositions[0] = Vector<int>(0, 0);
39 statusPositions[1] = Vector<int>(status[0].Width(), 0);
40 statusPositions[2] = Vector<int>(0, status[0].Height());
41 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
43 mainMenu.Add(Res().mainMenuItemText, 0);
44 mainMenu.Add(Res().mainMenuStatusText, 4);
45 mainMenu.Add(Res().mainMenuSpellText, 1);
46 mainMenu.Add(Res().mainMenuChangeText, 5);
47 mainMenu.Add(Res().mainMenuCapsuleText, 2);
48 mainMenu.Add(Res().mainMenuConfigText, 6);
49 mainMenu.Add(Res().mainMenuEquipmentText, 3);
50 mainMenu.Add(Res().mainMenuScenarioText, 7);
53 PartyMenu::~PartyMenu() {
58 void PartyMenu::OnEnterState(SDL_Surface *) {
62 void PartyMenu::OnExitState(SDL_Surface *) {
66 void PartyMenu::OnResumeState(SDL_Surface *) {
70 void PartyMenu::OnPauseState(SDL_Surface *) {
75 void PartyMenu::OnResize(int width, int height) {
80 void PartyMenu::HandleEvents(const Input &input) {
81 if (input.JustPressed(Input::ACTION_B)) {
86 if (input.JustPressed(Input::PAD_UP)) {
87 mainMenu.PreviousRow();
88 } else if (input.JustPressed(Input::PAD_RIGHT)) {
90 } else if (input.JustPressed(Input::PAD_DOWN)) {
92 } else if (input.JustPressed(Input::PAD_LEFT)) {
93 mainMenu.PreviousItem();
96 if (input.JustPressed(Input::ACTION_A)) {
97 switch (mainMenu.Selected()) {
99 Ctrl().PushState(new InventoryMenu(this));
101 case MENU_ITEM_SPELL:
102 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
104 case MENU_ITEM_CAPSULE:
106 case MENU_ITEM_EQUIP:
108 case MENU_ITEM_STATUS:
109 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
111 case MENU_ITEM_CHANGE:
112 Ctrl().PushState(new ChangeHero(this));
114 case MENU_ITEM_CONFIG:
116 case MENU_ITEM_SCENARIO:
124 void PartyMenu::UpdateWorld(float deltaT) {
128 void PartyMenu::Render(SDL_Surface *screen) {
129 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
131 RenderBackground(screen);
132 RenderHeros(screen, offset);
133 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
134 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
137 int PartyMenu::Width() const {
138 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
141 int PartyMenu::Height() const {
142 return 2 * Res().normalFont->CharHeight()
143 + 2 * status[0].Height()
144 + Res().normalFont->CharHeight()
145 + 8 * Res().normalFont->CharHeight()
146 + 5 * Res().normalFont->CharHeight()
147 + 2 * Res().normalFont->CharHeight();
150 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
151 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
154 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
155 for (int i(0); i < 4; ++i) {
156 status[i].Render(screen, offset + StatusOffset(i));
160 Vector<int> PartyMenu::StatusOffset(int index) const {
161 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
164 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
165 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
167 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
168 mainMenu.Draw(screen, offset + menuOffset);
171 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
172 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
174 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
175 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
177 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
178 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
180 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
181 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
183 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
184 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
185 if (game->state->time / 60 < 10) {
186 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
189 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
190 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
192 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
193 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
197 Resources &PartyMenu::Res() {
198 return *game->menuResources;
201 const Resources &PartyMenu::Res() const {
202 return *game->menuResources;
205 void PartyMenu::OnStatusSelect(void *ref, int index) {
206 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
207 self->Ctrl().ChangeState(
208 new StatusMenu(self, index));
211 void PartyMenu::OnSpellSelect(void *ref, int index) {
212 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
213 self->Ctrl().ChangeState(
214 new SpellMenu(self, index));