4 * Created on: Oct 21, 2012
10 #include "ChangeHero.h"
11 #include "InventoryMenu.h"
12 #include "Resources.h"
13 #include "SelectHero.h"
14 #include "StatusMenu.h"
15 #include "../app/Application.h"
16 #include "../app/Input.h"
17 #include "../common/GameConfig.h"
18 #include "../common/GameState.h"
19 #include "../geometry/Vector.h"
20 #include "../graphics/Font.h"
21 #include "../graphics/Frame.h"
22 #include "../graphics/Texture.h"
25 using common::GameConfig;
26 using geometry::Vector;
30 PartyMenu::PartyMenu(GameConfig *game)
32 , mainMenu(*game->menuResources->mainMenuProperties) {
33 for (int i(0); i < 4; ++i) {
34 status[i].SetHero(game->state->party, i);
35 status[i].SetResources(game->menuResources);
37 statusPositions[0] = Vector<int>(0, 0);
38 statusPositions[1] = Vector<int>(status[0].Width(), 0);
39 statusPositions[2] = Vector<int>(0, status[0].Height());
40 statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
42 mainMenu.Add(Res().mainMenuItemText, 0);
43 mainMenu.Add(Res().mainMenuStatusText, 4);
44 mainMenu.Add(Res().mainMenuSpellText, 1);
45 mainMenu.Add(Res().mainMenuChangeText, 5);
46 mainMenu.Add(Res().mainMenuCapsuleText, 2);
47 mainMenu.Add(Res().mainMenuConfigText, 6);
48 mainMenu.Add(Res().mainMenuEquipmentText, 3);
49 mainMenu.Add(Res().mainMenuScenarioText, 7);
52 PartyMenu::~PartyMenu() {
57 void PartyMenu::OnEnterState(SDL_Surface *) {
61 void PartyMenu::OnExitState(SDL_Surface *) {
65 void PartyMenu::OnResumeState(SDL_Surface *) {
69 void PartyMenu::OnPauseState(SDL_Surface *) {
74 void PartyMenu::OnResize(int width, int height) {
79 void PartyMenu::HandleEvents(const Input &input) {
80 if (input.JustPressed(Input::ACTION_B)) {
85 if (input.JustPressed(Input::PAD_UP)) {
86 mainMenu.PreviousRow();
87 } else if (input.JustPressed(Input::PAD_RIGHT)) {
89 } else if (input.JustPressed(Input::PAD_DOWN)) {
91 } else if (input.JustPressed(Input::PAD_LEFT)) {
92 mainMenu.PreviousItem();
95 if (input.JustPressed(Input::ACTION_A)) {
96 switch (mainMenu.Selected()) {
98 Ctrl().PushState(new InventoryMenu(this));
100 case MENU_ITEM_SPELL:
102 case MENU_ITEM_CAPSULE:
104 case MENU_ITEM_EQUIP:
106 case MENU_ITEM_STATUS:
107 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
109 case MENU_ITEM_CHANGE:
110 Ctrl().PushState(new ChangeHero(this));
112 case MENU_ITEM_CONFIG:
114 case MENU_ITEM_SCENARIO:
122 void PartyMenu::UpdateWorld(float deltaT) {
126 void PartyMenu::Render(SDL_Surface *screen) {
127 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
129 RenderBackground(screen);
130 RenderHeros(screen, offset);
131 RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
132 RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
135 int PartyMenu::Width() const {
136 return 2 * (status[0].Width() + Res().normalFont->CharWidth());
139 int PartyMenu::Height() const {
140 return 2 * Res().normalFont->CharHeight()
141 + 2 * status[0].Height()
142 + Res().normalFont->CharHeight()
143 + 8 * Res().normalFont->CharHeight()
144 + 5 * Res().normalFont->CharHeight()
145 + 2 * Res().normalFont->CharHeight();
148 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
149 Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
152 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
153 for (int i(0); i < 4; ++i) {
154 status[i].Render(screen, offset + StatusOffset(i));
158 Vector<int> PartyMenu::StatusOffset(int index) const {
159 return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
162 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
163 Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
165 Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
166 mainMenu.Draw(screen, offset + menuOffset);
169 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
170 Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
172 Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
173 Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
175 Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
176 Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
178 Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
179 Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
181 Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
182 Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
183 if (game->state->time / 60 < 10) {
184 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
187 Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
188 Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
190 Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
191 Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
195 Resources &PartyMenu::Res() {
196 return *game->menuResources;
199 const Resources &PartyMenu::Res() const {
200 return *game->menuResources;
203 void PartyMenu::OnStatusSelect(void *ref, int index) {
204 PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
205 self->Ctrl().ChangeState(
206 new StatusMenu(self, index));