From 455b8f560a63badb8e4fddf252209de16e0b28a7 Mon Sep 17 00:00:00 2001 From: MrLetsplay2003 Date: Tue, 12 Sep 2023 23:18:28 +0200 Subject: [PATCH] Fix offset UI origin, UIValue operators, Add utils --- src/kekengine/cpp/common/engine.cpp | 2 +- src/kekengine/cpp/ui/ui.cpp | 40 ++++++++--------------------- src/kekengine/cpp/ui/uielements.cpp | 5 +++- src/kekengine/include/color.h | 8 ++++++ src/kekengine/include/ui.h | 31 ++++++++++++++++++---- src/kekgame/cpp/kekgame.cpp | 6 +++++ 6 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/kekengine/cpp/common/engine.cpp b/src/kekengine/cpp/common/engine.cpp index 323945b..5edbd7c 100644 --- a/src/kekengine/cpp/common/engine.cpp +++ b/src/kekengine/cpp/common/engine.cpp @@ -280,7 +280,7 @@ int start() { prevTime = time; for(UIElement *uiEl : kekData.ui->elements) { - UIPoint pos = uiEl->getPosition(); + UIPoint pos = uiEl->getScreenPosition(); uiEl->drawAll(pos, uiProjection); } diff --git a/src/kekengine/cpp/ui/ui.cpp b/src/kekengine/cpp/ui/ui.cpp index 97fd15e..cb8bd2a 100644 --- a/src/kekengine/cpp/ui/ui.cpp +++ b/src/kekengine/cpp/ui/ui.cpp @@ -7,32 +7,6 @@ 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; -} - UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) { } @@ -46,17 +20,25 @@ UIPoint UIElement::getPosition() { return UIPoint(uiToScreen(x), uiToScreen(y)); } -// Returns the element's position on the screen in pixels -UIPoint UIElement::getScreenPosition() { +// Returns the element's origin position on the screen in pixels (not offset by getBounds()) +UIPoint UIElement::getScreenOriginPosition() { UIPoint pos = getPosition(); if(parent) { - UIPoint parentPos = parent->getPosition(); + UIPoint parentPos = parent->getScreenPosition(); pos.x += parentPos.x; pos.y += parentPos.y; } + return pos; } +// Returns the element's position on the screen in pixels (offset by getBounds()) +UIPoint UIElement::getScreenPosition() { + UIPoint pos = getScreenOriginPosition(); + UIBounds bounds = getBounds(); + return UIPoint(pos.x + bounds.x, pos.y + bounds.y); +} + std::vector UIElement::getChildren() { return children; } diff --git a/src/kekengine/cpp/ui/uielements.cpp b/src/kekengine/cpp/ui/uielements.cpp index 17f3e1b..3f090ea 100644 --- a/src/kekengine/cpp/ui/uielements.cpp +++ b/src/kekengine/cpp/ui/uielements.cpp @@ -145,7 +145,7 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { UIBounds offset = getBounds(); glUniformMatrix4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); - glm::vec4 bounds = glm::vec4(offset.x + screenPos.x, offset.y + screenPos.y, uiToScreen(w), uiToScreen(h)); + glm::vec4 bounds = glm::vec4(screenPos.x, screenPos.y, offset.w, offset.h); glUniform4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "bounds"), 1, glm::value_ptr(bounds)); glUniform4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "rectColor"), 1, color.valuePointer()); @@ -156,6 +156,9 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) { ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) { clickable = true; + color = Colors::CYAN; + hoverColor = Colors::CYAN.darker(); + text = new TextElement(uiPw(0.5), uiPh(0.5)); text->origin = Origin::CENTER; addChild(text); diff --git a/src/kekengine/include/color.h b/src/kekengine/include/color.h index dce7d39..a0ee9ac 100644 --- a/src/kekengine/include/color.h +++ b/src/kekengine/include/color.h @@ -15,6 +15,14 @@ struct Color { return &r; } + constexpr Color darker(float factor = 0.7) const { + return Color(r * factor, g * factor, b * factor, a); + } + + constexpr Color brighter(float factor = 0.7) const { + return Color(r / factor, g / factor, b / factor, a); + } + }; class Colors { diff --git a/src/kekengine/include/ui.h b/src/kekengine/include/ui.h index 79a75dc..103813e 100644 --- a/src/kekengine/include/ui.h +++ b/src/kekengine/include/ui.h @@ -90,13 +90,31 @@ struct UIValue { constexpr UIValue() = default; - constexpr UIValue &operator+=(const UIValue &rhs); + constexpr 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 &rhs); + constexpr 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 friend UIValue operator+(const UIValue &lhs, const UIValue &rhs); + constexpr friend UIValue operator+(const UIValue &lhs, const UIValue &rhs) { + return UIValue(lhs) += rhs; + } - constexpr friend UIValue operator-(const UIValue &lhs, const UIValue &rhs); + constexpr friend UIValue operator-(const UIValue &lhs, const UIValue &rhs) { + return UIValue(lhs) -= rhs; + } }; @@ -124,7 +142,10 @@ public: // Returns the element's position relative to its parent in pixels UIPoint getPosition(); - // Returns the element's position on the screen in pixels + // Returns the element's origin position on the screen in pixels (not offset by getBounds()) + UIPoint getScreenOriginPosition(); + + // Returns the element's position on the screen in pixels (offset by getBounds()) UIPoint getScreenPosition(); std::vector getChildren(); diff --git a/src/kekgame/cpp/kekgame.cpp b/src/kekgame/cpp/kekgame.cpp index 1dce2f7..be596f9 100644 --- a/src/kekgame/cpp/kekgame.cpp +++ b/src/kekgame/cpp/kekgame.cpp @@ -131,6 +131,12 @@ int main(int argc, char **argv) { button2->text->setText("Hi there!"); UI::addElement(button2); + ButtonElement *button3 = new ButtonElement(uiPx(10), uiPx(100), uiPx(200), uiPx(50)); + button3->origin = Origin::BOTTOM_LEFT; + RectangleElement *rect = new RectangleElement(uiPx(0), uiPx(0), uiPw(0.5), uiPh(0.5)); + button3->addChild(rect); + UI::addElement(button3); + if(Engine::start() != KEK_SUCCESS) return 1; return 0; }