TextBox validation + minor refactor, fixes

This commit is contained in:
MihailRis
2024-01-11 02:37:29 +03:00
parent 61ad839ebf
commit e1d30732df
7 changed files with 85 additions and 53 deletions
+39 -2
View File
@@ -144,7 +144,19 @@ TextBox::TextBox(wstring placeholder, vec4 padding)
void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord();
batch->texture(nullptr);
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_));
if (valid) {
if (isfocused()) {
batch->color = focusedColor;
} else if (hover_) {
batch->color = hoverColor;
} else {
batch->color = color_;
}
} else {
batch->color = invalidColor;
}
batch->rect(coord.x, coord.y, size_.x, size_.y);
if (!focused_ && supplier) {
input = supplier();
@@ -162,6 +174,24 @@ void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
void TextBox::typed(unsigned int codepoint) {
input += wstring({(wchar_t)codepoint});
validate();
}
bool TextBox::validate() {
if (validator) {
valid = validator(input);
} else {
valid = true;
}
return valid;
}
void TextBox::setValid(bool valid) {
this->valid = valid;
}
bool TextBox::isValid() const {
return valid;
}
void TextBox::keyPressed(int key) {
@@ -169,10 +199,11 @@ void TextBox::keyPressed(int key) {
case KEY_BACKSPACE:
if (!input.empty()){
input = input.substr(0, input.length()-1);
validate();
}
break;
case KEY_ENTER:
if (consumer) {
if (validate() && consumer) {
consumer(label->text());
}
defocus();
@@ -183,6 +214,7 @@ void TextBox::keyPressed(int key) {
const char* text = Window::getClipboardText();
if (text) {
input += util::str2wstr_utf8(text);
validate();
}
}
}
@@ -199,6 +231,10 @@ void TextBox::textConsumer(wstringconsumer consumer) {
this->consumer = consumer;
}
void TextBox::textValidator(wstringchecker validator) {
this->validator = validator;
}
wstring TextBox::text() const {
if (input.empty())
return placeholder;
@@ -297,6 +333,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
// ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
margin(vec4(0.0f, 0.0f, 5.0f, 0.0f));
}
void CheckBox::draw(Batch2D* batch, Assets* assets) {
+9
View File
@@ -23,6 +23,8 @@ namespace gui {
typedef std::function<bool()> boolsupplier;
typedef std::function<void(bool)> boolconsumer;
typedef std::function<bool(const std::wstring&)> wstringchecker;
class Label : public UINode {
protected:
std::wstring text_;
@@ -71,11 +73,14 @@ namespace gui {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
Label* label;
std::wstring input;
std::wstring placeholder;
wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr;
wstringchecker validator = nullptr;
bool valid = true;
public:
TextBox(std::wstring placeholder,
glm::vec4 padding=glm::vec4(2.0f));
@@ -87,8 +92,12 @@ namespace gui {
virtual void keyPressed(int key) override;
virtual void textSupplier(wstringsupplier supplier);
virtual void textConsumer(wstringconsumer consumer);
virtual void textValidator(wstringchecker validator);
virtual bool isfocuskeeper() const override {return true;}
virtual std::wstring text() const;
virtual bool validate();
virtual void setValid(bool valid);
virtual bool isValid() const;
};
class InputBindBox : public Panel {