TGUI  0.9.1
ChatBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
69  ChatBox(const char* typeName = "ChatBox", bool initRenderer = true);
70 
71 
78  static ChatBox::Ptr create();
79 
80 
90 
91 
97  const ChatBoxRenderer* getSharedRenderer() const;
98 
105  const ChatBoxRenderer* getRenderer() const;
106 
107 
116  void setSize(const Layout2d& size) override;
117  using Widget::setSize;
118 
119 
130  void addLine(const String& text);
131 
132 
144  void addLine(const String& text, Color color);
145 
146 
157  void addLine(const String& text, Color color, TextStyles style);
158 
159 
170  String getLine(std::size_t lineIndex) const;
171 
172 
181  Color getLineColor(std::size_t lineIndex) const;
182 
183 
189  TextStyles getLineTextStyle(std::size_t lineIndex) const;
190 
191 
201  bool removeLine(std::size_t lineIndex);
202 
203 
209 
210 
217  std::size_t getLineAmount();
218 
219 
229  void setLineLimit(std::size_t maxLines);
230 
231 
241  std::size_t getLineLimit();
242 
243 
248  void setTextSize(unsigned int size) override;
249 
250 
255  void setTextColor(Color color);
256 
257 
262  const Color& getTextColor() const;
263 
264 
270 
271 
277 
278 
290  void setLinesStartFromTop(bool startFromTop = true);
291 
292 
302  bool getLinesStartFromTop() const;
303 
304 
313  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
314 
315 
323 
324 
330  void setScrollbarValue(unsigned int value);
331 
332 
338  unsigned int getScrollbarValue() const;
339 
340 
347  bool isMouseOnWidget(Vector2f pos) const override;
348 
352  void leftMousePressed(Vector2f pos) override;
353 
357  void leftMouseReleased(Vector2f pos) override;
358 
362  void mouseMoved(Vector2f pos) override;
363 
367  bool mouseWheelScrolled(float delta, Vector2f pos) override;
368 
372  void mouseNoLongerOnWidget() override;
373 
377  void leftMouseButtonNoLongerDown() override;
378 
379 
381  private:
382 
384  // Recalculates the text attribute of the line
386  void recalculateLineText(Line& line);
387 
388 
390  // Recalculates all text attributes, recalculate the full text height and update the displayed text
392  void recalculateAllLines();
393 
394 
396  // Recalculates the space used by all the lines
398  void recalculateFullTextHeight();
399 
400 
402  // Updates the position of the lines
404  void updateDisplayedText();
405 
406 
408  // Updates the position and size of the panel and scrollbar
410  void updateRendering();
411 
412 
420  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
421 
422 
424  protected:
425 
432  void rendererChanged(const String& property) override;
433 
434 
438  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
439 
440 
444  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
445 
446 
448  // Returns the size without the borders
450  Vector2f getInnerSize() const;
451 
452 
454  // Makes a copy of the widget
456  Widget::Ptr clone() const override
457  {
458  return std::make_shared<ChatBox>(*this);
459  }
460 
461 
463  protected:
464 
465  Color m_textColor = Color::Black;
466  TextStyles m_textStyle;
467 
468  std::size_t m_maxLines = 0;
469 
470  float m_fullTextHeight = 0;
471 
472  bool m_linesStartFromTop = false;
473  bool m_newLinesBelowOthers = true;
474 
476 
477  std::deque<Line> m_lines;
478 
479  Sprite m_spriteBackground;
480 
481  // Cached renderer properties
482  Borders m_bordersCached;
483  Padding m_paddingCached;
484  Color m_backgroundColorCached;
485  Color m_borderColorCached;
486 
488  };
489 
491 }
492 
494 
495 #endif // TGUI_TEXT_BOX_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Definition: ChatBoxRenderer.hpp:37
Definition: ChatBox.hpp:43
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
std::size_t getLineAmount()
Returns the amount of lines in the chat box.
void setTextSize(unsigned int size) override
Changes the character size of the text.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox)
Makes a copy of another chat box.
void removeAllLines()
Removes all lines from the chat box.
bool getLinesStartFromTop() const
Returns whether the first lines start from the top or from the bottom of the chat box.
static ChatBox::Ptr create()
Creates a new chat box widget.
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:47
void setLinesStartFromTop(bool startFromTop=true)
Lets the first lines start from the top or from the bottom of the chat box.
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.
void addLine(const String &text, Color color)
Adds a new line of text to the chat box.
std::size_t getLineLimit()
Returns the maximum amount of lines in the chat box.
void addLine(const String &text, Color color, TextStyles style)
Adds a new line of text to the chat box.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void addLine(const String &text)
Adds a new line of text to the chat box.
bool getNewLinesBelowOthers() const
Returns whether new lines are added below the other lines or above them.
const Color & getTextColor() const
Returns the default color of the text.
Color getLineColor(std::size_t lineIndex) const
Returns the color of the requested line.
void setSize(const Layout2d &size) override
Changes the size of the chat box.
void setNewLinesBelowOthers(bool newLinesBelowOthers=true)
Sets whether new lines are added below the other lines or above them.
TextStyles getTextStyle() const
Returns the default text style.
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:46
TextStyles getLineTextStyle(std::size_t lineIndex) const
Returns the text style of the requested line.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
void setTextColor(Color color)
Changes the default color of the text.
void setTextStyle(TextStyles style)
Changes the default text style.
ChatBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
String getLine(std::size_t lineIndex) const
Returns the contents of the requested line.
void setLineLimit(std::size_t maxLines)
Sets a maximum amount of lines in the chat box.
bool removeLine(std::size_t lineIndex)
Removes the requested line.
ChatBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChatBox.hpp:456
Wrapper for colors.
Definition: Color.hpp:52
static const Color Black
Black predefined color.
Definition: Color.hpp:236
Definition: CopiedSharedPtr.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Definition: Outline.hpp:39
Definition: Sprite.hpp:49
Wrapper class to store strings.
Definition: String.hpp:74
Wrapper for text styles.
Definition: TextStyle.hpp:58
Definition: Text.hpp:44
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: ChatBox.hpp:53
States used for drawing.
Definition: RenderStates.hpp:39