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