TGUI  0.8.4
ChatBox.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_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 
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 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 
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);
245 
246 
251  unsigned int getTextSize() const;
252 
253 
258  void setTextColor(Color color);
259 
260 
265  const Color& getTextColor() const;
266 
267 
272  void setTextStyle(TextStyle style);
273 
274 
279  TextStyle getTextStyle() const;
280 
281 
293  void setLinesStartFromTop(bool startFromTop = true);
294 
295 
305  bool getLinesStartFromTop() const;
306 
307 
316  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
317 
318 
325  bool getNewLinesBelowOthers() const;
326 
327 
334  bool mouseOnWidget(Vector2f pos) const override;
335 
339  void leftMousePressed(Vector2f pos) override;
340 
344  void leftMouseReleased(Vector2f pos) override;
345 
349  void mouseMoved(Vector2f pos) override;
350 
354  bool mouseWheelScrolled(float delta, Vector2f pos) override;
355 
359  void mouseNoLongerOnWidget() override;
360 
364  void mouseNoLongerDown() override;
365 
366 
368  private:
369 
371  // Recalculates the text attribute of the line
373  void recalculateLineText(Line& line);
374 
375 
377  // Recalculates all text attributes, recalculate the full text height and update the displayed text
379  void recalculateAllLines();
380 
381 
383  // Recalculates the space used by all the lines
385  void recalculateFullTextHeight();
386 
387 
389  // Updates the position of the lines
391  void updateDisplayedText();
392 
393 
395  // Updates the position and size of the panel and scrollbar
397  void updateRendering();
398 
399 
407  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
408 
409 
411  protected:
412 
419  void rendererChanged(const std::string& property) override;
420 
421 
425  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
426 
427 
431  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
432 
433 
435  // Returns the size without the borders
437  Vector2f getInnerSize() const;
438 
439 
441  // Makes a copy of the widget
443  Widget::Ptr clone() const override
444  {
445  return std::make_shared<ChatBox>(*this);
446  }
447 
448 
450  protected:
451 
452  unsigned int m_textSize = 0;
453  Color m_textColor = Color::Black;
454  TextStyle m_textStyle;
455 
456  std::size_t m_maxLines = 0;
457 
458  float m_fullTextHeight = 0;
459 
460  bool m_linesStartFromTop = false;
461  bool m_newLinesBelowOthers = true;
462 
464 
465  std::deque<Line> m_lines;
466 
467  Sprite m_spriteBackground;
468 
469  // Cached renderer properties
470  Borders m_bordersCached;
471  Padding m_paddingCached;
472  Color m_backgroundColorCached;
473  Color m_borderColorCached;
474 
476  };
477 
479 }
480 
482 
483 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:44
Wrapper for colors.
Definition: Color.hpp:47
Wrapper for text styles.
Definition: TextStyle.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:247
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:47
Definition: Vector2f.hpp:37
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:46
Definition: ChatBox.hpp:52
static const Color Black
Black predefined color.
Definition: Color.hpp:195
Definition: Text.hpp:42
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChatBox.hpp:443
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Definition: ChatBoxRenderer.hpp:36
Definition: CopiedSharedPtr.hpp:39
Definition: Outline.hpp:38
Definition: ChatBox.hpp:42