1 #include "CapsuleNameMenu.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 "../math/Vector.h"
18 using common::Capsule;
19 using common::Inventory;
23 using graphics::Frame;
27 CapsuleNameMenu::CapsuleNameMenu(CapsuleMenu *parent)
29 , select(*parent->Res().capsuleNameCharSelectTemplate)
33 std::strncpy(buffer, GetCapsule().Name(), 6);
34 for (int i = 4; i > 0; --i) {
35 if (buffer[i] == '\0') {
42 void CapsuleNameMenu::OnEnterState(SDL_Surface *) {
46 void CapsuleNameMenu::OnExitState(SDL_Surface *) {
50 void CapsuleNameMenu::OnResumeState(SDL_Surface *) {
54 void CapsuleNameMenu::OnPauseState(SDL_Surface *) {
59 void CapsuleNameMenu::OnResize(int width, int height) {
64 void CapsuleNameMenu::HandleEvents(const Input &input) {
65 if (input.JustPressed(Input::PAD_UP)) {
68 if (input.JustPressed(Input::PAD_RIGHT)) {
71 if (input.JustPressed(Input::PAD_DOWN)) {
74 if (input.JustPressed(Input::PAD_LEFT)) {
78 if (input.JustPressed(Input::ACTION_A)) {
81 if (input.JustPressed(Input::ACTION_B)) {
85 if (input.JustPressed(Input::START) && cursor > 0) {
91 void CapsuleNameMenu::AddChar() {
94 buffer[0] = select.Selected();
95 for (int i = 1; i < 5; ++i) {
101 buffer[cursor] = select.Selected();
109 void CapsuleNameMenu::RemoveChar() {
113 buffer[cursor] = '_';
117 void CapsuleNameMenu::StoreName() {
118 // NOTE: this will leak the memory allocated for the new name
120 for (int i = 0; i < 6; ++i) {
121 if (buffer[i] == '_' || buffer[i] == '\0') {
123 name = new char[i + 1];
124 std::strncpy(name, buffer, i + 1);
129 GetCapsule().SetName(name);
133 void CapsuleNameMenu::UpdateWorld(Uint32 deltaT) {
137 void CapsuleNameMenu::Render(SDL_Surface *screen) {
138 const Font &font(*parent->Res().statusFont);
139 const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
140 const Vector<int> nameOffset(
141 4 * font.CharWidth(),
142 4 * font.CharHeight() - font.CharHeight() / 8);
143 const Vector<int> alphaOffset(
144 4 * font.CharWidth(),
145 7 * font.CharHeight() - font.CharHeight() / 8);
147 parent->RenderBackground(screen);
148 RenderName(screen, offset + nameOffset);
149 RenderAlphabet(screen, offset + alphaOffset);
152 void CapsuleNameMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
153 const Font &font(*parent->Res().normalFont);
154 const Frame &frame(*parent->Res().statusFrame);
155 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
156 const Vector<int> namebgOffset(8 * font.CharWidth(), 0);
157 const Vector<int> nameOffset(13 * font.CharWidth(), font.CharHeight());
159 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
160 font.DrawString(parent->Res().capsuleNameLabel, screen, offset + labelOffset, 5);
161 frame.Draw(screen, offset + namebgOffset, 16 * font.CharWidth(), 3 * font.CharHeight());
162 font.DrawString(buffer, screen, offset + nameOffset, 5);
165 void CapsuleNameMenu::RenderAlphabet(SDL_Surface *screen, const Vector<int> &offset) const {
166 const Font &font(*parent->Res().normalFont);
167 const Frame &frame(*parent->Res().statusFrame);
168 const Vector<int> selectOffset(2 * font.CharWidth(), 2 * font.CharHeight());
170 frame.Draw(screen, offset, 24 * font.CharWidth(), 17 * font.CharHeight());
171 select.Draw(screen, offset + selectOffset);
175 int CapsuleNameMenu::Width() const {
176 return parent->Width();
179 int CapsuleNameMenu::Height() const {
180 return parent->Height();
183 Capsule &CapsuleNameMenu::GetCapsule() {
184 return parent->GetCapsule();
187 const Capsule &CapsuleNameMenu::GetCapsule() const {
188 return parent->GetCapsule();