]> git.localhorst.tv Git - l2e.git/blob - src/menu/PartyMenu.cpp
added 'change' menu state
[l2e.git] / src / menu / PartyMenu.cpp
1 /*
2  * PartyMenu.cpp
3  *
4  *  Created on: Oct 21, 2012
5  *      Author: holy
6  */
7
8 #include "PartyMenu.h"
9
10 #include "ChangeHero.h"
11 #include "Resources.h"
12 #include "SelectHero.h"
13 #include "StatusMenu.h"
14 #include "../app/Application.h"
15 #include "../app/Input.h"
16 #include "../common/GameConfig.h"
17 #include "../common/GameState.h"
18 #include "../geometry/Vector.h"
19 #include "../graphics/Font.h"
20 #include "../graphics/Frame.h"
21 #include "../graphics/Texture.h"
22
23 using app::Input;
24 using common::GameConfig;
25 using geometry::Vector;
26
27 namespace menu {
28
29 PartyMenu::PartyMenu(GameConfig *game)
30 : game(game)
31 , mainMenu(*game->menuResources->mainMenuProperties) {
32         for (int i(0); i < 4; ++i) {
33                 status[i].SetHero(game->state->party, i);
34                 status[i].SetResources(game->menuResources);
35         }
36         statusPositions[0] = Vector<int>(0, 0);
37         statusPositions[1] = Vector<int>(status[0].Width(), 0);
38         statusPositions[2] = Vector<int>(0, status[0].Height());
39         statusPositions[3] = Vector<int>(status[0].Width(), status[0].Height());
40
41         mainMenu.Add(Res().mainMenuItemText, 0);
42         mainMenu.Add(Res().mainMenuStatusText, 4);
43         mainMenu.Add(Res().mainMenuSpellText, 1);
44         mainMenu.Add(Res().mainMenuChangeText, 5);
45         mainMenu.Add(Res().mainMenuCapsuleText, 2);
46         mainMenu.Add(Res().mainMenuConfigText, 6);
47         mainMenu.Add(Res().mainMenuEquipmentText, 3);
48         mainMenu.Add(Res().mainMenuScenarioText, 7);
49 }
50
51 PartyMenu::~PartyMenu() {
52
53 }
54
55
56 void PartyMenu::OnEnterState(SDL_Surface *) {
57
58 }
59
60 void PartyMenu::OnExitState(SDL_Surface *) {
61
62 }
63
64 void PartyMenu::OnResumeState(SDL_Surface *) {
65
66 }
67
68 void PartyMenu::OnPauseState(SDL_Surface *) {
69
70 }
71
72
73 void PartyMenu::OnResize(int width, int height) {
74
75 }
76
77
78 void PartyMenu::HandleEvents(const Input &input) {
79         if (input.JustPressed(Input::ACTION_B)) {
80                 Ctrl().PopState();
81                 return;
82         }
83
84         if (input.JustPressed(Input::PAD_UP)) {
85                 mainMenu.PreviousRow();
86         } else if (input.JustPressed(Input::PAD_RIGHT)) {
87                 mainMenu.NextItem();
88         } else if (input.JustPressed(Input::PAD_DOWN)) {
89                 mainMenu.NextRow();
90         } else if (input.JustPressed(Input::PAD_LEFT)) {
91                 mainMenu.PreviousItem();
92         }
93
94         if (input.JustPressed(Input::ACTION_A)) {
95                 switch (mainMenu.Selected()) {
96                         case MENU_ITEM_ITEM:
97                                 break;
98                         case MENU_ITEM_SPELL:
99                                 break;
100                         case MENU_ITEM_CAPSULE:
101                                 break;
102                         case MENU_ITEM_EQUIP:
103                                 break;
104                         case MENU_ITEM_STATUS:
105                                 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
106                                 break;
107                         case MENU_ITEM_CHANGE:
108                                 Ctrl().PushState(new ChangeHero(this));
109                                 break;
110                         case MENU_ITEM_CONFIG:
111                                 break;
112                         case MENU_ITEM_SCENARIO:
113                                 break;
114                         default:
115                                 break;
116                 }
117         }
118 }
119
120 void PartyMenu::UpdateWorld(float deltaT) {
121
122 }
123
124 void PartyMenu::Render(SDL_Surface *screen) {
125         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
126
127         RenderBackground(screen);
128         RenderHeros(screen, offset);
129         RenderMenu(screen, offset + Vector<int>(8 * Res().normalFont->CharWidth(), 13 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
130         RenderInfo(screen, offset + Vector<int>(14 * Res().normalFont->CharWidth(), 21 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 8));
131 }
132
133 int PartyMenu::Width() const {
134         return 2 * (status[0].Width() + Res().normalFont->CharWidth());
135 }
136
137 int PartyMenu::Height() const {
138         return 2 * Res().normalFont->CharHeight()
139                         + 2 * status[0].Height()
140                         + Res().normalFont->CharHeight()
141                         + 8 * Res().normalFont->CharHeight()
142                         + 5 * Res().normalFont->CharHeight()
143                         + 2 * Res().normalFont->CharHeight();
144 }
145
146 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
147         Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
148 }
149
150 void PartyMenu::RenderHeros(SDL_Surface *screen, const Vector<int> &offset) const {
151         for (int i(0); i < 4; ++i) {
152                 status[i].Render(screen, offset + StatusOffset(i));
153         }
154 }
155
156 Vector<int> PartyMenu::StatusOffset(int index) const {
157         return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
158 }
159
160 void PartyMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
161         Vector<int> menuOffset(3 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
162
163         Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
164         mainMenu.Draw(screen, offset + menuOffset);
165 }
166
167 void PartyMenu::RenderInfo(SDL_Surface *screen, const Vector<int> &offset) const {
168         Res().statusFrame->Draw(screen, offset, 17 * Res().normalFont->CharWidth(), 5 * Res().normalFont->CharHeight());
169
170         Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
171         Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
172
173         Vector<int> hoursOffset(timeLabelOffset.X() + 6 * Res().normalFont->CharWidth(), timeLabelOffset.Y());
174         Res().normalFont->DrawNumber(game->state->time / 60 / 60, screen, offset + hoursOffset, 4);
175
176         Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
177         Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
178
179         Vector<int> minutesOffset(timeSeparatorOffset.X() + Res().normalFont->CharWidth(), timeSeparatorOffset.Y());
180         Res().normalFont->DrawNumber(game->state->time / 60, screen, offset + minutesOffset, 2);
181         if (game->state->time / 60 < 10) {
182                 Res().normalFont->DrawChar('0', screen, offset + minutesOffset);
183         }
184
185         Vector<int> goldLabelOffset(2 * Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight() + Res().normalFont->CharHeight() * 3 / 4);
186         Res().normalFont->DrawString(Res().mainMenuGoldText, screen, offset + goldLabelOffset);
187
188         Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
189         Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
190 }
191
192
193 Resources &PartyMenu::Res() {
194         return *game->menuResources;
195 }
196
197 const Resources &PartyMenu::Res() const {
198         return *game->menuResources;
199 }
200
201 void PartyMenu::OnStatusSelect(void *ref, int index) {
202         PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
203         self->Ctrl().ChangeState(
204                         new StatusMenu(self, index));
205 }
206
207 }