TGUI  0.8.8
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  sf::String string;
56  };
57 
58 
60  public:
61 
63  // Default constructor
65  ChatBox();
66 
67 
74  static ChatBox::Ptr create();
75 
76 
86 
87 
93  const ChatBoxRenderer* getSharedRenderer() const;
94 
101  const ChatBoxRenderer* getRenderer() const;
102 
103 
112  void setSize(const Layout2d& size) override;
113  using Widget::setSize;
114 
115 
126  void addLine(const sf::String& text);
127 
128 
140  void addLine(const sf::String& text, Color color);
141 
142 
153  void addLine(const sf::String& text, Color color, TextStyle style);
154 
155 
166  sf::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 
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 
273 
274 
286  void setLinesStartFromTop(bool startFromTop = true);
287 
288 
298  bool getLinesStartFromTop() const;
299 
300 
309  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
310 
311 
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 std::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
void addLine(const sf::String &text, Color color, TextStyle style)
Adds a new line of text to the chat box.
Definition: ChatBoxRenderer.hpp:37
Definition: CopiedSharedPtr.hpp:40
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:46
static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox)
Makes a copy of another chat box.
bool getLinesStartFromTop() const
Returns whether the first lines start from the top or from the bottom of the chat box.
std::size_t getLineAmount()
Returns the amount of lines in the chat box.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setLinesStartFromTop(bool startFromTop=true)
Lets the first lines start from the top or from the bottom of the chat box.
void setTextSize(unsigned int size) override
Changes the character size of the text.
ChatBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setTextStyle(TextStyle style)
Changes the default text style.
bool removeLine(std::size_t lineIndex)
Removes the requested line.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
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:43
static ChatBox::Ptr create()
Creates a new chat box widget.
std::size_t getLineLimit()
Returns the maximum amount of lines in the chat box.
void setSize(const Layout2d &size) override
Changes the size of the chat box.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
const Color & getTextColor() const
Returns the default color of the text.
Wrapper for colors.
Definition: Color.hpp:49
void setLineLimit(std::size_t maxLines)
Sets a maximum amount of lines in the chat box.
Definition: ChatBox.hpp:53
ChatBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TextStyle getTextStyle() const
Returns the default text style.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
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.
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
void addLine(const sf::String &text, Color color)
Adds a new line of text to the chat box.
Definition: Sprite.hpp:46
void removeAllLines()
Removes all lines from the chat box.
sf::String getLine(std::size_t lineIndex) const
Returns the contents of the requested line.
Definition: Vector2f.hpp:39
TextStyle getLineTextStyle(std::size_t lineIndex) const
Returns the text style of the requested line.
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:47
void setNewLinesBelowOthers(bool newLinesBelowOthers=true)
Sets whether new lines are added below the other lines or above them.
Definition: Outline.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Definition: ChatBox.hpp:43
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
void setTextColor(Color color)
Changes the default color of the text.
Color getLineColor(std::size_t lineIndex) const
Returns the color of the requested line.
static const Color Black
Black predefined color.
Definition: Color.hpp:206
bool getNewLinesBelowOthers() const
Returns whether new lines are added below the other lines or above them.
Wrapper for text styles.
Definition: TextStyle.hpp:47
void addLine(const sf::String &text)
Adds a new line of text to the chat box.