#include "../common/GameState.h"
#include "../graphics/Font.h"
#include "../graphics/Frame.h"
+#include "../math/Vector.h"
#include <cstring>
using common::Capsule;
using common::Inventory;
using common::Item;
-using geometry::Vector;
+using math::Vector;
using graphics::Font;
using graphics::Frame;
CapsuleNameMenu::CapsuleNameMenu(CapsuleMenu *parent)
: parent(parent)
-, cursor(5) {
+, select(*parent->Res().capsuleNameCharSelectTemplate)
+, cursor(5)
+, first(true) {
+ bzero(buffer, 6);
std::strncpy(buffer, GetCapsule().Name(), 6);
- buffer[5] = '\0';
+ for (int i = 4; i > 0; --i) {
+ if (buffer[i] == '\0') {
+ buffer[i] = '_';
+ }
+ }
}
void CapsuleNameMenu::HandleEvents(const Input &input) {
- if (input.JustPressed(Input::START)) {
+ if (input.JustPressed(Input::PAD_UP)) {
+ select.PreviousRow();
+ }
+ if (input.JustPressed(Input::PAD_RIGHT)) {
+ select.NextCol();
+ }
+ if (input.JustPressed(Input::PAD_DOWN)) {
+ select.NextRow();
+ }
+ if (input.JustPressed(Input::PAD_LEFT)) {
+ select.PreviousCol();
+ }
+
+ if (input.JustPressed(Input::ACTION_A)) {
+ AddChar();
+ }
+ if (input.JustPressed(Input::ACTION_B)) {
+ RemoveChar();
+ }
+
+ if (input.JustPressed(Input::START) && cursor > 0) {
+ StoreName();
Ctrl().PopState();
}
}
-void CapsuleNameMenu::UpdateWorld(float deltaT) {
+void CapsuleNameMenu::AddChar() {
+ if (first) {
+ cursor = 1;
+ buffer[0] = select.Selected();
+ for (int i = 1; i < 5; ++i) {
+ buffer[i] = '_';
+ }
+ first = false;
+ } else {
+ if (cursor < 5) {
+ buffer[cursor] = select.Selected();
+ ++cursor;
+ } else {
+ // noise
+ }
+ }
+}
+
+void CapsuleNameMenu::RemoveChar() {
+ first = false;
+ if (cursor > 0) {
+ --cursor;
+ buffer[cursor] = '_';
+ }
+}
+
+void CapsuleNameMenu::StoreName() {
+ // NOTE: this will leak the memory allocated for the new name
+ char *name = 0;
+ for (int i = 0; i < 6; ++i) {
+ if (buffer[i] == '_' || buffer[i] == '\0') {
+ buffer[i] = '\0';
+ name = new char[i + 1];
+ std::strncpy(name, buffer, i + 1);
+ break;
+ }
+ }
+ if (name != 0) {
+ GetCapsule().SetName(name);
+ }
+}
+
+void CapsuleNameMenu::UpdateWorld(Uint32 deltaT) {
}
void CapsuleNameMenu::RenderAlphabet(SDL_Surface *screen, const Vector<int> &offset) const {
const Font &font(*parent->Res().normalFont);
const Frame &frame(*parent->Res().statusFrame);
+ const Vector<int> selectOffset(2 * font.CharWidth(), 2 * font.CharHeight());
frame.Draw(screen, offset, 24 * font.CharWidth(), 17 * font.CharHeight());
+ select.Draw(screen, offset + selectOffset);
}
return parent->Height();
}
+Capsule &CapsuleNameMenu::GetCapsule() {
+ return parent->GetCapsule();
+}
+
const Capsule &CapsuleNameMenu::GetCapsule() const {
return parent->GetCapsule();
}