TGUI  0.9-dev
ChatBox.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_CHAT_BOX_HPP
27 #define TGUI_CHAT_BOX_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Widgets/Scrollbar.hpp>
32 #include <TGUI/Renderers/ChatBoxRenderer.hpp>
33 #include <TGUI/Text.hpp>
34 #include <deque>
35 
37 
38 namespace tgui
39 {
41 
42  class TGUI_API ChatBox : public Widget
43  {
44  public:
45 
46  typedef std::shared_ptr<ChatBox> Ptr;
47  typedef std::shared_ptr<const ChatBox> ConstPtr;
48 
50  protected:
51 
52  struct Line
53  {
54  Text text;
55  String string;
56  };
57 
58 
60  public:
61 
63  // Default constructor
65  ChatBox();
66 
67 
74  static ChatBox::Ptr create();
75 
76 
85  static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox);
86 
87 
92  ChatBoxRenderer* getSharedRenderer();
93  const ChatBoxRenderer* getSharedRenderer() const;
94 
100  ChatBoxRenderer* getRenderer();
101  const ChatBoxRenderer* getRenderer() const;
102 
103 
112  void setSize(const Layout2d& size) override;
113  using Widget::setSize;
114 
115 
126  void addLine(const String& text);
127 
128 
140  void addLine(const String& text, Color color);
141 
142 
153  void addLine(const String& text, Color color, TextStyle style);
154 
155 
166  String getLine(std::size_t lineIndex) const;
167 
168 
177  Color getLineColor(std::size_t lineIndex) const;
178 
179 
185  TextStyle getLineTextStyle(std::size_t lineIndex) const;
186 
187 
197  bool removeLine(std::size_t lineIndex);
198 
199 
204  void removeAllLines();
205 
206 
213  std::size_t getLineAmount();
214 
215 
225  void setLineLimit(std::size_t maxLines);
226 
227 
237  std::size_t getLineLimit();
238 
239 
244  void setTextSize(unsigned int size) override;
245 
246 
251  void setTextColor(Color color);
252 
253 
258  const Color& getTextColor() const;
259 
260 
265  void setTextStyle(TextStyle style);
266 
267 
272  TextStyle getTextStyle() const;
273 
274 
286  void setLinesStartFromTop(bool startFromTop = true);
287 
288 
298  bool getLinesStartFromTop() const;
299 
300 
309  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
310 
311 
318  bool getNewLinesBelowOthers() const;
319 
320 
326  void setScrollbarValue(unsigned int value);
327 
328 
334  unsigned int getScrollbarValue() const;
335 
336 
343  bool mouseOnWidget(Vector2f pos) const override;
344 
348  void leftMousePressed(Vector2f pos) override;
349 
353  void leftMouseReleased(Vector2f pos) override;
354 
358  void mouseMoved(Vector2f pos) override;
359 
363  bool mouseWheelScrolled(float delta, Vector2f pos) override;
364 
368  void mouseNoLongerOnWidget() override;
369 
373  void leftMouseButtonNoLongerDown() override;
374 
375 
377  private:
378 
380  // Recalculates the text attribute of the line
382  void recalculateLineText(Line& line);
383 
384 
386  // Recalculates all text attributes, recalculate the full text height and update the displayed text
388  void recalculateAllLines();
389 
390 
392  // Recalculates the space used by all the lines
394  void recalculateFullTextHeight();
395 
396 
398  // Updates the position of the lines
400  void updateDisplayedText();
401 
402 
404  // Updates the position and size of the panel and scrollbar
406  void updateRendering();
407 
408 
416  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
417 
418 
420  protected:
421 
428  void rendererChanged(const String& property) override;
429 
430 
434  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
435 
436 
440  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
441 
442 
444  // Returns the size without the borders
446  Vector2f getInnerSize() const;
447 
448 
450  // Makes a copy of the widget
452  Widget::Ptr clone() const override
453  {
454  return std::make_shared<ChatBox>(*this);
455  }
456 
457 
459  protected:
460 
461  Color m_textColor = Color::Black;
462  TextStyle m_textStyle;
463 
464  std::size_t m_maxLines = 0;
465 
466  float m_fullTextHeight = 0;
467 
468  bool m_linesStartFromTop = false;
469  bool m_newLinesBelowOthers = true;
470 
472 
473  std::deque<Line> m_lines;
474 
475  Sprite m_spriteBackground;
476 
477  // Cached renderer properties
478  Borders m_bordersCached;
479  Padding m_paddingCached;
480  Color m_backgroundColorCached;
481  Color m_borderColorCached;
482 
484  };
485 
487 }
488 
490 
491 #endif // TGUI_TEXT_BOX_HPP
Definition: ChatBoxRenderer.hpp:36
Definition: CopiedSharedPtr.hpp:39
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:46
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChatBox.hpp:452
Definition: Text.hpp:42
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Wrapper for colors.
Definition: Color.hpp:47
Definition: ChatBox.hpp:52
Definition: Sprite.hpp:45
Wrapper class to store strings.
Definition: String.hpp:70
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:47
Definition: Outline.hpp:38
The parent class for every widget.
Definition: Widget.hpp:68
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: ChatBox.hpp:42
static const Color Black
Black predefined color.
Definition: Color.hpp:221
Wrapper for text styles.
Definition: TextStyle.hpp:44