TGUI  0.10-beta
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 const ScrollbarRenderer* getRenderer() const;
101
102
110 void setSize(const Layout2d& size) override;
111 using Widget::setSize;
112
113
122 void setMaximum(unsigned int maximum);
123
124
133 unsigned int getMaximum() const;
134
135
143 void setValue(unsigned int value);
144
145
154 unsigned int getValue() const;
155
156
169 void setViewportSize(unsigned int viewport);
170
171
176 unsigned int getViewportSize() const;
177
178
185 void setScrollAmount(unsigned int scrollAmount);
186
187
194 unsigned int getScrollAmount() const;
195
196
204 void setAutoHide(bool autoHide);
205
206
213 bool getAutoHide() const;
214
215
222 void setVerticalScroll(bool vertical);
223
224
229 bool getVerticalScroll() const;
230
231
239
240
247 bool isMouseOnWidget(Vector2f pos) const override;
248
252 void leftMousePressed(Vector2f pos) override;
253
257 void leftMouseReleased(Vector2f pos) override;
258
262 void mouseMoved(Vector2f pos) override;
263
267 bool mouseWheelScrolled(float delta, Vector2f pos) override;
268
272 void leftMouseButtonNoLongerDown() override;
273
274
282 void draw(BackendRenderTarget& target, RenderStates states) const override;
283
284
286 protected:
287
288
290 // Updates the scrollbar after a size change
292 void updateSize();
293
294
304 Signal& getSignal(String signalName) override;
305
306
313 void rendererChanged(const String& property) override;
314
315
319 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
320
321
325 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
326
327
329 // Updates the position of the thumb based on the current value of the slider
331 void updateThumbPosition();
332
333
335 // Makes a copy of the widget
337 Widget::Ptr clone() const override;
338
339
341 public:
342
343 SignalUInt onValueChange = {"ValueChanged"};
344
345
347 protected:
348
349 enum class Part
350 {
351 Track,
352 Thumb,
353 ArrowUp,
354 ArrowDown
355 };
356
357 // Keep track on which part of the scrollbar the mouse is standing
358 Part m_mouseHoverOverPart = Part::Thumb;
359
360 // When the mouse went down, did it go down on top of the thumb? If so, where?
361 bool m_mouseDownOnThumb = false;
362 Vector2f m_mouseDownOnThumbPos;
363
364 unsigned int m_maximum = 10;
365 unsigned int m_value = 0;
366
367 // Maximum should be above this value before the scrollbar is needed
368 unsigned int m_viewportSize = 1;
369
370 // Is the scrollbar draw vertically?
371 bool m_verticalScroll = true;
372
373 // Does the image lie vertically?
374 bool m_verticalImage = true;
375
376 // How far should the value change when pressing one of the arrows?
377 unsigned int m_scrollAmount = 1;
378
379 // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
380 bool m_autoHide = true;
381
382 // Did the mouse went down on one of the arrows?
383 bool m_mouseDownOnArrow = false;
384
385 bool m_sizeSet = false; // Has setSize been called?
386
387 FloatRect m_track;
388 FloatRect m_thumb;
389 FloatRect m_arrowUp;
390 FloatRect m_arrowDown;
391
392 Sprite m_spriteTrack;
393 Sprite m_spriteTrackHover;
394 Sprite m_spriteThumb;
395 Sprite m_spriteThumbHover;
396 Sprite m_spriteArrowUp;
397 Sprite m_spriteArrowUpHover;
398 Sprite m_spriteArrowDown;
399 Sprite m_spriteArrowDownHover;
400
401 // Cached renderer properties
402 Color m_thumbColorCached;
403 Color m_thumbColorHoverCached;
404 Color m_trackColorCached;
405 Color m_trackColorHoverCached;
406 Color m_arrowColorCached;
407 Color m_arrowColorHoverCached;
408 Color m_arrowBackgroundColorCached;
409 Color m_arrowBackgroundColorHoverCached;
410
412 };
413
414
419 class TGUI_API ScrollbarChildWidget : public Scrollbar
420 {
421 public:
422
426 bool isMouseDownOnThumb() const;
427
428
435 bool isShown() const;
436
437
444 void draw(BackendRenderTarget& target, RenderStates states) const override;
445
446
448 };
449
451}
452
454
455#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:420
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