TGUI  1.3-dev
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
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
35#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
36 #include <deque>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
44
45 class TGUI_API ChatBox : public Widget
46 {
47 public:
48
49 using Ptr = std::shared_ptr<ChatBox>;
50 using ConstPtr = std::shared_ptr<const ChatBox>;
51
52 static constexpr const char StaticWidgetType[] = "ChatBox";
53
54 struct Line
55 {
56 Text text;
57 String string;
58 };
59
60
68 ChatBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
69
70
77 TGUI_NODISCARD static ChatBox::Ptr create();
78
79
88 TGUI_NODISCARD static ChatBox::Ptr copy(const ChatBox::ConstPtr& chatBox);
89
90
95 TGUI_NODISCARD ChatBoxRenderer* getSharedRenderer() override;
96 TGUI_NODISCARD const ChatBoxRenderer* getSharedRenderer() const override;
97
103 TGUI_NODISCARD ChatBoxRenderer* getRenderer() override;
104
105
114 void setSize(const Layout2d& size) override;
115 using Widget::setSize;
116
117
128 void addLine(const String& text);
129
130
142 void addLine(const String& text, Color color);
143
144
155 void addLine(const String& text, Color color, TextStyles style);
156
157
168 TGUI_NODISCARD String getLine(std::size_t lineIndex) const;
169
170
179 TGUI_NODISCARD Color getLineColor(std::size_t lineIndex) const;
180
181
187 TGUI_NODISCARD TextStyles getLineTextStyle(std::size_t lineIndex) const;
188
189
199 bool removeLine(std::size_t lineIndex);
200
201
207
208
215 TGUI_NODISCARD std::size_t getLineAmount();
216
217
227 void setLineLimit(std::size_t maxLines);
228
229
239 TGUI_NODISCARD std::size_t getLineLimit();
240
241
246 void setTextColor(Color color);
247
248
253 TGUI_NODISCARD const Color& getTextColor() const;
254
255
261
262
267 TGUI_NODISCARD TextStyles getTextStyle() const;
268
269
281 void setLinesStartFromTop(bool startFromTop = true);
282
283
293 TGUI_NODISCARD bool getLinesStartFromTop() const;
294
295
304 void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
305
306
313 TGUI_NODISCARD bool getNewLinesBelowOthers() const;
314
315
321 void setScrollbarValue(unsigned int value);
322
323
329 TGUI_NODISCARD unsigned int getScrollbarValue() const;
330
331
338 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
339
343 bool leftMousePressed(Vector2f pos) override;
344
348 void leftMouseReleased(Vector2f pos) override;
349
353 void mouseMoved(Vector2f pos) override;
354
358 bool scrolled(float delta, Vector2f pos, bool touch) override;
359
363 void mouseNoLongerOnWidget() override;
364
368 void leftMouseButtonNoLongerDown() override;
369
370
372 private:
373
375 // Recalculates the text attribute of the line
377 void recalculateLineText(Line& line);
378
379
381 // Recalculates all text attributes, recalculate the full text height and update the displayed text
383 void recalculateAllLines();
384
385
387 // Recalculates the space used by all the lines
389 void recalculateFullTextHeight();
390
391
393 // Updates the position of the lines
395 void updateDisplayedText();
396
397
399 // Updates the position and size of the panel and scrollbar
401 void updateRendering();
402
403
405 protected:
406
414 void draw(BackendRenderTarget& target, RenderStates states) const override;
415
416
423 void rendererChanged(const String& property) override;
424
425
429 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
430
431
435 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
436
437
441 void updateTextSize() override;
442
443
445 // Returns the size without the borders
447 TGUI_NODISCARD Vector2f getInnerSize() const;
448
449
451 // Makes a copy of the widget
453 TGUI_NODISCARD Widget::Ptr clone() const override;
454
455
457 protected:
458
459 Color m_textColor = Color::Black;
460 TextStyles m_textStyle;
461
462 std::size_t m_maxLines = 0;
463
464 float m_fullTextHeight = 0;
465
466 bool m_linesStartFromTop = false;
467 bool m_newLinesBelowOthers = true;
468
470
471 std::deque<Line> m_lines;
472
473 Sprite m_spriteBackground;
474
475 // Cached renderer properties
476 Borders m_bordersCached;
477 Padding m_paddingCached;
478 Color m_backgroundColorCached;
479 Color m_borderColorCached;
480
482 };
483
485}
486
488
489#endif // TGUI_TEXT_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:48
Definition ChatBoxRenderer.hpp:37
Definition ChatBox.hpp:46
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition ChatBox.hpp:50
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...
TGUI_NODISCARD 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.
std::shared_ptr< ChatBox > Ptr
Shared widget pointer.
Definition ChatBox.hpp:49
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD unsigned int getScrollbarValue() const
Returns the thumb position of the scrollbar.
TGUI_NODISCARD std::size_t getLineLimit()
Returns the maximum amount of lines in the chat box.
void removeAllLines()
Removes all lines from the chat box.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the 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.
TGUI_NODISCARD TextStyles getTextStyle() const
Returns the default text style.
TGUI_NODISCARD String getLine(std::size_t lineIndex) const
Returns the contents of the requested line.
TGUI_NODISCARD bool getNewLinesBelowOthers() const
Returns whether new lines are added below the other lines or above them.
void addLine(const String &text, Color color, TextStyles style)
Adds a new line of text to the chat box.
static TGUI_NODISCARD ChatBox::Ptr copy(const ChatBox::ConstPtr &chatBox)
Makes a copy of another chat box.
TGUI_NODISCARD TextStyles getLineTextStyle(std::size_t lineIndex) const
Returns the text style of the requested line.
TGUI_NODISCARD ChatBoxRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void addLine(const String &text)
Adds a new line of text to the chat box.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
TGUI_NODISCARD ChatBoxRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
void setScrollbarValue(unsigned int value)
Changes the thumb position of the scrollbar.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setTextColor(Color color)
Changes the default color of the text.
void setTextStyle(TextStyles style)
Changes the default text style.
TGUI_NODISCARD std::size_t getLineAmount()
Returns the amount of lines in the chat box.
void setLineLimit(std::size_t maxLines)
Sets a maximum amount of lines in the chat box.
TGUI_NODISCARD Color getLineColor(std::size_t lineIndex) const
Returns the color of the requested line.
TGUI_NODISCARD bool getLinesStartFromTop() const
Returns whether the first lines start from the top or from the bottom of the chat box.
bool removeLine(std::size_t lineIndex)
Removes the requested line.
TGUI_NODISCARD const Color & getTextColor() const
Returns the default color of the text.
static TGUI_NODISCARD ChatBox::Ptr create()
Creates a new chat box widget.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Wrapper for colors.
Definition Color.hpp:72
Definition CopiedSharedPtr.hpp:45
Class to store the position or size of a widget.
Definition Layout.hpp:305
Definition Outline.hpp:39
Definition Sprite.hpp:48
Wrapper class to store strings.
Definition String.hpp:101
Wrapper for text styles.
Definition TextStyle.hpp:57
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:50
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Definition ChatBox.hpp:55
States used for drawing.
Definition RenderStates.hpp:39