mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-08-31 08:12:15 -04:00
UiService: Add is_selected for buttons (#2337)
This commit is contained in:
@@ -570,6 +570,9 @@ ModResult ui_pane_add_control(
|
||||
case UI_CONTROL_GROUP:
|
||||
spec.kind = desc.kind == UI_CONTROL_BUTTON ? ui::ModControlSpec::Kind::Button :
|
||||
ui::ModControlSpec::Kind::Group;
|
||||
if (desc.struct_size >= sizeof(UiControlDesc)) {
|
||||
spec.isSelected = wrap_predicate(mod, desc.is_selected, desc.user_data, pane);
|
||||
}
|
||||
spec.onPressed = [modPtr = &mod, fn = desc.on_pressed, userData = desc.user_data,
|
||||
guardHandle = pane] {
|
||||
if (!slot_live(guardHandle)) {
|
||||
@@ -1147,6 +1150,7 @@ bool valid_color_preset(const char* value, bool alpha) {
|
||||
|
||||
bool valid_control_desc(const UiControlDesc& desc) {
|
||||
constexpr size_t kLegacyDescSize = offsetof(UiControlDesc, color_presets);
|
||||
constexpr size_t kColorDescSize = offsetof(UiControlDesc, is_selected);
|
||||
if (desc.struct_size < kLegacyDescSize || desc.label == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -1160,7 +1164,7 @@ bool valid_control_desc(const UiControlDesc& desc) {
|
||||
case UI_CONTROL_SELECT:
|
||||
break;
|
||||
case UI_CONTROL_COLOR:
|
||||
if (desc.struct_size < sizeof(UiControlDesc)) {
|
||||
if (desc.struct_size < kColorDescSize) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -4,15 +4,20 @@ namespace dusk::ui {
|
||||
|
||||
GroupButton::GroupButton(Rml::Element* parent, Props props)
|
||||
: SelectButton{parent, {.key = std::move(props.text)}},
|
||||
mIsDisabled{std::move(props.isDisabled)} {
|
||||
mIsSelected{std::move(props.isSelected)}, mIsDisabled{std::move(props.isDisabled)} {
|
||||
mRoot->SetClass("group-button", true);
|
||||
}
|
||||
|
||||
void GroupButton::update() {
|
||||
set_selected(selected());
|
||||
set_disabled(disabled());
|
||||
SelectButton::update();
|
||||
}
|
||||
|
||||
bool GroupButton::selected() const {
|
||||
return mIsSelected ? mIsSelected() : SelectButton::selected();
|
||||
}
|
||||
|
||||
bool GroupButton::disabled() const {
|
||||
return mIsDisabled ? mIsDisabled() : SelectButton::disabled();
|
||||
}
|
||||
|
||||
@@ -8,15 +8,18 @@ class GroupButton : public SelectButton {
|
||||
public:
|
||||
struct Props {
|
||||
Rml::String text;
|
||||
std::function<bool()> isSelected;
|
||||
std::function<bool()> isDisabled;
|
||||
};
|
||||
|
||||
GroupButton(Rml::Element* parent, Props props);
|
||||
|
||||
void update() override;
|
||||
bool selected() const override;
|
||||
bool disabled() const override;
|
||||
|
||||
private:
|
||||
std::function<bool()> mIsSelected;
|
||||
std::function<bool()> mIsDisabled;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ Component* build_mod_control(Pane& pane, Pane* helpPane, ModControlSpec spec) {
|
||||
case ModControlSpec::Kind::Button:
|
||||
control = &pane.add_button(ControlledButton::Props{
|
||||
.text = s.label,
|
||||
.isSelected = s.isSelected,
|
||||
.isDisabled = s.isDisabled,
|
||||
})
|
||||
.on_pressed([shared] {
|
||||
@@ -28,6 +29,7 @@ Component* build_mod_control(Pane& pane, Pane* helpPane, ModControlSpec spec) {
|
||||
case ModControlSpec::Kind::Group:
|
||||
control = &pane.add_group_button(GroupButton::Props{
|
||||
.text = s.label,
|
||||
.isSelected = s.isSelected,
|
||||
.isDisabled = s.isDisabled,
|
||||
})
|
||||
.on_pressed([shared] {
|
||||
|
||||
@@ -29,6 +29,7 @@ struct ModControlSpec {
|
||||
std::function<void(int)> setInt;
|
||||
std::function<Rml::String()> getString;
|
||||
std::function<void(Rml::String)> setString;
|
||||
std::function<bool()> isSelected;
|
||||
std::function<bool()> isDisabled;
|
||||
std::function<bool()> isModified;
|
||||
int min = 0;
|
||||
|
||||
Reference in New Issue
Block a user