Add TextFieldElement obSubmit

This commit is contained in:
MrLetsplay 2023-10-16 16:01:41 +02:00
parent 275c10258b
commit fabcf3cbef
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 34 additions and 5 deletions

View File

@ -206,8 +206,6 @@ TextFieldElement::TextFieldElement(UIValue x, UIValue y, UIValue w, std::shared_
this->focusColor = Colors::GRAY; this->focusColor = Colors::GRAY;
this->text = ""; this->text = "";
RectangleElement::color = color;
this->textElement = new TextElement(uiPx(0), uiPx(0)); this->textElement = new TextElement(uiPx(0), uiPx(0));
textElement->textBounds = TextBounds::LINE; textElement->textBounds = TextBounds::LINE;
textElement->color = Colors::BLACK; textElement->color = Colors::BLACK;
@ -264,7 +262,6 @@ UIElementType TextFieldElement::getType() {
} }
void TextFieldElement::focusEnter() { void TextFieldElement::focusEnter() {
RectangleElement::color = focusColor;
cursor->visible = true; cursor->visible = true;
capture = Input::captureKeyboardInput( capture = Input::captureKeyboardInput(
@ -290,6 +287,7 @@ void TextFieldElement::focusEnter() {
switch(event.key) { switch(event.key) {
case GLFW_KEY_ENTER: case GLFW_KEY_ENTER:
Input::uncaptureKeyboardInput(_this->capture); Input::uncaptureKeyboardInput(_this->capture);
_this->onSubmit(_this->text);
break; break;
case GLFW_KEY_LEFT: { case GLFW_KEY_LEFT: {
std::u32string str = Unicode::convertStdToU32(_this->text); std::u32string str = Unicode::convertStdToU32(_this->text);
@ -321,7 +319,6 @@ void TextFieldElement::focusEnter() {
} }
void TextFieldElement::focusExit() { void TextFieldElement::focusExit() {
RectangleElement::color = color;
cursor->visible = false; cursor->visible = false;
Input::uncaptureKeyboardInput(capture); Input::uncaptureKeyboardInput(capture);
@ -332,6 +329,7 @@ void TextFieldElement::focusExit() {
} }
void TextFieldElement::draw(UIPoint screenPos, glm::mat4 projection) { void TextFieldElement::draw(UIPoint screenPos, glm::mat4 projection) {
RectangleElement::color = focused ? focusColor : color;
RectangleElement::draw(screenPos, projection); RectangleElement::draw(screenPos, projection);
double time = glfwGetTime(); double time = glfwGetTime();

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ui.h" #include "ui.h"
#include "utils.h"
#include <string> #include <string>
namespace kek { namespace kek {
@ -96,6 +97,8 @@ class ButtonElement: public RectangleElement {
virtual void draw(UIPoint screenPos, glm::mat4 projection); virtual void draw(UIPoint screenPos, glm::mat4 projection);
}; };
typedef GenericCallable<std::string> SubmitCallback;
class TextFieldElement: public RectangleElement { class TextFieldElement: public RectangleElement {
protected: protected:
@ -114,6 +117,8 @@ class TextFieldElement: public RectangleElement {
TextElement *textElement; TextElement *textElement;
RectangleElement *cursor; RectangleElement *cursor;
SubmitCallback onSubmit;
TextFieldElement(UIValue x, UIValue y, UIValue w, std::shared_ptr<Font> font); TextFieldElement(UIValue x, UIValue y, UIValue w, std::shared_ptr<Font> font);
TextFieldElement(UIValue x, UIValue y, UIValue w); TextFieldElement(UIValue x, UIValue y, UIValue w);

View File

@ -8,11 +8,14 @@
#include <BulletCollision/CollisionShapes/btBoxShape.h> #include <BulletCollision/CollisionShapes/btBoxShape.h>
#include <btBulletCollisionCommon.h> #include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h> #include <btBulletDynamicsCommon.h>
#include <memory>
#include <string>
#include "internal.h" #include "internal.h"
#include "internal/physics.h" #include "internal/physics.h"
#include "ui.h" #include "ui.h"
#include "uielements.h" #include "uielements.h"
#include "utils.h"
using namespace kek; using namespace kek;
@ -126,9 +129,32 @@ int main(int argc, char **argv) {
UI::addElement(button3); UI::addElement(button3);
TextFieldElement *textField = new TextFieldElement(uiPx(10), uiPx(200), uiPx(500)); TextFieldElement *textField = new TextFieldElement(uiPx(10), uiPx(200), uiPx(500));
textField->color = Colors::GREEN;
textField->focusColor = Colors::RED;
textField->textElement->color = Colors::WHITE;
struct es {
ButtonElement *b;
TextFieldElement *t;
~es() {
std::cout << "oh nyo, I got destroyed" << std::endl;
}
} e;
e.b = button3;
e.t = textField;
textField->onSubmit = SubmitCallback([](std::string text, void *data) {
es *ep = (es *) data;
ep->b->text->setText(text);
ep->t->setText("");
ep->t->onSubmit = SubmitCallback();
},
&e);
UI::addElement(textField); UI::addElement(textField);
TextElement *text = new TextElement(uiPx(0), uiPx(260)); TextElement *text = new TextElement(uiPx(10), uiPx(260));
text->setText("Lorem ipsum\ndolor sit amet\nsussy amogus, KekEngine sample text\nWhen the impostor is\nAmogus"); text->setText("Lorem ipsum\ndolor sit amet\nsussy amogus, KekEngine sample text\nWhen the impostor is\nAmogus");
UI::addElement(text); UI::addElement(text);