TGUI  1.6.1
Loading...
Searching...
No Matches
ChatBox.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <deque>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
41
42 class TGUI_API ChatBox : public Widget, public ScrollbarChildInterface
43 {
44 public:
45
46 using Ptr = std::shared_ptr<ChatBox>;
47 using ConstPtr = std::shared_ptr<const ChatBox>;
48
49 static constexpr const char StaticWidgetType[] = "ChatBox";
50
51 struct Line
52 {
53 Text text;
54 String string;
55 };
56
64 ChatBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
65
71 TGUI_NODISCARD static ChatBox::Ptr create();
72
80 TGUI_NODISCARD static ChatBox::Ptr copy(const ChatBox::ConstPtr& chatBox);
81
86 TGUI_NODISCARD ChatBoxRenderer* getSharedRenderer() override;
87 TGUI_NODISCARD const ChatBoxRenderer* getSharedRenderer() const override;
88
94 TGUI_NODISCARD ChatBoxRenderer* getRenderer() override;
95
103 void setSize(const Layout2d& size) override;
104 using Widget::setSize;
105
116 void addLine(const String& text);
117
129 void addLine(const String& text, Color color);
130
141 void addLine(const String& text, Color color, TextStyles style);
142
152 TGUI_NODISCARD String getLine(std::size_t lineIndex) const;
153
161 TGUI_NODISCARD Color getLineColor(std::size_t lineIndex) const;
162
168 TGUI_NODISCARD TextStyles getLineTextStyle(std::size_t lineIndex) const;
169
178 bool removeLine(std::size_t lineIndex);
179
184
190 TGUI_NODISCARD std::size_t getLineAmount() const;
191
200 void setLineLimit(std::size_t maxLines);
201
210 TGUI_NODISCARD std::size_t getLineLimit() const;
211
216 void setTextColor(Color color);
217
222 TGUI_NODISCARD const Color& getTextColor() const;
223
229
234 TGUI_NODISCARD TextStyles getTextStyle() const;
235
246 void setLinesStartFromTop(bool startFromTop = true);
247
256 TGUI_NODISCARD bool getLinesStartFromTop() const;
257
265 void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
266
272 TGUI_NODISCARD bool getNewLinesBelowOthers() const;
273
279 TGUI_DEPRECATED("Use getScrollbar()->setValue(value) instead") void setScrollbarValue(unsigned int value);
280
286 TGUI_DEPRECATED("Use getScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getScrollbarValue() const;
287
295 TGUI_DEPRECATED("Use getScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getScrollbarMaxValue() const;
296
302 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
303
307 bool leftMousePressed(Vector2f pos) override;
308
312 void leftMouseReleased(Vector2f pos) override;
313
317 void mouseMoved(Vector2f pos) override;
318
322 bool scrolled(float delta, Vector2f pos, bool touch) override;
323
327 void mouseNoLongerOnWidget() override;
328
332 void leftMouseButtonNoLongerDown() override;
333
335 private:
336
338 // Recalculates the text attribute of the line
340 void recalculateLineText(Line& line);
341
343 // Recalculates all text attributes, recalculate the full text height and update the displayed text
345 void recalculateAllLines();
346
348 // Recalculates the space used by all the lines
350 void recalculateFullTextHeight();
351
353 // Updates the position of the lines
355 void updateDisplayedText();
356
358 // Updates the position and size of the panel and scrollbar
360 void updateRendering();
361
363 protected:
364
371 void draw(BackendRenderTarget& target, RenderStates states) const override;
372
378 void rendererChanged(const String& property) override;
379
383 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
384
388 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
389
393 void updateTextSize() override;
394
396 // Returns the size without the borders
398 TGUI_NODISCARD Vector2f getInnerSize() const;
399
401 // Makes a copy of the widget
403 TGUI_NODISCARD Widget::Ptr clone() const override;
404
406 protected:
407
408 Color m_textColor = Color::Black;
409 TextStyles m_textStyle;
410
411 std::size_t m_maxLines = 0;
412
413 float m_fullTextHeight = 0;
414
415 bool m_linesStartFromTop = false;
416 bool m_newLinesBelowOthers = true;
417
418 std::deque<Line> m_lines;
419
420 Sprite m_spriteBackground;
421
422 // Cached renderer properties
423 Borders m_bordersCached;
424 Padding m_paddingCached;
425 Color m_backgroundColorCached;
426 Color m_borderColorCached;
427
429 };
430
432}
433
435
436#endif // TGUI_TEXT_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition ChatBoxRenderer.hpp:35
Definition ChatBox.hpp:43
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition ChatBox.hpp:47
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition ChatBox.hpp:46
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.
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.
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::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 ChatBox::Ptr copy(const ChatBox::ConstPtr &chatBox)
Makes a copy of another chat box.
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.
TextStyles getTextStyle() const
Returns the default text style.
TextStyles getLineTextStyle(std::size_t lineIndex) const
Returns the text style of the requested line.
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.
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition Outline.hpp:38
Base class for widgets with a single scrollbar.
Definition Scrollbar.hpp:586
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Wrapper for text styles.
Definition TextStyle.hpp:55
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition ChatBox.hpp:52
States used for drawing.
Definition RenderStates.hpp:38