TGUI  1.4.0
Loading...
Searching...
No Matches
Scrollbar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SCROLLBAR_HPP
26#define TGUI_SCROLLBAR_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/ScrollbarRenderer.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <chrono>
33#endif
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
42 class TGUI_API Scrollbar : public Widget
43 {
44 public:
45
46 using Ptr = std::shared_ptr<Scrollbar>;
47 using ConstPtr = std::shared_ptr<const Scrollbar>;
48
49 static constexpr const char StaticWidgetType[] = "Scrollbar";
50
52 enum class Policy
53 {
54 Automatic,
55 Always,
56 Never
57 };
58
66 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
67
73 TGUI_NODISCARD static Scrollbar::Ptr create();
74
82 TGUI_NODISCARD static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
83
88 TGUI_NODISCARD ScrollbarRenderer* getSharedRenderer() override;
89 TGUI_NODISCARD const ScrollbarRenderer* getSharedRenderer() const override;
90
96 TGUI_NODISCARD ScrollbarRenderer* getRenderer() override;
97
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
116 void setMaximum(unsigned int maximum);
117
125 TGUI_NODISCARD unsigned int getMaximum() const;
126
134 void setValue(unsigned int value);
135
143 TGUI_NODISCARD unsigned int getValue() const;
144
157 void setViewportSize(unsigned int viewport);
158
163 TGUI_NODISCARD unsigned int getViewportSize() const;
164
172 TGUI_NODISCARD unsigned int getMaxValue() const;
173
179 void setScrollAmount(unsigned int scrollAmount);
180
186 TGUI_NODISCARD unsigned int getScrollAmount() const;
187
195 void setAutoHide(bool autoHide);
196
203 TGUI_NODISCARD bool getAutoHide() const;
204
205#ifndef TGUI_REMOVE_DEPRECATED_CODE
212 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
213
218 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
219#endif
220
229 void setOrientation(Orientation orientation);
230
236 TGUI_NODISCARD Orientation getOrientation() const;
237
244 TGUI_NODISCARD float getDefaultWidth() const;
245
251 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
252
256 bool leftMousePressed(Vector2f pos) override;
257
261 void leftMouseReleased(Vector2f pos) override;
262
266 void mouseMoved(Vector2f pos) override;
267
271 bool scrolled(float delta, Vector2f pos, bool touch) override;
272
276 void leftMouseButtonNoLongerDown() override;
277
284 void draw(BackendRenderTarget& target, RenderStates states) const override;
285
287 protected:
288
290 // Updates the scrollbar after a size change
292 void updateSize();
293
303 TGUI_NODISCARD Signal& getSignal(String signalName) override;
304
310 void rendererChanged(const String& property) override;
311
315 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
316
320 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
321
323 // Updates the position of the thumb based on the current value of the slider
325 void updateThumbPosition();
326
328 // Makes a copy of the widget
330 TGUI_NODISCARD Widget::Ptr clone() const override;
331
333 public:
334
335 SignalUInt onValueChange = {"ValueChanged"};
336
338 protected:
339
340 enum class Part
341 {
342 Track,
343 Thumb,
344 ArrowUp,
345 ArrowDown
346 };
347
348 // Keep track on which part of the scrollbar the mouse is standing
349 Part m_mouseHoverOverPart = Part::Thumb;
350
351 // When the mouse went down, did it go down on top of the thumb? If so, where?
352 bool m_mouseDownOnThumb = false;
353 Vector2f m_mouseDownOnThumbPos;
354
355 unsigned int m_maximum = 10;
356 unsigned int m_value = 0;
357
358 // Maximum should be above this value before the scrollbar is needed
359 unsigned int m_viewportSize = 1;
360
361 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
362 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
363
364 // How far should the value change when pressing one of the arrows?
365 unsigned int m_scrollAmount = 1;
366
367 // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
368 bool m_autoHide = true;
369
370 // Did the mouse went down on one of the arrows?
371 bool m_mouseDownOnArrow = false;
372
373 bool m_sizeSet = false; // Has setSize been called?
374
375 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
376 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
377
378 FloatRect m_track;
379 FloatRect m_thumb;
380 FloatRect m_arrowUp;
381 FloatRect m_arrowDown;
382
383 Sprite m_spriteTrack;
384 Sprite m_spriteTrackHover;
385 Sprite m_spriteThumb;
386 Sprite m_spriteThumbHover;
387 Sprite m_spriteArrowUp;
388 Sprite m_spriteArrowUpHover;
389 Sprite m_spriteArrowDown;
390 Sprite m_spriteArrowDownHover;
391
392 // Cached renderer properties
393 Color m_thumbColorCached;
394 Color m_thumbColorHoverCached;
395 Color m_trackColorCached;
396 Color m_trackColorHoverCached;
397 Color m_arrowColorCached;
398 Color m_arrowColorHoverCached;
399 Color m_arrowBackgroundColorCached;
400 Color m_arrowBackgroundColorHoverCached;
401
403 };
404
409 class TGUI_API ScrollbarChildWidget : public Scrollbar
410 {
411 public:
412
416 TGUI_NODISCARD bool isMouseDownOnThumb() const;
417
424 TGUI_NODISCARD bool isShown() const;
425
432 void draw(BackendRenderTarget& target, RenderStates states) const override;
433
435 };
436
438}
439
441
442#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Class to store the position or size of a widget.
Definition Layout.hpp:313
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition Scrollbar.hpp:410
TGUI_NODISCARD bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TGUI_NODISCARD bool isShown() const
Returns whether the scrollbar is currently visible.
Definition ScrollbarRenderer.hpp:35
Scrollbar widget.
Definition Scrollbar.hpp:43
TGUI_NODISCARD unsigned int getMaximum() const
Returns the maximum value.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:46
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
TGUI_NODISCARD std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:53
TGUI_NODISCARD unsigned int getViewportSize() const
Returns the viewport size.
void setValue(unsigned int value)
Changes the current value.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void setOrientation(Orientation orientation)
Changes whether the scrollbar lies horizontally or vertically.
TGUI_NODISCARD bool getAutoHide() const
Returns whether the scrollbar is hiding automatically or not.
TGUI_NODISCARD ScrollbarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
void setMaximum(unsigned int maximum)
Sets a maximum value.
void setSize(const Layout2d &size) override
Changes the size of the scrollbar.
void setViewportSize(unsigned int viewport)
Changes the viewport size.
static TGUI_NODISCARD Scrollbar::Ptr copy(const Scrollbar::ConstPtr &scrollbar)
Makes a copy of another scrollbar.
TGUI_NODISCARD unsigned int getMaxValue() const
Returns the maximum value that can be set with the setValue function.
TGUI_NODISCARD float getDefaultWidth() const
Returns the default width of the scrollbar.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
TGUI_NODISCARD ScrollbarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD unsigned int getValue() const
Returns the current value.
TGUI_NODISCARD unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition Scrollbar.hpp:47
TGUI_NODISCARD Orientation getOrientation() const
Returns whether the scrollbar lies horizontally or vertically.
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
static TGUI_NODISCARD Scrollbar::Ptr create()
Creates a new scrollbar widget.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:51
States used for drawing.
Definition RenderStates.hpp:38