#include "textbox.h" #include "defConfigs.h" #include "util.h" using namespace game::core; using namespace game::util; using namespace irr::core; using namespace irr::video; namespace game{ namespace gui{ Textbox::TextboxButton::TextboxButton(Textbox *t, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { textbox = t; } void Textbox::TextboxButton::onClick() { if (!textbox->isEnabled()) textbox->enable(); else textbox->disable(); } Textbox::Textbox(vector2d pos, vector2d size) { this->pos = pos; this->size = size; textboxButton = new TextboxButton(this, pos, size, "TextboxButton", false); font = GameManager::getSingleton()->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/bigfont.png"); } Textbox::~Textbox() {} void Textbox::update() { GameManager *gm = GameManager::getSingleton(); gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 200, 200), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); font->draw(entry, rect(pos.X, pos.Y - 8, pos.X + size.X, pos.Y - 8 + size.Y), SColor(255, 255, 255, 255)); if (enabled) { if (canChangeCursor()) { canShowCursor = !canShowCursor; lastBlinkTime = getTime(); } if (canShowCursor) gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 100, 100, 100), rect(pos.X + 20 * (entry.size() - cursorPosOffset), pos.Y, pos.X + 4 + 20 * (entry.size() - cursorPosOffset), pos.Y + size.Y), nullptr); } } void Textbox::type(wchar_t ch) { if (!capitalLeters) ch = tolower(ch); else ch = toupper(ch); std::vector v; for (int i = 0; i < entry.size(); i++) v.push_back(entry[i]); v.insert(v.end() - cursorPosOffset, ch); entry += 0; for (int i = 0; i < v.size(); i++) entry[i] = v[i]; } void Textbox::moveCursor(bool left) { if (left && entry.size() - cursorPosOffset > 0) cursorPosOffset++; else if (!left && cursorPosOffset > 0) cursorPosOffset--; } void Textbox::deleteCharacter() { if (cursorPosOffset != entry.size()) { std::vector v; for (int i = 0; i < entry.size(); i++) v.push_back(entry[i]); v.erase(v.end() - 1 - cursorPosOffset); entry.erase(entry.size() - 1); for (int i = 0; i < v.size(); i++) entry[i] = v[i]; } } void Textbox::enable() { enabled = true; lastBlinkTime = getTime(); } void Textbox::disable() { enabled = false; } } }