TGUI  0.8.4
Label.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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_LABEL_HPP
27 #define TGUI_LABEL_HPP
28 
29 
30 #include <TGUI/Widgets/ClickableWidget.hpp>
31 #include <TGUI/Renderers/LabelRenderer.hpp>
32 #include <TGUI/CopiedSharedPtr.hpp>
33 #include <TGUI/Widgets/Scrollbar.hpp>
34 #include <TGUI/Text.hpp>
35 
37 
38 namespace tgui
39 {
43  class TGUI_API Label : public ClickableWidget
44  {
45  public:
46 
47  typedef std::shared_ptr<Label> Ptr;
48  typedef std::shared_ptr<const Label> ConstPtr;
49 
50 #ifndef TGUI_REMOVE_DEPRECATED_CODE
51  using ScrollbarPolicy TGUI_DEPRECATED("Use tgui::Scrollbar::Policy instead") = Scrollbar::Policy;
53 #endif
54 
59  {
60  Left,
61  Center,
62  Right
63  };
64 
65 
69  enum class VerticalAlignment
70  {
71  Top ,
72  Center,
73  Bottom
74  };
75 
76 
78  // Default constructor
80  Label();
81 
82 
91  static Label::Ptr create(sf::String text = "");
92 
93 
102  static Label::Ptr copy(Label::ConstPtr label);
103 
104 
109  LabelRenderer* getSharedRenderer();
110  const LabelRenderer* getSharedRenderer() const;
111 
117  LabelRenderer* getRenderer();
118  const LabelRenderer* getRenderer() const;
119 
120 
136  void setSize(const Layout2d& size) override;
137  using Widget::setSize;
138 
139 
150  void setText(const sf::String& text);
151 
152 
159  const sf::String& getText() const;
160 
161 
168  void setTextSize(unsigned int size);
169 
170 
177  unsigned int getTextSize() const;
178 
179 
188  void setHorizontalAlignment(HorizontalAlignment alignment);
189 
190 
197  HorizontalAlignment getHorizontalAlignment() const;
198 
199 
208  void setVerticalAlignment(VerticalAlignment alignment);
209 
210 
217  VerticalAlignment getVerticalAlignment() const;
218 
219 
224  void setScrollbarPolicy(Scrollbar::Policy policy);
225 
226 
231  Scrollbar::Policy getScrollbarPolicy() const;
232 
233 
245  void setAutoSize(bool autoSize);
246 
247 
254  bool getAutoSize() const;
255 
256 
269  void setMaximumTextWidth(float maximumWidth);
270 
271 
281  float getMaximumTextWidth() const;
282 
283 
291  void ignoreMouseEvents(bool ignore = true);
292 
293 
299  bool isIgnoringMouseEvents() const;
300 
301 
307  void setParent(Container* parent) override;
308 
309 
316  bool canGainFocus() const override;
317 
318 
320  void leftMousePressed(Vector2f pos) override;
321 
323  void leftMouseReleased(Vector2f pos) override;
324 
326  void mouseMoved(Vector2f pos) override;
327 
329  bool mouseWheelScrolled(float delta, Vector2f pos) override;
330 
332  void mouseNoLongerOnWidget() override;
333 
335  void mouseNoLongerDown() override;
336 
337 
345  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
346 
347 
349  protected:
350 
360  Signal& getSignal(std::string signalName) override;
361 
362 
369  void rendererChanged(const std::string& property) override;
370 
371 
375  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
376 
377 
381  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
382 
383 
385  // This function is called every frame with the time passed since the last frame.
387  void update(sf::Time elapsedTime) override;
388 
389 
391  // Rearrange the text, making use of the given size of maximum text width.
393  void rearrangeText();
394 
395 
397  // Makes a copy of the widget
399  Widget::Ptr clone() const override
400  {
401  return std::make_shared<Label>(*this);
402  }
403 
404 
406  public:
407 
408  SignalString onDoubleClick = {"DoubleClicked"};
409 
410 
412  protected:
413 
414  sf::String m_string;
415  std::vector<Text> m_lines;
416 
417  unsigned int m_textSize = 0;
418  HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
419  VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
420 
421  bool m_autoSize = true;
422 
423  float m_maximumTextWidth = 0;
424 
425  bool m_ignoringMouseEvents = false;
426 
427  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
428  bool m_possibleDoubleClick = false;
429 
431 #ifdef TGUI_NEXT
433 #else
434  Scrollbar::Policy m_scrollbarPolicy = Scrollbar::Policy::Never;
435 #endif
436 
437  // Cached renderer properties
438  Borders m_bordersCached;
439  Padding m_paddingCached;
440  TextStyle m_textStyleCached;
441  Color m_textColorCached;
442  Color m_borderColorCached;
443  Color m_backgroundColorCached;
444 
446  };
447 
449 }
450 
452 
453 #endif // TGUI_LABEL_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Show the scrollbar only when needed (default)
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Label.hpp:399
Wrapper for colors.
Definition: Color.hpp:47
Wrapper for text styles.
Definition: TextStyle.hpp:46
Never show the scrollbar, even if the contents does not fit.
Class to store the position or size of a widget.
Definition: Layout.hpp:247
Label widget.
Definition: Label.hpp:43
Definition: Vector2f.hpp:37
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
Clickable widget.
Definition: ClickableWidget.hpp:39
Definition: LabelRenderer.hpp:36
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Container widget.
Definition: Container.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:69
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:58
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:48