1 #include "CharSelect.h"
5 #include "../loader/Interpreter.h"
6 #include "../loader/TypeDescription.h"
11 using loader::FieldDescription;
12 using loader::Interpreter;
13 using loader::TypeDescription;
17 CharSelect::CharSelect()
30 void CharSelect::Draw(SDL_Surface *screen, const Vector<int> &positionIn) const {
31 Vector<int> position(positionIn);
32 Vector<int> lineHead(positionIn);
33 const Vector<int> step(2 * font->CharWidth(), 0);
34 const Vector<int> doubleStep(3 * font->CharWidth(), 0);
35 const Vector<int> newline(0, 2 * font->CharHeight());
36 const Vector<int> doubleNewline(0, 3 * font->CharHeight());
37 const Vector<int> cursorOffset(font->CharWidth() / 2, font->CharHeight() / 2);
39 for (int i = 0; chars[i] != '\0'; ++i) {
41 cursor->DrawCenter(screen, position + cursorOffset);
44 font->DrawChar(chars[i], screen, position);
46 if (i % width == width - 1) {
47 if (groupY > 0 && (i / width) % groupY == groupY - 1) {
48 lineHead += doubleNewline;
54 if (groupX > 0 && (i % width) % groupX == groupX - 1) {
55 position += doubleStep;
64 void CharSelect::NextCol() {
66 if (selected % width == 0) {
71 void CharSelect::PreviousCol() {
73 if (selected < 0 || selected % width == width - 1) {
78 void CharSelect::NextRow() {
80 if (selected >= numChars) {
85 void CharSelect::PreviousRow() {
93 char CharSelect::Selected() const {
94 return chars[selected];
98 void CharSelect::CreateTypeDescription() {
101 TypeDescription &td(TypeDescription::Create(TYPE_ID, "CharSelect"));
102 td.SetConstructor(&Construct);
104 td.SetSize(sizeof(CharSelect));
106 td.AddField("font", FieldDescription(((char *)&c.font) - ((char *)&c), Font::TYPE_ID).SetReferenced().SetDescription("the font to use for characters"));
107 td.AddField("cursor", FieldDescription(((char *)&c.cursor) - ((char *)&c), Sprite::TYPE_ID).SetReferenced().SetDescription("sprite for the cursor"));
108 td.AddField("chars", FieldDescription(((char *)&c.chars) - ((char *)&c), Interpreter::STRING_ID).SetReferenced().SetDescription("characters to select from"));
110 td.AddField("width", FieldDescription(((char *)&c.width) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("the width of one row"));
111 td.AddField("groupX", FieldDescription(((char *)&c.groupX) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("double space after this many columns"));
112 td.AddField("groupY", FieldDescription(((char *)&c.groupY) - ((char *)&c), Interpreter::NUMBER_ID).SetDescription("double space after this many rows"));
115 void CharSelect::Construct(void *data) {
116 new (data) CharSelect;
119 void CharSelect::Load(void *data) {
120 CharSelect *c = reinterpret_cast<CharSelect *>(data);
121 c->numChars = std::strlen(c->chars);