3 #include "CapsuleMenu.h"
4 #include "ChangeHero.h"
5 #include "ConfigMenu.h"
7 #include "InventoryMenu.h"
9 #include "ScenarioMenu.h"
10 #include "SelectHero.h"
11 #include "SpellMenu.h"
12 #include "StatusMenu.h"
13 #include "../app/Application.h"
14 #include "../app/Input.h"
15 #include "../common/GameConfig.h"
16 #include "../common/GameState.h"
17 #include "../geometry/Vector.h"
18 #include "../graphics/Font.h"
19 #include "../graphics/Frame.h"
20 #include "../graphics/Texture.h"
23 using common::GameConfig;
24 using geometry::Vector;
28 PartyMenu::PartyMenu(GameConfig *game)
30 , mainMenu(*game->menuResources->mainMenuProperties) {
31 for (int i(0); i < 4; ++i) {
32 status[i].SetHero(game->state->party, i);
33 status[i].SetResources(game->menuResources);
35 statusPositions[0] = Vector<int>(0, 0);
36 statusPositions[1] = Vector<int>(status[0].Width(), 0);
37 statusPositions[2] = Vector<int>(0, status[0].Height());
38 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
40 mainMenu.Add(Res().mainMenuItemText, 0);
41 mainMenu.Add(Res().mainMenuStatusText, 4);
42 mainMenu.Add(Res().mainMenuSpellText, 1);
43 mainMenu.Add(Res().mainMenuChangeText, 5);
44 mainMenu.Add(Res().mainMenuCapsuleText, 2, game->state->capsule);
45 mainMenu.Add(Res().mainMenuConfigText, 6);
46 mainMenu.Add(Res().mainMenuEquipmentText, 3);
47 mainMenu.Add(Res().mainMenuScenarioText, 7);
50 PartyMenu::~PartyMenu() {
55 void PartyMenu::OnEnterState(SDL_Surface *) {
59 void PartyMenu::OnExitState(SDL_Surface *) {
63 void PartyMenu::OnResumeState(SDL_Surface *) {
67 void PartyMenu::OnPauseState(SDL_Surface *) {
72 void PartyMenu::OnResize(int width, int height) {
77 void PartyMenu::HandleEvents(const Input &input) {
78 if (input.JustPressed(Input::ACTION_B)) {
83 if (input.JustPressed(Input::PAD_UP)) {
84 mainMenu.PreviousRow();
85 } else if (input.JustPressed(Input::PAD_RIGHT)) {
87 } else if (input.JustPressed(Input::PAD_DOWN)) {
89 } else if (input.JustPressed(Input::PAD_LEFT)) {
90 mainMenu.PreviousItem();
93 if (input.JustPressed(Input::ACTION_A)) {
94 switch (mainMenu.Selected()) {
96 Ctrl().PushState(new InventoryMenu(this));
99 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
101 case MENU_ITEM_CAPSULE:
102 if (game->state->capsule) {
103 Ctrl().PushState(new CapsuleMenu(this));
108 case MENU_ITEM_EQUIP:
109 Ctrl().PushState(new SelectHero(this, this, this, OnEquipSelect));
111 case MENU_ITEM_STATUS:
112 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
114 case MENU_ITEM_CHANGE:
115 Ctrl().PushState(new ChangeHero(this));
117 case MENU_ITEM_CONFIG:
118 Ctrl().PushState(new ConfigMenu(this));
120 case MENU_ITEM_SCENARIO:
121 Ctrl().PushState(new ScenarioMenu(this));
129 void PartyMenu::UpdateWorld(float deltaT) {
133 void PartyMenu::Render(SDL_Surface *screen) {
134 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
136 RenderBackground(screen);
137 RenderHeros(screen, offset);
138 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
139 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
142 int PartyMenu::Width() const {
143 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
146 int PartyMenu::Height() const {
147 return 2 * Res().normalFont->CharHeight()
148 + 2 * status[0].Height()
149 + Res().normalFont->CharHeight()
150 + 8 * Res().normalFont->CharHeight()
151 + 5 * Res().normalFont->CharHeight()
152 + 2 * Res().normalFont->CharHeight();
155 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
156 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
159 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
160 for (int i(0); i < 4; ++i) {
161 status[i].Render(screen, offset + StatusOffset(i));
165 Vector<int> PartyMenu::StatusOffset(int index) const {
166 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
169 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
170 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
172 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
173 mainMenu.Draw(screen, offset + menuOffset);
176 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
177 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
179 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
180 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
182 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
183 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
185 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
186 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
188 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
189 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
190 if (game->state->time / 60 < 10) {
191 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
194 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
195 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
197 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
198 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
202 Resources &PartyMenu::Res() {
203 return *game->menuResources;
206 const Resources &PartyMenu::Res() const {
207 return *game->menuResources;
210 void PartyMenu::OnEquipSelect(void *ref, int index) {
211 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
212 self->Ctrl().ChangeState(
213 new EquipMenu(self, index));
216 void PartyMenu::OnSpellSelect(void *ref, int index) {
217 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
218 self->Ctrl().ChangeState(
219 new SpellMenu(self, index));
222 void PartyMenu::OnStatusSelect(void *ref, int index) {
223 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
224 self->Ctrl().ChangeState(
225 new StatusMenu(self, index));