]> git.localhorst.tv Git - l2e.git/blobdiff - src/graphics/CharSelect.cpp
implemented capsule renaming
[l2e.git] / src / graphics / CharSelect.cpp
diff --git a/src/graphics/CharSelect.cpp b/src/graphics/CharSelect.cpp
new file mode 100644 (file)
index 0000000..415c7a6
--- /dev/null
@@ -0,0 +1,124 @@
+#include "CharSelect.h"
+
+#include "Font.h"
+#include "Sprite.h"
+#include "../loader/Interpreter.h"
+#include "../loader/TypeDescription.h"
+
+#include <cstring>
+
+using geometry::Vector;
+using loader::FieldDescription;
+using loader::Interpreter;
+using loader::TypeDescription;
+
+namespace graphics {
+
+CharSelect::CharSelect()
+: font(0)
+, cursor(0)
+, chars("")
+, numChars(0)
+, width(10)
+, groupX(0)
+, groupY(0)
+, selected(0) {
+
+}
+
+
+void CharSelect::Draw(SDL_Surface *screen, const Vector<int> &positionIn) const {
+       Vector<int> position(positionIn);
+       Vector<int> lineHead(positionIn);
+       const Vector<int> step(2 * font->CharWidth(), 0);
+       const Vector<int> doubleStep(3 * font->CharWidth(), 0);
+       const Vector<int> newline(0, 2 * font->CharHeight());
+       const Vector<int> doubleNewline(0, 3 * font->CharHeight());
+       const Vector<int> cursorOffset(font->CharWidth() / 2, font->CharHeight() / 2);
+
+       for (int i = 0; chars[i] != '\0'; ++i) {
+               if (i == selected) {
+                       cursor->DrawCenter(screen, position + cursorOffset);
+               }
+
+               font->DrawChar(chars[i], screen, position);
+
+               if (i % width == width - 1) {
+                       if (groupY > 0 && (i / width) % groupY == groupY - 1) {
+                               lineHead += doubleNewline;
+                       } else {
+                               lineHead += newline;
+                       }
+                       position = lineHead;
+               } else {
+                       if (groupX > 0 && (i % width) % groupX == groupX - 1) {
+                               position += doubleStep;
+                       } else {
+                               position += step;
+                       }
+               }
+       }
+}
+
+
+void CharSelect::NextCol() {
+       ++selected;
+       if (selected % width == 0) {
+               selected -= width;
+       }
+}
+
+void CharSelect::PreviousCol() {
+       --selected;
+       if (selected < 0 || selected % width == width - 1) {
+               selected += width;
+       }
+}
+
+void CharSelect::NextRow() {
+       selected += width;
+       if (selected >= numChars) {
+               selected -= numChars;
+       }
+}
+
+void CharSelect::PreviousRow() {
+       selected -= width;
+       if (selected < 0) {
+               selected += numChars;
+       }
+}
+
+
+char CharSelect::Selected() const {
+       return chars[selected];
+}
+
+
+void CharSelect::CreateTypeDescription() {
+       CharSelect c;
+
+       TypeDescription &td(TypeDescription::Create(TYPE_ID, "CharSelect"));
+       td.SetConstructor(&Construct);
+       td.SetLoader(&Load);
+       td.SetSize(sizeof(CharSelect));
+
+       td.AddField("font", FieldDescription(((char *)&c.font) - ((char *)&c), Font::TYPE_ID).SetReferenced().SetDescription("the font to use for characters"));
+       td.AddField("cursor", FieldDescription(((char *)&c.cursor) - ((char *)&c), Sprite::TYPE_ID).SetReferenced().SetDescription("sprite for the cursor"));
+       td.AddField("chars", FieldDescription(((char *)&c.chars) - ((char *)&c), Interpreter::STRING_ID).SetReferenced().SetDescription("characters to select from"));
+
+       td.AddField("width", FieldDescription(((char *)&c.width) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("the width of one row"));
+       td.AddField("groupX", FieldDescription(((char *)&c.groupX) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("double space after this many columns"));
+       td.AddField("groupY", FieldDescription(((char *)&c.groupY) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("double space after this many rows"));
+}
+
+void CharSelect::Construct(void *data) {
+       new (data) CharSelect;
+}
+
+void CharSelect::Load(void *data) {
+       CharSelect *c = reinterpret_cast<CharSelect *>(data);
+       c->numChars = std::strlen(c->chars);
+}
+
+}