https://github.com/libsdl-org/Maelstrom/commit/1d19d8cff4d273965dd0abb8e7ac43ee859b88f2
From 1d19d8cff4d273965dd0abb8e7ac43ee859b88f2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 5 Nov 2011 00:28:45 -0400
Subject: [PATCH] Added preferences binding for radio groups as well.
---
screenlib/UIElementRadio.cpp | 53 +++++++++++++++++++++++++++++++++---
screenlib/UIElementRadio.h | 9 +++++-
2 files changed, 57 insertions(+), 5 deletions(-)
diff --git a/screenlib/UIElementRadio.cpp b/screenlib/UIElementRadio.cpp
index fa2f32b8..924869d8 100644
--- a/screenlib/UIElementRadio.cpp
+++ b/screenlib/UIElementRadio.cpp
@@ -28,6 +28,44 @@ UIElementRadioGroup::UIElementRadioGroup(UIBaseElement *parent, const char *name
UIElement(parent, name, drawEngine)
{
m_value = -1;
+ m_valueBinding = NULL;
+}
+
+UIElementRadioGroup::~UIElementRadioGroup()
+{
+ if (m_valueBinding) {
+ SDL_free(m_valueBinding);
+ }
+}
+
+bool
+UIElementRadioGroup::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
+{
+ if (!UIElement::Load(node, templates)) {
+ return false;
+ }
+
+ LoadString(node, "bindValue", m_valueBinding);
+
+ return true;
+}
+
+void
+UIElementRadioGroup::LoadData(Prefs *prefs)
+{
+ if (m_valueBinding) {
+ SetValue(prefs->GetNumber(m_valueBinding, GetValue()));
+ }
+ UIElement::LoadData(prefs);
+}
+
+void
+UIElementRadioGroup::SaveData(Prefs *prefs)
+{
+ if (m_valueBinding) {
+ prefs->SetNumber(m_valueBinding, GetValue());
+ }
+ UIElement::SaveData(prefs);
}
UIElementRadioButton *
@@ -49,17 +87,24 @@ UIElementRadioGroup::GetRadioButton(int id)
}
void
-UIElementRadioGroup::RadioButtonChecked(UIElementRadioButton *button)
+UIElementRadioGroup::SetValue(int value)
{
array<UIElementRadioButton*> buttons;
+ if (value == m_value) {
+ return;
+ }
+
+ m_value = value;
+
FindElements<UIElementRadioButton>(buttons);
for (int i = 0; i < buttons.length(); ++i) {
- if (buttons[i] != button && buttons[i]->IsChecked()) {
+ if (buttons[i]->GetID() == value) {
+ buttons[i]->SetChecked(true);
+ } else {
buttons[i]->SetChecked(false);
}
}
- m_value = button->GetID();
}
@@ -97,7 +142,7 @@ UIElementRadioButton::OnChecked(bool checked)
{
if (checked) {
if (m_parent->IsA(UIElementRadioGroup::GetType())) {
- static_cast<UIElementRadioGroup*>(m_parent)->RadioButtonChecked(this);
+ static_cast<UIElementRadioGroup*>(m_parent)->SetValue(GetID());
}
}
}
diff --git a/screenlib/UIElementRadio.h b/screenlib/UIElementRadio.h
index c517870b..266dc2b8 100644
--- a/screenlib/UIElementRadio.h
+++ b/screenlib/UIElementRadio.h
@@ -40,10 +40,16 @@ class UIElementRadioGroup : public UIElement
DECLARE_TYPESAFE_CLASS(UIElement)
public:
UIElementRadioGroup(UIBaseElement *parent, const char *name, UIDrawEngine *drawEngine);
+ ~UIElementRadioGroup();
+
+ override bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
+
+ override void LoadData(Prefs *prefs);
+ override void SaveData(Prefs *prefs);
UIElementRadioButton *GetRadioButton(int id);
- void RadioButtonChecked(UIElementRadioButton *button);
+ void SetValue(int value);
int GetValue() const {
return m_value;
@@ -51,6 +57,7 @@ DECLARE_TYPESAFE_CLASS(UIElement)
protected:
int m_value;
+ char *m_valueBinding;
};
class UIElementRadioButton : public UIElementCheckbox