voxelcore/src/graphics/ui/elements/CheckBox.cpp
2026-05-24 14:15:31 +03:00

66 lines
1.6 KiB
C++

#include "CheckBox.hpp"
#include <utility>
#include "Label.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
using namespace gui;
CheckBox::CheckBox(GUI& gui, bool checked)
: UINode(gui, glm::vec2(32.0f)), checked(checked) {
setColor({0.0f, 0.0f, 0.0f, 0.5f});
setHoverColor({0.05f, 0.1f, 0.2f, 0.75f});
}
void CheckBox::draw(const DrawContext& pctx, const Assets&) {
if (supplier) {
checked = supplier();
}
glm::vec2 pos = calcPos();
auto batch = pctx.getBatch2D();
batch->texture(nullptr);
batch->setColor(checked ? checkColor : calcColor());
batch->rect(pos.x, pos.y, size.x, size.y);
}
void CheckBox::mouseRelease(int x, int y) {
UINode::mouseRelease(x, y);
checked = !checked;
if (consumer) {
consumer(checked);
}
}
void CheckBox::setSupplier(boolsupplier supplier) {
this->supplier = std::move(supplier);
}
void CheckBox::setConsumer(boolconsumer consumer) {
this->consumer = std::move(consumer);
}
CheckBox* CheckBox::setChecked(bool flag) {
checked = flag;
return this;
}
FullCheckBox::FullCheckBox(
GUI& gui, const std::wstring& text, glm::vec2 size, bool checked
)
: Panel(gui, size),
checkbox(std::make_shared<CheckBox>(gui, checked)),
label(std::make_shared<Label>(gui, text)) {
setColor(glm::vec4(0.0f));
setOrientation(Orientation::HORIZONTAL);
add(checkbox);
auto& checkboxActions = actions;
label->setMargin(glm::vec4(5.f, 5.f, 0.f, 0.f));
label->listenAction(UIAction::CLICK, [&checkboxActions](auto& gui) {
checkboxActions.notify(UIAction::CLICK, gui);
});
add(label);
}