Fix offset UI origin, UIValue operators, Add utils

This commit is contained in:
MrLetsplay 2023-09-12 23:18:28 +02:00
parent cc9a582717
commit 455b8f560a
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
6 changed files with 56 additions and 36 deletions

View File

@ -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);
}

View File

@ -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 *> UIElement::getChildren() {
return children;
}

View File

@ -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);

View File

@ -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 {

View File

@ -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<UIElement *> getChildren();

View File

@ -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;
}