4 * Created on: Oct 21, 2012
10 #include "ChangeHero.h"
11 #include "ConfigMenu.h"
12 #include "EquipMenu.h"
13 #include "InventoryMenu.h"
14 #include "Resources.h"
15 #include "ScenarioMenu.h"
16 #include "SelectHero.h"
17 #include "SpellMenu.h"
18 #include "StatusMenu.h"
19 #include "../app/Application.h"
20 #include "../app/Input.h"
21 #include "../common/GameConfig.h"
22 #include "../common/GameState.h"
23 #include "../geometry/Vector.h"
24 #include "../graphics/Font.h"
25 #include "../graphics/Frame.h"
26 #include "../graphics/Texture.h"
29 using common::GameConfig;
30 using geometry::Vector;
34 PartyMenu::PartyMenu(GameConfig *game)
36 , mainMenu(*game->menuResources->mainMenuProperties) {
37 for (int i(0); i < 4; ++i) {
38 status[i].SetHero(game->state->party, i);
39 status[i].SetResources(game->menuResources);
41 statusPositions[0] = Vector<int>(0, 0);
42 statusPositions[1] = Vector<int>(status[0].Width(), 0);
43 statusPositions[2] = Vector<int>(0, status[0].Height());
44 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
46 mainMenu.Add(Res().mainMenuItemText, 0);
47 mainMenu.Add(Res().mainMenuStatusText, 4);
48 mainMenu.Add(Res().mainMenuSpellText, 1);
49 mainMenu.Add(Res().mainMenuChangeText, 5);
50 mainMenu.Add(Res().mainMenuCapsuleText, 2);
51 mainMenu.Add(Res().mainMenuConfigText, 6);
52 mainMenu.Add(Res().mainMenuEquipmentText, 3);
53 mainMenu.Add(Res().mainMenuScenarioText, 7);
56 PartyMenu::~PartyMenu() {
61 void PartyMenu::OnEnterState(SDL_Surface *) {
65 void PartyMenu::OnExitState(SDL_Surface *) {
69 void PartyMenu::OnResumeState(SDL_Surface *) {
73 void PartyMenu::OnPauseState(SDL_Surface *) {
78 void PartyMenu::OnResize(int width, int height) {
83 void PartyMenu::HandleEvents(const Input &input) {
84 if (input.JustPressed(Input::ACTION_B)) {
89 if (input.JustPressed(Input::PAD_UP)) {
90 mainMenu.PreviousRow();
91 } else if (input.JustPressed(Input::PAD_RIGHT)) {
93 } else if (input.JustPressed(Input::PAD_DOWN)) {
95 } else if (input.JustPressed(Input::PAD_LEFT)) {
96 mainMenu.PreviousItem();
99 if (input.JustPressed(Input::ACTION_A)) {
100 switch (mainMenu.Selected()) {
102 Ctrl().PushState(new InventoryMenu(this));
104 case MENU_ITEM_SPELL:
105 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
107 case MENU_ITEM_CAPSULE:
109 case MENU_ITEM_EQUIP:
110 Ctrl().PushState(new SelectHero(this, this, this, OnEquipSelect));
112 case MENU_ITEM_STATUS:
113 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
115 case MENU_ITEM_CHANGE:
116 Ctrl().PushState(new ChangeHero(this));
118 case MENU_ITEM_CONFIG:
119 Ctrl().PushState(new ConfigMenu(this));
121 case MENU_ITEM_SCENARIO:
122 Ctrl().PushState(new ScenarioMenu(this));
130 void PartyMenu::UpdateWorld(float deltaT) {
134 void PartyMenu::Render(SDL_Surface *screen) {
135 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
137 RenderBackground(screen);
138 RenderHeros(screen, offset);
139 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
140 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
143 int PartyMenu::Width() const {
144 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
147 int PartyMenu::Height() const {
148 return 2 * Res().normalFont->CharHeight()
149 + 2 * status[0].Height()
150 + Res().normalFont->CharHeight()
151 + 8 * Res().normalFont->CharHeight()
152 + 5 * Res().normalFont->CharHeight()
153 + 2 * Res().normalFont->CharHeight();
156 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
157 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
160 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
161 for (int i(0); i < 4; ++i) {
162 status[i].Render(screen, offset + StatusOffset(i));
166 Vector<int> PartyMenu::StatusOffset(int index) const {
167 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
170 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
171 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
173 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
174 mainMenu.Draw(screen, offset + menuOffset);
177 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
178 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
180 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
181 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
183 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
184 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
186 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
187 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
189 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
190 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
191 if (game->state->time / 60 < 10) {
192 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
195 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
196 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
198 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
199 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
203 Resources &PartyMenu::Res() {
204 return *game->menuResources;
207 const Resources &PartyMenu::Res() const {
208 return *game->menuResources;
211 void PartyMenu::OnEquipSelect(void *ref, int index) {
212 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
213 self->Ctrl().ChangeState(
214 new EquipMenu(self, index));
217 void PartyMenu::OnSpellSelect(void *ref, int index) {
218 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
219 self->Ctrl().ChangeState(
220 new SpellMenu(self, index));
223 void PartyMenu::OnStatusSelect(void *ref, int index) {
224 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
225 self->Ctrl().ChangeState(
226 new StatusMenu(self, index));