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