ChatBox now uses the new renderer
parent
46e5867679
commit
91660444f4
|
@ -0,0 +1,156 @@
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// TGUI - Texus' Graphical User Interface
|
||||
// Copyright (C) 2012-2016 Bruno Van de Velde (vdv_b@tgui.eu)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#ifndef TGUI_CHAT_BOX_RENDERER_HPP
|
||||
#define TGUI_CHAT_BOX_RENDERER_HPP
|
||||
|
||||
|
||||
#include <TGUI/Renderers/WidgetRenderer.hpp>
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace tgui
|
||||
{
|
||||
struct TGUI_API ChatBoxRenderer : public WidgetRenderer
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the size of the borders
|
||||
///
|
||||
/// @param borders Size of the borders
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBorders(const Borders& borders);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the size of the borders
|
||||
///
|
||||
/// @return border size
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Borders getBorders() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the padding of the chat box
|
||||
///
|
||||
/// @param padding The padding width and height
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setPadding(const Padding& padding);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the size of the padding
|
||||
///
|
||||
/// @return padding size
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Padding getPadding() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the background color of the chat box
|
||||
///
|
||||
/// @param backgroundColor The new background color
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBackgroundColor(Color backgroundColor);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the background color
|
||||
///
|
||||
/// @return Background color
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Color getBackgroundColor() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the color of the borders
|
||||
///
|
||||
/// @param borderColor The new border color
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBorderColor(Color borderColor);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the color of the borders
|
||||
///
|
||||
/// @return Border color
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Color getBorderColor() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the background image of the chat box
|
||||
///
|
||||
/// @param texture The background texture
|
||||
///
|
||||
/// When this image is set, the background color property will be ignored.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setTextureBackground(const Texture& texture);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the background image of the chat box
|
||||
///
|
||||
/// @return Background texture
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Texture& getTextureBackground() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Sets the renderer data of the scrollbar
|
||||
///
|
||||
/// @param scrollbarRendererData Data about how the scrollbar should look
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setScrollbar(std::shared_ptr<RendererData> scrollbarRendererData);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the renderer data of the scrollbar
|
||||
///
|
||||
/// @return Data about how the scrollbar looks
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
std::shared_ptr<RendererData> getScrollbar() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#endif // TGUI_CHAT_BOX_RENDERER_HPP
|
|
@ -42,8 +42,8 @@
|
|||
#include <TGUI/Loading/WidgetSaver.hpp>
|
||||
|
||||
#include <TGUI/Widgets/Button.hpp>
|
||||
#include <TGUI/Widgets/Canvas.hpp>/**
|
||||
#include <TGUI/Widgets/ChatBox.hpp>*/
|
||||
#include <TGUI/Widgets/Canvas.hpp>
|
||||
#include <TGUI/Widgets/ChatBox.hpp>
|
||||
#include <TGUI/Widgets/CheckBox.hpp>/**
|
||||
#include <TGUI/Widgets/ChildWindow.hpp>*/
|
||||
#include <TGUI/Widgets/ClickableWidget.hpp>
|
||||
|
|
|
@ -49,6 +49,8 @@ namespace tgui
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
static float calculateExtraVerticalSpace(Font font, unsigned int characterSize, TextStyle style = sf::Text::Regular);
|
||||
|
||||
static sf::String wordWrap(float maxWidth, const sf::String& text, Font font, unsigned int textSize, bool bold, bool dropLeadingSpace = true);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
|
|
@ -27,9 +27,8 @@
|
|||
#define TGUI_CHAT_BOX_HPP
|
||||
|
||||
|
||||
#include <TGUI/Widget.hpp>
|
||||
#include <TGUI/Widgets/Panel.hpp>
|
||||
#include <TGUI/Widgets/Scrollbar.hpp>
|
||||
#include <TGUI/Renderers/ChatBoxRenderer.hpp>
|
||||
|
||||
#include <deque>
|
||||
|
||||
|
@ -37,9 +36,6 @@
|
|||
|
||||
namespace tgui
|
||||
{
|
||||
class Scrollbar;
|
||||
class ChatBoxRenderer;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class TGUI_API ChatBox : public Widget
|
||||
|
@ -49,16 +45,13 @@ namespace tgui
|
|||
typedef std::shared_ptr<ChatBox> Ptr; ///< Shared widget pointer
|
||||
typedef std::shared_ptr<const ChatBox> ConstPtr; ///< Shared constant widget pointer
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
protected:
|
||||
|
||||
struct Line
|
||||
{
|
||||
sf::Text text;
|
||||
Text text;
|
||||
sf::String string;
|
||||
unsigned int sublines = 1;
|
||||
std::shared_ptr<sf::Font> font;
|
||||
};
|
||||
|
||||
|
||||
|
@ -71,26 +64,6 @@ namespace tgui
|
|||
ChatBox();
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Copy constructor
|
||||
///
|
||||
/// @param copy Instance to copy
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
ChatBox(const ChatBox& copy);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Overload of assignment operator
|
||||
///
|
||||
/// @param right Instance to assign
|
||||
///
|
||||
/// @return Reference to itself
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
ChatBox& operator= (const ChatBox& right);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Makes a copy of another chat box
|
||||
///
|
||||
|
@ -105,12 +78,12 @@ namespace tgui
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the renderer, which gives access to functions that determine how the widget is displayed
|
||||
///
|
||||
/// @return Pointer to the renderer
|
||||
/// @return Temporary pointer to the renderer
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
std::shared_ptr<ChatBoxRenderer> getRenderer() const
|
||||
ChatBoxRenderer* getRenderer() const
|
||||
{
|
||||
return std::static_pointer_cast<ChatBoxRenderer>(m_renderer);
|
||||
return aurora::downcast<ChatBoxRenderer*>(m_renderer.get());
|
||||
}
|
||||
|
||||
|
||||
|
@ -126,17 +99,6 @@ namespace tgui
|
|||
using Transformable::setSize;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the full size of the chat box
|
||||
///
|
||||
/// This is the size including the borders.
|
||||
///
|
||||
/// @return Full size of the chat box
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual sf::Vector2f getFullSize() const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Adds a new line of text to the chat box
|
||||
///
|
||||
|
@ -236,7 +198,8 @@ namespace tgui
|
|||
///
|
||||
/// @param lineIndex The index of the line of which you request the font. The first line has index 0
|
||||
///
|
||||
/// @return The font of the requested line. The default font (set with setFont) when the index is too high
|
||||
/// @return The font of the requested line.
|
||||
/// When the index is too high then the default font (set with chatBox->getRenderer()->setFont(font)) is returned.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
std::shared_ptr<sf::Font> getLineFont(std::size_t lineIndex) const;
|
||||
|
@ -294,17 +257,6 @@ namespace tgui
|
|||
std::size_t getLineLimit();
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the default font of the text
|
||||
///
|
||||
/// @param font The new font
|
||||
///
|
||||
/// When you don't call this function then the font from the parent widget will be used.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void setFont(const Font& font) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the default character size of the text
|
||||
///
|
||||
|
@ -321,10 +273,7 @@ namespace tgui
|
|||
/// @return The currently used default text size
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
unsigned int getTextSize() const
|
||||
{
|
||||
return m_textSize;
|
||||
}
|
||||
unsigned int getTextSize() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -342,39 +291,7 @@ namespace tgui
|
|||
/// @return The currently used default text color
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
const sf::Color& getTextColor() const
|
||||
{
|
||||
return m_textColor;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the scrollbar of the chat box
|
||||
///
|
||||
/// @param scrollbar The new scrollbar to use in the chat box
|
||||
///
|
||||
/// Pass a nullptr to remove the scrollbar. Note that when removing the scrollbar while there are too many lines
|
||||
/// to fit in the chat box then the oldest lines will be removed.
|
||||
///
|
||||
/// The scrollbar should have no parent and you should not change it yourself.
|
||||
/// The function is meant to be used like this:
|
||||
/// @code
|
||||
/// chatBox->setScrollbar(theme->load("scrollbar"));
|
||||
/// @endcode
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setScrollbar(Scrollbar::Ptr scrollbar);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Access the scrollbar of the chat box
|
||||
///
|
||||
/// @return scrollbar in the chat box
|
||||
///
|
||||
/// You should not change the scrollbar yourself
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Scrollbar::Ptr getScrollbar() const;
|
||||
const sf::Color& getTextColor() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -423,34 +340,6 @@ namespace tgui
|
|||
bool getNewLinesBelowOthers() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the opacity of the widget
|
||||
///
|
||||
/// @param opacity The opacity of the widget. 0 means completely transparent, while 1 (default) means fully opaque
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void setOpacity(float opacity) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Returns the distance between the position where the widget is drawn and where the widget is placed
|
||||
///
|
||||
/// This is basically the width and height of the optional borders drawn around widgets.
|
||||
///
|
||||
/// @return Offset of the widget
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual sf::Vector2f getWidgetOffset() const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @internal
|
||||
/// This function is called when the widget is added to a container.
|
||||
/// You should not call this function yourself.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void setParent(Container* parent) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @internal
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -524,18 +413,25 @@ namespace tgui
|
|||
protected:
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Reloads the widget
|
||||
/// @brief Function called when one of the properties of the renderer is changed
|
||||
///
|
||||
/// @param primary Primary parameter for the loader
|
||||
/// @param secondary Secondary parameter for the loader
|
||||
/// @param force Try to only change the looks of the widget and not alter the widget itself when false
|
||||
///
|
||||
/// @throw Exception when the connected theme could not create the widget
|
||||
///
|
||||
/// When primary is an empty string the built-in white theme will be used.
|
||||
/// @param property Lowercase name of the property that was changed
|
||||
/// @param value New value of the property
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
|
||||
virtual void rendererChanged(const std::string& property, ObjectConverter& value) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Returns the size without the borders
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
sf::Vector2f getInnerSize() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Draws the widget on the render target.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -547,12 +443,6 @@ namespace tgui
|
|||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Draws the widget on the render target.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
protected:
|
||||
|
||||
|
@ -566,155 +456,13 @@ namespace tgui
|
|||
bool m_linesStartFromTop = false;
|
||||
bool m_newLinesBelowOthers = true;
|
||||
|
||||
Scrollbar::Ptr m_scroll = std::make_shared<Scrollbar>();
|
||||
ScrollbarChildWidget m_scroll;
|
||||
|
||||
std::deque<Line> m_lines;
|
||||
|
||||
friend class ChatBoxRenderer;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class TGUI_API ChatBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
|
||||
{
|
||||
public:
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Constructor
|
||||
///
|
||||
/// @param chatBox The chat box that is connected to the renderer
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
ChatBoxRenderer(ChatBox* chatBox) : m_chatBox{chatBox} {}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes a property of the renderer
|
||||
///
|
||||
/// @param property The property that you would like to change
|
||||
/// @param value The new serialized value that you like to assign to the property
|
||||
///
|
||||
/// @throw Exception when deserialization fails or when the widget does not have this property
|
||||
/// @throw Exception when loading scrollbar fails with the theme connected to the list box
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void setProperty(std::string property, const std::string& value) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes a property of the renderer
|
||||
///
|
||||
/// @param property The property that you would like to change
|
||||
/// @param value The new value that you like to assign to the property
|
||||
/// The ObjectConverter is implicitly constructed from the possible value types.
|
||||
///
|
||||
/// @throw Exception for unknown properties or when value was of a wrong type
|
||||
/// @throw Exception when loading scrollbar fails with the theme connected to the list box
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual void setProperty(std::string property, ObjectConverter&& value) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Retrieves the value of a certain property
|
||||
///
|
||||
/// @param property The property that you would like to retrieve
|
||||
///
|
||||
/// @return The value inside a ObjectConverter object which you can extract with the correct get function or
|
||||
/// an ObjectConverter object with type ObjectConverter::Type::None when the property did not exist.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual ObjectConverter getProperty(std::string property) const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Gets a map with all properties and their values
|
||||
///
|
||||
/// @return Property-value pairs of the renderer
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Sets the border color that will be used inside the chat box
|
||||
///
|
||||
/// @param borderColor The color of the borders
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBorderColor(Color borderColor);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Sets the background color that will be used inside the chat box
|
||||
///
|
||||
/// @param backgroundColor The new background color
|
||||
///
|
||||
/// Note that this color is ignored when you set a background image.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBackgroundColor(Color backgroundColor);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the background image
|
||||
///
|
||||
/// @param texture New background texture
|
||||
///
|
||||
/// When this image is set, the background color property will be ignored.
|
||||
/// Pass an empty string to unset the image, in this case the background color property will be used again.
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setBackgroundTexture(const Texture& texture);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// @brief Changes the size of the padding
|
||||
///
|
||||
/// @param padding Size of the padding
|
||||
///
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void setPadding(const Padding& padding) override;
|
||||
using WidgetPadding::setPadding;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Draws the widget on the render target.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
private:
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Returns the padding, which is possibly scaled with the background image.
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Padding getScaledPadding() const;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Makes a copy of the renderer
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
protected:
|
||||
|
||||
ChatBox* m_chatBox;
|
||||
|
||||
sf::Color m_borderColor;
|
||||
sf::Color m_backgroundColor;
|
||||
|
||||
Texture m_backgroundTexture;
|
||||
|
||||
friend class ChatBox;
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ set(TGUI_SRC
|
|||
Loading/WidgetLoader.cpp
|
||||
Loading/WidgetSaver.cpp
|
||||
Renderers/ButtonRenderer.cpp
|
||||
Renderers/ChatBoxRenderer.cpp
|
||||
Renderers/ComboBoxRenderer.cpp
|
||||
Renderers/EditBoxRenderer.cpp
|
||||
Renderers/KnobRenderer.cpp
|
||||
|
@ -41,7 +42,7 @@ set(TGUI_SRC
|
|||
Renderers/WidgetRenderer.cpp
|
||||
Widgets/Button.cpp
|
||||
Widgets/Canvas.cpp
|
||||
#Widgets/ChatBox.cpp
|
||||
Widgets/ChatBox.cpp
|
||||
Widgets/CheckBox.cpp
|
||||
#Widgets/ChildWindow.cpp
|
||||
Widgets/ClickableWidget.cpp
|
||||
|
|
|
@ -26,8 +26,8 @@
|
|||
#include <TGUI/Loading/Deserializer.hpp>
|
||||
#include <TGUI/Loading/WidgetLoader.hpp>
|
||||
#include <TGUI/Widgets/Button.hpp>
|
||||
#include <TGUI/Widgets/Canvas.hpp>/**
|
||||
#include <TGUI/Widgets/ChatBox.hpp>*/
|
||||
#include <TGUI/Widgets/Canvas.hpp>
|
||||
#include <TGUI/Widgets/ChatBox.hpp>
|
||||
#include <TGUI/Widgets/CheckBox.hpp>/**
|
||||
#include <TGUI/Widgets/ChildWindow.hpp>*/
|
||||
#include <TGUI/Widgets/ComboBox.hpp>
|
||||
|
@ -247,7 +247,7 @@ namespace tgui
|
|||
else
|
||||
return loadWidget(node, std::make_shared<Canvas>());
|
||||
}
|
||||
/**
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TGUI_API Widget::Ptr loadChatBox(std::shared_ptr<DataIO::Node> node, Widget::Ptr widget = nullptr)
|
||||
|
@ -267,13 +267,6 @@ namespace tgui
|
|||
if (node->propertyValuePairs["linelimit"])
|
||||
chatBox->setLineLimit(tgui::stoi(node->propertyValuePairs["linelimit"]->value));
|
||||
|
||||
for (auto& childNode : node->children)
|
||||
{
|
||||
if (toLower(childNode->name) == "scrollbar")
|
||||
chatBox->setScrollbar(std::static_pointer_cast<Scrollbar>(WidgetLoader::getLoadFunction("scrollbar")(childNode)));
|
||||
}
|
||||
REMOVE_CHILD("scrollbar");
|
||||
|
||||
for (auto& childNode : node->children)
|
||||
{
|
||||
if (toLower(childNode->name) == "line")
|
||||
|
@ -305,7 +298,7 @@ namespace tgui
|
|||
|
||||
return chatBox;
|
||||
}
|
||||
*/
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TGUI_API Widget::Ptr loadCheckBox(std::shared_ptr<DataIO::Node> node, Widget::Ptr widget = nullptr)
|
||||
|
@ -877,8 +870,8 @@ namespace tgui
|
|||
{"widget", std::bind(loadWidget, std::placeholders::_1, std::shared_ptr<Widget>{})},
|
||||
{"container", std::bind(loadContainer, std::placeholders::_1, std::shared_ptr<Container>{})},
|
||||
{"button", std::bind(loadButton, std::placeholders::_1, std::shared_ptr<Button>{})},
|
||||
{"canvas", std::bind(loadCanvas, std::placeholders::_1, std::shared_ptr<Canvas>{})},/**
|
||||
{"chatbox", std::bind(loadChatBox, std::placeholders::_1, std::shared_ptr<ChatBox>{})},*/
|
||||
{"canvas", std::bind(loadCanvas, std::placeholders::_1, std::shared_ptr<Canvas>{})},
|
||||
{"chatbox", std::bind(loadChatBox, std::placeholders::_1, std::shared_ptr<ChatBox>{})},
|
||||
{"checkbox", std::bind(loadCheckBox, std::placeholders::_1, std::shared_ptr<CheckBox>{})},/**
|
||||
{"childwindow", std::bind(loadChildWindow, std::placeholders::_1, std::shared_ptr<ChildWindow>{})},*/
|
||||
{"clickablewidget", std::bind(loadClickableWidget, std::placeholders::_1, std::shared_ptr<ClickableWidget>{})},
|
||||
|
|
|
@ -25,8 +25,8 @@
|
|||
|
||||
#include <TGUI/Loading/WidgetSaver.hpp>
|
||||
#include <TGUI/Loading/Serializer.hpp>
|
||||
#include <TGUI/Widgets/Button.hpp>/**
|
||||
#include <TGUI/Widgets/ChatBox.hpp>
|
||||
#include <TGUI/Widgets/Button.hpp>
|
||||
#include <TGUI/Widgets/ChatBox.hpp>/**
|
||||
#include <TGUI/Widgets/ChildWindow.hpp>*/
|
||||
#include <TGUI/Widgets/ComboBox.hpp>
|
||||
#include <TGUI/Widgets/EditBox.hpp>
|
||||
|
@ -154,7 +154,7 @@ namespace tgui
|
|||
SET_PROPERTY("TextSize", tgui::to_string(button->getTextSize()));
|
||||
return node;
|
||||
}
|
||||
/**
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TGUI_API std::shared_ptr<DataIO::Node> saveChatBox(ChatBox::Ptr chatBox)
|
||||
|
@ -195,16 +195,9 @@ namespace tgui
|
|||
node->children.push_back(lineNode);
|
||||
}
|
||||
|
||||
if (chatBox->getScrollbar())
|
||||
{
|
||||
node->children.push_back(WidgetSaver::getSaveFunction("scrollbar")(tgui::WidgetConverter{chatBox->getScrollbar()}));
|
||||
node->children.back()->parent = node.get();
|
||||
node->children.back()->name = "Scrollbar";
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TGUI_API std::shared_ptr<DataIO::Node> saveChildWindow(ChildWindow::Ptr childWindow)
|
||||
|
@ -555,8 +548,8 @@ namespace tgui
|
|||
{"widget", saveWidget},
|
||||
{"container", saveContainer},
|
||||
{"button", saveButton},
|
||||
{"canvas", saveWidget},/**
|
||||
{"chatbox", saveChatBox},*/
|
||||
{"canvas", saveWidget},
|
||||
{"chatbox", saveChatBox},
|
||||
{"checkbox", saveRadioButton},/**
|
||||
{"childwindow", saveChildWindow},*/
|
||||
{"clickablewidget", saveWidget},
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// TGUI - Texus' Graphical User Interface
|
||||
// Copyright (C) 2012-2016 Bruno Van de Velde (vdv_b@tgui.eu)
|
||||
//
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,
|
||||
// including commercial applications, and to alter it and redistribute it freely,
|
||||
// subject to the following restrictions:
|
||||
//
|
||||
// 1. The origin of this software must not be misrepresented;
|
||||
// you must not claim that you wrote the original software.
|
||||
// If you use this software in a product, an acknowledgment
|
||||
// in the product documentation would be appreciated but is not required.
|
||||
//
|
||||
// 2. Altered source versions must be plainly marked as such,
|
||||
// and must not be misrepresented as being the original software.
|
||||
//
|
||||
// 3. This notice may not be removed or altered from any source distribution.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#include <TGUI/Renderers/ChatBoxRenderer.hpp>
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace tgui
|
||||
{
|
||||
TGUI_RENDERER_PROPERTY_OUTLINE(ChatBoxRenderer, Borders)
|
||||
TGUI_RENDERER_PROPERTY_OUTLINE(ChatBoxRenderer, Padding)
|
||||
|
||||
TGUI_RENDERER_PROPERTY_COLOR(ChatBoxRenderer, BackgroundColor, sf::Color::White)
|
||||
TGUI_RENDERER_PROPERTY_COLOR(ChatBoxRenderer, BorderColor, sf::Color::Black)
|
||||
|
||||
TGUI_RENDERER_PROPERTY_TEXTURE(ChatBoxRenderer, TextureBackground)
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ChatBoxRenderer::setScrollbar(std::shared_ptr<RendererData> texture)
|
||||
{
|
||||
setProperty("scrollbar", {texture});
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::shared_ptr<RendererData> ChatBoxRenderer::getScrollbar() const
|
||||
{
|
||||
auto it = m_data->propertyValuePairs.find("scrollbar");
|
||||
if (it != m_data->propertyValuePairs.end())
|
||||
return it->second.getRenderer();
|
||||
else
|
||||
{
|
||||
m_data->propertyValuePairs["scrollbar"] = {std::make_shared<RendererData>()};
|
||||
return m_data->propertyValuePairs["scrollbar"].getRenderer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@ -221,6 +221,98 @@ namespace tgui
|
|||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
sf::String Text::wordWrap(float maxWidth, const sf::String& text, Font font, unsigned int textSize, bool bold, bool dropLeadingSpace)
|
||||
{
|
||||
if (font == nullptr)
|
||||
return "";
|
||||
|
||||
sf::String result;
|
||||
std::size_t index = 0;
|
||||
while (index < text.getSize())
|
||||
{
|
||||
std::size_t oldIndex = index;
|
||||
|
||||
// Find out how many characters we can get on this line
|
||||
float width = 0;
|
||||
sf::Uint32 prevChar = 0;
|
||||
for (std::size_t i = index; i < text.getSize(); ++i)
|
||||
{
|
||||
float charWidth;
|
||||
sf::Uint32 curChar = text[i];
|
||||
if (curChar == '\n')
|
||||
{
|
||||
index++;
|
||||
break;
|
||||
}
|
||||
else if (curChar == '\t')
|
||||
charWidth = font.getFont()->getGlyph(' ', textSize, bold).bounds.width * 4;
|
||||
else
|
||||
charWidth = font.getFont()->getGlyph(curChar, textSize, bold).bounds.width;
|
||||
|
||||
float kerning = font.getFont()->getKerning(prevChar, curChar, textSize);
|
||||
if ((maxWidth == 0) || (width + charWidth + kerning <= maxWidth))
|
||||
{
|
||||
// The advance of a glyph can be larger than the width of its bound (e.g. space has width 0 but a positive advance)
|
||||
if (curChar == '\t')
|
||||
width += kerning + (font.getFont()->getGlyph(' ', textSize, bold).advance * 4);
|
||||
else
|
||||
width += kerning + font.getFont()->getGlyph(curChar, textSize, bold).advance;
|
||||
|
||||
index++;
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
||||
prevChar = curChar;
|
||||
}
|
||||
|
||||
// Every line contains at least one character
|
||||
if (index == oldIndex)
|
||||
index++;
|
||||
|
||||
// Implement the word-wrap by removing the last few characters from the line
|
||||
if (text[index-1] != '\n')
|
||||
{
|
||||
std::size_t indexWithoutWordWrap = index;
|
||||
if ((index < text.getSize()) && (!isWhitespace(text[index])))
|
||||
{
|
||||
std::size_t wordWrapCorrection = 0;
|
||||
while ((index > oldIndex) && (!isWhitespace(text[index - 1])))
|
||||
{
|
||||
wordWrapCorrection++;
|
||||
index--;
|
||||
}
|
||||
|
||||
// The word can't be split but there is no other choice, it does not fit on the line
|
||||
if ((index - oldIndex) <= wordWrapCorrection)
|
||||
index = indexWithoutWordWrap;
|
||||
}
|
||||
}
|
||||
|
||||
// If the next line starts with just a space, then the space need not be visible
|
||||
if (dropLeadingSpace)
|
||||
{
|
||||
if ((index < text.getSize()) && (text[index] == ' '))
|
||||
{
|
||||
if ((index == 0) || (!isWhitespace(text[index-1])))
|
||||
{
|
||||
// But two or more spaces indicate that it is not a normal text and the spaces should not be ignored
|
||||
if (((index + 1 < text.getSize()) && (!isWhitespace(text[index + 1]))) || (index + 1 == text.getSize()))
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result += text.substring(oldIndex, index - oldIndex);
|
||||
if ((index < text.getSize()) && (text[index-1] != '\n'))
|
||||
result += "\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -248,8 +248,7 @@ namespace tgui
|
|||
|
||||
void Label::rearrangeText()
|
||||
{
|
||||
std::shared_ptr<sf::Font> font = getRenderer()->getFont();
|
||||
if (!font)
|
||||
if (getRenderer()->getFont() == nullptr)
|
||||
return;
|
||||
|
||||
// Find the maximum width of one line
|
||||
|
@ -258,83 +257,26 @@ namespace tgui
|
|||
maxWidth = m_maximumTextWidth;
|
||||
else
|
||||
{
|
||||
Borders borders = getRenderer()->getPadding() + getRenderer()->getBorders();
|
||||
if (getSize().x > borders.left + borders.right)
|
||||
maxWidth = getSize().x - getRenderer()->getPadding().left - getRenderer()->getPadding().right;
|
||||
Borders borders = getRenderer()->getBorders();
|
||||
Padding padding = getRenderer()->getPadding();
|
||||
if (getSize().x > borders.left + borders.right + padding.left + padding.right)
|
||||
maxWidth = getSize().x - borders.left - borders.right - padding.left - padding.right;
|
||||
else // There is no room for text
|
||||
return;
|
||||
}
|
||||
|
||||
// Split the text over multiple lines if needed
|
||||
// Fit the text in the available space
|
||||
sf::String string = Text::wordWrap(maxWidth, m_string, getRenderer()->getFont(), m_textSize, m_textStyle & sf::Text::Bold);
|
||||
|
||||
// Split the string in multiple lines
|
||||
m_lines.clear();
|
||||
unsigned int index = 0;
|
||||
unsigned int lineCount = 0;
|
||||
float calculatedLabelWidth = 0;
|
||||
bool bold = (m_textStyle & sf::Text::Bold) != 0;
|
||||
while (index < m_string.getSize())
|
||||
float width = 0;
|
||||
std::size_t searchPosStart = 0;
|
||||
std::size_t newLinePos;
|
||||
do
|
||||
{
|
||||
lineCount++;
|
||||
unsigned int oldIndex = index;
|
||||
newLinePos = string.find('\n', searchPosStart);
|
||||
|
||||
float width = 0;
|
||||
sf::Uint32 prevChar = 0;
|
||||
for (std::size_t i = index; i < m_string.getSize(); ++i)
|
||||
{
|
||||
float charWidth;
|
||||
sf::Uint32 curChar = m_string[i];
|
||||
if (curChar == '\n')
|
||||
{
|
||||
index++;
|
||||
break;
|
||||
}
|
||||
else if (curChar == '\t')
|
||||
charWidth = static_cast<float>(font->getGlyph(' ', m_textSize, bold).textureRect.width) * 4;
|
||||
else
|
||||
charWidth = static_cast<float>(font->getGlyph(curChar, m_textSize, bold).textureRect.width);
|
||||
|
||||
float kerning = static_cast<float>(font->getKerning(prevChar, curChar, m_textSize));
|
||||
if ((maxWidth == 0) || (width + charWidth + kerning <= maxWidth))
|
||||
{
|
||||
if (curChar == '\t')
|
||||
width += (static_cast<float>(font->getGlyph(' ', m_textSize, bold).advance) * 4) + kerning;
|
||||
else
|
||||
width += static_cast<float>(font->getGlyph(curChar, m_textSize, bold).advance) + kerning;
|
||||
|
||||
index++;
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
||||
prevChar = curChar;
|
||||
}
|
||||
|
||||
calculatedLabelWidth = std::max(calculatedLabelWidth, width);
|
||||
|
||||
// Every line contains at least one character
|
||||
if (index == oldIndex)
|
||||
index++;
|
||||
|
||||
// Implement the word-wrap
|
||||
if (m_string[index-1] != '\n')
|
||||
{
|
||||
unsigned int indexWithoutWordWrap = index;
|
||||
|
||||
if ((index < m_string.getSize()) && (!isWhitespace(m_string[index])))
|
||||
{
|
||||
unsigned int wordWrapCorrection = 0;
|
||||
while ((index > oldIndex) && (!isWhitespace(m_string[index - 1])))
|
||||
{
|
||||
wordWrapCorrection++;
|
||||
index--;
|
||||
}
|
||||
|
||||
// The word can't be split but there is no other choice, it does not fit on the line
|
||||
if ((index - oldIndex) <= wordWrapCorrection)
|
||||
index = indexWithoutWordWrap;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the next line
|
||||
m_lines.emplace_back();
|
||||
m_lines.back().setCharacterSize(getTextSize());
|
||||
m_lines.back().setFont(getRenderer()->getFont());
|
||||
|
@ -342,31 +284,26 @@ namespace tgui
|
|||
m_lines.back().setColor(getRenderer()->getTextColor());
|
||||
m_lines.back().setOpacity(getRenderer()->getOpacity());
|
||||
|
||||
if ((index < m_string.getSize()) && (m_string[index-1] != '\n'))
|
||||
m_lines.back().setString(m_string.substring(oldIndex, index - oldIndex) + "\n");
|
||||
if (newLinePos != sf::String::InvalidPos)
|
||||
m_lines.back().setString(string.substring(searchPosStart, newLinePos - searchPosStart));
|
||||
else
|
||||
m_lines.back().setString(m_string.substring(oldIndex, index - oldIndex));
|
||||
m_lines.back().setString(string.substring(searchPosStart));
|
||||
|
||||
// If the next line starts with just a space, then the space need not be visible
|
||||
if ((index < m_string.getSize()) && (m_string[index] == ' '))
|
||||
{
|
||||
if ((index == 0) || (!isWhitespace(m_string[index-1])))
|
||||
{
|
||||
// But two or more spaces indicate that it is not a normal text and the spaces should not be ignored
|
||||
if (((index + 1 < m_string.getSize()) && (!isWhitespace(m_string[index + 1]))) || (index + 1 == m_string.getSize()))
|
||||
index++;
|
||||
}
|
||||
}
|
||||
if (m_lines.back().getSize().x > width)
|
||||
width = m_lines.back().getSize().x;
|
||||
|
||||
searchPosStart = newLinePos + 1;
|
||||
}
|
||||
while (newLinePos != sf::String::InvalidPos);
|
||||
|
||||
// There is always at least one line
|
||||
lineCount = std::max(1u, lineCount);
|
||||
|
||||
std::shared_ptr<sf::Font> font = getRenderer()->getFont();
|
||||
Outline outline = getRenderer()->getPadding() + getRenderer()->getBorders();
|
||||
|
||||
// Update the size of the label
|
||||
if (m_autoSize)
|
||||
{
|
||||
m_size = {std::max(calculatedLabelWidth, maxWidth) + outline.left + outline.right,
|
||||
(lineCount * font->getLineSpacing(m_textSize)) + Text::calculateExtraVerticalSpace(font, m_textSize, m_textStyle) + outline.top + outline.bottom};
|
||||
m_size = {std::max(width, maxWidth) + outline.left + outline.right,
|
||||
(std::max<std::size_t>(m_lines.size(), 1) * font->getLineSpacing(m_textSize)) + Text::calculateExtraVerticalSpace(font, m_textSize, m_textStyle) + outline.top + outline.bottom};
|
||||
}
|
||||
|
||||
// Update the line positions
|
||||
|
|
|
@ -62,7 +62,7 @@ namespace tgui
|
|||
getRenderer()->setSelectedTextColor(sf::Color::White);
|
||||
|
||||
setSize({150, 154});
|
||||
setItemHeight(22);
|
||||
setItemHeight(m_itemHeight);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -314,7 +314,7 @@ namespace tgui
|
|||
bool Scrollbar::mouseOnWidget(float x, float y) const
|
||||
{
|
||||
// Don't make any calculations when no scrollbar is needed
|
||||
if ((m_maximum <= m_lowValue) && m_autoHide)
|
||||
if (m_autoHide && (m_maximum <= m_lowValue))
|
||||
return false;
|
||||
|
||||
return sf::FloatRect{0, 0, getSize().x, getSize().y}.contains(x, y);
|
||||
|
|
|
@ -1433,7 +1433,8 @@ namespace tgui
|
|||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// TODO: Word-wrap from Text class will add newlines to some places
|
||||
/// Remove these by looping over both the old and the new text to find where the real and where the inserted new lines are.
|
||||
void TextBox::rearrangeText(bool keepSelection)
|
||||
{
|
||||
// Don't continue when line height is 0 or when there is no font yet
|
||||
|
|
|
@ -23,7 +23,7 @@ set(TEST_SOURCES
|
|||
Loading/ThemeLoader.cpp
|
||||
Widgets/Button.cpp
|
||||
Widgets/Canvas.cpp
|
||||
#Widgets/ChatBox.cpp
|
||||
Widgets/ChatBox.cpp
|
||||
Widgets/CheckBox.cpp
|
||||
#Widgets/ChildWindow.cpp
|
||||
Widgets/ClickableWidget.cpp
|
||||
|
|
|
@ -56,14 +56,17 @@ void testSavingWidget(std::string name, std::shared_ptr<WidgetType> widget, bool
|
|||
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile(name + "WidgetFile1.txt"));
|
||||
|
||||
parent->removeAllWidgets();
|
||||
parent = std::make_shared<tgui::GuiContainer>();
|
||||
REQUIRE_NOTHROW(parent->loadWidgetsFromFile(name + "WidgetFile1.txt"));
|
||||
SECTION("Saving normally")
|
||||
{
|
||||
parent->removeAllWidgets();
|
||||
parent = std::make_shared<tgui::GuiContainer>();
|
||||
REQUIRE_NOTHROW(parent->loadWidgetsFromFile(name + "WidgetFile1.txt"));
|
||||
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile(name + "WidgetFile2.txt"));
|
||||
REQUIRE(compareFiles(name + "WidgetFile1.txt", name + "WidgetFile2.txt"));
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile(name + "WidgetFile2.txt"));
|
||||
REQUIRE(compareFiles(name + "WidgetFile1.txt", name + "WidgetFile2.txt"));
|
||||
}
|
||||
|
||||
SECTION("Copying widget")
|
||||
SECTION("Copying widget before saving")
|
||||
{
|
||||
// Copy constructor
|
||||
WidgetType temp1(*widget);
|
||||
|
@ -71,6 +74,7 @@ void testSavingWidget(std::string name, std::shared_ptr<WidgetType> widget, bool
|
|||
// Assignment operator
|
||||
WidgetType temp2;
|
||||
temp2 = temp1;
|
||||
temp2 = temp2;
|
||||
|
||||
// Move constructor
|
||||
WidgetType temp3 = std::move(temp2);
|
||||
|
|
|
@ -23,24 +23,38 @@
|
|||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "../Tests.hpp"
|
||||
#include <TGUI/Widgets/Label.hpp>
|
||||
#include <TGUI/Widgets/ChatBox.hpp>
|
||||
|
||||
TEST_CASE("[ChatBox]") {
|
||||
TEST_CASE("[ChatBox]")
|
||||
{
|
||||
tgui::ChatBox::Ptr chatBox = std::make_shared<tgui::ChatBox>();
|
||||
chatBox->setFont("resources/DroidSansArmenian.ttf");
|
||||
chatBox->getRenderer()->setFont("resources/DroidSansArmenian.ttf");
|
||||
|
||||
SECTION("WidgetType") {
|
||||
SECTION("WidgetType")
|
||||
{
|
||||
REQUIRE(chatBox->getWidgetType() == "ChatBox");
|
||||
}
|
||||
|
||||
SECTION("adding lines") {
|
||||
SECTION("Position and Size")
|
||||
{
|
||||
chatBox->setPosition(40, 30);
|
||||
chatBox->setSize(150, 100);
|
||||
chatBox->getRenderer()->setBorders(2);
|
||||
|
||||
REQUIRE(chatBox->getPosition() == sf::Vector2f(40, 30));
|
||||
REQUIRE(chatBox->getSize() == sf::Vector2f(150, 100));
|
||||
REQUIRE(chatBox->getFullSize() == chatBox->getSize());
|
||||
REQUIRE(chatBox->getWidgetOffset() == sf::Vector2f(0, 0));
|
||||
}
|
||||
|
||||
SECTION("Adding lines")
|
||||
{
|
||||
std::shared_ptr<sf::Font> font1 = tgui::Font{"resources/DroidSansArmenian.ttf"}.getFont();
|
||||
std::shared_ptr<sf::Font> font2 = tgui::Font{"resources/DroidSansArmenian.ttf"}.getFont();
|
||||
|
||||
chatBox->setTextColor(sf::Color::Black);
|
||||
chatBox->setTextSize(24);
|
||||
chatBox->setFont(font1);
|
||||
chatBox->getRenderer()->setFont(font1);
|
||||
|
||||
REQUIRE(chatBox->getLineAmount() == 0);
|
||||
chatBox->addLine("Line 1");
|
||||
|
@ -76,7 +90,8 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLineFont(4) == font2);
|
||||
}
|
||||
|
||||
SECTION("removing lines") {
|
||||
SECTION("Removing lines")
|
||||
{
|
||||
REQUIRE(!chatBox->removeLine(0));
|
||||
chatBox->addLine("Line 1");
|
||||
chatBox->addLine("Line 2");
|
||||
|
@ -94,10 +109,12 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLineAmount() == 0);
|
||||
}
|
||||
|
||||
SECTION("line limit") {
|
||||
SECTION("Line limit")
|
||||
{
|
||||
REQUIRE(chatBox->getLineLimit() == 0);
|
||||
|
||||
SECTION("oldest on top") {
|
||||
SECTION("Oldest on top")
|
||||
{
|
||||
chatBox->addLine("Line 1");
|
||||
chatBox->addLine("Line 2");
|
||||
chatBox->addLine("Line 3");
|
||||
|
@ -114,7 +131,8 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLine(1) == "Line 4");
|
||||
}
|
||||
|
||||
SECTION("oldest at the bottom") {
|
||||
SECTION("Oldest at the bottom")
|
||||
{
|
||||
chatBox->setNewLinesBelowOthers(false);
|
||||
|
||||
chatBox->addLine("Line 1");
|
||||
|
@ -134,7 +152,8 @@ TEST_CASE("[ChatBox]") {
|
|||
}
|
||||
}
|
||||
|
||||
SECTION("default text size") {
|
||||
SECTION("Default text size")
|
||||
{
|
||||
chatBox->setTextSize(30);
|
||||
REQUIRE(chatBox->getTextSize() == 30);
|
||||
|
||||
|
@ -142,7 +161,8 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLineTextSize(0) == 30);
|
||||
}
|
||||
|
||||
SECTION("default text color") {
|
||||
SECTION("Default text color")
|
||||
{
|
||||
chatBox->setTextColor(sf::Color::Red);
|
||||
REQUIRE(chatBox->getTextColor() == sf::Color::Red);
|
||||
|
||||
|
@ -150,13 +170,14 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLineColor(0) == sf::Color::Red);
|
||||
}
|
||||
|
||||
SECTION("get unexisting line") {
|
||||
SECTION("Get unexisting line")
|
||||
{
|
||||
std::shared_ptr<sf::Font> font1 = tgui::Font{"resources/DroidSansArmenian.ttf"}.getFont();
|
||||
std::shared_ptr<sf::Font> font2 = tgui::Font{"resources/DroidSansArmenian.ttf"}.getFont();
|
||||
|
||||
chatBox->setTextColor(sf::Color::Yellow);
|
||||
chatBox->setTextSize(26);
|
||||
chatBox->setFont(font1);
|
||||
chatBox->getRenderer()->setFont(font1);
|
||||
|
||||
chatBox->addLine("Text", sf::Color::Blue, 20, font2);
|
||||
REQUIRE(chatBox->getLine(1) == "");
|
||||
|
@ -165,7 +186,8 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(chatBox->getLineFont(4) == font1);
|
||||
}
|
||||
|
||||
SECTION("lines start from top or bottom") {
|
||||
SECTION("Lines start from top or bottom")
|
||||
{
|
||||
REQUIRE(!chatBox->getLinesStartFromTop());
|
||||
chatBox->setLinesStartFromTop(true);
|
||||
REQUIRE(chatBox->getLinesStartFromTop());
|
||||
|
@ -173,98 +195,89 @@ TEST_CASE("[ChatBox]") {
|
|||
REQUIRE(!chatBox->getLinesStartFromTop());
|
||||
}
|
||||
|
||||
SECTION("Scrollbar") {
|
||||
tgui::Scrollbar::Ptr scrollbar = std::make_shared<tgui::Theme>()->load("scrollbar");
|
||||
REQUIRE(chatBox->getScrollbar() != nullptr);
|
||||
REQUIRE(chatBox->getScrollbar() != scrollbar);
|
||||
chatBox->setScrollbar(nullptr);
|
||||
REQUIRE(chatBox->getScrollbar() == nullptr);
|
||||
chatBox->setScrollbar(scrollbar);
|
||||
REQUIRE(chatBox->getScrollbar() != nullptr);
|
||||
REQUIRE(chatBox->getScrollbar() == scrollbar);
|
||||
SECTION("Events / Signals")
|
||||
{
|
||||
SECTION("Widget")
|
||||
{
|
||||
testWidgetSignals(chatBox);
|
||||
}
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
SECTION("Renderer") {
|
||||
testWidgetRenderer(chatBox->getRenderer());
|
||||
SECTION("Renderer")
|
||||
{
|
||||
auto renderer = chatBox->getRenderer();
|
||||
|
||||
SECTION("colored") {
|
||||
SECTION("set serialized property") {
|
||||
SECTION("colored")
|
||||
{
|
||||
auto scrollbarRendererData = std::make_shared<tgui::RendererData>();
|
||||
scrollbarRendererData->propertyValuePairs["trackcolor"] = {sf::Color::Red};
|
||||
scrollbarRendererData->propertyValuePairs["thumbcolor"] = {sf::Color::Blue};
|
||||
|
||||
SECTION("set serialized property")
|
||||
{
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BackgroundColor", "rgb(10, 20, 30)"));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BorderColor", "rgb(40, 50, 60)"));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Borders", "(1, 2, 3, 4)"));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Padding", "(5, 6, 7, 8)"));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Scrollbar", "{ TrackColor = Red; ThumbColor = Blue; }"));
|
||||
}
|
||||
|
||||
SECTION("set object property") {
|
||||
SECTION("set object property")
|
||||
{
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BackgroundColor", sf::Color{10, 20, 30}));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BorderColor", sf::Color{40, 50, 60}));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Borders", tgui::Borders{1, 2, 3, 4}));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Padding", tgui::Borders{5, 6, 7, 8}));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("Scrollbar", scrollbarRendererData));
|
||||
}
|
||||
|
||||
SECTION("functions") {
|
||||
SECTION("functions")
|
||||
{
|
||||
renderer->setBackgroundColor({10, 20, 30});
|
||||
renderer->setBorderColor({40, 50, 60});
|
||||
renderer->setBorders({1, 2, 3, 4});
|
||||
renderer->setPadding({5, 6, 7, 8});
|
||||
|
||||
SECTION("getPropertyValuePairs") {
|
||||
auto pairs = renderer->getPropertyValuePairs();
|
||||
REQUIRE(pairs.size() == 4);
|
||||
REQUIRE(pairs["BackgroundColor"].getColor() == sf::Color(10, 20, 30));
|
||||
REQUIRE(pairs["BorderColor"].getColor() == sf::Color(40, 50, 60));
|
||||
REQUIRE(pairs["Borders"].getOutline() == tgui::Borders(1, 2, 3, 4));
|
||||
REQUIRE(pairs["Padding"].getOutline() == tgui::Borders(5, 6, 7, 8));
|
||||
}
|
||||
renderer->setScrollbar(scrollbarRendererData);
|
||||
}
|
||||
|
||||
REQUIRE(renderer->getProperty("BackgroundColor").getColor() == sf::Color(10, 20, 30));
|
||||
REQUIRE(renderer->getProperty("BorderColor").getColor() == sf::Color(40, 50, 60));
|
||||
REQUIRE(renderer->getProperty("Borders").getOutline() == tgui::Borders(1, 2, 3, 4));
|
||||
REQUIRE(renderer->getProperty("Padding").getOutline() == tgui::Borders(5, 6, 7, 8));
|
||||
|
||||
scrollbarRendererData = renderer->getProperty("Scrollbar").getRenderer();
|
||||
REQUIRE(scrollbarRendererData->propertyValuePairs.size() == 2);
|
||||
REQUIRE(scrollbarRendererData->propertyValuePairs["trackcolor"].getColor() == sf::Color::Red);
|
||||
REQUIRE(scrollbarRendererData->propertyValuePairs["thumbcolor"].getColor() == sf::Color::Blue);
|
||||
}
|
||||
|
||||
SECTION("textured") {
|
||||
SECTION("textured")
|
||||
{
|
||||
tgui::Texture textureBackground("resources/Black.png", {0, 154, 48, 48}, {16, 16, 16, 16});
|
||||
|
||||
REQUIRE(!renderer->getProperty("BackgroundImage").getTexture().isLoaded());
|
||||
|
||||
SECTION("set serialized property") {
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BackgroundImage", tgui::Serializer::serialize(textureBackground)));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("TextureBackground", tgui::Serializer::serialize(textureBackground)));
|
||||
}
|
||||
|
||||
SECTION("set object property") {
|
||||
REQUIRE_NOTHROW(renderer->setProperty("BackgroundImage", textureBackground));
|
||||
REQUIRE_NOTHROW(renderer->setProperty("TextureBackground", textureBackground));
|
||||
}
|
||||
|
||||
SECTION("functions") {
|
||||
renderer->setBackgroundTexture(textureBackground);
|
||||
|
||||
SECTION("getPropertyValuePairs") {
|
||||
auto pairs = renderer->getPropertyValuePairs();
|
||||
REQUIRE(pairs.size() == 4);
|
||||
REQUIRE(pairs["BackgroundImage"].getTexture().getData() == textureBackground.getData());
|
||||
}
|
||||
renderer->setTextureBackground(textureBackground);
|
||||
}
|
||||
|
||||
REQUIRE(renderer->getProperty("BackgroundImage").getTexture().isLoaded());
|
||||
REQUIRE(renderer->getProperty("TextureBackground").getTexture().isLoaded());
|
||||
|
||||
REQUIRE(renderer->getProperty("BackgroundImage").getTexture().getData() == textureBackground.getData());
|
||||
REQUIRE(renderer->getTextureBackground().getData() == textureBackground.getData());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Saving and loading from file") {
|
||||
REQUIRE_NOTHROW(chatBox = std::make_shared<tgui::Theme>()->load("ChatBox"));
|
||||
|
||||
auto theme = std::make_shared<tgui::Theme>("resources/Black.txt");
|
||||
REQUIRE_NOTHROW(chatBox = theme->load("ChatBox"));
|
||||
REQUIRE(chatBox->getPrimaryLoadingParameter() == "resources/Black.txt");
|
||||
REQUIRE(chatBox->getSecondaryLoadingParameter() == "chatbox");
|
||||
|
||||
auto parent = std::make_shared<tgui::GuiContainer>();
|
||||
parent->add(chatBox);
|
||||
|
||||
chatBox->setOpacity(0.8f);
|
||||
SECTION("Saving and loading from file")
|
||||
{
|
||||
chatBox->setTextColor(sf::Color::White);
|
||||
chatBox->setTextSize(34);
|
||||
chatBox->setLineLimit(5);
|
||||
|
@ -275,29 +288,12 @@ TEST_CASE("[ChatBox]") {
|
|||
chatBox->addLine("L3", sf::Color::Magenta);
|
||||
chatBox->addLine("L1", sf::Color::Cyan, 36);
|
||||
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile("WidgetFileChatBox1.txt"));
|
||||
|
||||
parent->removeAllWidgets();
|
||||
REQUIRE_NOTHROW(parent->loadWidgetsFromFile("WidgetFileChatBox1.txt"));
|
||||
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile("WidgetFileChatBox2.txt"));
|
||||
REQUIRE(compareFiles("WidgetFileChatBox1.txt", "WidgetFileChatBox2.txt"));
|
||||
testSavingWidget("ChatBox", chatBox);
|
||||
|
||||
// Make sure that the lines are still in the correct order
|
||||
REQUIRE(chatBox->getLine(0) == "L1");
|
||||
REQUIRE(chatBox->getLine(1) == "L3");
|
||||
REQUIRE(chatBox->getLine(2) == "L4");
|
||||
REQUIRE(chatBox->getLine(3) == "L2");
|
||||
|
||||
SECTION("Copying widget") {
|
||||
tgui::ChatBox temp;
|
||||
temp = *chatBox;
|
||||
|
||||
parent->removeAllWidgets();
|
||||
parent->add(tgui::ChatBox::copy(std::make_shared<tgui::ChatBox>(temp)));
|
||||
|
||||
REQUIRE_NOTHROW(parent->saveWidgetsToFile("WidgetFileChatBox2.txt"));
|
||||
REQUIRE(compareFiles("WidgetFileChatBox1.txt", "WidgetFileChatBox2.txt"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -217,7 +217,7 @@ TEST_CASE("[ListBox]")
|
|||
REQUIRE(listBox->getItemCount() == 3);
|
||||
REQUIRE(listBox->getItems()[0] == "Item 1");
|
||||
REQUIRE(listBox->getItems()[2] == "Item 3");
|
||||
|
||||
|
||||
listBox->addItem("Item 6");
|
||||
REQUIRE(listBox->getItemCount() == 3);
|
||||
REQUIRE(listBox->getItems()[0] == "Item 1");
|
||||
|
|
|
@ -7,9 +7,9 @@ Button {
|
|||
}
|
||||
|
||||
ChatBox {
|
||||
BackgroundImage = "Black.png" Part(0, 154, 48, 48) Middle(16, 16, 16, 16);
|
||||
Scrollbar = &Scrollbar;
|
||||
Padding = (3, 3, 3, 3);
|
||||
TextureBackground = "Black.png" Part(0, 154, 48, 48) Middle(16, 16, 16, 16);
|
||||
Scrollbar = &Scrollbar;
|
||||
Padding = (3, 3, 3, 3);
|
||||
}
|
||||
|
||||
Checkbox {
|
||||
|
|
Loading…
Reference in New Issue