diff --git a/src/kekengine/cpp/defaults.cpp b/src/kekengine/cpp/defaults.cpp index c5e8c2a..2c11c46 100644 --- a/src/kekengine/cpp/defaults.cpp +++ b/src/kekengine/cpp/defaults.cpp @@ -40,12 +40,12 @@ void defaultInput(GLFWwindow *window, void *data) { } void init() { - keyForward = Input::createKeyMapping("Forward", GLFW_KEY_W); - keyBackward = Input::createKeyMapping("Backward", GLFW_KEY_S); - keyLeft = Input::createKeyMapping("Left", GLFW_KEY_A); - keyRight = Input::createKeyMapping("Right", GLFW_KEY_D); - keyUp = Input::createKeyMapping("Up", GLFW_KEY_SPACE); - keyDown = Input::createKeyMapping("Down", GLFW_KEY_LEFT_CONTROL); + keyForward = Input::createKeyBinding("Forward", GLFW_KEY_W); + keyBackward = Input::createKeyBinding("Backward", GLFW_KEY_S); + keyLeft = Input::createKeyBinding("Left", GLFW_KEY_A); + keyRight = Input::createKeyBinding("Right", GLFW_KEY_D); + keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE); + keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL); Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr)); } diff --git a/src/kekengine/cpp/engine.cpp b/src/kekengine/cpp/engine.cpp index 7bfc84b..a5a3043 100644 --- a/src/kekengine/cpp/engine.cpp +++ b/src/kekengine/cpp/engine.cpp @@ -176,7 +176,7 @@ int init() { Defaults::init(); fpsText = new TextElement(px(0), px(0)); - //fpsText = new TextObject(new Font(KEK_DEFAULT_FONT), "HELLO WORLD!"); + UI::addElement(fpsText); return KEK_SUCCESS; } @@ -237,7 +237,6 @@ int start() { for(Light *light : shaderLights) { ShaderLight shLight; - //shLight.color = light->color; memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color)); switch(light->getType()) { case LightType::POINT: @@ -270,7 +269,6 @@ int start() { i++; } - // TODO: Fix broken lights glNamedBufferData(kekData.shader->lighting->ubo, kekData.shader->lighting->blockSize, &kekData.shader->lighting->buffer, GL_DYNAMIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, KEK_UNIFORM_LIGHTS_BINDING, kekData.shader->lighting->ubo); @@ -290,7 +288,10 @@ int start() { int time = (int) (glfwGetTime() * 10); if(time != prevTime) fpsText->setText("FPS: " + std::to_string((int) floor(1.0f / kekData.lastFrameTime)) + " (" + std::to_string(kekData.lastFrameTime * 1000) + " ms)"); prevTime = time; - fpsText->draw(UIPoint(0, 0), uiProjection); + + for(UIElement *uiEl : kekData.uiElements) { + uiEl->drawAll(UIPoint(0, 0), uiProjection); + } glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); diff --git a/src/kekengine/cpp/input.cpp b/src/kekengine/cpp/input.cpp index 4144451..ce3f191 100644 --- a/src/kekengine/cpp/input.cpp +++ b/src/kekengine/cpp/input.cpp @@ -38,33 +38,33 @@ void removeMouseListener(InputListener listener) { kekData.mouseCallbacks.erase(listener); } -KeyMapping createKeyMapping(std::string name, GLFWKey defaultKey) { - if(name == KEK_INVALID_KEY_MAPPING_NAME) return KEK_INVALID_KEY_MAPPING; - KeyMapping id = nextID++; - KeyMappingData d; +KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) { + if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING; + KeyBinding id = nextID++; + KeyBindingData d; d.name = name; d.key = defaultKey; - kekData.keyMappings.emplace(id, d); + kekData.keyBindings.emplace(id, d); return id; } -void reassignKeyMapping(KeyMapping mapping, GLFWKey key) { - auto it = kekData.keyMappings.find(mapping); - if(it == kekData.keyMappings.end()) return; - KeyMappingData d = it->second; +void reassignKeyBinding(KeyBinding mapping, GLFWKey key) { + auto it = kekData.keyBindings.find(mapping); + if(it == kekData.keyBindings.end()) return; + KeyBindingData d = it->second; d.key = key; it->second = d; } -KeyMappingData getKeyMapping(KeyMapping mapping) { - auto it = kekData.keyMappings.find(mapping); - if(it == kekData.keyMappings.end()) return {.name = KEK_INVALID_KEY_MAPPING_NAME}; +KeyBindingData getKeyBinding(KeyBinding mapping) { + auto it = kekData.keyBindings.find(mapping); + if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME}; return it->second; } -GLFWKeyState getKeyState(KeyMapping mapping) { - auto it = kekData.keyMappings.find(mapping); - if(it == kekData.keyMappings.end()) return -1; +GLFWKeyState getKeyState(KeyBinding mapping) { + auto it = kekData.keyBindings.find(mapping); + if(it == kekData.keyBindings.end()) return -1; return glfwGetKey(kekData.window, it->second.key); } diff --git a/src/kekengine/cpp/ui.cpp b/src/kekengine/cpp/ui.cpp index 60e6e8c..9ff0337 100644 --- a/src/kekengine/cpp/ui.cpp +++ b/src/kekengine/cpp/ui.cpp @@ -6,6 +6,32 @@ namespace kek { +constexpr UIValue &UIValue::operator+=(const UIValue& rhs) { + this->pixels += rhs.pixels; + this->parentWidth += rhs.parentWidth; + this->parentHeight += rhs.parentHeight; + this->screenWidth += rhs.screenWidth; + this->screenHeight += rhs.screenHeight; + return *this; +} + +constexpr UIValue &UIValue::operator-=(const UIValue& rhs) { + this->pixels -= rhs.pixels; + this->parentWidth -= rhs.parentWidth; + this->parentHeight -= rhs.parentHeight; + this->screenWidth -= rhs.screenWidth; + this->screenHeight -= rhs.screenHeight; + return *this; +} + +constexpr UIValue operator+(const UIValue& lhs, const UIValue& rhs) { + return UIValue(lhs) += rhs; +} + +constexpr UIValue operator-(const UIValue& lhs, const UIValue& rhs) { + return UIValue(lhs) -= rhs; +} + Font *UIElement::defaultFont = nullptr; UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) { @@ -32,6 +58,10 @@ UIPoint UIElement::getScreenPosition() { return pos; } +std::vector UIElement::getChildren() { + return children; +} + void UIElement::addChild(UIElement *child) { children.push_back(child); } @@ -182,4 +212,22 @@ void UIElement::init() { defaultFont = new Font(KEK_DEFAULT_FONT); } +void UI::addElement(UIElement *element) { + kekData.uiElements.push_back(element); +} + +void UI::removeElement(UIElement *element) { + for(auto it = kekData.uiElements.begin(); it < kekData.uiElements.end(); it++) { + if(*it == element) { + kekData.uiElements.erase(it); + delete element; + break; + } + } +} + +std::vector UI::getElements() { + return kekData.uiElements; +} + } diff --git a/src/kekengine/cpp/uielements.cpp b/src/kekengine/cpp/uielements.cpp index f6ad967..8669b1c 100644 --- a/src/kekengine/cpp/uielements.cpp +++ b/src/kekengine/cpp/uielements.cpp @@ -137,4 +137,20 @@ void TextElement::draw(UIPoint screenPos, glm::mat4 projection) { } } +UIWindow::UIWindow(UIValue x, UIValue y): UIElement(x, y) { + addChild(new TextElement(x, y)); +} + +UIWindow::~UIWindow() { + +} + +UIElementType UIWindow::getType() { + return UIElementType::WINDOW; +} + +void UIWindow::draw(UIPoint screenPos, glm::mat4 projection) { + +} + } diff --git a/src/kekengine/include/constants.h b/src/kekengine/include/constants.h index 48dc7a1..795dba9 100644 --- a/src/kekengine/include/constants.h +++ b/src/kekengine/include/constants.h @@ -11,7 +11,7 @@ #define KEK_UNIFORM_LIGHTS_BINDING 0 -#define KEK_LIGHT_LIMIT 64 // Also in shader/mesh/fragment.glsl +#define KEK_LIGHT_LIMIT 64 // Also in shader/include/constants.glsl #define KEK_NOCLIP_SPEED 10.0f @@ -24,8 +24,8 @@ #define KEK_LIGHT_MAX_DISTANCE 30 #define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE) -#define KEK_INVALID_KEY_MAPPING_NAME "INVALID" -#define KEK_INVALID_KEY_MAPPING -1 +#define KEK_INVALID_KEY_BINDING_NAME "INVALID" +#define KEK_INVALID_KEY_BINDING -1 #define KEK_FONT_RESOLUTION 64 #define KEK_FONT_BITMAP_WIDTH_BLOCKS 16 diff --git a/src/kekengine/include/input.h b/src/kekengine/include/input.h index b81f830..de54eea 100644 --- a/src/kekengine/include/input.h +++ b/src/kekengine/include/input.h @@ -14,10 +14,10 @@ typedef generic_callable_t MouseCallback; // mouse typedef unsigned int InputListener; typedef unsigned int GLFWKey; typedef unsigned int GLFWKeyState; -typedef unsigned int KeyMapping; +typedef unsigned int KeyBinding; typedef unsigned int GLFWMouseButton; -struct KeyMappingData { +struct KeyBindingData { std::string name; GLFWKey key; }; @@ -38,12 +38,12 @@ InputListener addMouseListener(MouseCallback callback); void removeMouseListener(InputListener listener); -KeyMapping createKeyMapping(std::string name, GLFWKey defaultKey); +KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey); -void reassignKeyMapping(KeyMapping mapping, GLFWKey key); +void reassignKeyBinding(KeyBinding mapping, GLFWKey key); -KeyMappingData getKeyMapping(KeyMapping mapping); +KeyBindingData getKeyBinding(KeyBinding mapping); -GLFWKeyState getKeyState(KeyMapping mapping); +GLFWKeyState getKeyState(KeyBinding mapping); } diff --git a/src/kekengine/include/internal.h b/src/kekengine/include/internal.h index d2b36ca..96ebc7f 100644 --- a/src/kekengine/include/internal.h +++ b/src/kekengine/include/internal.h @@ -9,6 +9,7 @@ #include "scene.h" #include "texture.h" #include "fonts.h" +#include "ui.h" namespace kek { @@ -16,7 +17,7 @@ struct KekData { std::map periodicCallbacks; std::map keyCallbacks; std::map mouseCallbacks; - std::map keyMappings; + std::map keyBindings; GLFWwindow *window; Shader *shader; @@ -32,6 +33,8 @@ struct KekData { std::map> loadedTextures; FT_Library freetype; + + std::vector uiElements; }; extern KekData kekData; diff --git a/src/kekengine/include/ui.h b/src/kekengine/include/ui.h index 15d8842..f10cec8 100644 --- a/src/kekengine/include/ui.h +++ b/src/kekengine/include/ui.h @@ -16,21 +16,13 @@ namespace kek { struct UIPoint { int x, y; - UIPoint(int x, int y) { - this->x = x; - this->y = y; - } + UIPoint(int x, int y): x(x), y(y) {} }; struct UIBounds { int x, y, w, h; - UIBounds(int x, int y, int w, int h) { - this->x = x; - this->y = y; - this->w = w; - this->h = h; - } + UIBounds(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} inline bool contains(UIPoint pos) { return pos.x < x || pos.y < y || pos.x >= x + w || pos.y >= y + h; @@ -38,6 +30,7 @@ struct UIBounds { }; enum class UIElementType { + WINDOW, RECTANGLE, TEXT, BUTTON, @@ -153,6 +146,8 @@ public: // Returns the element's position on the screen in pixels UIPoint getScreenPosition(); + std::vector getChildren(); + void addChild(UIElement *child); void removeChild(UIElement *child); @@ -200,4 +195,14 @@ public: }; +namespace UI { + +void addElement(UIElement *element); + +void removeElement(UIElement *element); + +std::vector getElements(); + +}; + } diff --git a/src/kekengine/include/uielements.h b/src/kekengine/include/uielements.h index cdcae9c..295a1b8 100644 --- a/src/kekengine/include/uielements.h +++ b/src/kekengine/include/uielements.h @@ -4,6 +4,18 @@ namespace kek { +class UIWindow: public UIElement { + + UIWindow(UIValue x, UIValue y); + + virtual ~UIWindow(); + + virtual UIElementType getType(); + + virtual void draw(UIPoint screenPos, glm::mat4 projection); + +}; + class TextElement: public UIElement { protected: