TGUI  1.0-beta
Loading...
Searching...
No Matches
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 using Ptr = std::shared_ptr<Scrollbar>;
45 using ConstPtr = std::shared_ptr<const Scrollbar>;
46
47 static constexpr const char StaticWidgetType[] = "Scrollbar";
48
49
51 enum class Policy
52 {
53 Automatic,
54 Always,
55 Never
56 };
57
58
66 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
67
68
76
77
86 static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
87
88
94 const ScrollbarRenderer* getSharedRenderer() const;
95
102
103
111 void setSize(const Layout2d& size) override;
112 using Widget::setSize;
113
114
123 void setMaximum(unsigned int maximum);
124
125
134 unsigned int getMaximum() const;
135
136
144 void setValue(unsigned int value);
145
146
155 unsigned int getValue() const;
156
157
170 void setViewportSize(unsigned int viewport);
171
172
177 unsigned int getViewportSize() const;
178
179
186 void setScrollAmount(unsigned int scrollAmount);
187
188
195 unsigned int getScrollAmount() const;
196
197
205 void setAutoHide(bool autoHide);
206
207
214 bool getAutoHide() const;
215
216
223 void setVerticalScroll(bool vertical);
224
225
230 bool getVerticalScroll() const;
231
232
240
241
248 bool isMouseOnWidget(Vector2f pos) const override;
249
253 void leftMousePressed(Vector2f pos) override;
254
258 void leftMouseReleased(Vector2f pos) override;
259
263 void mouseMoved(Vector2f pos) override;
264
268 bool mouseWheelScrolled(float delta, Vector2f pos) override;
269
273 void leftMouseButtonNoLongerDown() override;
274
275
283 void draw(BackendRenderTarget& target, RenderStates states) const override;
284
285
287 protected:
288
289
291 // Updates the scrollbar after a size change
293 void updateSize();
294
295
305 Signal& getSignal(String signalName) override;
306
307
314 void rendererChanged(const String& property) override;
315
316
320 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
321
322
326 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
327
328
330 // Updates the position of the thumb based on the current value of the slider
332 void updateThumbPosition();
333
334
336 // Makes a copy of the widget
338 Widget::Ptr clone() const override;
339
340
342 public:
343
344 SignalUInt onValueChange = {"ValueChanged"};
345
346
348 protected:
349
350 enum class Part
351 {
352 Track,
353 Thumb,
354 ArrowUp,
355 ArrowDown
356 };
357
358 // Keep track on which part of the scrollbar the mouse is standing
359 Part m_mouseHoverOverPart = Part::Thumb;
360
361 // When the mouse went down, did it go down on top of the thumb? If so, where?
362 bool m_mouseDownOnThumb = false;
363 Vector2f m_mouseDownOnThumbPos;
364
365 unsigned int m_maximum = 10;
366 unsigned int m_value = 0;
367
368 // Maximum should be above this value before the scrollbar is needed
369 unsigned int m_viewportSize = 1;
370
371 // Is the scrollbar draw vertically?
372 bool m_verticalScroll = true;
373
374 // Does the image lie vertically?
375 bool m_verticalImage = true;
376
377 // How far should the value change when pressing one of the arrows?
378 unsigned int m_scrollAmount = 1;
379
380 // When no scrollbar is needed, should the scrollbar be drawn or stay hidden?
381 bool m_autoHide = true;
382
383 // Did the mouse went down on one of the arrows?
384 bool m_mouseDownOnArrow = false;
385
386 bool m_sizeSet = false; // Has setSize been called?
387
388 FloatRect m_track;
389 FloatRect m_thumb;
390 FloatRect m_arrowUp;
391 FloatRect m_arrowDown;
392
393 Sprite m_spriteTrack;
394 Sprite m_spriteTrackHover;
395 Sprite m_spriteThumb;
396 Sprite m_spriteThumbHover;
397 Sprite m_spriteArrowUp;
398 Sprite m_spriteArrowUpHover;
399 Sprite m_spriteArrowDown;
400 Sprite m_spriteArrowDownHover;
401
402 // Cached renderer properties
403 Color m_thumbColorCached;
404 Color m_thumbColorHoverCached;
405 Color m_trackColorCached;
406 Color m_trackColorHoverCached;
407 Color m_arrowColorCached;
408 Color m_arrowColorHoverCached;
409 Color m_arrowBackgroundColorCached;
410 Color m_arrowBackgroundColorHoverCached;
411
413 };
414
415
420 class TGUI_API ScrollbarChildWidget : public Scrollbar
421 {
422 public:
423
427 bool isMouseDownOnThumb() const;
428
429
436 bool isShown() const;
437
438
445 void draw(BackendRenderTarget& target, RenderStates states) const override;
446
447
449 };
450
452}
453
455
456#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:421
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
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:44
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:52
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.
static Scrollbar::Ptr copy(const Scrollbar::ConstPtr &scrollbar)
Makes a copy of another scrollbar.
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.
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.
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
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39