TGUI  0.7.8
ChatBox.hpp
1
2//
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
38namespace 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
101
102
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
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
376
377
387
388
400 void setLinesStartFromTop(bool startFromTop = true);
401
402
413
414
423 void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
424
425
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
Definition: Borders.hpp:38
Definition: ChatBox.hpp:591
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
ChatBoxRenderer(ChatBox *chatBox)
Constructor.
Definition: ChatBox.hpp:600
void setBackgroundTexture(const Texture &texture)
Changes the background image.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
void setBorderColor(const Color &borderColor)
Set the border color that will be used inside the chat box.
void setPadding(const Padding &padding) override
Changes the size of the padding.
void setBackgroundColor(const Color &backgroundColor)
Set the background color that will be used inside the chat box.
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
Definition: ChatBox.hpp:46
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
std::size_t getLineAmount()
Returns the amount of lines in the chat box.
void setScrollbar(Scrollbar::Ptr scrollbar)
Changes the scrollbar of the chat box.
ChatBox(const ChatBox &copy)
Copy constructor.
void setTextSize(unsigned int size)
Changes the default character size of the text.
virtual void setFont(const Font &font) override
Changes the default font of the text.
static ChatBox::Ptr copy(ChatBox::ConstPtr chatBox)
Makes a copy of another chat box.
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
void removeAllLines()
Removes all lines from the chat box.
virtual sf::Vector2f getFullSize() const override
Returns the full size of the chat box.
bool getLinesStartFromTop() const
Returns whether the first lines start from the top or from the bottom of the chat box.
static ChatBox::Ptr create()
Creates a new chat box widget.
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition: ChatBox.hpp:50
void setLinesStartFromTop(bool startFromTop=true)
Let the first lines start from the top or from the bottom of the chat box.
std::size_t getLineLimit()
Returns the maximum amount of lines in the chat box.
void addLine(const sf::String &text, unsigned int textSize)
Add a new line of text to the chat box.
void setTextColor(const Color &color)
Changes the default color of the text.
bool getNewLinesBelowOthers() const
Returns whether new lines are added below the other lines or above them.
void addLine(const sf::String &text, const sf::Color &color)
Add a new line of text to the chat box.
void setSize(const Layout2d &size) override
Changes the size of the chat box.
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
void setNewLinesBelowOthers(bool newLinesBelowOthers=true)
Set whether new lines are added below the other lines or above them.
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition: ChatBox.hpp:49
sf::String getLine(std::size_t lineIndex) const
Returns the contents of the requested line.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
unsigned int getLineTextSize(std::size_t lineIndex) const
Returns the text size of the requested line.
std::shared_ptr< sf::Font > getLineFont(std::size_t lineIndex) const
Returns the font of the requested line.
sf::Color getLineColor(std::size_t lineIndex) const
Returns the color of the requested line.
void addLine(const sf::String &text)
Add a new line of text to the chat box.
unsigned int getTextSize() const
Returns the default character size of the text.
Definition: ChatBox.hpp:333
void setLineLimit(std::size_t maxLines)
Set a maximum amount of lines in the chat box.
Scrollbar::Ptr getScrollbar() const
Access the scrollbar of the chat box.
bool removeLine(std::size_t lineIndex)
Removes the requested line.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChatBox.hpp:553
const sf::Color & getTextColor() const
Returns the default color of the text.
Definition: ChatBox.hpp:354
void addLine(const sf::String &text, const sf::Color &color, unsigned int textSize, const Font &font=nullptr)
Add a new line of text to the chat box.
Implicit converter for colors.
Definition: Color.hpp:40
Container widget.
Definition: Container.hpp:48
Definition: Font.hpp:38
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition: Scrollbar.hpp:53
Definition: Texture.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Parent class for every widget that has borders.
Definition: Borders.hpp:137
Parent class for every widget that has padding.
Definition: Borders.hpp:211
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34
Definition: ChatBox.hpp:57