]> git.localhorst.tv Git - l2e.git/blob - src/menu/CapsuleChangeMenu.cpp
implemented capsule/class selection
[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 geometry::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_B)) {
55                 Ctrl().PopState();
56         }
57
58         if (input.JustPressed(Input::PAD_UP)) {
59                 NextClass();
60         }
61         if (input.JustPressed(Input::PAD_RIGHT)) {
62                 NextCapsule();
63         }
64         if (input.JustPressed(Input::PAD_DOWN)) {
65                 PreviousClass();
66         }
67         if (input.JustPressed(Input::PAD_LEFT)) {
68                 PreviousCapsule();
69         }
70 }
71
72 void CapsuleChangeMenu::NextCapsule() {
73         int &index = parent->Game().state->capsule;
74         ++index;
75         index %= parent->Game().state->NumCapsules();
76         // skip unexplored
77         while (parent->Game().state->GetCapsule().MaxClass() <= 0) {
78                 ++index;
79                 index %= parent->Game().state->NumCapsules();
80         }
81 }
82
83 void CapsuleChangeMenu::PreviousCapsule() {
84         int &index = parent->Game().state->capsule;
85         --index;
86         if (index < 0) index += parent->Game().state->NumCapsules();
87         // skip unexplored
88         while (GetCapsule().MaxClass() <= 0) {
89                 --index;
90                 if (index < 0) index += parent->Game().state->NumCapsules();
91         }
92 }
93
94 void CapsuleChangeMenu::NextClass() {
95         GetCapsule().NextClass();
96 }
97
98 void CapsuleChangeMenu::PreviousClass() {
99         GetCapsule().PreviousClass();
100 }
101
102
103 void CapsuleChangeMenu::UpdateWorld(float deltaT) {
104
105 }
106
107 void CapsuleChangeMenu::Render(SDL_Surface *screen) {
108         const Font &font(*parent->Res().statusFont);
109         const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
110         const Vector<int> capsuleOffset(
111                         6 * font.CharWidth(),
112                         12 * font.CharHeight());
113         const Vector<int> infoOffset(
114                         12 * font.CharWidth(),
115                         2 * font.CharHeight() - font.CharHeight() / 8);
116         // TODO: wheel offset: top left, center, or center bottom?
117         const Vector<int> wheelOffset(
118                         6 * font.CharWidth(),
119                         19 * font.CharHeight() - font.CharHeight() / 8);
120         const Vector<int> classesOffset(
121                         12 * font.CharWidth(),
122                         13 * font.CharHeight() - font.CharHeight() / 8);
123         const Vector<int> menuOffset(
124                         font.CharWidth(),
125                         24 * font.CharHeight() - font.CharHeight() / 8);
126
127         parent->RenderBackground(screen);
128         parent->RenderCapsule(screen, offset + capsuleOffset);
129         parent->RenderInfo(screen, offset + infoOffset);
130         parent->RenderWheel(screen, offset + wheelOffset);
131         RenderClasses(screen, offset + classesOffset);
132         parent->RenderMenu(screen, offset + menuOffset);
133 }
134
135 void CapsuleChangeMenu::RenderClasses(SDL_Surface *screen, const Vector<int> &offset) const {
136         Vector<int> cursor(offset);
137
138         int numClasses = 0;
139         for (int i = 0; i < parent->Game().state->NumCapsules(); ++i) {
140                 if (numClasses < parent->Game().state->capsules[i].NumClasses()) {
141                         numClasses = parent->Game().state->capsules[i].NumClasses();
142                 }
143         }
144
145         parent->Res().capsuleSelectTopLeft->Draw(screen, cursor);
146         cursor.Y() += parent->Res().capsuleSelectTopLeft->Height();
147         Vector<int> target(
148                         cursor.X() + parent->Res().capsuleSelectTopLeft->Width(),
149                         cursor.Y() + numClasses * parent->Res().capsuleSelectLadder->Height());
150         parent->Res().capsuleSelectLeftRepeat->Render(screen, cursor, target);
151         cursor.Y() = target.Y();
152         parent->Res().capsuleSelectBottomLeft->Draw(screen, cursor);
153         cursor.X() += parent->Res().capsuleSelectTopLeft->Width();
154
155         for (int i = 0; i < parent->Game().state->NumCapsules(); ++i) {
156                 cursor.Y() = offset.Y();
157                 parent->Res().capsuleSelectTopRepeat->Draw(screen, cursor);
158                 cursor.Y() += parent->Res().capsuleSelectTopRepeat->Height();
159                 for (int j = numClasses - 1; j >= 0; --j) {
160                         parent->Res().capsuleSelectLadder->Draw(
161                                         screen, cursor,
162                                         j < parent->Game().state->capsules[i].MaxClass(), j);
163                         if (i == parent->Game().state->capsule
164                                         && parent->Game().state->capsules[i].CurrentClass() == j) {
165                                 parent->Res().capsuleSelectCursor->Draw(screen, cursor);
166                         }
167                         cursor.Y() += parent->Res().capsuleSelectLadder->Height();
168                 }
169                 parent->Res().capsuleSelectBottomRepeat->Draw(screen, cursor);
170                 if (parent->Game().state->capsules[i].AlignmentSprite()) {
171                         parent->Game().state->capsules[i].AlignmentSprite()->Draw(screen, cursor);
172                 }
173                 cursor.X() += parent->Res().capsuleSelectLadder->Width();
174         }
175
176         cursor.Y() = offset.Y();
177         parent->Res().capsuleSelectTopRight->Draw(screen, cursor);
178         cursor.Y() += parent->Res().capsuleSelectTopRight->Height();
179         target = Vector<int>(
180                         cursor.X() + parent->Res().capsuleSelectTopRight->Width(),
181                         cursor.Y() + numClasses * parent->Res().capsuleSelectLadder->Height());
182         parent->Res().capsuleSelectRightRepeat->Render(screen, cursor, target);
183         cursor.Y() = target.Y();
184         parent->Res().capsuleSelectBottomRight->Draw(screen, cursor);
185 }
186
187
188 int CapsuleChangeMenu::Width() const {
189         return parent->Width();
190 }
191
192 int CapsuleChangeMenu::Height() const {
193         return parent->Height();
194 }
195
196 Capsule &CapsuleChangeMenu::GetCapsule() {
197         return parent->GetCapsule();
198 }
199
200 const Capsule &CapsuleChangeMenu::GetCapsule() const {
201         return parent->GetCapsule();
202 }
203
204 }