TGUI  0.8-dev
ChatBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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/Widgets/Scrollbar.hpp>
31 #include <TGUI/Renderers/ChatBoxRenderer.hpp>
32 #include <TGUI/Text.hpp>
33 #include <deque>
34 
36 
37 namespace tgui
38 {
40 
41  class TGUI_API ChatBox : public Widget
42  {
43  public:
44 
45  typedef std::shared_ptr<ChatBox> Ptr;
46  typedef std::shared_ptr<const ChatBox> ConstPtr;
47 
49  protected:
50 
51  struct Line
52  {
53  Text text;
54  sf::String string;
55  };
56 
57 
59  public:
60 
62  // Default constructor
64  ChatBox();
65 
66 
73  static ChatBox::Ptr create();
74 
75 
84  static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox);
85 
86 
91  ChatBoxRenderer* getSharedRenderer();
92  const ChatBoxRenderer* getSharedRenderer() const;
93 
99  ChatBoxRenderer* getRenderer();
100  const ChatBoxRenderer* getRenderer() const;
101 
102 
109  void setPosition(const Layout2d& position) override;
110  using Widget::setPosition;
111 
112 
121  void setSize(const Layout2d& size) override;
122  using Widget::setSize;
123 
124 
136  void addLine(const sf::String& text);
137 
138 
151  void addLine(const sf::String& text, unsigned int textSize);
152 
153 
166  void addLine(const sf::String& text, const sf::Color& color);
167 
168 
181  void addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, const Font& font = nullptr);
182 
183 
194  sf::String getLine(std::size_t lineIndex) const;
195 
196 
205  sf::Color getLineColor(std::size_t lineIndex) const;
206 
207 
216  unsigned int getLineTextSize(std::size_t lineIndex) const;
217 
218 
228  std::shared_ptr<sf::Font> getLineFont(std::size_t lineIndex) const;
229 
230 
240  bool removeLine(std::size_t lineIndex);
241 
242 
247  void removeAllLines();
248 
249 
256  std::size_t getLineAmount();
257 
258 
268  void setLineLimit(std::size_t maxLines);
269 
270 
280  std::size_t getLineLimit();
281 
282 
290  void setTextSize(unsigned int size);
291 
292 
299  unsigned int getTextSize() const;
300 
301 
308  void setTextColor(Color color);
309 
310 
317  const sf::Color& getTextColor() const;
318 
319 
331  void setLinesStartFromTop(bool startFromTop = true);
332 
333 
343  bool getLinesStartFromTop() const;
344 
345 
354  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
355 
356 
363  bool getNewLinesBelowOthers() const;
364 
365 
372  bool mouseOnWidget(sf::Vector2f pos) const override;
373 
377  void leftMousePressed(sf::Vector2f pos) override;
378 
382  void leftMouseReleased(sf::Vector2f pos) override;
383 
387  void mouseMoved(sf::Vector2f pos) override;
388 
392  void mouseWheelScrolled(float delta, sf::Vector2f pos) override;
393 
397  void mouseNoLongerOnWidget() override;
398 
402  void mouseNoLongerDown() override;
403 
404 
406  private:
407 
409  // Recalculates the text attribute of the line
411  void recalculateLineText(Line& line);
412 
413 
415  // Recalculates all text attributes, recalculate the full text height and update the displayed text
417  void recalculateAllLines();
418 
419 
421  // Recalculates the space used by all the lines
423  void recalculateFullTextHeight();
424 
425 
427  // Updates the position of the lines
429  void updateDisplayedText();
430 
431 
433  // Updates the position and size of the panel and scrollbar
435  void updateRendering();
436 
437 
445  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
446 
447 
449  protected:
450 
457  void rendererChanged(const std::string& property) override;
458 
459 
461  // Returns the size without the borders
463  sf::Vector2f getInnerSize() const;
464 
465 
467  // Makes a copy of the widget
469  Widget::Ptr clone() const override
470  {
471  return std::make_shared<ChatBox>(*this);
472  }
473 
474 
476  protected:
477 
478  unsigned int m_textSize = 18;
479  sf::Color m_textColor = sf::Color::Black;
480 
481  std::size_t m_maxLines = 0;
482 
483  float m_fullTextHeight = 0;
484 
485  bool m_linesStartFromTop = false;
486  bool m_newLinesBelowOthers = true;
487 
488  ScrollbarChildWidget m_scroll;
489 
490  std::deque<Line> m_lines;
491 
492  Sprite m_spriteBackground;
493 
494  // Cached renderer properties
495  Borders m_bordersCached;
496  Padding m_paddingCached;
497  Color m_backgroundColorCached;
498  Color m_borderColorCached;
499 
501  };
502 
504 }
505 
507 
508 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Definition: Font.hpp:41
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:62
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:46
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:45
Definition: ChatBox.hpp:51
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:372
Definition: Text.hpp:41
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&#39;t know its exact type.
Definition: ChatBox.hpp:469
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
Definition: ChatBoxRenderer.hpp:36
Definition: Outline.hpp:38
Definition: ChatBox.hpp:41