minor refactor

This commit is contained in:
MihailRis
2024-05-05 17:02:11 +03:00
parent 5fcefaf52e
commit bad871b0a2
8 changed files with 21 additions and 27 deletions
+1 -5
View File
@@ -79,11 +79,7 @@ void Button::drawBackground(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
if (isEnabled()) { batch->setColor(calcColor());
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
batch->setColor({color.r, color.g, color.b, color.a * 0.5f});
}
batch->rect(pos.x, pos.y, size.x, size.y); batch->rect(pos.x, pos.y, size.x, size.y);
} }
+1 -1
View File
@@ -17,7 +17,7 @@ void CheckBox::draw(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->setColor(checked ? checkColor : (hover ? hoverColor : color)); batch->setColor(checked ? checkColor : calcColor());
batch->rect(pos.x, pos.y, size.x, size.y); batch->rect(pos.x, pos.y, size.x, size.y);
} }
+1 -6
View File
@@ -92,12 +92,7 @@ void Container::draw(const DrawContext* pctx, Assets* assets) {
} }
void Container::drawBackground(const DrawContext* pctx, Assets* assets) { void Container::drawBackground(const DrawContext* pctx, Assets* assets) {
glm::vec4 color = this->color; glm::vec4 color = calcColor();
if (isEnabled()) {
color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
color = glm::vec4(color.r, color.g, color.b, color.a * 0.5f);
}
if (color.a <= 0.001f) if (color.a <= 0.001f)
return; return;
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
+4 -8
View File
@@ -14,7 +14,6 @@ Image::Image(std::string texture, glm::vec2 size) : UINode(size), texture(textur
void Image::draw(const DrawContext* pctx, Assets* assets) { void Image::draw(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
glm::vec4 color = getColor();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
auto texture = assets->getTexture(this->texture); auto texture = assets->getTexture(this->texture);
@@ -22,13 +21,10 @@ void Image::draw(const DrawContext* pctx, Assets* assets) {
setSize(glm::vec2(texture->getWidth(), texture->getHeight())); setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
} }
batch->texture(texture); batch->texture(texture);
if (isEnabled()) { batch->rect(
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color)); pos.x, pos.y, size.x, size.y,
} else { 0, 0, 0, UVRegion(), false, true, calcColor()
batch->setColor({color.r, color.g, color.b, color.a * 0.5f}); );
}
batch->rect(pos.x, pos.y, size.x, size.y,
0, 0, 0, UVRegion(), false, true, batch->getColor());
} }
void Image::setAutoResize(bool flag) { void Image::setAutoResize(bool flag) {
+1 -1
View File
@@ -19,7 +19,7 @@ void InputBindBox::drawBackground(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->setColor(isFocused() ? focusedColor : (hover ? hoverColor : color)); batch->setColor(isFocused() ? focusedColor : calcColor());
batch->rect(pos.x, pos.y, size.x, size.y); batch->rect(pos.x, pos.y, size.x, size.y);
label->setText(util::str2wstr_utf8(binding.text())); label->setText(util::str2wstr_utf8(binding.text()));
} }
+1 -6
View File
@@ -148,12 +148,7 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
if (cache.resetFlag) { if (cache.resetFlag) {
cache.update(text, multiline, textWrap); cache.update(text, multiline, textWrap);
} }
batch->setColor(calcColor());
if (isEnabled()) {
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
batch->setColor({color.r, color.g, color.b, color.a * 0.5f});
}
uint lineHeight = font->getLineHeight(); uint lineHeight = font->getLineHeight();
if (cache.lines.size() > 1) { if (cache.lines.size() > 1) {
+10
View File
@@ -132,6 +132,16 @@ void UINode::scrolled(int value) {
} }
} }
glm::vec4 UINode::calcColor() const {
glm::vec4 color = this->color;
if (isEnabled()) {
color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
color = glm::vec4(color.r, color.g, color.b, color.a * 0.5f);
}
return color;
}
void UINode::setPos(glm::vec2 pos) { void UINode::setPos(glm::vec2 pos) {
this->pos = pos; this->pos = pos;
} }
+2
View File
@@ -172,6 +172,8 @@ namespace gui {
virtual void setResizing(bool flag); virtual void setResizing(bool flag);
virtual bool isResizing() const; virtual bool isResizing() const;
virtual glm::vec4 calcColor() const;
/* Get inner content offset. Used for scroll */ /* Get inner content offset. Used for scroll */
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);}; virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
/* Calculate screen position of the element */ /* Calculate screen position of the element */