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