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 "SelectHero.h"
16 #include "SpellMenu.h"
17 #include "StatusMenu.h"
18 #include "../app/Application.h"
19 #include "../app/Input.h"
20 #include "../common/GameConfig.h"
21 #include "../common/GameState.h"
22 #include "../geometry/Vector.h"
23 #include "../graphics/Font.h"
24 #include "../graphics/Frame.h"
25 #include "../graphics/Texture.h"
28 using common::GameConfig;
29 using geometry::Vector;
33 PartyMenu::PartyMenu(GameConfig *game)
35 , mainMenu(*game->menuResources->mainMenuProperties) {
36 for (int i(0); i < 4; ++i) {
37 status[i].SetHero(game->state->party, i);
38 status[i].SetResources(game->menuResources);
40 statusPositions[0] = Vector<int>(0, 0);
41 statusPositions[1] = Vector<int>(status[0].Width(), 0);
42 statusPositions[2] = Vector<int>(0, status[0].Height());
43 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
45 mainMenu.Add(Res().mainMenuItemText, 0);
46 mainMenu.Add(Res().mainMenuStatusText, 4);
47 mainMenu.Add(Res().mainMenuSpellText, 1);
48 mainMenu.Add(Res().mainMenuChangeText, 5);
49 mainMenu.Add(Res().mainMenuCapsuleText, 2);
50 mainMenu.Add(Res().mainMenuConfigText, 6);
51 mainMenu.Add(Res().mainMenuEquipmentText, 3);
52 mainMenu.Add(Res().mainMenuScenarioText, 7);
55 PartyMenu::~PartyMenu() {
60 void PartyMenu::OnEnterState(SDL_Surface *) {
64 void PartyMenu::OnExitState(SDL_Surface *) {
68 void PartyMenu::OnResumeState(SDL_Surface *) {
72 void PartyMenu::OnPauseState(SDL_Surface *) {
77 void PartyMenu::OnResize(int width, int height) {
82 void PartyMenu::HandleEvents(const Input &input) {
83 if (input.JustPressed(Input::ACTION_B)) {
88 if (input.JustPressed(Input::PAD_UP)) {
89 mainMenu.PreviousRow();
90 } else if (input.JustPressed(Input::PAD_RIGHT)) {
92 } else if (input.JustPressed(Input::PAD_DOWN)) {
94 } else if (input.JustPressed(Input::PAD_LEFT)) {
95 mainMenu.PreviousItem();
98 if (input.JustPressed(Input::ACTION_A)) {
99 switch (mainMenu.Selected()) {
101 Ctrl().PushState(new InventoryMenu(this));
103 case MENU_ITEM_SPELL:
104 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
106 case MENU_ITEM_CAPSULE:
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:
128 void PartyMenu::UpdateWorld(float deltaT) {
132 void PartyMenu::Render(SDL_Surface *screen) {
133 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
135 RenderBackground(screen);
136 RenderHeros(screen, offset);
137 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
138 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
141 int PartyMenu::Width() const {
142 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
145 int PartyMenu::Height() const {
146 return 2 * Res().normalFont->CharHeight()
147 + 2 * status[0].Height()
148 + Res().normalFont->CharHeight()
149 + 8 * Res().normalFont->CharHeight()
150 + 5 * Res().normalFont->CharHeight()
151 + 2 * Res().normalFont->CharHeight();
154 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
155 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
158 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
159 for (int i(0); i < 4; ++i) {
160 status[i].Render(screen, offset + StatusOffset(i));
164 Vector<int> PartyMenu::StatusOffset(int index) const {
165 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
168 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
169 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
171 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
172 mainMenu.Draw(screen, offset + menuOffset);
175 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
176 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
178 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
179 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
181 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
182 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
184 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
185 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
187 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
188 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
189 if (game->state->time / 60 < 10) {
190 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
193 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
194 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
196 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
197 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
201 Resources &PartyMenu::Res() {
202 return *game->menuResources;
205 const Resources &PartyMenu::Res() const {
206 return *game->menuResources;
209 void PartyMenu::OnEquipSelect(void *ref, int index) {
210 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
211 self->Ctrl().ChangeState(
212 new EquipMenu(self, index));
215 void PartyMenu::OnSpellSelect(void *ref, int index) {
216 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
217 self->Ctrl().ChangeState(
218 new SpellMenu(self, index));
221 void PartyMenu::OnStatusSelect(void *ref, int index) {
222 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
223 self->Ctrl().ChangeState(
224 new StatusMenu(self, index));