TGUI 1.11
Loading...
Searching...
No Matches
ChatBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_CHAT_BOX_HPP
26#define TGUI_CHAT_BOX_HPP
27
28#include <TGUI/Widgets/Scrollbar.hpp>
29#include <TGUI/Renderers/ChatBoxRenderer.hpp>
30#include <TGUI/Text.hpp>
31
32#include <deque>
33
35
36namespace tgui
37{
39
40 class TGUI_API ChatBox : public Widget, public ScrollbarChildInterface
41 {
42 public:
43
44 using Ptr = std::shared_ptr<ChatBox>;
45 using ConstPtr = std::shared_ptr<const ChatBox>;
46
47 static constexpr const char StaticWidgetType[] = "ChatBox";
48
49 struct Line
50 {
51 Text text;
52 String string;
53 };
54
62 ChatBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
63
69 TGUI_NODISCARD static ChatBox::Ptr create();
70
78 TGUI_NODISCARD static ChatBox::Ptr copy(const ChatBox::ConstPtr& chatBox);
79
84 TGUI_NODISCARD ChatBoxRenderer* getSharedRenderer() override;
85 TGUI_NODISCARD const ChatBoxRenderer* getSharedRenderer() const override;
86
92 TGUI_NODISCARD ChatBoxRenderer* getRenderer() override;
93
101 void setSize(const Layout2d& size) override;
102 using Widget::setSize;
103
114 void addLine(const String& text);
115
127 void addLine(const String& text, Color color);
128
139 void addLine(const String& text, Color color, TextStyles style);
140
150 TGUI_NODISCARD String getLine(std::size_t lineIndex) const;
151
159 TGUI_NODISCARD Color getLineColor(std::size_t lineIndex) const;
160
166 TGUI_NODISCARD TextStyles getLineTextStyle(std::size_t lineIndex) const;
167
176 bool removeLine(std::size_t lineIndex);
177
182
188 TGUI_NODISCARD std::size_t getLineAmount() const;
189
198 void setLineLimit(std::size_t maxLines);
199
208 TGUI_NODISCARD std::size_t getLineLimit() const;
209
214 void setTextColor(Color color);
215
220 TGUI_NODISCARD const Color& getTextColor() const;
221
227
232 TGUI_NODISCARD TextStyles getTextStyle() const;
233
244 void setLinesStartFromTop(bool startFromTop = true);
245
254 TGUI_NODISCARD bool getLinesStartFromTop() const;
255
263 void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
264
270 TGUI_NODISCARD bool getNewLinesBelowOthers() const;
271
277 TGUI_DEPRECATED("Use getScrollbar()->setValue(value) instead") void setScrollbarValue(unsigned int value);
278
284 TGUI_DEPRECATED("Use getScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getScrollbarValue() const;
285
293 TGUI_DEPRECATED("Use getScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getScrollbarMaxValue() const;
294
300 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
301
305 bool leftMousePressed(Vector2f pos) override;
306
310 void leftMouseReleased(Vector2f pos) override;
311
315 void mouseMoved(Vector2f pos) override;
316
320 bool scrolled(float delta, Vector2f pos, bool touch) override;
321
325 void mouseNoLongerOnWidget() override;
326
330 void leftMouseButtonNoLongerDown() override;
331
333 private:
334
336 // Recalculates the text attribute of the line
338 void recalculateLineText(Line& line);
339
341 // Recalculates all text attributes, recalculate the full text height and update the displayed text
343 void recalculateAllLines();
344
346 // Recalculates the space used by all the lines
348 void recalculateFullTextHeight();
349
351 // Updates the position of the lines
353 void updateDisplayedText();
354
356 // Updates the position and size of the panel and scrollbar
358 void updateRendering();
359
361 protected:
362
369 void draw(BackendRenderTarget& target, RenderStates states) const override;
370
376 void rendererChanged(const String& property) override;
377
381 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
382
386 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
387
391 void updateTextSize() override;
392
394 // Returns the size without the borders
396 TGUI_NODISCARD Vector2f getInnerSize() const;
397
399 // Makes a copy of the widget
401 TGUI_NODISCARD Widget::Ptr clone() const override;
402
404 protected:
405
406 Color m_textColor = Color::Black;
407 TextStyles m_textStyle;
408
409 std::size_t m_maxLines = 0;
410
411 float m_fullTextHeight = 0;
412
413 bool m_linesStartFromTop = false;
414 bool m_newLinesBelowOthers = true;
415
416 std::deque<Line> m_lines;
417
418 Sprite m_spriteBackground;
419
420 // Cached renderer properties
421 Borders m_bordersCached;
422 Padding m_paddingCached;
423 Color m_backgroundColorCached;
424 Color m_borderColorCached;
425
427 };
428
430}
431
433
434#endif // TGUI_TEXT_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition ChatBoxRenderer.hpp:35
unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition ChatBox.hpp:45
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition ChatBox.hpp:44
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void removeAllLines()
Removes all lines from the chat box.
bool getLinesStartFromTop() const
Returns whether the first lines start from the top or from the bottom of the chat box.
ChatBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
static ChatBox::Ptr create()
Creates a new chat box widget.
void setLinesStartFromTop(bool startFromTop=true)
Lets the first lines start from the top or from the bottom of the chat box.
void addLine(const String &text, Color color)
Adds a new line of text to the chat box.
unsigned int getScrollbarMaxValue() const
Returns the maximum thumb position of the scrollbar.
void addLine(const String &text, Color color, TextStyles style)
Adds a new line of text to the chat box.
const Color & getTextColor() const
Returns the default color of the text.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::size_t getLineLimit() const
Returns the maximum amount of lines in the chat box.
void addLine(const String &text)
Adds a new line of text to the chat box.
bool getNewLinesBelowOthers() const
Returns whether new lines are added below the other lines or above them.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition ChatBox.hpp:47
static ChatBox::Ptr copy(const ChatBox::ConstPtr &chatBox)
Makes a copy of another chat box.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
Color getLineColor(std::size_t lineIndex) const
Returns the color of the requested line.
void setSize(const Layout2d &size) override
Changes the size of the chat box.
void setNewLinesBelowOthers(bool newLinesBelowOthers=true)
Sets whether new lines are added below the other lines or above them.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
TextStyles getTextStyle() const
Returns the default text style.
TextStyles getLineTextStyle(std::size_t lineIndex) const
Returns the text style of the requested line.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
ChatBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::size_t getLineAmount() const
Returns the amount of lines in the chat box.
void setTextColor(Color color)
Changes the default color of the text.
void setTextStyle(TextStyles style)
Changes the default text style.
String getLine(std::size_t lineIndex) const
Returns the contents of the requested line.
void setLineLimit(std::size_t maxLines)
Sets a maximum amount of lines in the chat box.
bool removeLine(std::size_t lineIndex)
Removes the requested line.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Wrapper for colors.
Definition Color.hpp:71
Class to store the position or size of a widget.
Definition Layout.hpp:321
ScrollbarChildInterface()
Default constructor.
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:94
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Definition ChatBox.hpp:50
States used for drawing.
Definition RenderStates.hpp:38