Extra2D/src/ui/check_box.cpp

190 lines
4.3 KiB
C++
Raw Normal View History

#include <core/string.h>
#include <renderer/renderer.h>
#include <ui/check_box.h>
2026-02-11 19:40:26 +08:00
namespace extra2d {
/**
* @brief
*/
2026-02-11 19:40:26 +08:00
CheckBox::CheckBox() {
setAnchor(0.0f, 0.0f);
setSize(boxSize_, boxSize_);
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @return
*/
Ptr<CheckBox> CheckBox::create() { return shared<CheckBox>(); }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param label
* @return
*/
2026-02-11 19:40:26 +08:00
Ptr<CheckBox> CheckBox::create(const std::string &label) {
auto cb = shared<CheckBox>();
cb->setLabel(label);
return cb;
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param checked
*/
2026-02-11 19:40:26 +08:00
void CheckBox::setChecked(bool checked) {
if (checked_ != checked) {
checked_ = checked;
if (onStateChange_) {
onStateChange_(checked_);
2026-02-11 19:40:26 +08:00
}
}
2026-02-11 19:40:26 +08:00
}
/**
* @brief
*/
void CheckBox::toggle() { setChecked(!checked_); }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param label
*/
void CheckBox::setLabel(const std::string &label) { label_ = label; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param font
*/
void CheckBox::setFont(Ptr<FontAtlas> font) { font_ = font; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param color
*/
void CheckBox::setTextColor(const Color &color) { textColor_ = color; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param size
*/
void CheckBox::setBoxSize(float size) { boxSize_ = size; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param spacing
*/
void CheckBox::setSpacing(float spacing) { spacing_ = spacing; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param color
*/
void CheckBox::setCheckedColor(const Color &color) { checkedColor_ = color; }
2026-02-11 19:40:26 +08:00
/**
* @brief
* @param color
*/
2026-02-11 19:40:26 +08:00
void CheckBox::setUncheckedColor(const Color &color) {
uncheckedColor_ = color;
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param color
*/
2026-02-11 19:40:26 +08:00
void CheckBox::setCheckMarkColor(const Color &color) {
checkMarkColor_ = color;
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param callback
*/
2026-02-11 19:40:26 +08:00
void CheckBox::setOnStateChange(Function<void(bool)> callback) {
onStateChange_ = callback;
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @return
*/
Rect CheckBox::boundingBox() const {
Vec2 position = pos();
float width = boxSize_;
if (!label_.empty() && font_) {
Vec2 textSize = font_->measureText(label_);
width += spacing_ + textSize.x;
}
return Rect(position.x, position.y, width, boxSize_);
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param renderer
*/
void CheckBox::onDrawWidget(Renderer &renderer) {
Vec2 position = pos();
Rect boxRect(position.x, position.y + (size().height - boxSize_) * 0.5f,
boxSize_, boxSize_);
Color boxColor = checked_ ? checkedColor_ : uncheckedColor_;
renderer.fillRect(boxRect, boxColor);
renderer.drawRect(boxRect, Colors::White, 1.0f);
if (checked_) {
float padding = boxSize_ * 0.2f;
float x1 = boxRect.origin.x + padding;
float y1 = boxRect.origin.y + boxSize_ * 0.5f;
float x2 = boxRect.origin.x + boxSize_ * 0.4f;
float y2 = boxRect.origin.y + boxSize_ - padding;
float x3 = boxRect.origin.x + boxSize_ - padding;
float y3 = boxRect.origin.y + padding;
renderer.drawLine(Vec2(x1, y1), Vec2(x2, y2), checkMarkColor_, 2.0f);
renderer.drawLine(Vec2(x2, y2), Vec2(x3, y3), checkMarkColor_, 2.0f);
}
if (!label_.empty() && font_) {
Vec2 textPos(position.x + boxSize_ + spacing_, position.y);
renderer.drawText(*font_, label_, textPos, textColor_);
}
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param event
* @return
*/
2026-02-11 19:40:26 +08:00
bool CheckBox::onMousePress(const MouseEvent &event) {
if (event.button == MouseButton::Left) {
pressed_ = true;
return true;
}
return false;
2026-02-11 19:40:26 +08:00
}
/**
* @brief
* @param event
* @return
*/
2026-02-11 19:40:26 +08:00
bool CheckBox::onMouseRelease(const MouseEvent &event) {
if (event.button == MouseButton::Left && pressed_) {
pressed_ = false;
Vec2 position = pos();
Rect boxRect(position.x, position.y, boxSize_, boxSize_);
if (boxRect.containsPoint(Point(event.x, event.y))) {
toggle();
2026-02-11 19:40:26 +08:00
}
return true;
}
return false;
2026-02-11 19:40:26 +08:00
}
} // namespace extra2d