TGUI  0.8.1
ChatBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
138  void addLine(const sf::String& text, Color color);
139 
140 
151  sf::String getLine(std::size_t lineIndex) const;
152 
153 
162  Color getLineColor(std::size_t lineIndex) const;
163 
164 
174  bool removeLine(std::size_t lineIndex);
175 
176 
181  void removeAllLines();
182 
183 
190  std::size_t getLineAmount();
191 
192 
202  void setLineLimit(std::size_t maxLines);
203 
204 
214  std::size_t getLineLimit();
215 
216 
221  void setTextSize(unsigned int size);
222 
223 
228  unsigned int getTextSize() const;
229 
230 
235  void setTextColor(Color color);
236 
237 
242  const Color& getTextColor() const;
243 
244 
256  void setLinesStartFromTop(bool startFromTop = true);
257 
258 
268  bool getLinesStartFromTop() const;
269 
270 
279  void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
280 
281 
288  bool getNewLinesBelowOthers() const;
289 
290 
297  bool mouseOnWidget(Vector2f pos) const override;
298 
302  void leftMousePressed(Vector2f pos) override;
303 
307  void leftMouseReleased(Vector2f pos) override;
308 
312  void mouseMoved(Vector2f pos) override;
313 
317  bool mouseWheelScrolled(float delta, Vector2f pos) override;
318 
322  void mouseNoLongerOnWidget() override;
323 
327  void mouseNoLongerDown() override;
328 
329 
331  private:
332 
334  // Recalculates the text attribute of the line
336  void recalculateLineText(Line& line);
337 
338 
340  // Recalculates all text attributes, recalculate the full text height and update the displayed text
342  void recalculateAllLines();
343 
344 
346  // Recalculates the space used by all the lines
348  void recalculateFullTextHeight();
349 
350 
352  // Updates the position of the lines
354  void updateDisplayedText();
355 
356 
358  // Updates the position and size of the panel and scrollbar
360  void updateRendering();
361 
362 
370  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
371 
372 
374  protected:
375 
382  void rendererChanged(const std::string& property) override;
383 
384 
388  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
389 
390 
394  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
395 
396 
398  // Returns the size without the borders
400  Vector2f getInnerSize() const;
401 
402 
404  // Makes a copy of the widget
406  Widget::Ptr clone() const override
407  {
408  return std::make_shared<ChatBox>(*this);
409  }
410 
411 
413  protected:
414 
415  unsigned int m_textSize = 0;
416  Color m_textColor = Color::Black;
417 
418  std::size_t m_maxLines = 0;
419 
420  float m_fullTextHeight = 0;
421 
422  bool m_linesStartFromTop = false;
423  bool m_newLinesBelowOthers = true;
424 
426 
427  std::deque<Line> m_lines;
428 
429  Sprite m_spriteBackground;
430 
431  // Cached renderer properties
432  Borders m_bordersCached;
433  Padding m_paddingCached;
434  Color m_backgroundColorCached;
435  Color m_borderColorCached;
436 
438  };
439 
441 }
442 
444 
445 #endif // TGUI_TEXT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:43
Wrapper for colors.
Definition: Color.hpp:47
Class to store the position or size of a widget.
Definition: Layout.hpp:245
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&#39;t know its exact type.
Definition: ChatBox.hpp:406
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