Fix offset UI origin, UIValue operators, Add utils
This commit is contained in:
parent
cc9a582717
commit
455b8f560a
@ -280,7 +280,7 @@ int start() {
|
|||||||
prevTime = time;
|
prevTime = time;
|
||||||
|
|
||||||
for(UIElement *uiEl : kekData.ui->elements) {
|
for(UIElement *uiEl : kekData.ui->elements) {
|
||||||
UIPoint pos = uiEl->getPosition();
|
UIPoint pos = uiEl->getScreenPosition();
|
||||||
uiEl->drawAll(pos, uiProjection);
|
uiEl->drawAll(pos, uiProjection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,32 +7,6 @@
|
|||||||
|
|
||||||
namespace kek {
|
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) {
|
UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -46,17 +20,25 @@ UIPoint UIElement::getPosition() {
|
|||||||
return UIPoint(uiToScreen(x), uiToScreen(y));
|
return UIPoint(uiToScreen(x), uiToScreen(y));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 UIElement::getScreenPosition() {
|
UIPoint UIElement::getScreenOriginPosition() {
|
||||||
UIPoint pos = getPosition();
|
UIPoint pos = getPosition();
|
||||||
if(parent) {
|
if(parent) {
|
||||||
UIPoint parentPos = parent->getPosition();
|
UIPoint parentPos = parent->getScreenPosition();
|
||||||
pos.x += parentPos.x;
|
pos.x += parentPos.x;
|
||||||
pos.y += parentPos.y;
|
pos.y += parentPos.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pos;
|
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() {
|
std::vector<UIElement *> UIElement::getChildren() {
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) {
|
|||||||
UIBounds offset = getBounds();
|
UIBounds offset = getBounds();
|
||||||
|
|
||||||
glUniformMatrix4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
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, "bounds"), 1, glm::value_ptr(bounds));
|
||||||
glUniform4fv(glGetUniformLocation(kekData.ui->rectangleShader->id, "rectColor"), 1, color.valuePointer());
|
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) {
|
ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) {
|
||||||
clickable = true;
|
clickable = true;
|
||||||
|
|
||||||
|
color = Colors::CYAN;
|
||||||
|
hoverColor = Colors::CYAN.darker();
|
||||||
|
|
||||||
text = new TextElement(uiPw(0.5), uiPh(0.5));
|
text = new TextElement(uiPw(0.5), uiPh(0.5));
|
||||||
text->origin = Origin::CENTER;
|
text->origin = Origin::CENTER;
|
||||||
addChild(text);
|
addChild(text);
|
||||||
|
@ -15,6 +15,14 @@ struct Color {
|
|||||||
return &r;
|
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 {
|
class Colors {
|
||||||
|
@ -90,13 +90,31 @@ struct UIValue {
|
|||||||
|
|
||||||
constexpr UIValue() = default;
|
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
|
// Returns the element's position relative to its parent in pixels
|
||||||
UIPoint getPosition();
|
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();
|
UIPoint getScreenPosition();
|
||||||
|
|
||||||
std::vector<UIElement *> getChildren();
|
std::vector<UIElement *> getChildren();
|
||||||
|
@ -131,6 +131,12 @@ int main(int argc, char **argv) {
|
|||||||
button2->text->setText("Hi there!");
|
button2->text->setText("Hi there!");
|
||||||
UI::addElement(button2);
|
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;
|
if(Engine::start() != KEK_SUCCESS) return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user