TGUI  0.8.8
ProgressBar.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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_PROGRESS_BAR_HPP
27 #define TGUI_PROGRESS_BAR_HPP
28 
29 
30 #include <TGUI/Renderers/ProgressBarRenderer.hpp>
31 #include <TGUI/Widgets/ClickableWidget.hpp>
32 #include <TGUI/Text.hpp>
33 
35 
36 namespace tgui
37 {
41  class TGUI_API ProgressBar : public ClickableWidget
42  {
43  public:
44 
45  typedef std::shared_ptr<ProgressBar> Ptr;
46  typedef std::shared_ptr<const ProgressBar> ConstPtr;
47 
48 
55  enum class FillDirection
56  {
57  LeftToRight,
58  RightToLeft,
59  TopToBottom,
60  BottomToTop
61  };
62 
63 
65  // Default constructor
67  ProgressBar();
68 
69 
77 
78 
88 
89 
95  const ProgressBarRenderer* getSharedRenderer() const;
96 
103  const ProgressBarRenderer* getRenderer() const;
104 
105 
112  void setSize(const Layout2d& size) override;
113  using Widget::setSize;
114 
115 
124  void setMinimum(unsigned int minimum);
125 
126 
133  unsigned int getMinimum() const;
134 
135 
144  void setMaximum(unsigned int maximum);
145 
146 
153  unsigned int getMaximum() const;
154 
155 
164  void setValue(unsigned int value);
165 
166 
173  unsigned int getValue() const;
174 
175 
184  unsigned int incrementValue();
185 
186 
195  void setText(const sf::String& text);
196 
197 
204  const sf::String& getText() const;
205 
206 
214  void setTextSize(unsigned int size) override;
215 
216 
223  unsigned int getTextSize() const override;
224 
225 
235 
236 
246 
247 
252  bool mouseOnWidget(Vector2f pos) const override;
253 
254 
262  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
263 
264 
266  protected:
267 
277  Signal& getSignal(std::string signalName) override;
278 
279 
286  void rendererChanged(const std::string& property) override;
287 
288 
292  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
293 
294 
298  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
299 
300 
302  // Returns the size without the borders
304  Vector2f getInnerSize() const;
305 
306 
308  // When the value changes, or when the minimum/maximum limits change then a smaller of bigger piece of the front image
309  // must be drawn. This function is called to calculate the size of the piece to draw.
311  void recalculateFillSize();
312 
313 
315  // Returns the size of the front image.
317  Vector2f getFrontImageSize() const;
318 
319 
321  // Makes a copy of the widget
323  Widget::Ptr clone() const override
324  {
325  return std::make_shared<ProgressBar>(*this);
326  }
327 
328 
330  public:
331 
332  SignalUInt onValueChange = {"ValueChanged"};
333  Signal onFull = {"Full"};
334 
335 
337  protected:
338 
339  unsigned int m_minimum = 0;
340  unsigned int m_maximum = 100;
341  unsigned int m_value = 0;
342 
343  Text m_textBack;
344  Text m_textFront;
345 
346  FloatRect m_backRect;
347  FloatRect m_frontRect;
348 
349  FillDirection m_fillDirection = FillDirection::LeftToRight;
350 
351  Sprite m_spriteBackground;
352  Sprite m_spriteFill;
353 
354  // Cached renderer properties
355  Borders m_bordersCached;
356  Color m_borderColorCached;
357  Color m_backgroundColorCached;
358  Color m_fillColorCached;
359  };
360 
361 
363 }
364 
366 
367 #endif // TGUI_PROGRESS_BAR_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
static ProgressBar::Ptr copy(ProgressBar::ConstPtr progressBar)
Makes a copy of another progress bar.
std::shared_ptr< const ProgressBar > ConstPtr
Shared constant widget pointer.
Definition: ProgressBar.hpp:46
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ProgressBar.hpp:323
static ProgressBar::Ptr create()
Creates a new progress bar widget.
Definition: ProgressBarRenderer.hpp:37
FillDirection
The fill direction of the progress bar.
Definition: ProgressBar.hpp:56
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
unsigned int getValue() const
Returns the current value.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
unsigned int incrementValue()
Increments the value.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void setTextSize(unsigned int size) override
Changes the character size of the text.
Definition: Text.hpp:43
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
unsigned int getMinimum() const
Returns the minimum value.
unsigned int getTextSize() const override
Returns the character size of the text.
void setSize(const Layout2d &size) override
Changes the size of the progress bar.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
const sf::String & getText() const
Returns the caption of the progress bar.
Wrapper for colors.
Definition: Color.hpp:49
Progress bar widget.
Definition: ProgressBar.hpp:42
std::shared_ptr< ProgressBar > Ptr
Shared widget pointer.
Definition: ProgressBar.hpp:45
void setMaximum(unsigned int maximum)
Sets a maximum value.
void setMinimum(unsigned int minimum)
Sets a minimum value.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
Definition: Sprite.hpp:46
Definition: FloatRect.hpp:37
ProgressBarRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int getMaximum() const
Returns the maximum value.
Definition: Vector2f.hpp:39
Definition: Outline.hpp:39
FillDirection getFillDirection() const
Returns the fill direction of the progress bar.
void setFillDirection(FillDirection direction)
Changes the fill direction of the progress bar.
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.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
void setText(const sf::String &text)
Changes the caption of the progress bar.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
ProgressBarRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Clickable widget.
Definition: ClickableWidget.hpp:40
void setValue(unsigned int value)
Changes the current value.