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