TGUI  0.9-dev
Label.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_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 
54  {
55  Left,
56  Center,
57  Right
58  };
59 
60 
64  enum class VerticalAlignment
65  {
66  Top ,
67  Center,
68  Bottom
69  };
70 
71 
73  // Default constructor
75  Label();
76 
77 
86  static Label::Ptr create(String text = "");
87 
88 
97  static Label::Ptr copy(Label::ConstPtr label);
98 
99 
104  LabelRenderer* getSharedRenderer();
105  const LabelRenderer* getSharedRenderer() const;
106 
112  LabelRenderer* getRenderer();
113  const LabelRenderer* getRenderer() const;
114 
115 
131  void setSize(const Layout2d& size) override;
132  using Widget::setSize;
133 
134 
145  void setText(const String& text);
146 
147 
154  const String& getText() const;
155 
156 
163  void setTextSize(unsigned int size) override;
164 
165 
174  void setHorizontalAlignment(HorizontalAlignment alignment);
175 
176 
183  HorizontalAlignment getHorizontalAlignment() const;
184 
185 
194  void setVerticalAlignment(VerticalAlignment alignment);
195 
196 
203  VerticalAlignment getVerticalAlignment() const;
204 
205 
210  void setScrollbarPolicy(Scrollbar::Policy policy);
211 
212 
217  Scrollbar::Policy getScrollbarPolicy() const;
218 
219 
231  void setAutoSize(bool autoSize);
232 
233 
240  bool getAutoSize() const;
241 
242 
255  void setMaximumTextWidth(float maximumWidth);
256 
257 
267  float getMaximumTextWidth() const;
268 
269 
277  void ignoreMouseEvents(bool ignore = true);
278 
279 
285  bool isIgnoringMouseEvents() const;
286 
287 
293  void setParent(Container* parent) override;
294 
295 
302  bool canGainFocus() const override;
303 
304 
309  bool mouseOnWidget(Vector2f pos) const override;
310 
311 
313  void leftMousePressed(Vector2f pos) override;
314 
316  void leftMouseReleased(Vector2f pos) override;
317 
319  void mouseMoved(Vector2f pos) override;
320 
322  bool mouseWheelScrolled(float delta, Vector2f pos) override;
323 
325  void mouseNoLongerOnWidget() override;
326 
328  void leftMouseButtonNoLongerDown() override;
329 
330 
338  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
339 
340 
342  protected:
343 
353  Signal& getSignal(String signalName) override;
354 
355 
362  void rendererChanged(const String& property) override;
363 
364 
368  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
369 
370 
374  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
375 
376 
378  // This function is called every frame with the time passed since the last frame.
380  void update(Duration elapsedTime) override;
381 
382 
384  // Rearrange the text, making use of the given size of maximum text width.
386  void rearrangeText();
387 
388 
390  // Makes a copy of the widget
392  Widget::Ptr clone() const override
393  {
394  return std::make_shared<Label>(*this);
395  }
396 
397 
399  public:
400 
401  SignalString onDoubleClick = {"DoubleClicked"};
402 
403 
405  protected:
406 
407  String m_string;
408  std::vector<Text> m_lines;
409 
410  HorizontalAlignment m_horizontalAlignment = HorizontalAlignment::Left;
411  VerticalAlignment m_verticalAlignment = VerticalAlignment::Top;
412 
413  bool m_autoSize = true;
414 
415  float m_maximumTextWidth = 0;
416 
417  bool m_ignoringMouseEvents = false;
418 
419  // Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
420  bool m_possibleDoubleClick = false;
421 
424 
425  Sprite m_spriteBackground;
426 
427  // Cached renderer properties
428  Borders m_bordersCached;
429  Padding m_paddingCached;
430  TextStyle m_textStyleCached;
431  Color m_textColorCached;
432  Color m_borderColorCached;
433  Color m_backgroundColorCached;
434  Color m_textOutlineColorCached;
435  float m_textOutlineThicknessCached = 0;
436 
438  };
439 
441 }
442 
444 
445 #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:392
Definition: CopiedSharedPtr.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
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:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
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:47
HorizontalAlignment
The horizontal text alignment.
Definition: Label.hpp:53
Wrapper for durations.
Definition: Duration.hpp:48
Definition: Sprite.hpp:45
Definition: LabelRenderer.hpp:36
Wrapper class to store strings.
Definition: String.hpp:70
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:49
Definition: Outline.hpp:38
VerticalAlignment
The vertical text alignment.
Definition: Label.hpp:64
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:44