Rose
ScrollArea.h
1 
7 #pragma once
8 
9 #include <utility>
10 
11 #include "SingleChild.h"
12 
18 namespace rose {
23  class ScrollArea : public SingleChild {
24  protected:
25  std::optional<SDL_TimerID> sdlTimerId{};
26  int mIndWidth{10};
27 
29  uint32_t mLastScrollTick;
30 
33 
36 
37  float mRatioX{};
38  float mRatioY{};
39 
40  bool mShowHorInd{};
41  bool mShowVerInd{};
42 
44  sdl::Texture generateScrollIndicator(sdl::Renderer &renderer, Size indicatorSize);
45 
46  void startTimer();
47 
48  static constexpr int MaximumChildSize = 4096;
49 
50  public:
54  ScrollArea() : SingleChild(), mScrollOffset(0, 0) {
55  mSupportsDrag = true;
56  mLastScrollTick = 0;
57  mClassName = "ScrollArea";
58  mAcceptsFocus = true;
59  mSupportsDrag = true;
60  mSupportsScrollWheel = true;
61  }
62 
63  ~ScrollArea() override = default;
64 
71  static Uint32 TimerCallbackStub(Uint32 interval, void *param) {
72  auto scrollArea = static_cast<ScrollArea *>(param);
73  scrollArea->scrollIndicatorsOff();
74  return interval;
75  }
76 
81  if (sdlTimerId) {
82  SDL_RemoveTimer(sdlTimerId.value());
83  sdlTimerId.reset();
84  }
85  mShowHorInd = mShowVerInd = false;
86  setNeedsDrawing();
87  }
88 
92  void draw(sdl::Renderer &renderer, Rectangle parentRect) override;
93 
102  Rectangle widgetLayout(sdl::Renderer &renderer, Rectangle available, uint layoutStage) override;
103 
108  void setScrollOffset(Position scrollOffset) {
109  mScrollOffset = scrollOffset;
110  setNeedsDrawing();
111  }
112 
119  std::shared_ptr<Widget> findWidget(const Position &pos) override;
120 
126 
128  bool mouseEnterEvent(const Position &p, bool enter) override;
129 
131  bool mouseButtonEvent(const Position &mousePos, int button, bool down, int modifiers) override;
132 
134  bool mouseMotionEvent(const Position &cursorPosition, const Position &rel, int button, int modifiers) override;
135 
137  bool mouseDragEvent(const Position &mousePos, const Position &rel, int button, int modifiers) override;
138 
140  bool scrollEvent(const Position &mousePos, int32_t x, int32_t y) override;
141 
142 // /// Handle a keyboard event (default implementation: do nothing)
143 // bool keyboardEvent(int key, int scancode, int action, int modifiers) override;
144 
146  bool keyboardCharacterEvent(unsigned int codepoint) override;
147  };
148 }
149 
150 
bool keyboardCharacterEvent(unsigned int codepoint) override
Handle text input (UTF-32 format) (default implementation: do nothing)
Definition: ScrollArea.cpp:100
int mIndWidth
The width of the indicator bars.
Definition: ScrollArea.h:26
void scrollIndicatorsOff()
Timer callback to turn scroll indicators off.
Definition: ScrollArea.h:80
bool scrollEvent(const Position &mousePos, int32_t x, int32_t y) override
Handle a mouse scroll event (default implementation: propagate to children)
Definition: ScrollArea.cpp:129
bool mShowVerInd
If True, display the vertical scroll indicator.
Definition: ScrollArea.h:41
Size mHorIndSize
The size of the horizontal indicator.
Definition: ScrollArea.h:34
void setScrollOffset(Position scrollOffset)
Set the scroll offset of the ScrollArea.
Definition: ScrollArea.h:108
void draw(sdl::Renderer &renderer, Rectangle parentRect) override
See Widget::draw.
Definition: ScrollArea.cpp:30
Position getScrollOffset() const
Get the current scroll offset of the ScrollArea.
Definition: ScrollArea.h:125
Rectangle widgetLayout(sdl::Renderer &renderer, Rectangle available, uint layoutStage) override
Determine the desired size of the child widgets.
Definition: ScrollArea.cpp:12
Position mScrollOffset
The current scroll offset.
Definition: ScrollArea.h:28
An encapsulation of the SDL_Texture structure.
Definition: Texture.h:40
uint32_t mLastScrollTick
Used to track how fast the scroll wheel is turning.
Definition: ScrollArea.h:29
bool mShowHorInd
If True, display the horizontal scroll indicator.
Definition: ScrollArea.h:40
float mRatioY
The vertical ratio.
Definition: ScrollArea.h:38
A position in integer (x, y) co-ordinates.
Definition: Types.h:95
bool mouseDragEvent(const Position &mousePos, const Position &rel, int button, int modifiers) override
Handle a mouse drag event (default implementation: do nothing)
Definition: ScrollArea.cpp:104
sdl::Texture generateScrollIndicator(sdl::Renderer &renderer, Size indicatorSize)
Generate a scroll indicator.
Definition: ScrollArea.cpp:169
class ScrollArea brief A Container that provides scrolling to a single child.
Definition: ScrollArea.h:23
bool mouseButtonEvent(const Position &mousePos, int button, bool down, int modifiers) override
Handle a mouse button event (default implementation: propagate to children)
Definition: ScrollArea.cpp:88
bool mouseEnterEvent(const Position &p, bool enter) override
Handle a mouse enter event.
Definition: ScrollArea.cpp:84
sdl::Texture mVerticalInd
An indicator of the position of the Vertical Scroll.
Definition: ScrollArea.h:32
bool mouseMotionEvent(const Position &cursorPosition, const Position &rel, int button, int modifiers) override
Handle a mouse motion event (default implementation: propagate to children)
Definition: ScrollArea.cpp:92
static constexpr int MaximumChildSize
The maximum width and height of a widget behind a ScrollArea.
Definition: ScrollArea.h:48
ScrollArea()
Constructor.
Definition: ScrollArea.h:54
A composite of a Position and a Size.
Definition: Types.h:307
std::shared_ptr< Widget > findWidget(const Position &pos) override
Find the Widget which uniquely contains the position.
Definition: ScrollArea.cpp:190
Written as a workaround for an issue in the SDL2 Library.
Definition: Renderer.h:64
sdl::Texture mHorizontalInd
An indicator of the position of the Horizontal Scroll.
Definition: ScrollArea.h:31
void startTimer()
Start the time to hid the scroll indicators.
Definition: ScrollArea.cpp:178
A size in integer dimensions.
Definition: Types.h:230
static Uint32 TimerCallbackStub(Uint32 interval, void *param)
The SDL_Timer callback.
Definition: ScrollArea.h:71
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
std::optional< SDL_TimerID > sdlTimerId
Timer to turn off scroll indicators.
Definition: ScrollArea.h:25
float mRatioX
The horizontal ratio.
Definition: ScrollArea.h:37
Size mVerIndSize
The size of the vertical indicator.
Definition: ScrollArea.h:35