TGUI  1.3-dev
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
26#ifndef TGUI_SCROLLBAR_HPP
27#define TGUI_SCROLLBAR_HPP
28
29
30#include <TGUI/Widget.hpp>
31#include <TGUI/Renderers/ScrollbarRenderer.hpp>
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <chrono>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
44 class TGUI_API Scrollbar : public Widget
45 {
46 public:
47
48 using Ptr = std::shared_ptr<Scrollbar>;
49 using ConstPtr = std::shared_ptr<const Scrollbar>;
50
51 static constexpr const char StaticWidgetType[] = "Scrollbar";
52
53
55 enum class Policy
56 {
57 Automatic,
58 Always,
59 Never
60 };
61
62
70 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
71
72
79 TGUI_NODISCARD static Scrollbar::Ptr create();
80
81
90 TGUI_NODISCARD static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
91
92
97 TGUI_NODISCARD ScrollbarRenderer* getSharedRenderer() override;
98 TGUI_NODISCARD const ScrollbarRenderer* getSharedRenderer() const override;
99
105 TGUI_NODISCARD ScrollbarRenderer* getRenderer() override;
106
107
115 void setSize(const Layout2d& size) override;
116 using Widget::setSize;
117
118
127 void setMaximum(unsigned int maximum);
128
129
138 TGUI_NODISCARD unsigned int getMaximum() const;
139
140
148 void setValue(unsigned int value);
149
150
159 TGUI_NODISCARD unsigned int getValue() const;
160
161
174 void setViewportSize(unsigned int viewport);
175
176
181 TGUI_NODISCARD unsigned int getViewportSize() const;
182
183
190 void setScrollAmount(unsigned int scrollAmount);
191
192
199 TGUI_NODISCARD unsigned int getScrollAmount() const;
200
201
209 void setAutoHide(bool autoHide);
210
211
218 TGUI_NODISCARD bool getAutoHide() const;
219
220
227 void setVerticalScroll(bool vertical);
228
229
234 TGUI_NODISCARD bool getVerticalScroll() const;
235
236
243 TGUI_NODISCARD float getDefaultWidth();
244
245
252 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
253
257 bool leftMousePressed(Vector2f pos) override;
258
262 void leftMouseReleased(Vector2f pos) override;
263
267 void mouseMoved(Vector2f pos) override;
268
272 bool scrolled(float delta, Vector2f pos, bool touch) override;
273
277 void leftMouseButtonNoLongerDown() override;
278
279
287 void draw(BackendRenderTarget& target, RenderStates states) const override;
288
289
291 protected:
292
293
295 // Updates the scrollbar after a size change
297 void updateSize();
298
299
309 TGUI_NODISCARD Signal& getSignal(String signalName) override;
310
311
318 void rendererChanged(const String& property) override;
319
320
324 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
325
326
330 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
331
332
334 // Updates the position of the thumb based on the current value of the slider
336 void updateThumbPosition();
337
338
340 // Makes a copy of the widget
342 TGUI_NODISCARD Widget::Ptr clone() const override;
343
344
346 public:
347
348 SignalUInt onValueChange = {"ValueChanged"};
349
350
352 protected:
353
354 enum class Part
355 {
356 Track,
357 Thumb,
358 ArrowUp,
359 ArrowDown
360 };
361
362 // Keep track on which part of the scrollbar the mouse is standing
363 Part m_mouseHoverOverPart = Part::Thumb;
364
365 // When the mouse went down, did it go down on top of the thumb? If so, where?
366 bool m_mouseDownOnThumb = false;
367 Vector2f m_mouseDownOnThumbPos;
368
369 unsigned int m_maximum = 10;
370 unsigned int m_value = 0;
371
372 // Maximum should be above this value before the scrollbar is needed
373 unsigned int m_viewportSize = 1;
374
375 // Is the scrollbar draw vertically?
376 bool m_verticalScroll = true;
377
378 // Does the image lie vertically?
379 bool m_verticalImage = true;
380
381 // How far should the value change when pressing one of the arrows?
382 unsigned int m_scrollAmount = 1;
383
384 // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
385 bool m_autoHide = true;
386
387 // Did the mouse went down on one of the arrows?
388 bool m_mouseDownOnArrow = false;
389
390 bool m_sizeSet = false; // Has setSize been called?
391
392 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
393 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
394
395 FloatRect m_track;
396 FloatRect m_thumb;
397 FloatRect m_arrowUp;
398 FloatRect m_arrowDown;
399
400 Sprite m_spriteTrack;
401 Sprite m_spriteTrackHover;
402 Sprite m_spriteThumb;
403 Sprite m_spriteThumbHover;
404 Sprite m_spriteArrowUp;
405 Sprite m_spriteArrowUpHover;
406 Sprite m_spriteArrowDown;
407 Sprite m_spriteArrowDownHover;
408
409 // Cached renderer properties
410 Color m_thumbColorCached;
411 Color m_thumbColorHoverCached;
412 Color m_trackColorCached;
413 Color m_trackColorHoverCached;
414 Color m_arrowColorCached;
415 Color m_arrowColorHoverCached;
416 Color m_arrowBackgroundColorCached;
417 Color m_arrowBackgroundColorHoverCached;
418
420 };
421
422
427 class TGUI_API ScrollbarChildWidget : public Scrollbar
428 {
429 public:
430
434 bool isMouseDownOnThumb() const;
435
436
443 bool isShown() const;
444
445
452 void draw(BackendRenderTarget& target, RenderStates states) const override;
453
454
456 };
457
459}
460
462
463#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Class to store the position or size of a widget.
Definition Layout.hpp:305
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition Scrollbar.hpp:428
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
bool isShown() const
Returns whether the scrollbar is currently visible.
Definition ScrollbarRenderer.hpp:37
Scrollbar widget.
Definition Scrollbar.hpp:45
TGUI_NODISCARD unsigned int getMaximum() const
Returns the maximum value.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:48
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:56
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.
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.
TGUI_NODISCARD float getDefaultWidth()
Returns the default width of the scrollbar.
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 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 setVerticalScroll(bool vertical)
Changes whether the scrollbar lies horizontally or vertically.
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:49
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
TGUI_NODISCARD bool getVerticalScroll() const
Returns whether the scrollbar lies horizontally or vertically.
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:62
Wrapper class to store strings.
Definition String.hpp:101
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
States used for drawing.
Definition RenderStates.hpp:39