Add KeyCallback to captureKeyboardInput
This commit is contained in:
parent
1941060d4c
commit
6c88bcd385
@ -53,9 +53,14 @@ static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action,
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -66,7 +71,7 @@ static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action,
|
||||
|
||||
static void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) {
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
kekData.activeKeyboardCapture.callback(window, codepoint);
|
||||
kekData.activeKeyboardCapture.charCallback(window, codepoint);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -99,11 +99,12 @@ GLFWCursorMode getCursorMode() {
|
||||
return kekData.uiCursorMode;
|
||||
}
|
||||
|
||||
KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback) {
|
||||
KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) {
|
||||
KeyboardCapture id = nextID++;
|
||||
kekData.activeKeyboardCapture = {
|
||||
id,
|
||||
callback,
|
||||
charCallback,
|
||||
keyCallback,
|
||||
uncaptureCallback
|
||||
};
|
||||
return id;
|
||||
@ -114,6 +115,7 @@ bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
||||
kekData.activeKeyboardCapture = {
|
||||
KEK_INVALID_ID,
|
||||
KeyCharCallback(),
|
||||
KeyCallback(),
|
||||
Callable()
|
||||
};
|
||||
return true;
|
||||
|
@ -63,7 +63,7 @@ GLFWKeyState getKeyState(KeyBinding mapping);
|
||||
void setCursorMode(GLFWCursorMode mode);
|
||||
GLFWCursorMode getCursorMode();
|
||||
|
||||
KeyboardCapture captureKeyboardInput(KeyCharCallback callback, Callable uncaptureCallback);
|
||||
KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback);
|
||||
bool uncaptureKeyboardInput(KeyboardCapture capture);
|
||||
bool isKeyboardCaptured();
|
||||
|
||||
|
@ -15,7 +15,8 @@ namespace kek {
|
||||
|
||||
struct ActiveKeyboardCapture {
|
||||
KeyboardCapture id = KEK_INVALID_ID;
|
||||
KeyCharCallback callback;
|
||||
KeyCharCallback charCallback;
|
||||
KeyCallback keyCallback;
|
||||
Callable uncaptureCallback;
|
||||
};
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
using namespace kek;
|
||||
|
||||
static ButtonElement *button;
|
||||
static KeyboardCapture capture = KEK_INVALID_ID;
|
||||
|
||||
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) {
|
||||
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());
|
||||
if(codepoint == KEK_INPUT_DELETE) {
|
||||
str = str.substr(0, str.length() - 1);
|
||||
@ -29,11 +30,15 @@ void onButtonClick(void *data) {
|
||||
str.push_back(codepoint);
|
||||
}
|
||||
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());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Engine::init();
|
||||
if(Engine::init() != KEK_SUCCESS) return 1;
|
||||
|
||||
MemoryBuffer *buf = Resource::loadResource("object/sphere/Sphere.obj");
|
||||
Mesh *mesh = ObjParser::parseMesh(buf, "object/sphere/");
|
||||
@ -76,5 +81,6 @@ int main(int argc, char **argv) {
|
||||
button->onClick = Callable(onButtonClick, nullptr);
|
||||
UI::addElement(button);
|
||||
|
||||
Engine::start();
|
||||
if(Engine::start() != KEK_SUCCESS) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user