Add KeyCallback to captureKeyboardInput

This commit is contained in:
MrLetsplay 2022-11-07 09:05:00 +01:00
parent 1941060d4c
commit 6c88bcd385
5 changed files with 24 additions and 10 deletions

View File

@ -53,9 +53,14 @@ static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action,
if(Input::isKeyboardCaptured()) { if(Input::isKeyboardCaptured()) {
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
Input::uncaptureKeyboardInput(kekData.activeKeyboardCapture.id); Input::uncaptureKeyboardInput(kekData.activeKeyboardCapture.id);
}else if(key == GLFW_KEY_BACKSPACE && (action == GLFW_PRESS || action == GLFW_REPEAT)) { return;
kekData.activeKeyboardCapture.callback(window, KEK_INPUT_DELETE);
} }
if(key == GLFW_KEY_BACKSPACE && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
kekData.activeKeyboardCapture.charCallback(window, KEK_INPUT_DELETE);
}
kekData.activeKeyboardCapture.keyCallback(window, key, scancode, action, mods);
return; return;
} }
@ -66,7 +71,7 @@ static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action,
static void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) { static void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) {
if(Input::isKeyboardCaptured()) { if(Input::isKeyboardCaptured()) {
kekData.activeKeyboardCapture.callback(window, codepoint); kekData.activeKeyboardCapture.charCallback(window, codepoint);
return; return;
} }

View File

@ -99,11 +99,12 @@ GLFWCursorMode getCursorMode() {
return kekData.uiCursorMode; return kekData.uiCursorMode;
} }
KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback) { KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) {
KeyboardCapture id = nextID++; KeyboardCapture id = nextID++;
kekData.activeKeyboardCapture = { kekData.activeKeyboardCapture = {
id, id,
callback, charCallback,
keyCallback,
uncaptureCallback uncaptureCallback
}; };
return id; return id;
@ -114,6 +115,7 @@ bool uncaptureKeyboardInput(KeyboardCapture capture) {
kekData.activeKeyboardCapture = { kekData.activeKeyboardCapture = {
KEK_INVALID_ID, KEK_INVALID_ID,
KeyCharCallback(), KeyCharCallback(),
KeyCallback(),
Callable() Callable()
}; };
return true; return true;

View File

@ -63,7 +63,7 @@ GLFWKeyState getKeyState(KeyBinding mapping);
void setCursorMode(GLFWCursorMode mode); void setCursorMode(GLFWCursorMode mode);
GLFWCursorMode getCursorMode(); GLFWCursorMode getCursorMode();
KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback); KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback);
bool uncaptureKeyboardInput(KeyboardCapture capture); bool uncaptureKeyboardInput(KeyboardCapture capture);
bool isKeyboardCaptured(); bool isKeyboardCaptured();

View File

@ -15,7 +15,8 @@ namespace kek {
struct ActiveKeyboardCapture { struct ActiveKeyboardCapture {
KeyboardCapture id = KEK_INVALID_ID; KeyboardCapture id = KEK_INVALID_ID;
KeyCharCallback callback; KeyCharCallback charCallback;
KeyCallback keyCallback;
Callable uncaptureCallback; Callable uncaptureCallback;
}; };

View File

@ -7,6 +7,7 @@
using namespace kek; using namespace kek;
static ButtonElement *button; static ButtonElement *button;
static KeyboardCapture capture = KEK_INVALID_ID;
void periodicCallback(GLFWwindow *window, void *data){ void periodicCallback(GLFWwindow *window, void *data){
@ -21,7 +22,7 @@ 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) { capture = 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) { if(codepoint == KEK_INPUT_DELETE) {
str = str.substr(0, str.length() - 1); str = str.substr(0, str.length() - 1);
@ -29,11 +30,15 @@ void onButtonClick(void *data) {
str.push_back(codepoint); str.push_back(codepoint);
} }
button->text->setText(Unicode::convertU32ToStd(str)); button->text->setText(Unicode::convertU32ToStd(str));
}, nullptr), KeyCallback([](GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
if(key == GLFW_KEY_ENTER && action == GLFW_PRESS) {
Input::uncaptureKeyboardInput(capture);
}
}, nullptr), Callable()); }, nullptr), Callable());
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
Engine::init(); if(Engine::init() != KEK_SUCCESS) return 1;
MemoryBuffer *buf = Resource::loadResource("object/sphere/Sphere.obj"); MemoryBuffer *buf = Resource::loadResource("object/sphere/Sphere.obj");
Mesh *mesh = ObjParser::parseMesh(buf, "object/sphere/"); Mesh *mesh = ObjParser::parseMesh(buf, "object/sphere/");
@ -76,5 +81,6 @@ int main(int argc, char **argv) {
button->onClick = Callable(onButtonClick, nullptr); button->onClick = Callable(onButtonClick, nullptr);
UI::addElement(button); UI::addElement(button);
Engine::start(); if(Engine::start() != KEK_SUCCESS) return 1;
return 0;
} }