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

View File

@ -79,11 +79,7 @@ void Button::drawBackground(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
if (isEnabled()) {
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
batch->setColor({color.r, color.g, color.b, color.a * 0.5f});
}
batch->setColor(calcColor());
batch->rect(pos.x, pos.y, size.x, size.y);
}

View File

@ -17,7 +17,7 @@ void CheckBox::draw(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
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);
}

View File

@ -92,12 +92,7 @@ void Container::draw(const DrawContext* pctx, Assets* assets) {
}
void Container::drawBackground(const DrawContext* pctx, Assets* assets) {
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);
}
glm::vec4 color = calcColor();
if (color.a <= 0.001f)
return;
glm::vec2 pos = calcPos();

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) {
glm::vec2 pos = calcPos();
glm::vec4 color = getColor();
auto batch = pctx->getBatch2D();
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()));
}
batch->texture(texture);
if (isEnabled()) {
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,
0, 0, 0, UVRegion(), false, true, batch->getColor());
batch->rect(
pos.x, pos.y, size.x, size.y,
0, 0, 0, UVRegion(), false, true, calcColor()
);
}
void Image::setAutoResize(bool flag) {

View File

@ -19,7 +19,7 @@ void InputBindBox::drawBackground(const DrawContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
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);
label->setText(util::str2wstr_utf8(binding.text()));
}

View File

@ -148,12 +148,7 @@ void Label::draw(const DrawContext* pctx, Assets* assets) {
if (cache.resetFlag) {
cache.update(text, multiline, textWrap);
}
if (isEnabled()) {
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
} else {
batch->setColor({color.r, color.g, color.b, color.a * 0.5f});
}
batch->setColor(calcColor());
uint lineHeight = font->getLineHeight();
if (cache.lines.size() > 1) {

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) {
this->pos = pos;
}

View File

@ -172,6 +172,8 @@ namespace gui {
virtual void setResizing(bool flag);
virtual bool isResizing() const;
virtual glm::vec4 calcColor() const;
/* Get inner content offset. Used for scroll */
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
/* Calculate screen position of the element */