X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=src%2Fgraphics%2FCharSelect.cpp;fp=src%2Fgraphics%2FCharSelect.cpp;h=415c7a623f9247db2f9e26ae23d34f25d276b961;hb=9666839d0ca6c794d28226a007870c82ef4ddb20;hp=0000000000000000000000000000000000000000;hpb=60e0fe29ce6cd033edc78b181d9d07fa72c11172;p=l2e.git diff --git a/src/graphics/CharSelect.cpp b/src/graphics/CharSelect.cpp new file mode 100644 index 0000000..415c7a6 --- /dev/null +++ b/src/graphics/CharSelect.cpp @@ -0,0 +1,124 @@ +#include "CharSelect.h" + +#include "Font.h" +#include "Sprite.h" +#include "../loader/Interpreter.h" +#include "../loader/TypeDescription.h" + +#include + +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 &positionIn) const { + Vector position(positionIn); + Vector lineHead(positionIn); + const Vector step(2 * font->CharWidth(), 0); + const Vector doubleStep(3 * font->CharWidth(), 0); + const Vector newline(0, 2 * font->CharHeight()); + const Vector doubleNewline(0, 3 * font->CharHeight()); + const Vector 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(data); + c->numChars = std::strlen(c->chars); +} + +}