]> git.localhorst.tv Git - l2e.git/blob - src/menu/PartyMenu.cpp
46e271bd70218fc95f8d889d6b74f703f9021b2d
[l2e.git] / src / menu / PartyMenu.cpp
1 #include "PartyMenu.h"
2
3 #include "CapsuleMenu.h"
4 #include "ChangeHero.h"
5 #include "ConfigMenu.h"
6 #include "EquipMenu.h"
7 #include "InventoryMenu.h"
8 #include "Resources.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 "../math/Vector.h"
18 #include "../graphics/Font.h"
19 #include "../graphics/Frame.h"
20 #include "../graphics/Texture.h"
21
22 using app::Input;
23 using common::GameConfig;
24 using math::Vector;
25
26 namespace menu {
27
28 PartyMenu::PartyMenu(GameConfig *game)
29 : game(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);
34         }
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());
39
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);
48 }
49
50 PartyMenu::~PartyMenu() {
51
52 }
53
54
55 void PartyMenu::OnEnterState(SDL_Surface *) {
56
57 }
58
59 void PartyMenu::OnExitState(SDL_Surface *) {
60
61 }
62
63 void PartyMenu::OnResumeState(SDL_Surface *) {
64
65 }
66
67 void PartyMenu::OnPauseState(SDL_Surface *) {
68
69 }
70
71
72 void PartyMenu::OnResize(int width, int height) {
73
74 }
75
76
77 void PartyMenu::HandleEvents(const Input &input) {
78         if (input.JustPressed(Input::ACTION_B)) {
79                 Ctrl().PopState();
80                 return;
81         }
82
83         if (input.JustPressed(Input::PAD_UP)) {
84                 mainMenu.PreviousRow();
85         } else if (input.JustPressed(Input::PAD_RIGHT)) {
86                 mainMenu.NextItem();
87         } else if (input.JustPressed(Input::PAD_DOWN)) {
88                 mainMenu.NextRow();
89         } else if (input.JustPressed(Input::PAD_LEFT)) {
90                 mainMenu.PreviousItem();
91         }
92
93         if (input.JustPressed(Input::ACTION_A)) {
94                 switch (mainMenu.Selected()) {
95                         case MENU_ITEM_ITEM:
96                                 Ctrl().PushState(new InventoryMenu(this));
97                                 break;
98                         case MENU_ITEM_SPELL:
99                                 Ctrl().PushState(new SelectHero(this, this, this, OnSpellSelect));
100                                 break;
101                         case MENU_ITEM_CAPSULE:
102                                 if (game->state->capsule) {
103                                         Ctrl().PushState(new CapsuleMenu(this));
104                                 } else {
105                                         // noise and blur
106                                 }
107                                 break;
108                         case MENU_ITEM_EQUIP:
109                                 Ctrl().PushState(new SelectHero(this, this, this, OnEquipSelect));
110                                 break;
111                         case MENU_ITEM_STATUS:
112                                 Ctrl().PushState(new SelectHero(this, this, this, OnStatusSelect));
113                                 break;
114                         case MENU_ITEM_CHANGE:
115                                 Ctrl().PushState(new ChangeHero(this));
116                                 break;
117                         case MENU_ITEM_CONFIG:
118                                 Ctrl().PushState(new ConfigMenu(this));
119                                 break;
120                         case MENU_ITEM_SCENARIO:
121                                 Ctrl().PushState(new ScenarioMenu(this));
122                                 break;
123                         default:
124                                 break;
125                 }
126         }
127 }
128
129 void PartyMenu::UpdateWorld(Uint32 deltaT) {
130
131 }
132
133 void PartyMenu::Render(SDL_Surface *screen) {
134         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
135
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));
140 }
141
142 int PartyMenu::Width() const {
143         return 2 * (status[0].Width() + Res().normalFont->CharWidth());
144 }
145
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();
153 }
154
155 void PartyMenu::RenderBackground(SDL_Surface *screen) const {
156         Res().menubg->Render(screen, Vector<int>(), Vector<int>(screen->w, screen->h));
157 }
158
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));
162         }
163 }
164
165 Vector<int> PartyMenu::StatusOffset(int index) const {
166         return statusPositions[index] + Vector<int>(Res().normalFont->CharWidth(), 2 * Res().normalFont->CharHeight());
167 }
168
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);
171
172         Res().statusFrame->Draw(screen, offset, 23 * Res().normalFont->CharWidth(), 8 * Res().normalFont->CharHeight());
173         mainMenu.Draw(screen, offset + menuOffset);
174 }
175
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());
178
179         Vector<int> timeLabelOffset(2 * Res().normalFont->CharWidth(), Res().normalFont->CharHeight() + Res().normalFont->CharHeight() / 4);
180         Res().normalFont->DrawString(Res().mainMenuTimeText, screen, offset + timeLabelOffset);
181
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);
184
185         Vector<int> timeSeparatorOffset(hoursOffset.X() + 4 * Res().normalFont->CharWidth(), hoursOffset.Y());
186         Res().normalFont->DrawChar(':', screen, offset + timeSeparatorOffset);
187
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);
192         }
193
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);
196
197         Vector<int> goldOffset(goldLabelOffset.X() + 6 * Res().normalFont->CharWidth(), goldLabelOffset.Y());
198         Res().normalFont->DrawNumber(game->state->money, screen, offset + goldOffset, 7);
199 }
200
201
202 Resources &PartyMenu::Res() {
203         return *game->menuResources;
204 }
205
206 const Resources &PartyMenu::Res() const {
207         return *game->menuResources;
208 }
209
210 void PartyMenu::OnEquipSelect(void *ref, int index) {
211         PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
212         self->Ctrl().ChangeState(
213                         new EquipMenu(self, index));
214 }
215
216 void PartyMenu::OnSpellSelect(void *ref, int index) {
217         PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
218         self->Ctrl().ChangeState(
219                         new SpellMenu(self, index));
220 }
221
222 void PartyMenu::OnStatusSelect(void *ref, int index) {
223         PartyMenu *self(reinterpret_cast<PartyMenu *>(ref));
224         self->Ctrl().ChangeState(
225                         new StatusMenu(self, index));
226 }
227
228 }