]> git.localhorst.tv Git - l2e.git/blob - src/menu/CapsuleChangeMenu.cpp
de9492988875c6181d69a2ae0a6abd5ae62cf702
[l2e.git] / src / menu / CapsuleChangeMenu.cpp
1 #include "CapsuleChangeMenu.h"
2
3 #include "CapsuleMenu.h"
4 #include "Resources.h"
5 #include "../app/Application.h"
6 #include "../app/Input.h"
7 #include "../common/Inventory.h"
8 #include "../common/Item.h"
9 #include "../common/GameConfig.h"
10 #include "../common/GameState.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
13 #include "../graphics/Texture.h"
14
15 using app::Input;
16 using common::Capsule;
17 using common::Inventory;
18 using common::Item;
19 using math::Vector;
20 using graphics::Font;
21 using graphics::Frame;
22
23 namespace menu {
24
25 CapsuleChangeMenu::CapsuleChangeMenu(CapsuleMenu *parent)
26 : parent(parent) {
27
28 }
29
30
31 void CapsuleChangeMenu::OnEnterState(SDL_Surface *) {
32
33 }
34
35 void CapsuleChangeMenu::OnExitState(SDL_Surface *) {
36
37 }
38
39 void CapsuleChangeMenu::OnResumeState(SDL_Surface *) {
40
41 }
42
43 void CapsuleChangeMenu::OnPauseState(SDL_Surface *) {
44
45 }
46
47
48 void CapsuleChangeMenu::OnResize(int width, int height) {
49
50 }
51
52
53 void CapsuleChangeMenu::HandleEvents(const Input &input) {
54         if (input.JustPressed(Input::ACTION_A)
55                         || input.JustPressed(Input::ACTION_B)) {
56                 Ctrl().PopState();
57         }
58
59         if (input.JustPressed(Input::PAD_UP)) {
60                 NextClass();
61         }
62         if (input.JustPressed(Input::PAD_RIGHT)) {
63                 NextCapsule();
64         }
65         if (input.JustPressed(Input::PAD_DOWN)) {
66                 PreviousClass();
67         }
68         if (input.JustPressed(Input::PAD_LEFT)) {
69                 PreviousCapsule();
70         }
71 }
72
73 void CapsuleChangeMenu::NextCapsule() {
74         int storedClass = parent->Game().state->GetCapsule().ClassIndex();
75         int &index = parent->Game().state->capsule;
76         ++index;
77         index %= parent->Game().state->NumCapsules();
78         // skip unexplored
79         while (parent->Game().state->GetCapsule().MaxClass() <= 0) {
80                 ++index;
81                 index %= parent->Game().state->NumCapsules();
82         }
83         parent->Game().state->GetCapsule().SetClass(storedClass);
84 }
85
86 void CapsuleChangeMenu::PreviousCapsule() {
87         int storedClass = parent->Game().state->GetCapsule().ClassIndex();
88         int &index = parent->Game().state->capsule;
89         --index;
90         if (index < 0) index += parent->Game().state->NumCapsules();
91         // skip unexplored
92         while (GetCapsule().MaxClass() <= 0) {
93                 --index;
94                 if (index < 0) index += parent->Game().state->NumCapsules();
95         }
96         parent->Game().state->GetCapsule().SetClass(storedClass);
97 }
98
99 void CapsuleChangeMenu::NextClass() {
100         GetCapsule().NextClass();
101 }
102
103 void CapsuleChangeMenu::PreviousClass() {
104         GetCapsule().PreviousClass();
105 }
106
107
108 void CapsuleChangeMenu::UpdateWorld(float deltaT) {
109
110 }
111
112 void CapsuleChangeMenu::Render(SDL_Surface *screen) {
113         const Font &font(*parent->Res().statusFont);
114         const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
115         const Vector<int> capsuleOffset(
116                         6 * font.CharWidth(),
117                         12 * font.CharHeight());
118         const Vector<int> infoOffset(
119                         12 * font.CharWidth(),
120                         2 * font.CharHeight() - font.CharHeight() / 8);
121         // TODO: wheel offset: top left, center, or center bottom?
122         const Vector<int> wheelOffset(
123                         6 * font.CharWidth(),
124                         19 * font.CharHeight() - font.CharHeight() / 8);
125         const Vector<int> classesOffset(
126                         12 * font.CharWidth(),
127                         13 * font.CharHeight() - font.CharHeight() / 8);
128         const Vector<int> menuOffset(
129                         font.CharWidth(),
130                         24 * font.CharHeight() - font.CharHeight() / 8);
131
132         parent->RenderBackground(screen);
133         parent->RenderCapsule(screen, offset + capsuleOffset);
134         parent->RenderInfo(screen, offset + infoOffset);
135         parent->RenderWheel(screen, offset + wheelOffset);
136         RenderClasses(screen, offset + classesOffset);
137         parent->RenderMenu(screen, offset + menuOffset);
138 }
139
140 void CapsuleChangeMenu::RenderClasses(SDL_Surface *screen, const Vector<int> &offset) const {
141         Vector<int> cursor(offset);
142
143         int numClasses = 0;
144         for (int i = 0; i < parent->Game().state->NumCapsules(); ++i) {
145                 if (numClasses < parent->Game().state->capsules[i].NumClasses()) {
146                         numClasses = parent->Game().state->capsules[i].NumClasses();
147                 }
148         }
149
150         parent->Res().capsuleSelectTopLeft->Draw(screen, cursor);
151         cursor.Y() += parent->Res().capsuleSelectTopLeft->Height();
152         Vector<int> target(
153                         cursor.X() + parent->Res().capsuleSelectTopLeft->Width(),
154                         cursor.Y() + numClasses * parent->Res().capsuleSelectLadder->Height());
155         parent->Res().capsuleSelectLeftRepeat->Render(screen, cursor, target);
156         cursor.Y() = target.Y();
157         parent->Res().capsuleSelectBottomLeft->Draw(screen, cursor);
158         cursor.X() += parent->Res().capsuleSelectTopLeft->Width();
159
160         for (int i = 0; i < parent->Game().state->NumCapsules(); ++i) {
161                 cursor.Y() = offset.Y();
162                 parent->Res().capsuleSelectTopRepeat->Draw(screen, cursor);
163                 cursor.Y() += parent->Res().capsuleSelectTopRepeat->Height();
164                 for (int j = numClasses - 1; j >= 0; --j) {
165                         parent->Res().capsuleSelectLadder->Draw(
166                                         screen, cursor,
167                                         j < parent->Game().state->capsules[i].MaxClass(), j);
168                         if (i == parent->Game().state->capsule
169                                         && parent->Game().state->capsules[i].CurrentClass() == j) {
170                                 parent->Res().capsuleSelectCursor->Draw(screen, cursor);
171                         }
172                         cursor.Y() += parent->Res().capsuleSelectLadder->Height();
173                 }
174                 parent->Res().capsuleSelectBottomRepeat->Draw(screen, cursor);
175                 if (parent->Game().state->capsules[i].AlignmentSprite()) {
176                         parent->Game().state->capsules[i].AlignmentSprite()->Draw(screen, cursor);
177                 }
178                 cursor.X() += parent->Res().capsuleSelectLadder->Width();
179         }
180
181         cursor.Y() = offset.Y();
182         parent->Res().capsuleSelectTopRight->Draw(screen, cursor);
183         cursor.Y() += parent->Res().capsuleSelectTopRight->Height();
184         target = Vector<int>(
185                         cursor.X() + parent->Res().capsuleSelectTopRight->Width(),
186                         cursor.Y() + numClasses * parent->Res().capsuleSelectLadder->Height());
187         parent->Res().capsuleSelectRightRepeat->Render(screen, cursor, target);
188         cursor.Y() = target.Y();
189         parent->Res().capsuleSelectBottomRight->Draw(screen, cursor);
190 }
191
192
193 int CapsuleChangeMenu::Width() const {
194         return parent->Width();
195 }
196
197 int CapsuleChangeMenu::Height() const {
198         return parent->Height();
199 }
200
201 Capsule &CapsuleChangeMenu::GetCapsule() {
202         return parent->GetCapsule();
203 }
204
205 const Capsule &CapsuleChangeMenu::GetCapsule() const {
206         return parent->GetCapsule();
207 }
208
209 }