Rose
Button.h
1 
10 #pragma once
11 
12 #include "Constants.h"
13 #include "Frame.h"
14 #include "Label.h"
15 #include "Entypo.h"
16 
17 namespace rose {
18 
34  class ButtonFrame : public Frame {
35  friend class RadioBehavior;
36 
37  protected:
38 
39  SignalSerialNumber mSignalSerialNumber{};
40 
42 
43  bool mSelectProgress{};
45 
49  bool mouseButtonEvent(const Position &mousePos, int button, bool down, int modifiers) override;
50 
52  bool clickTransactionCancel(const Position &mousePos, int button, bool down, int modifiers) override;
53 
56 
57  std::shared_ptr<Slot<std::string>> mSettingsUpdateRx{};
58 
59  public:
60  ButtonFrame() = delete;
61 
62  ~ButtonFrame() override = default;
63 
64  ButtonFrame(ButtonFrame &&) = delete;
65 
66  ButtonFrame(const ButtonFrame &) = delete;
67 
68  ButtonFrame &operator=(ButtonFrame &&) = delete;
69 
70  ButtonFrame &operator=(const ButtonFrame &) = delete;
71 
72  explicit ButtonFrame(int padding = 0) : ButtonFrame(Padding{padding}) {}
73 
74  explicit ButtonFrame(Padding padding);
75 
79  void initializeComposite() override;
80 
81  using SignalType = std::pair<bool, SignalToken>;
82 
87  std::shared_ptr<Slot<SignalType>> rxPushed;
88 
95 
100  std::shared_ptr<Slot<SignalType>> rxState;
101 
106 
112  mButtonSelectState = state;
113  switch (mButtonType) {
114  case NormalButton:
115  case CancelButton:
116  case MenuCascade:
117  case OkButton:
118  setInvert(false);
119  break;
120  case ToggleButton:
121  case RadioButton:
122  case TabButton:
123  setInvert(!state);
124  break;
125  }
126  }
127 
133 
135  void setStateId(const StateId &stateId) override {
136  Widget::setStateId(stateId);
138  !mStateId.empty() && rose()->hasSettings()) {
139  setInvert(rose()->settings()->getValue(mStateId.value(), 0) != 0);
140  rose()->settings()->dataChangeTx.connect(mSettingsUpdateRx);
141 
142  }
143  }
144  };
145 
161  class Button : public ButtonFrame {
162  protected:
163  std::string mLabelText{};
164  ImageId mImageId{RoseImageInvalid};
165  int mLabelFontSize{};
167 
168  public:
169  Button() : ButtonFrame(0) {}
170 
171  ~Button() override = default;
172 
173  Button(Button &&) = delete;
174 
175  Button(const Button &) = delete;
176 
177  Button &operator=(Button &&) = delete;
178 
179  Button &operator=(const Button &) = delete;
180 
185  explicit Button(const std::string &labelString, ButtonType type = ButtonType::NormalButton, int fontSize = 0);
186 
191  explicit Button(const Id &id, ButtonType type = ButtonType::NormalButton, int fontSize = 0);
192 
193  template<typename String>
194  explicit Button(String labelString, ButtonType type = ButtonType::NormalButton)
195  : Button(std::string{labelString}, type) {}
196 
202  explicit Button(RoseImageId imageId, ButtonType type = ButtonType::NormalButton);
203 
205  void initializeComposite() override;
206 
211  void setHorizontalAlignment(HorizontalAlignment alignment) override {
212  if (auto label = getSingleChild<Label>(); label)
213  label->setHorizontalAlignment(alignment);
214  }
215 
220  void setVerticalAlignment(VerticalAlignment alignment) override {
221  if (auto label = getSingleChild<Label>(); label)
222  label->setVerticalAlignment(alignment);
223  }
224 
229  void setText(const std::string &text) override {
230  if (auto label = getSingleChild<Label>(); label)
231  label->setText(text);
232  }
233 
238  std::string getText() const {
239  if (auto label = getSingleChild<Label>(); label)
240  return label->getText();
241  return std::string{""};
242  }
243 
248  void setFontName(std::string &fontName) {
249  if (auto label = getSingleChild<Label>(); label)
250  label->setFontName(fontName);
251  }
252 
257  void setFontSize(int fontSize) override {
258  if (auto label = getSingleChild<Label>(); label)
259  label->setFontSize(fontSize);
260  }
261 
266  void setImageId(ImageId imageId) override {
267  if (auto label = getSingleChild<Label>(); label)
268  label->setImageId(imageId);
269  }
270 
275  void setRenderFlip(sdl::RenderFlip renderFlip) {
276  if (auto label = getSingleChild<Label>(); label)
277  label->setRenderFlip(renderFlip);
278  }
279 
281  void setSize(Size size) override;
282 
283  };
284 
286  public:
287  enum class State {
288  None,
289  SetClear,
290  Set,
291  };
292 
293  protected:
294  State mState{None};
295  int mSelected{0};
296  bool mNoneIsValid{};
297 
298  using ButtonListType = std::pair<SignalToken,shared_ptr<ButtonFrame>>;
299  std::vector<ButtonListType> mButtons{};
300 
301  SignalSerialNumber mSignalSerialNumber{};
302 
303  std::shared_ptr<Slot<ButtonFrame::SignalType>> buttonStateRx{};
304 
305  public:
306  using SignalType = std::tuple<State,int,SignalToken>;
307 
308  RadioBehavior();
309 
310  ~RadioBehavior() = default;
311 
312  Signal<SignalType> stateTx{};
313 
318  explicit RadioBehavior(bool noneIsValid) : RadioBehavior() { mNoneIsValid = noneIsValid; }
319 
324  void emplace_back(std::shared_ptr<ButtonFrame> &button);
325 
330  void clear();
331 
337  void clearState();
338 
343  [[nodiscard]] std::pair<State,int> getState() const {
344  return std::make_pair(mState, mSelected);
345  }
346 
351  void setState(std::pair<State,int> state) {
352  setState(state.first, state.second);
353  }
354 
361  void setState(State state, int selected);
362  };
363 }
364 
370 inline std::shared_ptr<rose::Button>
371 operator<<(std::shared_ptr<rose::Button> widget, rose::sdl::RenderFlip &renderFlip) {
372  widget->setRenderFlip(renderFlip);
373  return widget;
374 }
375 
Invalid image ID.
Definition: Constants.h:172
State mState
The object state Id string.
Definition: Visual.h:140
A normal push button.
Definition: Constants.h:90
void setSelectState(ButtonSetState state)
Set the button select state.
Definition: Button.h:111
bool clickTransactionCancel(const Position &mousePos, int button, bool down, int modifiers) override
Handle a click transaction cancel event (default implementation: propagate to children) ...
Definition: Button.cpp:106
A Tab widget button.
Definition: Constants.h:95
RoseImageId
Identifiers for Textures created by the Rose object and available from the Rose ImageRepository.
Definition: Constants.h:171
std::pair< bool, SignalToken > SignalType
The content type of ButtonFrame signals and slots.
Definition: Button.h:81
void setState(std::pair< State, int > state)
Set the state, see setState(State,int).
Definition: Button.h:351
Definition: Button.h:285
A toggle button.
Definition: Constants.h:93
A frame that supports ButtonSemantics.
Definition: Button.h:23
void setVerticalAlignment(VerticalAlignment alignment) override
Set the horizontal alignment of the text within the label.
Definition: Button.h:220
A normal button with a check badge.
Definition: Constants.h:92
Signal< SignalType > txPushed
A Signal to transmit Pusehd signals on.
Definition: Button.h:94
void setStateId(const StateId &stateId) override
Set the ButtonFrame StateId.
Definition: Button.h:135
void setImageId(ImageId imageId) override
Set the ImageId for the ButtonFrame (Label).
Definition: Button.h:266
void updateStateSetting(ButtonSetState state)
Update the button state in the Setting database.
Definition: Button.cpp:127
std::string getText() const
Get the text of the label.
Definition: Button.h:238
A position in integer (x, y) co-ordinates.
Definition: Types.h:95
HorizontalAlignment
Horizontal alignment values.
Definition: Constants.h:254
VerticalAlignment
Vertical alignment values.
Definition: Constants.h:265
std::pair< State, int > getState() const
Get the current state of the RadioBehavior.
Definition: Button.h:343
Constants and Enumerations.
Abstraction of space consumed around an object for space, borders, etc.
Definition: Types.h:454
ButtonSetState
A type alias for bool to set the button state.
Definition: Constants.h:80
ButtonSetState mButtonSelectState
The current selected state of the button.
Definition: Button.h:44
Signal< SignalType > txState
A Signal to transmit.
Definition: Button.h:105
void setHorizontalAlignment(HorizontalAlignment alignment) override
Set the horizontal alignment of the text within the label.
Definition: Button.h:211
ButtonType
The button type.
Definition: PointerInteractions.h:21
void setFontSize(int fontSize) override
Set the font size.
Definition: Button.h:257
A radio button.
Definition: Constants.h:94
A Cascade Button, treated as a NormalButton.
Definition: Constants.h:96
A normal button with a cancel badge.
Definition: Constants.h:91
SignalSerialNumber mSignalSerialNumber
The button serial number.
Definition: Button.h:39
void setText(const std::string &text) override
Set the text of the label.
Definition: Button.h:229
std::shared_ptr< Slot< std::string > > mSettingsUpdateRx
Slot to receive settings updates on.
Definition: Button.h:57
ButtonType mButtonType
The button type.
Definition: Button.h:41
bool mSelectProgress
True when button selection is in progress.
Definition: Button.h:43
A Widget manipulator to indicate if and how rendering a texture should be flipped.
Definition: Renderer.h:33
RadioBehavior(bool noneIsValid)
Constructor.
Definition: Button.h:318
std::shared_ptr< Slot< SignalType > > rxPushed
A Slot to receive Pushed signals on.
Definition: Button.h:87
A configurable button class.
Definition: Button.h:161
A type to specify an Id value.
Definition: StructuredTypes.h:148
bool mouseButtonEvent(const Position &mousePos, int button, bool down, int modifiers) override
Handle mouse button events.
Definition: Button.cpp:51
std::shared_ptr< Slot< SignalType > > rxState
A Slot to receive State signals on.
Definition: Button.h:100
void initializeComposite() override
See Widget::initializeComposite.
Definition: Button.cpp:29
void setFontName(std::string &fontName)
Set the font name.
Definition: Button.h:248
A size in integer dimensions.
Definition: Types.h:230
void setRenderFlip(sdl::RenderFlip renderFlip)
Set RenderFlip for the ButtonFrame (Label).
Definition: Button.h:275
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
void setState(bool state)
Set the state, true = inverted.
Definition: Frame.h:165
void setSize(const Size &size)
Set preferred Size.
Definition: Visual.h:178
This is a list of icon codes for the entypo.ttf font by Daniel Bruce.
ButtonSetState getSelectState() const
Access the button select state.
Definition: Button.h:132