1 #include "CapsuleChangeMenu.h"
3 #include "CapsuleMenu.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"
17 using common::Capsule;
18 using common::Inventory;
22 using graphics::Frame;
26 CapsuleChangeMenu::CapsuleChangeMenu(CapsuleMenu *parent)
32 void CapsuleChangeMenu::OnEnterState(SDL_Surface *) {
36 void CapsuleChangeMenu::OnExitState(SDL_Surface *) {
40 void CapsuleChangeMenu::OnResumeState(SDL_Surface *) {
44 void CapsuleChangeMenu::OnPauseState(SDL_Surface *) {
49 void CapsuleChangeMenu::OnResize(int width, int height) {
54 void CapsuleChangeMenu::HandleEvents(const Input &input) {
55 if (input.JustPressed(Input::ACTION_A)
56 || input.JustPressed(Input::ACTION_B)) {
60 if (input.JustPressed(Input::PAD_UP)) {
63 if (input.JustPressed(Input::PAD_RIGHT)) {
66 if (input.JustPressed(Input::PAD_DOWN)) {
69 if (input.JustPressed(Input::PAD_LEFT)) {
74 void CapsuleChangeMenu::NextCapsule() {
75 int storedClass = parent->Game().state->GetCapsule().ClassIndex();
76 int &index = parent->Game().state->capsule;
78 index %= parent->Game().state->NumCapsules();
80 while (parent->Game().state->GetCapsule().MaxClass() <= 0) {
82 index %= parent->Game().state->NumCapsules();
84 parent->Game().state->GetCapsule().SetClass(storedClass);
87 void CapsuleChangeMenu::PreviousCapsule() {
88 int storedClass = parent->Game().state->GetCapsule().ClassIndex();
89 int &index = parent->Game().state->capsule;
91 if (index < 0) index += parent->Game().state->NumCapsules();
93 while (GetCapsule().MaxClass() <= 0) {
95 if (index < 0) index += parent->Game().state->NumCapsules();
97 parent->Game().state->GetCapsule().SetClass(storedClass);
100 void CapsuleChangeMenu::NextClass() {
101 GetCapsule().NextClass();
104 void CapsuleChangeMenu::PreviousClass() {
105 GetCapsule().PreviousClass();
109 void CapsuleChangeMenu::UpdateWorld(Uint32 deltaT) {
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(
131 24 * font.CharHeight() - font.CharHeight() / 8);
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);
141 void CapsuleChangeMenu::RenderClasses(SDL_Surface *screen, const Vector<int> &offset) const {
142 Vector<int> cursor(offset);
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();
151 parent->Res().capsuleSelectTopLeft->Draw(screen, cursor);
152 cursor.Y() += parent->Res().capsuleSelectTopLeft->Height();
154 cursor.X() + parent->Res().capsuleSelectTopLeft->Width(),
155 cursor.Y() + numClasses * parent->Res().capsuleSelectLadder->Height());
156 parent->Res().capsuleSelectLeftRepeat->Render(screen, cursor, target);
157 cursor.Y() = target.Y();
158 parent->Res().capsuleSelectBottomLeft->Draw(screen, cursor);
159 cursor.X() += parent->Res().capsuleSelectTopLeft->Width();
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(
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);
173 cursor.Y() += parent->Res().capsuleSelectLadder->Height();
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);
179 cursor.X() += parent->Res().capsuleSelectLadder->Width();
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->Render(screen, cursor, target);
189 cursor.Y() = target.Y();
190 parent->Res().capsuleSelectBottomRight->Draw(screen, cursor);
194 int CapsuleChangeMenu::Width() const {
195 return parent->Width();
198 int CapsuleChangeMenu::Height() const {
199 return parent->Height();
202 Capsule &CapsuleChangeMenu::GetCapsule() {
203 return parent->GetCapsule();
206 const Capsule &CapsuleChangeMenu::GetCapsule() const {
207 return parent->GetCapsule();