Rose
Button.h
1 
8 #pragma once
9 
10 #include <memory>
11 #include <utility>
12 #include "Frame.h"
13 #include "PointerInteractions.h"
14 #include "Text.h"
15 #include "Image.h"
16 
17 namespace rose {
18 
23  class ButtonFrame : public Frame {
24  protected:
25  bool mCentreHorizontal{false};
26  bool mCentreVertical{false};
27 
28  std::unique_ptr<ButtonSemantics> mButtonSemantics{};
29 
30  ButtonDisplayCallback mButtonDisplayCallback{};
31  ButtonStateChangeCallback mButtonStateChangeCallback{};
32 
33  public:
34  ~ButtonFrame() override = default;
35 
36  ButtonFrame(const ButtonFrame&) = delete;
37 
38  ButtonFrame(ButtonFrame&&) = delete;
39 
40  ButtonFrame& operator=(const ButtonFrame&) = delete;
41 
42  ButtonFrame& operator=(ButtonFrame&&) = delete;
43 
44  explicit ButtonFrame(ButtonType buttonType) noexcept;
45 
46  static constexpr std::string_view id = "ButtonFrame";
47  std::string_view nodeId() const noexcept override {
48  return id;
49  }
50 
56  void setButtonCommandCallback(uint commandId, ButtonCommandCallback buttonCommandCallback) {
57  mButtonSemantics->setButtonCommandCallback(commandId, std::move(buttonCommandCallback));
58  }
59  };
60 
65  class TextButton : public ButtonFrame , public Text {
66  protected:
67  friend class TextButtonLayoutManager;
68 
69  Rectangle layoutContent(gm::Context &context, const Rectangle &screenRect);
70 
71  public:
72  ~TextButton() override = default;
73 
74  TextButton(const TextButton&) = delete;
75 
76  TextButton(TextButton&&) = delete;
77 
78  TextButton& operator=(const TextButton&) = delete;
79 
80  TextButton& operator=(TextButton&&) = delete;
81 
82  explicit TextButton(ButtonType buttonType = ButtonType::PushButton) noexcept;
83 
84  explicit TextButton(const std::string& text, ButtonType buttonType = ButtonType::PushButton);
85 
86  explicit TextButton(const char* text, ButtonType buttonType = ButtonType::PushButton)
87  : TextButton(std::string{text}, buttonType) {}
88 
89  explicit TextButton(const std::string_view& text, ButtonType buttonType = ButtonType::PushButton)
90  : TextButton(std::string{text}, buttonType) {}
91 
92  explicit TextButton(const Id &id, ButtonType buttonType = ButtonType::PushButton);
93 
94  explicit TextButton(const Id &id, ButtonStateChangeCallback stateChangeCB,
96  : TextButton(id, buttonType) {
97  mButtonSemantics->setButtonStateChangeCallback(std::move(stateChangeCB));
98  }
99 
100  explicit TextButton(const std::string& text, ButtonStateChangeCallback stateChangeCB, ButtonType buttonType = ButtonType::PushButton)
101  : TextButton(text, buttonType) {
102  mButtonSemantics->setButtonStateChangeCallback(std::move(stateChangeCB));
103  }
104 
105  explicit TextButton(const char* text, ButtonStateChangeCallback stateChangeCB, ButtonType buttonType = ButtonType::PushButton)
106  : TextButton(std::string{text}, buttonType) {
107  mButtonSemantics->setButtonStateChangeCallback(std::move(stateChangeCB));
108  }
109 
110  explicit TextButton(const std::string_view& text, ButtonStateChangeCallback stateChangeCB, ButtonType buttonType = ButtonType::PushButton)
111  : TextButton(std::string{text}, buttonType) {
112  mButtonSemantics->setButtonStateChangeCallback(std::move(stateChangeCB));
113  }
114 
115  static constexpr std::string_view id = "TextButton";
116  std::string_view nodeId() const noexcept override {
117  return id;
118  }
119 
126  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
127 
134  void draw(gm::Context &context, const Position<int> &containerPosition) override;
135  };
136 
137  class TextLabel : public TextButton {
138  public:
139  TextLabel() noexcept : TextButton(ButtonType::Label) {}
140 
141  ~TextLabel() override = default;
142 
143  TextLabel(const TextLabel&) = delete;
144 
145  TextLabel(TextLabel&&) = delete;
146 
147  TextLabel& operator=(const TextLabel&) = delete;
148 
149  TextLabel& operator=(TextLabel&&) = delete;
150 
151  explicit TextLabel(const std::string& text) noexcept : TextButton(text, ButtonType::Label) {}
152 
153  explicit TextLabel(const char* text)
154  : TextButton(std::string{text}, ButtonType::Label) {}
155 
156  explicit TextLabel(const std::string_view& text)
157  : TextButton(std::string{text}, ButtonType::Label) {}
158 
159  explicit TextLabel(const Id& id) noexcept : TextButton(id, ButtonType::Label) {}
160 
161  static constexpr std::string_view id = "TextLabel";
162  std::string_view nodeId() const noexcept override {
163  return id;
164  }
165  };
166 
168  protected:
169  TextButton& mTextButton;
170 
171  public:
172 
173  TextButtonLayoutManager() = delete;
174  ~TextButtonLayoutManager() override = default;
175 
176  explicit TextButtonLayoutManager(TextButton& textButton);
177 
178  Rectangle layoutContent(gm::Context &context, const Rectangle &screenRect, LayoutManager::Itr first,
179  LayoutManager::Itr last) override;
180  };
181 
182  class ImageButton : public ButtonFrame, public Image {
183  protected:
184  friend class ImageButtonLayoutManager;
185 
186  Rectangle layoutContent(gm::Context &context, const Rectangle &screenRect);
187 
188  ImageId mImageId{ImageId::ThreeDots};
189 
190  Size mRequestedSize{};
191 
192  gm::RenderFlip mRenderFlip{SDL_FLIP_NONE};
193 
194  public:
195  ~ImageButton() override = default;
196 
197  ImageButton(const ImageButton&) = delete;
198 
199  ImageButton(ImageButton&&) = delete;
200 
201  ImageButton& operator=(const ImageButton&) = delete;
202 
203  ImageButton& operator=(ImageButton&&) = delete;
204 
205  explicit ImageButton(ButtonType buttonType = ButtonType::PushButton) noexcept;
206 
207  explicit ImageButton(ImageId imageId, ButtonType buttonType = ButtonType::PushButton) noexcept;
208 
209  static constexpr std::string_view id = "ImageButton";
210  std::string_view nodeId() const noexcept override {
211  return id;
212  }
213 
220  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
221 
228  void draw(gm::Context &context, const Position<int> &containerPosition) override;
229 
234  void setImage(ImageId imageId);
235 
236  void setRenderFlip(gm::RenderFlip renderFlip) {
237  mRenderFlip = renderFlip;
238  }
239  };
240 
241  class ImageLabel : public ImageButton {
242  public:
243  ~ImageLabel() override = default;
244 
245  ImageLabel(const ImageLabel&) = delete;
246 
247  ImageLabel(ImageLabel&&) = delete;
248 
249  ImageLabel& operator=(const ImageLabel&) = delete;
250 
251  ImageLabel& operator=(ImageLabel&&) = delete;
252 
253  explicit ImageLabel() noexcept : ImageButton(ButtonType::Label) {}
254 
255  explicit ImageLabel(ImageId imageId) noexcept : ImageButton(imageId, ButtonType::Label) {}
256 
257  static constexpr std::string_view id = "ImageLabel";
258  std::string_view nodeId() const noexcept override {
259  return id;
260  }
261 
262  };
263 
265  protected:
266  ImageButton& mImageButton;
267 
268  public:
269 
270  ImageButtonLayoutManager() = delete;
271  ~ImageButtonLayoutManager() override = default;
272 
273  explicit ImageButtonLayoutManager(ImageButton& imageButton);
274 
275  Rectangle layoutContent(gm::Context &context, const Rectangle &screenRect, LayoutManager::Itr first,
276  LayoutManager::Itr last) override;
277  };
278 }
279 
A pure virtual base class for layout managers.
Definition: Visual.h:392
When pressed and released signals an action.
std::function< void(ButtonStateChange buttonStateChange, uint commandId)> ButtonCommandCallback
Button command callback.
Definition: Callbacks.h:82
Definition: Button.h:137
void setButtonCommandCallback(uint commandId, ButtonCommandCallback buttonCommandCallback)
Definition: Button.h:56
Definition: Button.h:241
std::function< void(ButtonStateChange buttonStateChange)> ButtonStateChangeCallback
Button state change callback.
Definition: Callbacks.h:75
A frame that supports ButtonSemantics.
Definition: Button.h:23
Definition: Image.h:19
Definition: Button.h:264
void draw(gm::Context &context, const Position< int > &containerPosition) override
Draw the visual.
Definition: Frame.h:252
Definition: Button.h:167
Definition: Button.h:65
Definition: Frame.h:214
Rectangle layout(gm::Context &context, const Rectangle &screenRect) override
Layout the visual.
Definition: Frame.cpp:425
Context
Definition: GraphicsModel.h:76
Encapsulation of code for rendering Text.
Definition: Text.h:42
std::function< void(ButtonDisplayState buttonDisplayState)> ButtonDisplayCallback
Button display state callback.
Definition: Callbacks.h:69
ButtonType
The button type.
Definition: PointerInteractions.h:21
A composite of a Position and a Size.
Definition: Types.h:307
A type to specify an Id value.
Definition: StructuredTypes.h:148
A size in integer dimensions.
Definition: Types.h:230
Definition: Button.h:182
A Widget manipulator to indicate if and how rendering a texture should be flipped.
Definition: GraphicsModel.h:62
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
A button without any button semantics but can be inverted and animated.