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"
17 using common::Capsule;
18 using common::Inventory;
20 using geometry::Vector;
22 using graphics::Frame;
26 CapsuleNameMenu::CapsuleNameMenu(CapsuleMenu *parent)
28 , select(*parent->Res().capsuleNameCharSelectTemplate)
32 std::strncpy(buffer, GetCapsule().Name(), 6);
33 for (int i = 4; i > 0; --i) {
34 if (buffer[i] == '\0') {
41 void CapsuleNameMenu::OnEnterState(SDL_Surface *) {
45 void CapsuleNameMenu::OnExitState(SDL_Surface *) {
49 void CapsuleNameMenu::OnResumeState(SDL_Surface *) {
53 void CapsuleNameMenu::OnPauseState(SDL_Surface *) {
58 void CapsuleNameMenu::OnResize(int width, int height) {
63 void CapsuleNameMenu::HandleEvents(const Input &input) {
64 if (input.JustPressed(Input::PAD_UP)) {
67 if (input.JustPressed(Input::PAD_RIGHT)) {
70 if (input.JustPressed(Input::PAD_DOWN)) {
73 if (input.JustPressed(Input::PAD_LEFT)) {
77 if (input.JustPressed(Input::ACTION_A)) {
80 if (input.JustPressed(Input::ACTION_B)) {
84 if (input.JustPressed(Input::START) && cursor > 0) {
90 void CapsuleNameMenu::AddChar() {
93 buffer[0] = select.Selected();
94 for (int i = 1; i < 5; ++i) {
100 buffer[cursor] = select.Selected();
108 void CapsuleNameMenu::RemoveChar() {
112 buffer[cursor] = '_';
116 void CapsuleNameMenu::StoreName() {
117 // NOTE: this will leak the memory allocated for the new name
119 for (int i = 0; i < 6; ++i) {
120 if (buffer[i] == '_' || buffer[i] == '\0') {
122 name = new char[i + 1];
123 std::strncpy(name, buffer, i + 1);
128 GetCapsule().SetName(name);
132 void CapsuleNameMenu::UpdateWorld(float deltaT) {
136 void CapsuleNameMenu::Render(SDL_Surface *screen) {
137 const Font &font(*parent->Res().statusFont);
138 const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
139 const Vector<int> nameOffset(
140 4 * font.CharWidth(),
141 4 * font.CharHeight() - font.CharHeight() / 8);
142 const Vector<int> alphaOffset(
143 4 * font.CharWidth(),
144 7 * font.CharHeight() - font.CharHeight() / 8);
146 parent->RenderBackground(screen);
147 RenderName(screen, offset + nameOffset);
148 RenderAlphabet(screen, offset + alphaOffset);
151 void CapsuleNameMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
152 const Font &font(*parent->Res().normalFont);
153 const Frame &frame(*parent->Res().statusFrame);
154 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
155 const Vector<int> namebgOffset(8 * font.CharWidth(), 0);
156 const Vector<int> nameOffset(13 * font.CharWidth(), font.CharHeight());
158 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
159 font.DrawString(parent->Res().capsuleNameLabel, screen, offset + labelOffset, 5);
160 frame.Draw(screen, offset + namebgOffset, 16 * font.CharWidth(), 3 * font.CharHeight());
161 font.DrawString(buffer, screen, offset + nameOffset, 5);
164 void CapsuleNameMenu::RenderAlphabet(SDL_Surface *screen, const Vector<int> &offset) const {
165 const Font &font(*parent->Res().normalFont);
166 const Frame &frame(*parent->Res().statusFrame);
167 const Vector<int> selectOffset(2 * font.CharWidth(), 2 * font.CharHeight());
169 frame.Draw(screen, offset, 24 * font.CharWidth(), 17 * font.CharHeight());
170 select.Draw(screen, offset + selectOffset);
174 int CapsuleNameMenu::Width() const {
175 return parent->Width();
178 int CapsuleNameMenu::Height() const {
179 return parent->Height();
182 Capsule &CapsuleNameMenu::GetCapsule() {
183 return parent->GetCapsule();
186 const Capsule &CapsuleNameMenu::GetCapsule() const {
187 return parent->GetCapsule();