Add delete char callback

This commit is contained in:
MrLetsplay 2022-11-06 09:56:55 +01:00
parent eb45f02afa
commit 1941060d4c
4 changed files with 22 additions and 8 deletions

View File

@ -51,7 +51,11 @@ static void onCursorPosCallback(GLFWwindow *window, double x, double y) {
static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if(Input::isKeyboardCaptured()) { if(Input::isKeyboardCaptured()) {
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) Input::uncaptureKeyboardInput(kekData.activeKeyboardCapture.id); if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
Input::uncaptureKeyboardInput(kekData.activeKeyboardCapture.id);
}else if(key == GLFW_KEY_BACKSPACE && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
kekData.activeKeyboardCapture.callback(window, KEK_INPUT_DELETE);
}
return; return;
} }

View File

@ -101,17 +101,21 @@ GLFWCursorMode getCursorMode() {
KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback) { KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback) {
KeyboardCapture id = nextID++; KeyboardCapture id = nextID++;
kekData.activeKeyboardCapture.id = id; kekData.activeKeyboardCapture = {
kekData.activeKeyboardCapture.callback = callback; id,
kekData.activeKeyboardCapture.uncaptureCallback = uncaptureCallback; callback,
uncaptureCallback
};
return id; return id;
} }
bool uncaptureKeyboardInput(KeyboardCapture capture) { bool uncaptureKeyboardInput(KeyboardCapture capture) {
if(capture == KEK_INVALID_ID || capture != kekData.activeKeyboardCapture.id) return false; if(capture == KEK_INVALID_ID || capture != kekData.activeKeyboardCapture.id) return false;
kekData.activeKeyboardCapture.id = KEK_INVALID_ID; kekData.activeKeyboardCapture = {
kekData.activeKeyboardCapture.callback = KeyCharCallback(); KEK_INVALID_ID,
kekData.activeKeyboardCapture.uncaptureCallback = Callable(); KeyCharCallback(),
Callable()
};
return true; return true;
} }

View File

@ -40,3 +40,5 @@
#define KEK_DEFAULT_FONT "font/MaredivRegular-yeg3.ttf" #define KEK_DEFAULT_FONT "font/MaredivRegular-yeg3.ttf"
#define KEK_DEFAULT_FONT_SIZE_PIXELS 24 #define KEK_DEFAULT_FONT_SIZE_PIXELS 24
#define KEK_INPUT_DELETE -1u

View File

@ -23,7 +23,11 @@ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, v
void onButtonClick(void *data) { void onButtonClick(void *data) {
Input::captureKeyboardInput(KeyCharCallback([](GLFWwindow *window, unsigned int codepoint, void *data) { Input::captureKeyboardInput(KeyCharCallback([](GLFWwindow *window, unsigned int codepoint, void *data) {
std::u32string str = Unicode::convertStdToU32(button->text->getText()); std::u32string str = Unicode::convertStdToU32(button->text->getText());
if(codepoint == KEK_INPUT_DELETE) {
str = str.substr(0, str.length() - 1);
}else {
str.push_back(codepoint); str.push_back(codepoint);
}
button->text->setText(Unicode::convertU32ToStd(str)); button->text->setText(Unicode::convertU32ToStd(str));
}, nullptr), Callable()); }, nullptr), Callable());
} }