Rose
Frame.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <cstdint>
11 #include "Animation.h"
12 #include "Color.h"
13 #include "Theme.h"
14 #include "ImageStore.h"
15 #include "Visual.h"
16 
17 namespace rose {
18 
23  enum UseBorder {
24  None,
29  };
30 
35  class FrameElements {
36  protected:
37  color::RGBA mBaseColor;
38  color::RGBA mInvertColor;
39  color::RGBA mTopColor;
40  color::RGBA mBotColor;
41  color::RGBA mLeftColor;
42  color::RGBA mRightColor;
43  color::RGBA mActiveColor;
44  color::RGBA mInactiveColor;
45  float mColorValue{};
46  int mFrameWidth{2};
47  Padding mFramePadding{};
48  CornerStyle mCornerStyle{CornerStyle::Round};
49  bool mInvert{};
50  gm::Texture mBorder{};
51  gm::Texture mInactiveBG{};
52  gm::Texture mAnimatedBG{};
53 
54  FrameSettings mFrameSettings{};
55 
60  enum SelectedCorners : uint32_t {
61  NoCorners = 0,
62  TopLeftCorner = 0x8,
63  TopRightCorner = 0x4,
64  TopCorners = TopLeftCorner | TopRightCorner,
65  BottomLeftCorner = 0x2,
66  BottomRightCorner = 0x1,
67  BottomCorners = BottomLeftCorner | BottomRightCorner,
68  LeftCorners = TopLeftCorner | BottomLeftCorner,
69  RightCorners = TopRightCorner | BottomRightCorner,
70  AllCorners = 0xf,
71  };
72 
77  enum SelectedSides : uint32_t {
78  NoSides = 0,
79  TopSide = 1,
80  BotSide = 2,
81  LeftSide = 4,
82  RightSide = 8,
83  AllSides = TopSide | BotSide | LeftSide | RightSide,
84  TabTopSides = TopSide | LeftSide | RightSide,
85  };
86 
95  static void
96  trimCorners(gm::Surface &surface, color::RGBA color, FrameElements::SelectedCorners selectedCorners, Size cornerSize,
97  Size frameSize);
98 
106  static void
107  renderSelectedCorners(gm::Context &context, SelectedCorners selectedCorners, ImageId corner,
108  const Size &size);
109 
119  void
120  renderSelectedSides(gm::Context &context, FrameElements::SelectedSides selectedSides, BorderStyle useBorder,
121  ImageId corner, const Size &size, int extend = 0);
122 
130  void drawBackground(gm::Context &context, Rectangle &src, Rectangle &dst);
131 
133  createBackgroundTexture(gm::Context &context, Rectangle &src, Rectangle &dst, const color::RGBA &color);
134 
135  std::tuple<UseBorder,SelectedCorners> decoration();
136 
142  void drawFrame(gm::Context &context, Rectangle widgetRect);
143 
148  void buttonDisplayStateChange(ButtonDisplayState buttonDisplayState);
149 
150  public:
151  FrameElements();
152  virtual ~FrameElements() = default;
153 
154  explicit FrameElements(int padding) : mFramePadding(padding) {}
155 
157  void set(const CornerStyle cornerStyle) {
158  mCornerStyle = cornerStyle;
159  mBorder.reset();
160  mAnimatedBG.reset();
161  mInactiveBG.reset();
162  }
163 
165  void setState(bool state) {
166  mInvert = state;
167  mBorder.reset();
168  mAnimatedBG.reset();
169  mInactiveBG.reset();
170  }
171 
173  void setAnimateColor(const color::RGBA &color) {
174  mActiveColor = color;
175  mAnimatedBG.reset();
176  }
177 
179  void setInactiveColor(const color::RGBA &color) {
180  mInactiveColor = color;
181  mInactiveBG.reset();
182  }
183 
184  [[nodiscard]] bool getState() const { return mInvert; }
185 
186  static gm::Texture
187  createBackgroundMask(gm::Context &context, Size size, int frameWidth, bool roundCorners = false);
188 
189  static void colorBackgroundMask(gm::Context &context, gm::Texture &mask, const color::RGBA &base,
190  const color::RGBA &active, float value);
191 
192  void frameSettings(const FrameSettings& frameSettings) {
193  mFrameSettings = frameSettings;
194  mBorder.reset();
195  mInactiveBG.reset();
196  mAnimatedBG.reset();
197  }
198  };
199 
201  public:
203  mMaxContent = 1;
204  }
205 
206  ~FrameLayoutManager() override = default;
207 
209  Rectangle layoutContent(gm::Context &context, const Rectangle &screenRect, LayoutManager::Itr first,
210  LayoutManager::Itr last) override;
211 
212  };
213 
214  class Frame : public Manager, public FrameElements, public Animation {
215  public:
216  Frame() noexcept : Manager(), FrameElements() {
217  mLayoutManager = std::make_unique<FrameLayoutManager>();
218  mAnimationCallback = [&](gm::Context &context, const Position<int> &position, uint32_t frame) {
219  if (mActionCurve) {
220  auto idx = frame % mActionCurve->size();
221  mColorValue = (*mActionCurve)[idx];
222  draw(context, position);
223  } else {
224  removeAnimation(getWindow(), getNode<Animation>());
225  }
226  };
227 
228  mAnimationEnableStateCallback = [&](AnimationEnable animationEnable){
229  if (animationEnable == AnimationEnable::Disable && mAnimationEnableState == AnimationEnable::Enable) {
230  removeAnimation(getWindow(), getNode<Animation>());
231  }
232  };
233  }
234 
235  explicit Frame(bool invert) noexcept : Frame() {
236  mInvert = invert;
237  }
238 
239  ~Frame() override = default;
240 
241  explicit Frame(int padding) noexcept : Frame() {
242  mPadding = Padding{padding};
243  }
244 
245  static constexpr std::string_view id = "Frame";
246  std::string_view nodeId() const noexcept override {
247  return id;
248  }
249 
250  void drawAnimate(gm::Context &context, const Position<int> &containerPosition);
251 
252  void draw(gm::Context &context, const Position<int> &containerPosition) override {
253  if (mActionCurve && mAnimationEnableState == AnimationEnable::Enable)
254  setAnimation(getWindow(), getNode<Animation>(), containerPosition);
255  drawAnimate(context,containerPosition);
256  }
257 
258  Rectangle layout(gm::Context &context, const Rectangle &screenRect) override;
259 
260  };
261 
262  enum class FrameColorType {
263  InactiveColor,
264  AnimateColor,
265  };
266 
267  struct FrameColor {
268  FrameColorType type{};
269  color::RGBA rgba{};
270 
271  explicit FrameColor(FrameColorType type, color::RGBA color) : type(type), rgba(color) {}
272  };
273 }
274 
275 template<typename ManagerClass>
276 inline std::shared_ptr<ManagerClass> operator<<(std::shared_ptr<ManagerClass> manager, const rose::CornerStyle cornerStyle) {
277  static_assert(std::is_base_of_v<rose::FrameElements,ManagerClass>, "ManagerClass must be derived from rose::FrameElements." );
278  manager->set(cornerStyle);
279  return manager;
280 }
281 
282 template <class ManagerClass>
283 inline std::shared_ptr<ManagerClass> operator<<(std::shared_ptr<ManagerClass> manager, const rose::FrameColor& frameColor) {
284  static_assert(std::is_base_of_v<rose::FrameElements,ManagerClass>, "ManagerClass must be derived from rose::FrameElements." );
285  switch (frameColor.type) {
286  case rose::FrameColorType::InactiveColor:
287  manager->setInactiveColor(frameColor.rgba);
288  break;
289  case rose::FrameColorType::AnimateColor:
290  manager->setAnimateColor(frameColor.rgba);
291  break;
292  }
293  return manager;
294 }
295 
296 template <class ManagerClass>
297 inline std::shared_ptr<ManagerClass> operator<<(std::shared_ptr<ManagerClass> manager, const rose::FrameSettings& frameSettings) {
298  static_assert(std::is_base_of_v<rose::FrameElements, ManagerClass>,
299  "ManagerClass must be derived from rose::FrameElements.");
300  manager->frameSettings(frameSettings);
301  return manager;
302 }
A pure virtual base class for layout managers.
Definition: Visual.h:392
SelectedCorners
Specify corners selected for some process, usually drawing.
Definition: Frame.h:60
A Widget which manages contained Widgets.
Definition: Visual.h:697
A beveled border that gives the illusion the frame is sunk into the display.
Definition: Frame.h:26
A wrapper class for SDL_Surface pointers.
Definition: Surface.h:50
AnimationEnable
An enumeration to enable or disable animation.
Definition: Animation.h:24
Red Green Blue Alpha representation of a color.
Definition: Color.h:64
SelectedSides
Specify sides selected for some process, usually drawing.
Definition: Frame.h:77
A notched border that looks like a trench surrounding the frame.
Definition: Frame.h:28
CornerStyle
Types of corners supported.
Definition: Types.h:63
UseBorder
The type of border to draw.
Definition: Frame.h:23
Definition: Frame.h:200
void draw(gm::Context &context, const Position< int > &containerPosition) override
Draw the visual.
Definition: Frame.h:252
void renderSelectedSides(gm::Context &context, FrameElements::SelectedSides selectedSides, BorderStyle useBorder, ImageId corner, const Size &size, int extend=0)
Render sides as part of a border around the frame.
Definition: Frame.cpp:93
Definition: Frame.h:214
Definition: Frame.h:267
Definition: Animation.h:157
No border.
Definition: Frame.h:24
Abstraction of space consumed around an object for space, borders, etc.
Definition: Types.h:454
ButtonDisplayState
The visible state a button is in.
Definition: Callbacks.h:21
Abstraction of SDL_Texture.
Definition: Texture.h:46
Context
Definition: GraphicsModel.h:76
A beveled border that gives the illusion the frame stands up from the display.
Definition: Frame.h:25
void drawBackground(gm::Context &context, Rectangle &src, Rectangle &dst)
Draw the background for the Frame.
Definition: Frame.cpp:256
void drawFrame(gm::Context &context, Rectangle widgetRect)
Draw the Frame and background.
Definition: Frame.cpp:349
void setAnimateColor(const color::RGBA &color)
Set the active color.
Definition: Frame.h:173
A notched border that looks like a ridge surrounding the frame.
Definition: Frame.h:27
void setInactiveColor(const color::RGBA &color)
Set the background color.
Definition: Frame.h:179
A composite of a Position and a Size.
Definition: Types.h:307
A size in integer dimensions.
Definition: Types.h:230
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
static void trimCorners(gm::Surface &surface, color::RGBA color, FrameElements::SelectedCorners selectedCorners, Size cornerSize, Size frameSize)
Trim corners on a background surface to allow for rounded corners.
Definition: Frame.cpp:27
BorderStyle
The types of border supported.
Definition: Types.h:50
void setState(bool state)
Set the state, true = inverted.
Definition: Frame.h:165
User Interface Visual types.
Definition: Types.h:75
static void renderSelectedCorners(gm::Context &context, SelectedCorners selectedCorners, ImageId corner, const Size &size)
Render corner images as part of a border around the frame.
Definition: Frame.cpp:59
Encapsulation of the visual elements of a Frame.
Definition: Frame.h:35
void buttonDisplayStateChange(ButtonDisplayState buttonDisplayState)
Make visual changes to FrameElements for button display.
Definition: Frame.cpp:378