TGUI  0.7.4
ChatBox.hpp
1 //
3 // TGUI - Texus's 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/Widget.hpp>
31 #include <TGUI/Widgets/Panel.hpp>
32 #include <TGUI/Widgets/Scrollbar.hpp>
33 
34 #include <deque>
35 
37 
38 namespace tgui
39 {
40  class Scrollbar;
41  class ChatBoxRenderer;
42 
44 
45  class TGUI_API ChatBox : public Widget
46  {
47  public:
48 
49  typedef std::shared_ptr<ChatBox> Ptr;
50  typedef std::shared_ptr<const ChatBox> ConstPtr;
51 
52 
54  protected:
55 
56  struct Line
57  {
58  sf::Text text;
59  sf::String string;
60  unsigned int sublines = 1;
61  std::shared_ptr<sf::Font> font;
62  };
63 
64 
66  public:
67 
69  // Default constructor
71  ChatBox();
72 
73 
80  ChatBox(const ChatBox& copy);
81 
82 
91  ChatBox& operator= (const ChatBox& right);
92 
93 
100  static ChatBox::Ptr create();
101 
102 
111  static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox);
112 
113 
120  std::shared_ptr<ChatBoxRenderer> getRenderer() const
121  {
122  return std::static_pointer_cast<ChatBoxRenderer>(m_renderer);
123  }
124 
125 
134  void setSize(const Layout2d& size) override;
136 
137 
146  virtual sf::Vector2f getFullSize() const override;
147 
148 
160  void addLine(const sf::String& text);
161 
162 
175  void addLine(const sf::String& text, unsigned int textSize);
176 
177 
190  void addLine(const sf::String& text, const sf::Color& color);
191 
192 
205  void addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, const Font& font = nullptr);
206 
207 
218  sf::String getLine(std::size_t lineIndex) const;
219 
220 
229  sf::Color getLineColor(std::size_t lineIndex) const;
230 
231 
240  unsigned int getLineTextSize(std::size_t lineIndex) const;
241 
242 
251  std::shared_ptr<sf::Font> getLineFont(std::size_t lineIndex) const;
252 
253 
263  bool removeLine(std::size_t lineIndex);
264 
265 
270  void removeAllLines();
271 
272 
279  std::size_t getLineAmount();
280 
281 
291  void setLineLimit(std::size_t maxLines);
292 
293 
303  std::size_t getLineLimit();
304 
305 
314  virtual void setFont(const Font& font) override;
315 
316 
324  void setTextSize(unsigned int size);
325 
326 
333  unsigned int getTextSize() const
334  {
335  return m_textSize;
336  }
337 
338 
345  void setTextColor(const Color& color);
346 
347 
354  const sf::Color& getTextColor() const
355  {
356  return m_textColor;
357  }
358 
359 
375  void setScrollbar(Scrollbar::Ptr scrollbar);
376 
377 
386  Scrollbar::Ptr getScrollbar() const;
387 
388 
400  void setLinesStartFromTop(bool startFromTop = true);
401 
402 
412  bool getLinesStartFromTop() const;
413 
414 
423  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
424 
425 
432  bool getNewLinesBelowOthers() const;
433 
434 
441  virtual void setOpacity(float opacity) override;
442 
443 
452  virtual sf::Vector2f getWidgetOffset() const override;
453 
454 
460  virtual void setParent(Container* parent) override;
461 
462 
466  virtual bool mouseOnWidget(float x, float y) const override;
467 
471  virtual void leftMousePressed(float x, float y) override;
472 
476  virtual void leftMouseReleased(float x, float y) override;
477 
481  virtual void mouseMoved(float x, float y) override;
482 
486  virtual void mouseWheelMoved(int delta, int x, int y) override;
487 
491  virtual void mouseNoLongerOnWidget() override;
492 
496  virtual void mouseNoLongerDown() override;
497 
498 
500  private:
501 
503  // Recalculates the text attribute of the line
505  void recalculateLineText(Line& line);
506 
507 
509  // Recalculates all text attributes, recalculate the full text height and update the displayed text
511  void recalculateAllLines();
512 
513 
515  // Recalculates the space used by all the lines
517  void recalculateFullTextHeight();
518 
519 
521  // Updates the position of the lines
523  void updateDisplayedText();
524 
525 
527  // Updates the position and size of the panel and scrollbar
529  void updateRendering();
530 
531 
533  protected:
534 
547  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
548 
549 
551  // Makes a copy of the widget
553  virtual Widget::Ptr clone() const override
554  {
555  return std::make_shared<ChatBox>(*this);
556  }
557 
558 
560  // Draws the widget on the render target.
562  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
563 
564 
566  protected:
567 
568  unsigned int m_textSize = 18;
569  sf::Color m_textColor = sf::Color::Black;
570 
571  std::size_t m_maxLines = 0;
572 
573  float m_fullTextHeight = 0;
574 
575  bool m_linesStartFromTop = false;
576  bool m_newLinesBelowOthers = true;
577 
578  Scrollbar::Ptr m_scroll = std::make_shared<Scrollbar>();
579 
580  std::deque<Line> m_lines;
581 
582  friend class ChatBoxRenderer;
583 
585  };
586 
587 
589 
590  class TGUI_API ChatBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
591  {
592  public:
593 
600  ChatBoxRenderer(ChatBox* chatBox) : m_chatBox{chatBox} {}
601 
602 
613  virtual void setProperty(std::string property, const std::string& value) override;
614 
615 
627  virtual void setProperty(std::string property, ObjectConverter&& value) override;
628 
629 
639  virtual ObjectConverter getProperty(std::string property) const override;
640 
641 
648  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
649 
650 
657  void setBorderColor(const Color& borderColor);
658 
659 
668  void setBackgroundColor(const Color& backgroundColor);
669 
670 
680  void setBackgroundTexture(const Texture& texture);
681 
682 
692  void setPadding(const Padding& padding) override;
694 
695 
697  // Draws the widget on the render target.
699  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
700 
701 
703  private:
704 
706  // Returns the padding, which is possibly scaled with the background image.
708  Padding getScaledPadding() const;
709 
710 
712  // Makes a copy of the renderer
714  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
715 
716 
718  protected:
719 
720  ChatBox* m_chatBox;
721 
722  sf::Color m_borderColor;
723  sf::Color m_backgroundColor;
724 
725  Texture m_backgroundTexture;
726 
727  friend class ChatBox;
728  };
729 
731 }
732 
734 
735 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ChatBox.hpp:553
Implicit converter for colors.
Definition: Color.hpp:39
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:50
ChatBoxRenderer(ChatBox *chatBox)
Constructor.
Definition: ChatBox.hpp:600
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:49
Definition: ChatBox.hpp:56
std::shared_ptr< ChatBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ChatBox.hpp:120
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has padding.
Definition: Borders.hpp:210
const sf::Color & getTextColor() const
Returns the default color of the text.
Definition: ChatBox.hpp:354
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
unsigned int getTextSize() const
Returns the default character size of the text.
Definition: ChatBox.hpp:333
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Container widget.
Definition: Container.hpp:47
Definition: ChatBox.hpp:590
Definition: Texture.hpp:44
Definition: Borders.hpp:37
Base class for all renderer classes.
Definition: Widget.hpp:682
Definition: ChatBox.hpp:45