From 96eb91ff7889fe9862bdf4fd78eb648d4dc2d816 Mon Sep 17 00:00:00 2001 From: MrLetsplay2003 Date: Thu, 3 Nov 2022 20:14:11 +0100 Subject: [PATCH] Add UI click --- src/kekengine/cpp/defaults.cpp | 31 ++++++++++++++++++++++++++++-- src/kekengine/cpp/engine.cpp | 2 +- src/kekengine/cpp/ui.cpp | 1 + src/kekengine/cpp/uielements.cpp | 23 +++++++++------------- src/kekengine/include/uielements.h | 10 +++------- src/kekgame/cpp/kekgame.cpp | 9 +++++++-- 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/kekengine/cpp/defaults.cpp b/src/kekengine/cpp/defaults.cpp index 745aaae..9efab40 100644 --- a/src/kekengine/cpp/defaults.cpp +++ b/src/kekengine/cpp/defaults.cpp @@ -45,7 +45,7 @@ static void defaultInput(GLFWwindow *window, void *data) { } } -static void defaultKey(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) { +static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) { if(key == Input::getKeyBinding(keyOptions).key && action == GLFW_PRESS) { options->visible = !options->visible; } @@ -93,6 +93,32 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d } } +static void defaultMouseButtonCallback(GLFWwindow* window, int button, int action, int mods, void *data) { + switch(Input::getCursorMode()) { + case GLFWCursorMode::CAPTURE: + { + // TODO + } + case GLFWCursorMode::FREE: + case GLFWCursorMode::HIDDEN: + { + switch(action) { + case GLFW_PRESS: + break; + case GLFW_RELEASE: + { + double x, y; + glfwGetCursorPos(window, &x, &y); + for(UIElement *element : kekData.uiElements) { + UIPoint childPos = element->getPosition(); + if(element->clickAll(UIPoint((int) x - childPos.x, (int) y - childPos.y), UIPoint((int) x, (int) y), (GLFWMouseButton) button)) break; + } + } + } + } + } +} + void init() { keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W); keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S); @@ -104,8 +130,9 @@ void init() { keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB); Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr)); - Input::addKeyListener(KeyCallback(defaultKey, nullptr)); + Input::addKeyListener(KeyCallback(defaultKeyCallback, nullptr)); Input::addMouseListener(MouseCallback(defaultMouseCallback, nullptr)); + Input::addMouseButtonListener(MouseButtonCallback(defaultMouseButtonCallback, nullptr)); options = new ButtonElement(px(0), px(100), px(100), px(50)); //UI::addElement(options); diff --git a/src/kekengine/cpp/engine.cpp b/src/kekengine/cpp/engine.cpp index 31869c4..b728684 100644 --- a/src/kekengine/cpp/engine.cpp +++ b/src/kekengine/cpp/engine.cpp @@ -177,7 +177,7 @@ int init() { Defaults::init(); fpsText = new TextElement(px(0), px(0)); - //UI::addElement(fpsText); + UI::addElement(fpsText); return KEK_SUCCESS; } diff --git a/src/kekengine/cpp/ui.cpp b/src/kekengine/cpp/ui.cpp index 569d80d..dee3501 100644 --- a/src/kekengine/cpp/ui.cpp +++ b/src/kekengine/cpp/ui.cpp @@ -66,6 +66,7 @@ void UIElement::addChild(UIElement *child) { } void UIElement::removeChild(UIElement *child) { + child->parent = nullptr; std::remove(children.begin(), children.end(), child); } diff --git a/src/kekengine/cpp/uielements.cpp b/src/kekengine/cpp/uielements.cpp index eea474c..15e767d 100644 --- a/src/kekengine/cpp/uielements.cpp +++ b/src/kekengine/cpp/uielements.cpp @@ -153,7 +153,7 @@ static float rectangleVerts[] = { RectangleElement::RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h): UIElement(x, y) { this->w = w; this->h = h; - this->color = Colors::BLACK; + color = Colors::BLACK; glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); @@ -195,7 +195,9 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { } ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) { - this->text = new TextElement(pw(0.5), ph(0.5)); + clickable = true; + + text = new TextElement(pw(0.5), ph(0.5)); text->origin = Origin::CENTER; addChild(text); } @@ -208,20 +210,13 @@ UIElementType ButtonElement::getType() { return UIElementType::BUTTON; } -void ButtonElement::hover(UIPoint pos, UIPoint screenPos) { - -} - -void ButtonElement::hoverEnter(UIPoint pos, UIPoint screenPos) { - color = hoverColor; -} - -void ButtonElement::hoverExit() { - color = defaultColor; -} - void ButtonElement::click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) { + onClick(); +} +void ButtonElement::draw(UIPoint screenPos, glm::mat4 projection) { + RectangleElement::color = hovering ? hoverColor : color; + RectangleElement::draw(screenPos, projection); } } diff --git a/src/kekengine/include/uielements.h b/src/kekengine/include/uielements.h index a0f31d8..08e8af6 100644 --- a/src/kekengine/include/uielements.h +++ b/src/kekengine/include/uielements.h @@ -70,7 +70,7 @@ class ButtonElement: public RectangleElement { public: TextElement *text; - Color defaultColor; + Color color; Color hoverColor; Callable onClick; @@ -80,14 +80,10 @@ public: virtual UIElementType getType(); - virtual void hover(UIPoint pos, UIPoint screenPos); - - virtual void hoverEnter(UIPoint pos, UIPoint screenPos); - - virtual void hoverExit(); - virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button); + virtual void draw(UIPoint screenPos, glm::mat4 projection); + }; } diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index 9a6c5a4..6ebbf55 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -18,6 +18,10 @@ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, v } +void onButtonClick(void *data) { + std::cout << "Clicked!" << std::endl; +} + int main(int argc, char **argv) { Engine::init(); @@ -54,11 +58,12 @@ int main(int argc, char **argv) { Input::addKeyListener(KeyCallback(keyCallback, nullptr)); Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr)); - ButtonElement *btn = new ButtonElement(px(0), px(100), px(200), px(50)); + ButtonElement *btn = new ButtonElement(px(10), px(100), px(200), px(50)); btn->text->color = Colors::BLACK; - btn->defaultColor = btn->color = Colors::WHITE; + btn->color = Colors::WHITE; btn->hoverColor = Colors::GRAY; btn->text->setText("Hello There!"); + btn->onClick = Callable(onButtonClick, nullptr); UI::addElement(btn); Engine::start();