TGUI  1.0-alpha
Scrollbar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
34
35namespace tgui
36{
40 class TGUI_API Scrollbar : public Widget
41 {
42 public:
43
44 typedef std::shared_ptr<Scrollbar> Ptr;
45 typedef std::shared_ptr<const Scrollbar> ConstPtr;
46
47
49 enum class Policy
50 {
51 Automatic,
52 Always,
53 Never
54 };
55
56
64 Scrollbar(const char* typeName = "Scrollbar", bool initRenderer = true);
65
66
74
75
85
86
92 const ScrollbarRenderer* getSharedRenderer() const;
93
100
101
109 void setSize(const Layout2d& size) override;
110 using Widget::setSize;
111
112
121 void setMaximum(unsigned int maximum);
122
123
132 unsigned int getMaximum() const;
133
134
142 void setValue(unsigned int value);
143
144
153 unsigned int getValue() const;
154
155
168 void setViewportSize(unsigned int viewport);
169
170
175 unsigned int getViewportSize() const;
176
177
184 void setScrollAmount(unsigned int scrollAmount);
185
186
193 unsigned int getScrollAmount() const;
194
195
203 void setAutoHide(bool autoHide);
204
205
212 bool getAutoHide() const;
213
214
221 void setVerticalScroll(bool vertical);
222
223
228 bool getVerticalScroll() const;
229
230
238
239
246 bool isMouseOnWidget(Vector2f pos) const override;
247
251 void leftMousePressed(Vector2f pos) override;
252
256 void leftMouseReleased(Vector2f pos) override;
257
261 void mouseMoved(Vector2f pos) override;
262
266 bool mouseWheelScrolled(float delta, Vector2f pos) override;
267
271 void leftMouseButtonNoLongerDown() override;
272
273
281 void draw(BackendRenderTarget& target, RenderStates states) const override;
282
283
285 protected:
286
287
289 // Updates the scrollbar after a size change
291 void updateSize();
292
293
303 Signal& getSignal(String signalName) override;
304
305
312 void rendererChanged(const String& property) override;
313
314
318 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
319
320
324 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
325
326
328 // Updates the position of the thumb based on the current value of the slider
330 void updateThumbPosition();
331
332
334 // Makes a copy of the widget
336 Widget::Ptr clone() const override;
337
338
340 public:
341
342 SignalUInt onValueChange = {"ValueChanged"};
343
344
346 protected:
347
348 enum class Part
349 {
350 Track,
351 Thumb,
352 ArrowUp,
353 ArrowDown
354 };
355
356 // Keep track on which part of the scrollbar the mouse is standing
357 Part m_mouseHoverOverPart = Part::Thumb;
358
359 // When the mouse went down, did it go down on top of the thumb? If so, where?
360 bool m_mouseDownOnThumb = false;
361 Vector2f m_mouseDownOnThumbPos;
362
363 unsigned int m_maximum = 10;
364 unsigned int m_value = 0;
365
366 // Maximum should be above this value before the scrollbar is needed
367 unsigned int m_viewportSize = 1;
368
369 // Is the scrollbar draw vertically?
370 bool m_verticalScroll = true;
371
372 // Does the image lie vertically?
373 bool m_verticalImage = true;
374
375 // How far should the value change when pressing one of the arrows?
376 unsigned int m_scrollAmount = 1;
377
378 // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
379 bool m_autoHide = true;
380
381 // Did the mouse went down on one of the arrows?
382 bool m_mouseDownOnArrow = false;
383
384 bool m_sizeSet = false; // Has setSize been called?
385
386 FloatRect m_track;
387 FloatRect m_thumb;
388 FloatRect m_arrowUp;
389 FloatRect m_arrowDown;
390
391 Sprite m_spriteTrack;
392 Sprite m_spriteTrackHover;
393 Sprite m_spriteThumb;
394 Sprite m_spriteThumbHover;
395 Sprite m_spriteArrowUp;
396 Sprite m_spriteArrowUpHover;
397 Sprite m_spriteArrowDown;
398 Sprite m_spriteArrowDownHover;
399
400 // Cached renderer properties
401 Color m_thumbColorCached;
402 Color m_thumbColorHoverCached;
403 Color m_trackColorCached;
404 Color m_trackColorHoverCached;
405 Color m_arrowColorCached;
406 Color m_arrowColorHoverCached;
407 Color m_arrowBackgroundColorCached;
408 Color m_arrowBackgroundColorHoverCached;
409
411 };
412
413
418 class TGUI_API ScrollbarChildWidget : public Scrollbar
419 {
420 public:
421
425 bool isMouseDownOnThumb() const;
426
427
434 bool isShown() const;
435
436
443 void draw(BackendRenderTarget& target, RenderStates states) const override;
444
445
447 };
448
450}
451
453
454#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:284
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:419
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:41
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
unsigned int getMaximum() const
Returns the maximum value.
void setValue(unsigned int value)
Changes the current value.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool getVerticalScroll() const
Returns whether the scrollbar lies horizontally or vertically.
static Scrollbar::Ptr create()
Creates a new scrollbar widget.
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.
float getDefaultWidth()
Returns the default width of the scrollbar.
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.
unsigned int getViewportSize() const
Returns the viewport size.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
ScrollbarRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int getValue() const
Returns the current value.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setVerticalScroll(bool vertical)
Changes whether the scrollbar lies horizontally or vertically.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
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:45
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
static Scrollbar::Ptr copy(Scrollbar::ConstPtr scrollbar)
Makes a copy of another scrollbar.
ScrollbarRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getAutoHide() const
Returns whether the scrollbar is hiding automatically or not.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:79
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39