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"
16 using common::Capsule;
17 using common::Inventory;
19 using geometry::Vector;
21 using graphics::Frame;
25 CapsuleChangeMenu::CapsuleChangeMenu(CapsuleMenu *parent)
31 void CapsuleChangeMenu::OnEnterState(SDL_Surface *) {
35 void CapsuleChangeMenu::OnExitState(SDL_Surface *) {
39 void CapsuleChangeMenu::OnResumeState(SDL_Surface *) {
43 void CapsuleChangeMenu::OnPauseState(SDL_Surface *) {
48 void CapsuleChangeMenu::OnResize(int width, int height) {
53 void CapsuleChangeMenu::HandleEvents(const Input &input) {
54 if (input.JustPressed(Input::ACTION_A)
55 || input.JustPressed(Input::ACTION_B)) {
59 if (input.JustPressed(Input::PAD_UP)) {
62 if (input.JustPressed(Input::PAD_RIGHT)) {
65 if (input.JustPressed(Input::PAD_DOWN)) {
68 if (input.JustPressed(Input::PAD_LEFT)) {
73 void CapsuleChangeMenu::NextCapsule() {
74 int storedClass = parent->Game().state->GetCapsule().ClassIndex();
75 int &index = parent->Game().state->capsule;
77 index %= parent->Game().state->NumCapsules();
79 while (parent->Game().state->GetCapsule().MaxClass() <= 0) {
81 index %= parent->Game().state->NumCapsules();
83 parent->Game().state->GetCapsule().SetClass(storedClass);
86 void CapsuleChangeMenu::PreviousCapsule() {
87 int storedClass = parent->Game().state->GetCapsule().ClassIndex();
88 int &index = parent->Game().state->capsule;
90 if (index < 0) index += parent->Game().state->NumCapsules();
92 while (GetCapsule().MaxClass() <= 0) {
94 if (index < 0) index += parent->Game().state->NumCapsules();
96 parent->Game().state->GetCapsule().SetClass(storedClass);
99 void CapsuleChangeMenu::NextClass() {
100 GetCapsule().NextClass();
103 void CapsuleChangeMenu::PreviousClass() {
104 GetCapsule().PreviousClass();
108 void CapsuleChangeMenu::UpdateWorld(float deltaT) {
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(
130 24 * font.CharHeight() - font.CharHeight() / 8);
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);
140 void CapsuleChangeMenu::RenderClasses(SDL_Surface *screen, const Vector<int> &offset) const {
141 Vector<int> cursor(offset);
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();
150 parent->Res().capsuleSelectTopLeft->Draw(screen, cursor);
151 cursor.Y() += parent->Res().capsuleSelectTopLeft->Height();
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();
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(
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);
172 cursor.Y() += parent->Res().capsuleSelectLadder->Height();
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);
178 cursor.X() += parent->Res().capsuleSelectLadder->Width();
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);
193 int CapsuleChangeMenu::Width() const {
194 return parent->Width();
197 int CapsuleChangeMenu::Height() const {
198 return parent->Height();
201 Capsule &CapsuleChangeMenu::GetCapsule() {
202 return parent->GetCapsule();
205 const Capsule &CapsuleChangeMenu::GetCapsule() const {
206 return parent->GetCapsule();