--- --- TGUI: include/TGUI/Widgets/ChatBox.hpp Source File
TGUI  1.x-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#ifndef TGUI_CHAT_BOX_HPP
26#define TGUI_CHAT_BOX_HPP
27
28#include <TGUI/CopiedSharedPtr.hpp>
29#include <TGUI/Widgets/Scrollbar.hpp>
30#include <TGUI/Renderers/ChatBoxRenderer.hpp>
31#include <TGUI/Text.hpp>
32
33#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <deque>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
42
43 class TGUI_API ChatBox : public Widget
44 {
45 public:
46
47 using Ptr = std::shared_ptr<ChatBox>;
48 using ConstPtr = std::shared_ptr<const ChatBox>;
49
50 static constexpr const char StaticWidgetType[] = "ChatBox";
51
52 struct Line
53 {
54 Text text;
55 String string;
56 };
57
65 ChatBox(const char* typeName = StaticWidgetType, bool initRenderer = true);
66
72 TGUI_NODISCARD static ChatBox::Ptr create();
73
81 TGUI_NODISCARD static ChatBox::Ptr copy(const ChatBox::ConstPtr& chatBox);
82
87 TGUI_NODISCARD ChatBoxRenderer* getSharedRenderer() override;
88 TGUI_NODISCARD const ChatBoxRenderer* getSharedRenderer() const override;
89
95 TGUI_NODISCARD ChatBoxRenderer* getRenderer() override;
96
104 void setSize(const Layout2d& size) override;
105 using Widget::setSize;
106
117 void addLine(const String& text);
118
130 void addLine(const String& text, Color color);
131
142 void addLine(const String& text, Color color, TextStyles style);
143
153 TGUI_NODISCARD String getLine(std::size_t lineIndex) const;
154
162 TGUI_NODISCARD Color getLineColor(std::size_t lineIndex) const;
163
169 TGUI_NODISCARD TextStyles getLineTextStyle(std::size_t lineIndex) const;
170
179 bool removeLine(std::size_t lineIndex);
180
185
191 TGUI_NODISCARD std::size_t getLineAmount() const;
192
201 void setLineLimit(std::size_t maxLines);
202
211 TGUI_NODISCARD std::size_t getLineLimit() const;
212
217 void setTextColor(Color color);
218
223 TGUI_NODISCARD const Color& getTextColor() const;
224
230
235 TGUI_NODISCARD TextStyles getTextStyle() const;
236
247 void setLinesStartFromTop(bool startFromTop = true);
248
257 TGUI_NODISCARD bool getLinesStartFromTop() const;
258
266 void setNewLinesBelowOthers(bool newLinesBelowOthers = true);
267
273 TGUI_NODISCARD bool getNewLinesBelowOthers() const;
274
280 void setScrollbarValue(unsigned int value);
281
287 TGUI_NODISCARD unsigned int getScrollbarValue() const;
288
296 TGUI_NODISCARD unsigned int getScrollbarMaxValue() const;
297
303 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
304
308 bool leftMousePressed(Vector2f pos) override;
309
313 void leftMouseReleased(Vector2f pos) override;
314
318 void mouseMoved(Vector2f pos) override;
319
323 bool scrolled(float delta, Vector2f pos, bool touch) override;
324
328 void mouseNoLongerOnWidget() override;
329
333 void leftMouseButtonNoLongerDown() override;
334
336 private:
337
339 // Recalculates the text attribute of the line
341 void recalculateLineText(Line& line);
342
344 // Recalculates all text attributes, recalculate the full text height and update the displayed text
346 void recalculateAllLines();
347
349 // Recalculates the space used by all the lines
351 void recalculateFullTextHeight();
352
354 // Updates the position of the lines
356 void updateDisplayedText();
357
359 // Updates the position and size of the panel and scrollbar
361 void updateRendering();
362
364 protected:
365
372 void draw(BackendRenderTarget& target, RenderStates states) const override;
373
379 void rendererChanged(const String& property) override;
380
384 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
385
389 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
390
394 void updateTextSize() override;
395
397 // Returns the size without the borders
399 TGUI_NODISCARD Vector2f getInnerSize() const;
400
402 // Makes a copy of the widget
404 TGUI_NODISCARD Widget::Ptr clone() const override;
405
407 protected:
408
409 Color m_textColor = Color::Black;
410 TextStyles m_textStyle;
411
412 std::size_t m_maxLines = 0;
413
414 float m_fullTextHeight = 0;
415
416 bool m_linesStartFromTop = false;
417 bool m_newLinesBelowOthers = true;
418
420
421 std::deque<Line> m_lines;
422
423 Sprite m_spriteBackground;
424
425 // Cached renderer properties
426 Borders m_bordersCached;
427 Padding m_paddingCached;
428 Color m_backgroundColorCached;
429 Color m_borderColorCached;
430
432 };
433
435}
436
438
439#endif // TGUI_TEXT_BOX_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition ChatBox.hpp:44
std::shared_ptr< const ChatBox > ConstPtr
Shared constant widget pointer.
Definition ChatBox.hpp:48
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:47
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.
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 std::size_t getLineAmount() const
Returns the amount of lines in the chat box.
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.
TGUI_NODISCARD std::size_t getLineLimit() const
Returns the maximum amount of lines in the chat box.
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 unsigned int getScrollbarMaxValue() const
Returns the maximum thumb position of the scrollbar.
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...
Definition ChatBoxRenderer.hpp:35
Wrapper for colors.
Definition Color.hpp:73
Definition CopiedSharedPtr.hpp:42
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition Outline.hpp:38
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
Wrapper for text styles.
Definition TextStyle.hpp:55
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Definition ChatBox.hpp:53
States used for drawing.
Definition RenderStates.hpp:38