TGUI  0.8.7
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) override;
169 
170 
179  void setHorizontalAlignment(HorizontalAlignment alignment);
180 
181 
188  HorizontalAlignment getHorizontalAlignment() const;
189 
190 
199  void setVerticalAlignment(VerticalAlignment alignment);
200 
201 
208  VerticalAlignment getVerticalAlignment() const;
209 
210 
215  void setScrollbarPolicy(Scrollbar::Policy policy);
216 
217 
222  Scrollbar::Policy getScrollbarPolicy() const;
223 
224 
236  void setAutoSize(bool autoSize);
237 
238 
245  bool getAutoSize() const;
246 
247 
260  void setMaximumTextWidth(float maximumWidth);
261 
262 
272  float getMaximumTextWidth() const;
273 
274 
282  void ignoreMouseEvents(bool ignore = true);
283 
284 
290  bool isIgnoringMouseEvents() const;
291 
292 
298  void setParent(Container* parent) override;
299 
300 
307  bool canGainFocus() const override;
308 
309 
314  bool mouseOnWidget(Vector2f pos) const override;
315 
316 
318  void leftMousePressed(Vector2f pos) override;
319 
321  void leftMouseReleased(Vector2f pos) override;
322 
324  void mouseMoved(Vector2f pos) override;
325 
327  bool mouseWheelScrolled(float delta, Vector2f pos) override;
328 
330  void mouseNoLongerOnWidget() override;
331 
333  void leftMouseButtonNoLongerDown() override;
334 
335 
343  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
344 
345 
347  protected:
348 
358  Signal& getSignal(std::string signalName) override;
359 
360 
367  void rendererChanged(const std::string& property) override;
368 
369 
373  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
374 
375 
379  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
380 
381 
383  // This function is called every frame with the time passed since the last frame.
385  void update(sf::Time elapsedTime) override;
386 
387 
389  // Rearrange the text, making use of the given size of maximum text width.
391  void rearrangeText();
392 
393 
395  // Makes a copy of the widget
397  Widget::Ptr clone() const override
398  {
399  return std::make_shared<Label>(*this);
400  }
401 
402 
404  public:
405 
406  SignalString onDoubleClick = {"DoubleClicked"};
407 
408 
410  protected:
411 
412  sf::String m_string;
413  std::vector<Text> m_lines;
414 
415  HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
416  VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
417 
418  bool m_autoSize = true;
419 
420  float m_maximumTextWidth = 0;
421 
422  bool m_ignoringMouseEvents = false;
423 
424  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
425  bool m_possibleDoubleClick = false;
426 
428 #ifdef TGUI_NEXT
430 #else
431  Scrollbar::Policy m_scrollbarPolicy = Scrollbar::Policy::Never;
432 #endif
433 
434  Sprite m_spriteBackground;
435 
436  // Cached renderer properties
437  Borders m_bordersCached;
438  Padding m_paddingCached;
439  TextStyle m_textStyleCached;
440  Color m_textColorCached;
441  Color m_borderColorCached;
442  Color m_backgroundColorCached;
443  Color m_textOutlineColorCached;
444  float m_textOutlineThicknessCached = 0;
445 
447  };
448 
450 }
451 
453 
454 #endif // TGUI_LABEL_HPP
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Label.hpp:397
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
std::shared_ptr< const Label > ConstPtr
Shared constant widget pointer.
Definition: Label.hpp:48
Container widget.
Definition: Container.hpp:47
Label widget.
Definition: Label.hpp:43
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
@ Automatic
Show the scrollbar only when needed (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:48
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:58
Definition: Sprite.hpp:45
Definition: LabelRenderer.hpp:36
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
Definition: Vector2f.hpp:38
Definition: Outline.hpp:38
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:69
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Clickable widget.
Definition: ClickableWidget.hpp:39
Wrapper for text styles.
Definition: TextStyle.hpp:46
@ Never
Never show the scrollbar, even if the contents does not fit.