text box caret
This commit is contained in:
@@ -36,6 +36,14 @@ std::wstring Label::getText() const {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Label::setFontName(std::string name) {
|
||||||
|
this->fontName = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& Label::getFontName() const {
|
||||||
|
return fontName;
|
||||||
|
}
|
||||||
|
|
||||||
void Label::draw(const GfxContext* pctx, Assets* assets) {
|
void Label::draw(const GfxContext* pctx, Assets* assets) {
|
||||||
if (supplier) {
|
if (supplier) {
|
||||||
setText(supplier());
|
setText(supplier());
|
||||||
@@ -246,6 +254,24 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
|
|||||||
setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f));
|
setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextBox::draw(const GfxContext* pctx, Assets* assets) {
|
||||||
|
Panel::draw(pctx, assets);
|
||||||
|
|
||||||
|
if (!isFocused())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (int((Window::time() - caretLastMove) * 2) % 2 == 0) {
|
||||||
|
auto batch = pctx->getBatch2D();
|
||||||
|
batch->texture(nullptr);
|
||||||
|
batch->color = glm::vec4(1.0f);
|
||||||
|
|
||||||
|
glm::vec2 lcoord = label->calcCoord();
|
||||||
|
auto font = assets->getFont(label->getFontName());
|
||||||
|
int width = font->calcWidth(input.substr(0, caret));
|
||||||
|
batch->rect(lcoord.x + width, lcoord.y, 2, font->getLineHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||||
glm::vec2 coord = calcCoord();
|
glm::vec2 coord = calcCoord();
|
||||||
|
|
||||||
@@ -279,11 +305,22 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
|||||||
setScrollable(false);
|
setScrollable(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::typed(unsigned int codepoint) {
|
void TextBox::paste(const std::wstring& text) {
|
||||||
input += std::wstring({(wchar_t)codepoint});
|
if (caret >= input.length()) {
|
||||||
|
input += text;
|
||||||
|
} else {
|
||||||
|
auto left = input.substr(0, caret);
|
||||||
|
auto right = input.substr(caret);
|
||||||
|
input = left + text + right;
|
||||||
|
}
|
||||||
|
caret += text.length();
|
||||||
validate();
|
validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextBox::typed(unsigned int codepoint) {
|
||||||
|
paste(std::wstring({(wchar_t)codepoint}));
|
||||||
|
}
|
||||||
|
|
||||||
bool TextBox::validate() {
|
bool TextBox::validate() {
|
||||||
if (validator) {
|
if (validator) {
|
||||||
valid = validator(getText());
|
valid = validator(getText());
|
||||||
@@ -308,6 +345,7 @@ void TextBox::setOnEditStart(runnable oneditstart) {
|
|||||||
void TextBox::focus(GUI* gui) {
|
void TextBox::focus(GUI* gui) {
|
||||||
Panel::focus(gui);
|
Panel::focus(gui);
|
||||||
if (onEditStart){
|
if (onEditStart){
|
||||||
|
caret = input.size();
|
||||||
onEditStart();
|
onEditStart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,8 +357,12 @@ void TextBox::refresh() {
|
|||||||
|
|
||||||
void TextBox::keyPressed(int key) {
|
void TextBox::keyPressed(int key) {
|
||||||
if (key == keycode::BACKSPACE) {
|
if (key == keycode::BACKSPACE) {
|
||||||
if (!input.empty()){
|
if (caret > 0 && input.length() > 0) {
|
||||||
input = input.substr(0, input.length()-1);
|
if (caret > input.length()) {
|
||||||
|
caret = input.length();
|
||||||
|
}
|
||||||
|
input = input.substr(0, caret-1) + input.substr(caret);
|
||||||
|
caret--;
|
||||||
validate();
|
validate();
|
||||||
}
|
}
|
||||||
} else if (key == keycode::ENTER) {
|
} else if (key == keycode::ENTER) {
|
||||||
@@ -328,13 +370,26 @@ void TextBox::keyPressed(int key) {
|
|||||||
consumer(label->getText());
|
consumer(label->getText());
|
||||||
}
|
}
|
||||||
defocus();
|
defocus();
|
||||||
|
} else if (key == keycode::LEFT) {
|
||||||
|
if (caret > 0) {
|
||||||
|
if (caret > input.length()) {
|
||||||
|
caret = input.length()-1;
|
||||||
|
} else {
|
||||||
|
caret--;
|
||||||
|
}
|
||||||
|
caretLastMove = Window::time();
|
||||||
|
}
|
||||||
|
} else if (key == keycode::RIGHT) {
|
||||||
|
if (caret < input.length()) {
|
||||||
|
caret++;
|
||||||
|
caretLastMove = Window::time();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Pasting text from clipboard
|
// Pasting text from clipboard
|
||||||
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) {
|
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) {
|
||||||
const char* text = Window::getClipboardText();
|
const char* text = Window::getClipboardText();
|
||||||
if (text) {
|
if (text) {
|
||||||
input += util::str2wstr_utf8(text);
|
paste(util::str2wstr_utf8(text));
|
||||||
validate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -389,6 +444,14 @@ void TextBox::setPlaceholder(const std::wstring& placeholder) {
|
|||||||
this->placeholder = placeholder;
|
this->placeholder = placeholder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint TextBox::getCaret() const {
|
||||||
|
return caret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TextBox::setCaret(uint position) {
|
||||||
|
this->caret = position;
|
||||||
|
}
|
||||||
|
|
||||||
// ============================== InputBindBox ================================
|
// ============================== InputBindBox ================================
|
||||||
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
|
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
|
||||||
: Panel(glm::vec2(100,32), padding, 0),
|
: Panel(glm::vec2(100,32), padding, 0),
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ namespace gui {
|
|||||||
virtual void setText(std::wstring text);
|
virtual void setText(std::wstring text);
|
||||||
std::wstring getText() const;
|
std::wstring getText() const;
|
||||||
|
|
||||||
|
virtual void setFontName(std::string name);
|
||||||
|
const std::string& getFontName() const;
|
||||||
|
|
||||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||||
|
|
||||||
virtual void textSupplier(wstringsupplier supplier);
|
virtual void textSupplier(wstringsupplier supplier);
|
||||||
@@ -105,12 +108,18 @@ namespace gui {
|
|||||||
wstringchecker validator = nullptr;
|
wstringchecker validator = nullptr;
|
||||||
runnable onEditStart = nullptr;
|
runnable onEditStart = nullptr;
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
|
/// @brief text input pointer, value may be greather than text length
|
||||||
|
uint caret = 0;
|
||||||
|
double caretLastMove = 0.0;
|
||||||
|
|
||||||
|
void paste(const std::wstring& text);
|
||||||
public:
|
public:
|
||||||
TextBox(std::wstring placeholder,
|
TextBox(std::wstring placeholder,
|
||||||
glm::vec4 padding=glm::vec4(4.0f));
|
glm::vec4 padding=glm::vec4(4.0f));
|
||||||
|
|
||||||
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
|
||||||
|
|
||||||
|
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||||
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
|
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
|
||||||
virtual void typed(unsigned int codepoint) override;
|
virtual void typed(unsigned int codepoint) override;
|
||||||
virtual void keyPressed(int key) override;
|
virtual void keyPressed(int key) override;
|
||||||
@@ -128,6 +137,8 @@ namespace gui {
|
|||||||
virtual void setText(std::wstring value);
|
virtual void setText(std::wstring value);
|
||||||
virtual std::wstring getPlaceholder() const;
|
virtual std::wstring getPlaceholder() const;
|
||||||
virtual void setPlaceholder(const std::wstring&);
|
virtual void setPlaceholder(const std::wstring&);
|
||||||
|
virtual uint getCaret() const;
|
||||||
|
virtual void setCaret(uint position);
|
||||||
virtual bool validate();
|
virtual bool validate();
|
||||||
virtual void setValid(bool valid);
|
virtual void setValid(bool valid);
|
||||||
virtual bool isValid() const;
|
virtual bool isValid() const;
|
||||||
|
|||||||
@@ -385,13 +385,15 @@ void Hud::update(bool visible) {
|
|||||||
contentAccess->setMinSize(glm::vec2(1, Window::height));
|
contentAccess->setMinSize(glm::vec2(1, Window::height));
|
||||||
hotbarView->setVisible(visible);
|
hotbarView->setVisible(visible);
|
||||||
|
|
||||||
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
|
if (!gui->isFocusCaught() && !pause) {
|
||||||
if (Events::jpressed(i)) {
|
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
|
||||||
player->setChosenSlot(i - keycode::NUM_1);
|
if (Events::jpressed(i)) {
|
||||||
|
player->setChosenSlot(i - keycode::NUM_1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Events::jpressed(keycode::NUM_0)) {
|
||||||
|
player->setChosenSlot(9);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (Events::jpressed(keycode::NUM_0)) {
|
|
||||||
player->setChosenSlot(9);
|
|
||||||
}
|
}
|
||||||
if (!pause && !inventoryOpen && Events::scroll) {
|
if (!pause && !inventoryOpen && Events::scroll) {
|
||||||
int slot = player->getChosenSlot();
|
int slot = player->getChosenSlot();
|
||||||
|
|||||||
Reference in New Issue
Block a user