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