2026-02-11 19:40:26 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-02-25 06:23:53 +08:00
|
|
|
#include <core/types.h>
|
|
|
|
|
#include <graphics/font.h>
|
|
|
|
|
#include <ui/widget.h>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
namespace extra2d {
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// 单选按钮组件
|
|
|
|
|
// ============================================================================
|
|
|
|
|
class RadioButton : public Widget {
|
|
|
|
|
public:
|
2026-02-26 00:55:13 +08:00
|
|
|
RadioButton();
|
|
|
|
|
~RadioButton() override = default;
|
|
|
|
|
|
|
|
|
|
static Ptr<RadioButton> create();
|
|
|
|
|
static Ptr<RadioButton> create(const std::string &label);
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 选择状态
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setSelected(bool selected);
|
|
|
|
|
bool isSelected() const { return selected_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 标签设置
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setLabel(const std::string &label);
|
|
|
|
|
const std::string &getLabel() const { return label_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 字体设置
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setFont(Ptr<FontAtlas> font);
|
|
|
|
|
Ptr<FontAtlas> getFont() const { return font_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 文字颜色
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setTextColor(const Color &color);
|
|
|
|
|
Color getTextColor() const { return textColor_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 圆形尺寸
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setCircleSize(float size);
|
|
|
|
|
float getCircleSize() const { return circleSize_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 间距
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setSpacing(float spacing);
|
|
|
|
|
float getSpacing() const { return spacing_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 颜色设置
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setSelectedColor(const Color &color);
|
|
|
|
|
Color getSelectedColor() const { return selectedColor_; }
|
|
|
|
|
|
|
|
|
|
void setUnselectedColor(const Color &color);
|
|
|
|
|
Color getUnselectedColor() const { return unselectedColor_; }
|
|
|
|
|
|
|
|
|
|
void setDotColor(const Color &color);
|
|
|
|
|
Color getDotColor() const { return dotColor_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 分组
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setGroupId(int groupId);
|
|
|
|
|
int getGroupId() const { return groupId_; }
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// 回调设置
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
void setOnStateChange(Function<void(bool)> callback);
|
|
|
|
|
|
|
|
|
|
Rect boundingBox() const override;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
protected:
|
2026-02-26 00:59:16 +08:00
|
|
|
void onDrawWidget(Renderer &renderer) override;
|
2026-02-26 00:55:13 +08:00
|
|
|
bool onMousePress(const MouseEvent &event) override;
|
|
|
|
|
bool onMouseRelease(const MouseEvent &event) override;
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
private:
|
2026-02-26 00:55:13 +08:00
|
|
|
bool selected_ = false;
|
|
|
|
|
std::string label_;
|
|
|
|
|
Ptr<FontAtlas> font_;
|
|
|
|
|
Color textColor_ = Colors::White;
|
|
|
|
|
|
|
|
|
|
float circleSize_ = 20.0f;
|
|
|
|
|
float spacing_ = 8.0f;
|
|
|
|
|
|
|
|
|
|
Color selectedColor_ = Color(0.2f, 0.6f, 1.0f, 1.0f);
|
|
|
|
|
Color unselectedColor_ = Color(0.3f, 0.3f, 0.3f, 1.0f);
|
|
|
|
|
Color dotColor_ = Colors::White;
|
|
|
|
|
|
|
|
|
|
int groupId_ = 0;
|
|
|
|
|
|
|
|
|
|
bool pressed_ = false;
|
|
|
|
|
Function<void(bool)> onStateChange_;
|
2026-02-11 19:40:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// 单选按钮组管理器
|
|
|
|
|
// ============================================================================
|
|
|
|
|
class RadioButtonGroup {
|
|
|
|
|
public:
|
2026-02-26 00:55:13 +08:00
|
|
|
void addButton(RadioButton *button);
|
|
|
|
|
void removeButton(RadioButton *button);
|
|
|
|
|
void selectButton(RadioButton *button);
|
|
|
|
|
RadioButton *getSelectedButton() const { return selectedButton_; }
|
|
|
|
|
|
|
|
|
|
void setOnSelectionChange(Function<void(RadioButton *)> callback);
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
private:
|
2026-02-26 00:55:13 +08:00
|
|
|
std::vector<RadioButton *> buttons_;
|
|
|
|
|
RadioButton *selectedButton_ = nullptr;
|
|
|
|
|
Function<void(RadioButton *)> onSelectionChange_;
|
2026-02-11 19:40:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace extra2d
|