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_B)) {
58 if (input.JustPressed(Input::PAD_UP)) {
61 if (input.JustPressed(Input::PAD_RIGHT)) {
64 if (input.JustPressed(Input::PAD_DOWN)) {
67 if (input.JustPressed(Input::PAD_LEFT)) {
72 void CapsuleChangeMenu::NextCapsule() {
73 int &index = parent->Game().state->capsule;
75 index %= parent->Game().state->NumCapsules();
77 while (parent->Game().state->GetCapsule().MaxClass() <= 0) {
79 index %= parent->Game().state->NumCapsules();
83 void CapsuleChangeMenu::PreviousCapsule() {
84 int &index = parent->Game().state->capsule;
86 if (index < 0) index += parent->Game().state->NumCapsules();
88 while (GetCapsule().MaxClass() <= 0) {
90 if (index < 0) index += parent->Game().state->NumCapsules();
94 void CapsuleChangeMenu::NextClass() {
95 GetCapsule().NextClass();
98 void CapsuleChangeMenu::PreviousClass() {
99 GetCapsule().PreviousClass();
103 void CapsuleChangeMenu::UpdateWorld(float deltaT) {
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(
125 24 * font.CharHeight() - font.CharHeight() / 8);
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);
135 void CapsuleChangeMenu::RenderClasses(SDL_Surface *screen, const Vector<int> &offset) const {
136 Vector<int> cursor(offset);
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();
145 parent->Res().capsuleSelectTopLeft->Draw(screen, cursor);
146 cursor.Y() += parent->Res().capsuleSelectTopLeft->Height();
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();
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(
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);
167 cursor.Y() += parent->Res().capsuleSelectLadder->Height();
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);
173 cursor.X() += parent->Res().capsuleSelectLadder->Width();
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);
188 int CapsuleChangeMenu::Width() const {
189 return parent->Width();
192 int CapsuleChangeMenu::Height() const {
193 return parent->Height();
196 Capsule &CapsuleChangeMenu::GetCapsule() {
197 return parent->GetCapsule();
200 const Capsule &CapsuleChangeMenu::GetCapsule() const {
201 return parent->GetCapsule();