HoverRace  2.0
RadioButton.h
Go to the documentation of this file.
1 
2 // RadioButton.h
3 //
4 // Copyright (c) 2014 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "../Exception.h"
25 
26 #include "StateButton.h"
27 
28 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
29 # ifdef MR_ENGINE
30 # define MR_DllDeclare __declspec( dllexport )
31 # else
32 # define MR_DllDeclare __declspec( dllimport )
33 # endif
34 #else
35 # define MR_DllDeclare
36 #endif
37 
38 namespace HoverRace {
39  namespace Display {
40  class Display;
41  }
42 }
43 
44 namespace HoverRace {
45 namespace Display {
46 
52 {
53  using SUPER = StateButton;
54 
55 public:
56  BaseRadioButton(Display &display, const std::string &text,
57  uiLayoutFlags_t layoutFlags = 0);
58  BaseRadioButton(Display &display, const Vec2 &size, const std::string &text,
59  uiLayoutFlags_t layoutFlags = 0);
60  virtual ~BaseRadioButton() { }
61 
62 private:
63  void Init();
64  void InitIcon(bool enabled, bool checked);
65 };
66 
77 template<class T>
79 {
81 
82 public:
83  RadioButton(Display &display, const std::string &text, const T &value,
84  uiLayoutFlags_t layoutFlags = 0) :
85  SUPER(display, text, layoutFlags), value(value) { }
86  RadioButton(Display &display, const Vec2 &size, const std::string &text,
87  const T &value, uiLayoutFlags_t layoutFlags = 0) :
88  SUPER(display, size, text, layoutFlags), value(value) { }
89  virtual ~RadioButton() { }
90 
91 public:
92  const T &GetValue() const { return value; }
93 
94 private:
95  T value;
96 };
97 
103 template<class T>
105 {
106 public:
107  RadioGroup() : selButton(nullptr) { }
108 
109 public:
111 
112 private:
114  {
115  if (selButton != sel) {
116  if (selButton) {
117  selButton->SetChecked(false);
118  }
119  selButton = sel;
120  sel->SetChecked(true);
121 
122  valueChangedSignal();
123  }
124  }
125 
126 public:
131  void Add(std::shared_ptr<button_t> button)
132  {
133  button_t *addBtn = button.get();
134 
135  clickedConns.emplace_back(new boost::signals2::scoped_connection(
136  button->GetClickedSignal().connect([=](ClickRegion&) {
137  SetSelectedButton(addBtn);
138  })));
139 
140  if (!selButton) {
141  selButton = button.get();
142  selButton->SetChecked(true);
143  }
144 
145  buttons.emplace_back(button);
146  }
147 
148  const T &GetValue() const
149  {
150  if (!selButton) throw Exception("No selected value");
151  return selButton->GetValue();
152  }
153 
162  void SetValue(const T &val)
163  {
164  // Find the first radio button matching this value.
165  // We assume that the list is small enough that we don't need a
166  // lookup table.
167  button_t *newSel = nullptr;
168  for (const auto &btn : buttons) {
169  if (btn->GetValue() == val) {
170  newSel = btn.get();
171  break;
172  }
173  }
174 
175  if (newSel && selButton != newSel) {
176  selButton->SetChecked(false);
177  selButton = newSel;
178  newSel->SetChecked(true);
179  }
180  }
181 
182 public:
183  using valueChangedSignal_t = boost::signals2::signal<void()>;
184  valueChangedSignal_t &GetValueChangedSignal() { return valueChangedSignal; }
185 
186 private:
187  std::vector<std::shared_ptr<button_t>> buttons;
188  std::vector<std::unique_ptr<boost::signals2::scoped_connection>> clickedConns;
191 };
192 
193 } // namespace Display
194 } // namespace HoverRace
195 
196 #undef MR_DllDeclare
#define MR_DllDeclare
Definition: RadioButton.h:35
boost::signals2::signal< void()> valueChangedSignal_t
Definition: RadioButton.h:183
std::vector< std::unique_ptr< boost::signals2::scoped_connection > > clickedConns
Definition: RadioButton.h:188
MR_DllDeclare void Init()
Definition: ColorTools.cpp:142
valueChangedSignal_t & GetValueChangedSignal()
Definition: RadioButton.h:184
virtual ~BaseRadioButton()
Definition: RadioButton.h:60
Base class for radio buttons.
Definition: RadioButton.h:51
const T & GetValue() const
Definition: RadioButton.h:92
valueChangedSignal_t valueChangedSignal
Definition: RadioButton.h:190
void SetSelectedButton(button_t *sel)
Definition: RadioButton.h:113
Definition: Vec.h:38
T value
Definition: RadioButton.h:95
MR_UInt32 uiLayoutFlags_t
Definition: UiLayoutFlags.h:53
Base class for display managers.
Definition: Display.h:73
A group of radio buttons.
Definition: RadioButton.h:104
void SetChecked(bool checked)
Set the button state.
Definition: StateButton.cpp:66
luabind::object val
Definition: Rulebook.cpp:52
void Add(std::shared_ptr< button_t > button)
Add a radio button to the group.
Definition: RadioButton.h:131
std::vector< std::shared_ptr< button_t > > buttons
Definition: RadioButton.h:187
virtual ~RadioButton()
Definition: RadioButton.h:89
A single radio button.
Definition: RadioButton.h:78
RadioButton(Display &display, const Vec2 &size, const std::string &text, const T &value, uiLayoutFlags_t layoutFlags=0)
Definition: RadioButton.h:86
Base class for clickable areas.
Definition: ClickRegion.h:49
RadioGroup()
Definition: RadioButton.h:107
Definition: Announcement.h:24
Base class for buttons with state (i.e.
Definition: StateButton.h:50
const T & GetValue() const
Definition: RadioButton.h:148
Base exception, providing constructors for setting the message.
Definition: Exception.h:42
void SetValue(const T &val)
Set the selected radio button by value.
Definition: RadioButton.h:162
RadioButton(Display &display, const std::string &text, const T &value, uiLayoutFlags_t layoutFlags=0)
Definition: RadioButton.h:83
button_t * selButton
Definition: RadioButton.h:189