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 
94  {
95  return aurora::downcast<ChatBoxRenderer*>(m_renderer.get());
96  }
97 
98 
105  virtual void setPosition(const Layout2d& position) override;
107 
108 
117  void setSize(const Layout2d& size) override;
119 
120 
132  void addLine(const sf::String& text);
133 
134 
147  void addLine(const sf::String& text, unsigned int textSize);
148 
149 
162  void addLine(const sf::String& text, const sf::Color& color);
163 
164 
177  void addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, const Font& font = nullptr);
178 
179 
190  sf::String getLine(std::size_t lineIndex) const;
191 
192 
201  sf::Color getLineColor(std::size_t lineIndex) const;
202 
203 
212  unsigned int getLineTextSize(std::size_t lineIndex) const;
213 
214 
224  std::shared_ptr<sf::Font> getLineFont(std::size_t lineIndex) const;
225 
226 
236  bool removeLine(std::size_t lineIndex);
237 
238 
243  void removeAllLines();
244 
245 
252  std::size_t getLineAmount();
253 
254 
264  void setLineLimit(std::size_t maxLines);
265 
266 
276  std::size_t getLineLimit();
277 
278 
286  void setTextSize(unsigned int size);
287 
288 
295  unsigned int getTextSize() const;
296 
297 
304  void setTextColor(Color color);
305 
306 
313  const sf::Color& getTextColor() const;
314 
315 
327  void setLinesStartFromTop(bool startFromTop = true);
328 
329 
339  bool getLinesStartFromTop() const;
340 
341 
350  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
351 
352 
359  bool getNewLinesBelowOthers() const;
360 
361 
365  virtual bool mouseOnWidget(sf::Vector2f pos) const override;
366 
370  virtual void leftMousePressed(sf::Vector2f pos) override;
371 
375  virtual void leftMouseReleased(sf::Vector2f pos) override;
376 
380  virtual void mouseMoved(sf::Vector2f pos) override;
381 
385  virtual void mouseWheelScrolled(float delta, int x, int y) override;
386 
390  virtual void mouseNoLongerOnWidget() override;
391 
395  virtual void mouseNoLongerDown() override;
396 
397 
399  private:
400 
402  // Recalculates the text attribute of the line
404  void recalculateLineText(Line& line);
405 
406 
408  // Recalculates all text attributes, recalculate the full text height and update the displayed text
410  void recalculateAllLines();
411 
412 
414  // Recalculates the space used by all the lines
416  void recalculateFullTextHeight();
417 
418 
420  // Updates the position of the lines
422  void updateDisplayedText();
423 
424 
426  // Updates the position and size of the panel and scrollbar
428  void updateRendering();
429 
430 
438  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
439 
440 
442  protected:
443 
450  virtual void rendererChanged(const std::string& property) override;
451 
452 
454  // Returns the size without the borders
456  sf::Vector2f getInnerSize() const;
457 
458 
460  // Makes a copy of the widget
462  virtual Widget::Ptr clone() const override
463  {
464  return std::make_shared<ChatBox>(*this);
465  }
466 
467 
469  protected:
470 
471  unsigned int m_textSize = 18;
472  sf::Color m_textColor = sf::Color::Black;
473 
474  std::size_t m_maxLines = 0;
475 
476  float m_fullTextHeight = 0;
477 
478  bool m_linesStartFromTop = false;
479  bool m_newLinesBelowOthers = true;
480 
481  ScrollbarChildWidget m_scroll;
482 
483  std::deque<Line> m_lines;
484 
485  Sprite m_spriteBackground;
486 
487  // Cached renderer properties
488  Borders m_bordersCached;
489  Padding m_paddingCached;
490  Color m_backgroundColorCached;
491  Color m_borderColorCached;
492 
494  };
495 
497 }
498 
500 
501 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
ChatBoxRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: ChatBox.hpp:93
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: ChatBox.hpp:462
Wrapper for colors.
Definition: Color.hpp:46
Definition: Font.hpp:41
Class to store the position or size of a widget.
Definition: Layout.hpp:263
The parent class for every widget.
Definition: Widget.hpp:72
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 setSize(const Layout2d &size)
Changes the size of the widget.
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition: Scrollbar.hpp:355
Definition: Text.hpp:41
virtual void setPosition(const Layout2d &position)
Sets the position of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
Definition: ChatBoxRenderer.hpp:36
Definition: Outline.hpp:37
Definition: ChatBox.hpp:41