Files
minecraft-pe-0.6.1/src/client/gui/components/TextBox.cpp

91 lines
2.1 KiB
C++
Executable File

#include "TextBox.h"
#include "../../Minecraft.h"
#include "../../../AppPlatform.h"
#include "platform/input/Mouse.h"
TextBox::TextBox( int id, const std::string& msg )
: TextBox(id, 0, 0, msg) {}
TextBox::TextBox( int id, int x, int y, const std::string& msg )
: TextBox(id, x, y, 24, msg) {}
TextBox::TextBox( int id, int x, int y, int w, const std::string& msg )
: GuiElement(true, true, x, y, w, Font::DefaultLineHeight + 4),
id(id), hint(msg), focused(false), blink(false) {}
void TextBox::setFocus(Minecraft* minecraft) {
if(!focused) {
minecraft->platform()->showKeyboard();
focused = true;
blinkTicks = 0;
blink = false;
}
}
bool TextBox::loseFocus(Minecraft* minecraft) {
if(focused) {
minecraft->platform()->showKeyboard();
focused = false;
return true;
}
return false;
}
void TextBox::mouseClicked(Minecraft* minecraft, int x, int y, int buttonNum) {
if (buttonNum == MouseAction::ACTION_LEFT) {
if (pointInside(x, y)) {
setFocus(minecraft);
} else {
loseFocus(minecraft);
}
}
}
void TextBox::handleChar(char c) {
if (focused) {
text.push_back(c);
}
}
void TextBox::handleKey(int key) {
if (focused && key == Keyboard::KEY_BACKSPACE && !text.empty()) {
text.pop_back();
}
}
void TextBox::tick(Minecraft* minecraft) {
blinkTicks++;
if (blinkTicks >= 5) {
blink = !blink;
blinkTicks = 0;
}
}
void TextBox::render( Minecraft* minecraft, int xm, int ym ) {
// textbox like in beta 1.7.3
fill(x, y, x + width, y + height, 0xffa0a0a0);
fill(x + 1, y + 1, x + width - 1, y + height - 1, 0xff000000);
glEnable2(GL_SCISSOR_TEST);
glScissor(
Gui::GuiScale * (x + 2),
minecraft->height - Gui::GuiScale * (y + height - 2),
Gui::GuiScale * (width - 2),
Gui::GuiScale * (height - 2)
);
if (text.empty() && !focused) {
drawString(minecraft->font, hint, x + 2, y + 2, 0xff5e5e5e);
}
if (focused && blink) text.push_back('_');
drawString(minecraft->font, text, x + 2, y + 2, 0xffffffff);
if (focused && blink) text.pop_back();
glDisable2(GL_SCISSOR_TEST);
}