Improved internal text rendering and made some other changes

List of changes:
- Text auto sizing was improved
- Every widget now has a font and a setFont function. The setTextFont
and setGlobalFont functions were removed.
- You can now set a custom text size in ListBox
- ListBox now has an auto scroll option to move the scrollbar down when
a new item is added (enabled by default)
- setTexture functions were added to Picture to change the image after
it was loaded
- HorizontalLayout and VerticalLayout can now decently handle widgets
with borders
- All widgets with borders now correctly implement getFullSize
- Fixed loading MessageBox from a theme file
- Fixed positioning of LoadingBar front image from Black theme
- Added new tests
0.8
Bruno Van de Velde 2015-09-19 13:14:13 +02:00
parent 52f7921c48
commit b9d1590102
67 changed files with 1733 additions and 985 deletions

View File

@ -93,7 +93,7 @@ MessageBox {
ProgressBar {
BackImage : "Black.png" Part(180, 64, 90, 40) Middle(20, 0, 50, 40);
FrontImage : "Black.png" Part(180, 108, 82, 32) Middle(16, 0, 50, 32);
FrontImage : "Black.png" Part(180, 108, 90, 32) Middle(16, 0, 50, 32);
TextColorBack : rgb(190, 190, 190);
TextColorFront : rgb(250, 250, 250);
}
@ -137,7 +137,6 @@ Tab {
SelectedImage : "Black.png" Part(0, 32, 60, 32) Middle(16, 0, 28, 32);
TextColor : rgb(190, 190, 190);
SelectedTextColor : rgb(255, 255, 255);
BorderColor : rgb( 0, 0, 0);
DistanceToSide : 8;
}
@ -151,7 +150,7 @@ TextBox {
Scrollbar : "Scrollbar";
}
Tooltip {
Label.Tooltip {
TextColor : rgb(190, 190, 190);
BackgroundColor : rgb( 80, 80, 80);
BorderColor : rgb( 0, 0, 0);

View File

@ -23,7 +23,7 @@ int main(int argc, char *argv[])
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "");
tgui::Gui gui(window);
gui.setGlobalFont("fonts/DejaVuSans.ttf");
gui.setFont("fonts/DejaVuSans.ttf");
auto theme = std::make_shared<tgui::Theme>("widgets/Black.txt");
auto picLandscape = std::make_shared<tgui::Picture>("Background-Landscape.png");

View File

@ -7,7 +7,7 @@ int main()
window.setFramerateLimit(60);
tgui::Gui gui(window);
gui.setGlobalFont("../../fonts/DejaVuSans.ttf");
gui.setFont("../../fonts/DejaVuSans.ttf");
tgui::Theme::Ptr theme = std::make_shared<tgui::Theme>("../../widgets/Black.txt");
@ -186,7 +186,7 @@ int main()
sprite.setTexture(texture);
sprite.setScale(200.f / texture.getSize().x, 140.f / texture.getSize().y);
sf::Text text{"SFML Canvas", *gui.getGlobalFont(), 24};
sf::Text text{"SFML Canvas", *gui.getFont(), 24};
text.setPosition(25, 100);
text.setColor({200, 200, 200});

View File

@ -55,7 +55,7 @@ int main()
try
{
// Load the font
gui.setGlobalFont("../../fonts/DejaVuSans.ttf");
gui.setFont("../../fonts/DejaVuSans.ttf");
// Load the widgets
loadWidgets(gui);

View File

@ -4,7 +4,7 @@ int main()
{
sf::RenderWindow window(sf::VideoMode(400, 300), "TGUI window");
tgui::Gui gui(window);
gui.setGlobalFont("../../fonts/DejaVuSans.ttf");
gui.setFont("../../fonts/DejaVuSans.ttf");
// Create the background image
gui.add(std::make_shared<tgui::Picture>("../Linux.jpg"));

View File

@ -63,6 +63,17 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font the child widgets.
///
/// @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 Insert a widget to the layout.
///

View File

@ -79,41 +79,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
/// @brief Changes the font of the text in the widget and its children.
///
/// This font will be used by all widgets that are created after calling this function.
/// @param font The new font.
///
/// @param filename Path of the font file to load
///
/// @throw Exception when loading fails
/// When you don't call this function then the font from the parent widget will be used.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setGlobalFont(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
///
/// This font will be used by all widgets that are created after calling this function.
///
/// @param font Font to use
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setGlobalFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the global font.
///
/// This is the font that is used for newly created widget by default.
///
/// @return global font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getGlobalFont() const
{
return m_font;
}
virtual void setFont(const Font& font) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -320,7 +293,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getWidgetsOffset() const
{
return sf::Vector2f(0, 0);
return sf::Vector2f{0, 0};
}
@ -415,13 +388,6 @@ namespace tgui
virtual Widget::Ptr askToolTip(sf::Vector2f mousePos) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

111
include/TGUI/Font.hpp Normal file
View File

@ -0,0 +1,111 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus's Graphical User Interface
// Copyright (C) 2012-2015 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_FONT_HPP
#define TGUI_FONT_HPP
#include <TGUI/Global.hpp>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API Font
{
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Default constructor which will set the font to nullptr
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor which will explicitly set the font to nullptr
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font(std::nullptr_t);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor to create the font from a string (filename by default)
///
/// @param id String to pass to the Deserializer class to load the font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font(const std::string& id);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor to create the font from a string (filename by default)
///
/// @param id String to pass to the Deserializer class to load the font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font(const char* id);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor to share the font directly
///
/// @param font Font to share
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructor to set a copy of your font
///
/// @param font Font to copy
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font(const sf::Font& font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Get the underlying SFML font
///
/// @return Font stored in this object
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getFont() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
std::shared_ptr<sf::Font> m_font;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif // TGUI_TEXTURE_HPP

View File

@ -36,6 +36,7 @@
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -172,13 +173,28 @@ namespace tgui
/// @internal
// Returns the color with its alpha channel multiplied with the alpha parameter
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_API inline sf::Color calcColorOpacity(sf::Color color, float alpha)
{
if (alpha == 1)
return color;
else
return {color.r, color.g, color.b, static_cast<sf::Uint8>(color.a * alpha)};
}
TGUI_API sf::Color calcColorOpacity(sf::Color color, float alpha);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// The line spacing returned by sfml is correct but there is extra space on top.
// The text has to be moved up so that the line spacing really corresponds with the height of every line.
// This function returns the offset that the text has to be moved.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float getTextVerticalCorrection(std::shared_ptr<sf::Font> font, unsigned int characterSize, sf::Uint32 style = 0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/// @brief Finds the best character size for the text
///
/// @param font Font of the text
/// @param height Height that the text should fill
/// @param fit 0 to choose best fit, 1 to select font of at least that height, -1 to select font of maximum that height
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int findBestTextSize(std::shared_ptr<sf::Font> font, float height, int fit = 0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -192,44 +192,24 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
///
/// This font will be used by all widgets that are created after calling this function.
///
/// @param filename Path of the font file to load
///
/// @throw Exception when loading fails
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setGlobalFont(const std::string& filename)
{
m_container->setGlobalFont(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
///
/// This font will be used by all widgets that are created after calling this function.
///
/// @param font Font to use
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setGlobalFont(std::shared_ptr<sf::Font> font)
void setFont(const Font& font)
{
m_container->setGlobalFont(font);
m_container->setFont(font);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the global font.
///
/// This is the font that is used for newly created widget by default.
///
/// @return global font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getGlobalFont() const
std::shared_ptr<sf::Font> getFont() const
{
return m_container->getGlobalFont();
return m_container->getFont();
}

View File

@ -344,14 +344,33 @@ namespace tgui
{
for (auto& signalName : extractSignalNames(signalNames))
{
if (m_signals.find(toLower(signalName)) == m_signals.end())
throw Exception{"Cannot connect to unknown signal '" + signalName + "'."};
try {
m_signals[toLower(signalName)]->connect(++m_lastId, func, args...);
if (m_signals.find(toLower(signalName)) != m_signals.end())
{
try {
m_signals[toLower(signalName)]->connect(m_lastId, func, args...);
m_lastId++;
}
catch (const Exception& e) {
throw Exception{e.what() + (" The parameters are not valid for the '" + signalName + "' signal.")};
}
}
catch (const Exception& e) {
throw Exception{e.what() + (" The parameters are not valid for the '" + signalName + "' signal.")};
else
{
if (toLower(signalName) != "all")
throw Exception{"Cannot connect to unknown signal '" + signalName + "'."};
else
{
for (auto& signal : m_signals)
{
try {
signal.second->connect(m_lastId, func, args...);
m_lastId++;
}
catch (const Exception& e) {
throw Exception{e.what() + (" The parameters are not valid for the '" + signalName + "' signal.")};
}
}
}
}
}
@ -376,14 +395,33 @@ namespace tgui
{
for (auto& name : extractSignalNames(signalName))
{
if (m_signals.find(toLower(name)) == m_signals.end())
throw Exception{"Cannot connect to unknown signal '" + name + "'."};
try {
m_signals[toLower(name)]->connectEx(++m_lastId, func, args...);
if (m_signals.find(toLower(name)) != m_signals.end())
{
try {
m_signals[toLower(name)]->connectEx(m_lastId, func, args...);
m_lastId++;
}
catch (const Exception& e) {
throw Exception{e.what() + (" since it is not valid for the '" + name + "' signal.")};
}
}
catch (const Exception& e) {
throw Exception{e.what() + (" since it is not valid for the '" + name + "' signal.")};
else // Signal name does not exist
{
if (toLower(name) != "all")
throw Exception{"Cannot connect to unknown signal '" + name + "'."};
else
{
for (auto& signal : m_signals)
{
try {
signal.second->connectEx(m_lastId, func, args...);
m_lastId++;
}
catch (const Exception& e) {
throw Exception{e.what() + (" since it is not valid for the '" + name + "' signal.")};
}
}
}
}
}

View File

@ -31,6 +31,7 @@
#include <TGUI/Signal.hpp>
#include <TGUI/Transformable.hpp>
#include <TGUI/Texture.hpp>
#include <TGUI/Font.hpp>
#include <TGUI/Loading/Deserializer.hpp>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -390,16 +391,26 @@ namespace tgui
Widget::Ptr getToolTip();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @param font The new font.
///
/// When you don't call this function then the font from the parent widget will be used.
///
/// Some widget don't need a font and won't do anything when this function is called.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setFont(const Font& font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the font associated with the widget (if any)
///
/// @return Font used by widget
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getFont() const
{
return m_font;
}
std::shared_ptr<sf::Font> getFont() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -447,6 +458,20 @@ namespace tgui
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @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
{
return sf::Vector2f{0, 0};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called every frame with the time passed since the last frame.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -518,13 +543,6 @@ namespace tgui
virtual Widget::Ptr askToolTip(sf::Vector2f mousePos);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Makes a copy of the widget if you don't know its exact type
///
@ -540,6 +558,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Attach a theme to the widget
///
@ -705,10 +730,9 @@ namespace tgui
virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) = 0;
friend class Widget;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
friend class Widget;
};

View File

@ -110,6 +110,28 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the button
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the button
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 caption of the button.
///
@ -162,6 +184,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -186,12 +219,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///
@ -308,18 +335,6 @@ namespace tgui
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the color of the text.
///

View File

@ -192,7 +192,7 @@ namespace tgui
/// @param font Font of the text (nullptr to use default font)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, std::shared_ptr<sf::Font> font = nullptr);
void addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, const Font& font = nullptr);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -254,13 +254,12 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the default font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
/// @param font The new font.
///
/// @param font The new font
/// When you don't call this function then the font from the parent widget will be used.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
void setFont(const Font& font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -271,7 +270,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getTextFont()
{
return m_panel->getGlobalFont();
return m_panel->getFont();
}
@ -385,6 +384,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -420,12 +430,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void mouseNoLongerDown() override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const parent) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:

View File

@ -137,27 +137,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
/// @brief Changes the font of the text in the widget and its children.
///
/// This font will be used by all widgets that are created after calling this function.
/// @param font The new font.
///
/// @param filename Path of the font file to load
///
/// @throw Exception when loading fails
/// When you don't call this function then the font from the parent widget will be used.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setGlobalFont(const std::string& filename) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the global font.
///
/// This font will be used by all widgets that are created after calling this function.
///
/// @param font Font to use
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void setGlobalFont(std::shared_ptr<sf::Font> font) override;
virtual void setFont(const Font& font) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -335,13 +322,6 @@ namespace tgui
virtual void mouseNoLongerDown() override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
@ -582,6 +562,8 @@ namespace tgui
sf::Color m_backgroundColor;
sf::Color m_borderColor;
sf::String m_closeButtonClassName = "";
friend class ChildWindow;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -160,6 +160,17 @@ namespace tgui
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 number of items that are displayed in the list.
///
@ -539,6 +550,26 @@ namespace tgui
std::size_t getMaximumItems() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the text size of the items
///
/// @param textSize The size size of the text
///
/// By default (or when passing 0 to this function) the text will be auto-sized to nicely fit inside the combo box.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextSize(unsigned int textSize);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the text size of the items
///
/// @return The text size
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int getTextSize() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the opacity of the widget.
///
@ -548,6 +579,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -577,12 +619,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///
@ -887,18 +923,6 @@ namespace tgui
void setArrowDownHoverTexture(const Texture& texture);
//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the items.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the size of the borders.
///

View File

@ -132,6 +132,28 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the edit box
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the edit box
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 text of the editbox.
///
@ -381,6 +403,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -455,12 +488,6 @@ namespace tgui
void recalculateTextPositions();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Makes a copy of the widget
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -604,18 +631,6 @@ namespace tgui
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the padding of the edit box.
///

View File

@ -109,6 +109,17 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the knob
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the knob
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Set the start rotation, which is the place where the value should be minimal.
///
@ -279,6 +290,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -118,6 +118,28 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the label
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the label
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 text.
///
@ -143,30 +165,6 @@ namespace tgui
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the font of the text.
///
/// @return Pointer to the font that is currently being used.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> getTextFont() const
{
return m_font;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the style of the text
///
@ -293,10 +291,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// Tell the widget about its parent
/// @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 void initialize(Container *const container) override;
virtual sf::Vector2f getWidgetOffset() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -308,6 +310,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// Tell the widget about its parent
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///

View File

@ -157,6 +157,17 @@ namespace tgui
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 Adds an item to the list.
///
@ -438,9 +449,7 @@ namespace tgui
///
/// @param itemHeight The size of a single item in the list
///
/// @remarks
/// - This size is always a little big greater than the text size.
/// - When there is no scrollbar then the items will be removed when they no longer fit inside the list box.
/// @warning When there is no scrollbar then the items will be removed when they no longer fit inside the list box.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setItemHeight(unsigned int itemHeight);
@ -452,10 +461,30 @@ namespace tgui
/// @return The item height
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int getItemHeight() const
{
return m_itemHeight;
}
unsigned int getItemHeight() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the text size of the items
///
/// @param textSize The size size of the text
///
/// This will not change the height that each item has. By default (or when passing 0 to this function) the text will
/// be auto-sized to nicely fit inside this item height.
///
/// @see setItemHeight
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextSize(unsigned int textSize);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the text size of the items
///
/// @return The text size
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int getTextSize() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -483,6 +512,30 @@ namespace tgui
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes whether the list box scrolls to the bottom when a new item is added
///
/// @param autoScroll Should list box scroll to the bottom when new items are added?
///
/// Auto scrolling is enabled by default.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setAutoScroll(bool autoScroll);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the maximum items that the list box can contain.
///
/// @return The maximum items inside the list box.
/// If the function returns 0 then there is no limit.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool getAutoScroll() const
{
return m_autoScroll;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the opacity of the widget.
///
@ -492,6 +545,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -537,12 +601,6 @@ namespace tgui
void updateItemColors();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///
@ -593,8 +651,9 @@ namespace tgui
int m_hoveringItem = -1;
// The size must be stored
unsigned int m_itemHeight = 24;
unsigned int m_textSize = 19;
unsigned int m_itemHeight = 22;
unsigned int m_requestedTextSize = 0;
unsigned int m_textSize = 18;
// This will store the maximum number of items in the list box (zero by default, meaning that there is no limit)
std::size_t m_maxItems = 0;
@ -605,6 +664,8 @@ namespace tgui
// Will be set to true after the first click, but gets reset to false when the second click does not occur soon after
bool m_possibleDoubleClick = false;
bool m_autoScroll = true;
// ComboBox contains a list box internally and it should be able to adjust it.
friend class ComboBox;
friend class ListBoxRenderer;
@ -765,18 +826,6 @@ namespace tgui
void setBackgroundTexture(const Texture& texture);
//////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the items.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
//////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the padding of the list box.
///

View File

@ -112,6 +112,17 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 Adds a new menu.
///

View File

@ -105,6 +105,17 @@ namespace tgui
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget and its children.
///
/// @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 Change the text of the message box.
///
@ -164,13 +175,6 @@ namespace tgui
void addButton(const sf::String& buttonCaption);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
@ -283,18 +287,6 @@ namespace tgui
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the color of the text.
///

View File

@ -92,13 +92,60 @@ namespace tgui
/// @brief Constructor to create the picture from an image
///
/// @param texture The texture to load the picture from
/// @param fullyClickable This affects what happens when clicking on a transparent pixel in the image.
/// Is the click caught by the picture, or does the event pass to the widgets behind it?
///
/// @code
/// auto picture = std::make_shared<tgui::Picture>(tgui::Texture{"image.png", {10, 10, 80, 80}});
/// @endcode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Picture(const Texture& texture);
Picture(const Texture& texture, bool fullyClickable = true);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Change the image
///
/// @param filename The absolute or relative filename of the image that should be loaded
/// @param fullyClickable This affects what happens when clicking on a transparent pixel in the image.
/// Is the click caught by the picture, or does the event pass to the widgets behind it?
///
/// @throw Exception when the image could not be loaded (probably not found)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTexture(const std::string& filename, bool fullyClickable = true);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Change the image
///
/// @param texture The texture to load the picture from
///
/// Note that the texture will be copied, so any changes applied to it afterwards will not affect the picture.
///
/// @code
/// sf::Texture texture;
/// texture.loadFromFile("image.png", {10, 10, 80, 80});
/// picture->setTexture(texture);
/// @endcode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTexture(const sf::Texture& texture);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Change the image
///
/// @param texture The texture to load the picture from
/// @param fullyClickable This affects what happens when clicking on a transparent pixel in the image.
/// Is the click caught by the picture, or does the event pass to the widgets behind it?
///
/// @code
/// picture->setTexture({"image.png", {10, 10, 80, 80}});
/// @endcode
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTexture(const Texture& texture, bool fullyClickable = true);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -128,6 +128,28 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the progress bar
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the progress bar
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 Sets a minimum value.
///
@ -287,15 +309,20 @@ namespace tgui
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;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///

View File

@ -124,6 +124,17 @@ namespace tgui
virtual sf::Vector2f getFullSize() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 Checks the radio button.
///
@ -237,12 +248,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///
@ -368,18 +373,6 @@ namespace tgui
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the color of the text.
///

View File

@ -109,6 +109,17 @@ namespace tgui
using Transformable::setSize;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the full size of the slider
///
/// The size returned by this function includes the borders.
///
/// @return Full size of the slider
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets a minimum value.
///
@ -198,6 +209,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -228,6 +228,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -143,6 +143,17 @@ namespace tgui
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 Adds a new tab.
///
@ -376,6 +387,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -397,12 +419,6 @@ namespace tgui
void recalculateTabsWidth();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Reload the widget
///
@ -435,7 +451,9 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
unsigned int m_textSize = 0;
unsigned int m_requestedTextSize = 0;
unsigned int m_textSize = 22;
float m_maximumTabWidth = 0;
int m_selectedTab = -1;
@ -514,18 +532,6 @@ namespace tgui
virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the tabs.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Set the text color that will be used inside the tabs.
///

View File

@ -159,6 +159,17 @@ namespace tgui
virtual sf::Vector2f getFullSize() const override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text in the widget.
///
/// @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 text of the text box.
///
@ -300,6 +311,17 @@ namespace tgui
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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -401,12 +423,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called when the widget is added to a container.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual void initialize(Container *const container) override;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// This function is called every frame with the time passed since the last frame.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -448,7 +464,7 @@ namespace tgui
sf::String m_text;
unsigned int m_textSize = 18;
unsigned int m_lineHeight = 40;
unsigned int m_lineHeight = 24;
std::vector<sf::String> m_lines = std::vector<sf::String>{""}; // Did not compile in VS2013 with just braces
@ -627,18 +643,6 @@ namespace tgui
void setBackgroundTexture(const Texture& texture);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the font of the text.
///
/// When you don't call this function then the global font will be use.
/// This global font can be changed with the setGlobalFont function from the parent.
///
/// @param font The new font
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setTextFont(std::shared_ptr<sf::Font> font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes the padding of the text box.
///

View File

@ -22,6 +22,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BoxLayout::setFont(const Font& font)
{
Panel::setFont(font);
updateWidgetPositions();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool BoxLayout::insert(std::size_t index, const tgui::Widget::Ptr& widget, const sf::String& widgetName)
{
Container::add(widget, widgetName);

View File

@ -4,6 +4,7 @@ set(TGUI_SRC
BoxLayout.cpp
Clipboard.cpp
Container.cpp
Font.cpp
Global.cpp
Gui.cpp
HorizontalLayout.cpp

View File

@ -81,18 +81,12 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::setGlobalFont(const std::string& filename)
void Container::setFont(const Font& font)
{
m_font = std::make_shared<sf::Font>();
if (!m_font->loadFromFile(getResourcePath() + filename))
throw Exception{"Failed to load font '" + filename + "'."};
}
Widget::setFont(font);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::setGlobalFont(std::shared_ptr<sf::Font> font)
{
m_font = font;
for (auto& widget : m_widgets)
widget->setFont(font);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -101,6 +95,10 @@ namespace tgui
{
assert(widgetPtr != nullptr);
// Let the widget inherit our font if it did not had a font yet
if (!widgetPtr->getFont() && getFont())
widgetPtr->setFont(getFont());
widgetPtr->initialize(this);
m_widgets.push_back(widgetPtr);
m_objName.push_back(widgetName);
@ -626,21 +624,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
setGlobalFont(m_parent->getGlobalFont());
// Re-initialize any widget that was already added
// They were most likely not given a font yet, as we are only receiving a font now
for (auto& widget : m_widgets)
widget->initialize(this);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Container::update(sf::Time elapsedTime)
{
Widget::update(elapsedTime);

85
src/TGUI/Font.cpp Normal file
View File

@ -0,0 +1,85 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus's Graphical User Interface
// Copyright (C) 2012-2015 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/Font.hpp>
#include <TGUI/Loading/Deserializer.hpp>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font() :
m_font{nullptr}
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font(std::nullptr_t) :
m_font{nullptr}
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font(const std::string& id) :
m_font{Deserializer::deserialize(ObjectConverter::Type::Font, id).getFont()}
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font(const char* id) :
Font(std::string{id})
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font(std::shared_ptr<sf::Font> font) :
m_font{font}
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Font::Font(const sf::Font& font) :
m_font{std::make_shared<sf::Font>(font)}
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> Font::getFont() const
{
return m_font;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -177,6 +177,74 @@ namespace tgui
return tokens;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Color calcColorOpacity(sf::Color color, float alpha)
{
if (alpha == 1)
return color;
else
return {color.r, color.g, color.b, static_cast<sf::Uint8>(color.a * alpha)};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float getTextVerticalCorrection(std::shared_ptr<sf::Font> font, unsigned int characterSize, sf::Uint32 style)
{
if (!font)
return 0;
bool bold = (style & sf::Text::Bold) != 0;
// Calculate the height of the first line (char size = everything above baseline, height + top = part below baseline)
float lineHeight = characterSize
+ font->getGlyph('g', characterSize, bold).bounds.height
+ font->getGlyph('g', characterSize, bold).bounds.top;
// Get the line spacing sfml returns
float lineSpacing = font->getLineSpacing(characterSize);
// Calculate the offset of the text
return lineHeight - lineSpacing;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int findBestTextSize(std::shared_ptr<sf::Font> font, float height, int fit)
{
if (!font)
return 0;
if (height < 2)
return 1;
std::vector<unsigned int> textSizes(static_cast<std::size_t>(height));
for (std::size_t i = 0; i < static_cast<std::size_t>(height); ++i)
textSizes[i] = i + 1;
auto high = std::lower_bound(textSizes.begin(), textSizes.end(), height, [&font](unsigned int charSize, float h){ return font->getLineSpacing(charSize) < h; });
if (high == textSizes.end())
return height;
float highLineSpacing = font->getLineSpacing(*high);
if (highLineSpacing == height)
return *high;
auto low = high - 1;
float lowLineSpacing = font->getLineSpacing(*low);
if (fit < 0)
return *low;
else if (fit > 0)
return *high;
else
{
if (std::abs(height - lowLineSpacing) < std::abs(height - highLineSpacing))
return *low;
else
return *high;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

View File

@ -49,6 +49,17 @@ namespace tgui
m_layoutWidgets[i]->setSize((m_size.x - sumFixedSize) * m_widgetsRatio[i] / sumRatio, m_size.y);
currentRatio += m_widgetsRatio[i] / sumRatio;
}
// Correct the size for widgets that have borders around it or a text next to them
if (m_layoutWidgets[i]->getFullSize() != m_layoutWidgets[i]->getSize())
{
sf::Vector2f newSize = m_layoutWidgets[i]->getSize() - (m_layoutWidgets[i]->getFullSize() - m_layoutWidgets[i]->getSize());
if (newSize.x > 0 && newSize.y > 0)
{
m_layoutWidgets[i]->setSize(newSize);
m_layoutWidgets[i]->setPosition(m_layoutWidgets[i]->getPosition() + m_layoutWidgets[i]->getWidgetOffset());
}
}
}
}

View File

@ -77,9 +77,14 @@ namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_API ObjectConverter deserializeFont(const std::string& /*value*/)
TGUI_API ObjectConverter deserializeFont(const std::string& value)
{
return {std::shared_ptr<sf::Font>()}; /// TODO: Support deserializing fonts
if (value == "null" || value == "nullptr")
return std::shared_ptr<sf::Font>();
auto font = std::make_shared<sf::Font>();
font->loadFromFile(Deserializer::deserialize(ObjectConverter::Type::String, value).getString());
return font;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -32,16 +32,16 @@ namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_API std::string serializeEmptyObject(ObjectConverter&& )
TGUI_API std::string serializeEmptyObject(ObjectConverter&&)
{
throw Exception{"Can't serialize empty object"};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_API std::string serializeFont(ObjectConverter&& /*value*/)
TGUI_API std::string serializeFont(ObjectConverter&&)
{
return "FONT_PLACEHOLDER"; /// TODO: Support deserializing fonts
return "null";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -74,8 +74,7 @@ namespace tgui
if (widget->getOpacity() != 255)
SET_PROPERTY("Opacity", tgui::to_string(widget->getOpacity()));
/// TODO: Font
/// TODO: ToolTip
/// TODO: Font and ToolTip
if (widget->getRenderer())
{

View File

@ -49,6 +49,17 @@ namespace tgui
m_layoutWidgets[i]->setSize(m_size.x, (m_size.y - sumFixedSize) * m_widgetsRatio[i] / sumRatio);
currentRatio += m_widgetsRatio[i] / sumRatio;
}
// Correct the size for widgets that have borders around it or a text next to them
if (m_layoutWidgets[i]->getFullSize() != m_layoutWidgets[i]->getSize())
{
sf::Vector2f newSize = m_layoutWidgets[i]->getSize() - (m_layoutWidgets[i]->getFullSize() - m_layoutWidgets[i]->getSize());
if (newSize.x > 0 && newSize.y > 0)
{
m_layoutWidgets[i]->setSize(newSize);
m_layoutWidgets[i]->setPosition(m_layoutWidgets[i]->getPosition() + m_layoutWidgets[i]->getWidgetOffset());
}
}
}
}

View File

@ -383,6 +383,20 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Widget::setFont(const Font& font)
{
m_font = font.getFont();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<sf::Font> Widget::getFont() const
{
return m_font;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Widget::detachTheme()
{
if (m_theme)
@ -542,7 +556,6 @@ namespace tgui
void WidgetRenderer::setProperty(std::string property, const std::string&)
{
/// TODO: Font in every widget
throw Exception{"Could not set property '" + property + "', widget does not has this property."};
}
@ -550,16 +563,6 @@ namespace tgui
void WidgetRenderer::setProperty(std::string property, ObjectConverter&&)
{
/// TODO: Font in every widget
/**
if (value.getType() == ObjectConverter::Type::Font)
{
if (property == "font")
setTextFont(value.getFont());
else
return false;
}
*/
throw Exception{"Could not set property '" + property + "', widget does not has this property."};
}
@ -567,7 +570,6 @@ namespace tgui
ObjectConverter WidgetRenderer::getProperty(std::string) const
{
/// TODO: Font in every widget
return {};
}
@ -575,7 +577,6 @@ namespace tgui
std::map<std::string, ObjectConverter> WidgetRenderer::getPropertyValuePairs() const
{
/// TODO: Font in every widget
return std::map<std::string, ObjectConverter>{};
}

View File

@ -92,6 +92,23 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Button::getFullSize() const
{
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Button::setFont(const Font& font)
{
Widget::setFont(font);
m_text.setFont(font);
setText(m_string);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Button::setText(const sf::String& text)
{
m_string = text;
@ -116,13 +133,12 @@ namespace tgui
// Auto size the text when necessary
if (m_textSize == 0)
{
// Calculate a possible text size
float size = getSize().x * 0.75f;
m_text.setTextSize(static_cast<unsigned int>(size));
unsigned int textSize = findBestTextSize(getFont(), getSize().x * 0.85f);
m_text.setTextSize(textSize);
// Make the text smaller when it's too high
if (m_text.getSize().y > (getSize().y * 0.8f))
m_text.setTextSize(static_cast<unsigned int>(size * getSize().y * 0.8f / m_text.getSize().y));
if (m_text.getSize().y > (getSize().y * 0.85f))
m_text.setTextSize(static_cast<unsigned int>(textSize * getSize().y * 0.85f / m_text.getSize().y));
}
}
else // The width of the button is big enough
@ -132,13 +148,12 @@ namespace tgui
// Auto size the text when necessary
if (m_textSize == 0)
{
// Calculate a possible text size
float size = getSize().y * 0.75f;
m_text.setTextSize(static_cast<unsigned int>(size));
unsigned int textSize = findBestTextSize(getFont(), getSize().y * 0.85f);
m_text.setTextSize(textSize);
// Make the text smaller when it's too width
if (m_text.getSize().x > (getSize().x * 0.8f))
m_text.setTextSize(static_cast<unsigned int>(size * getSize().x * 0.8f / m_text.getSize().x));
if (m_text.getSize().x > (getSize().x * 0.85f))
m_text.setTextSize(static_cast<unsigned int>(textSize * ((getSize().x * 0.85f) / m_text.getSize().x)));
}
}
@ -179,6 +194,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Button::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Button::leftMousePressed(float x, float y)
{
ClickableWidget::leftMousePressed(x, y);
@ -219,16 +241,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Button::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Button::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -458,17 +470,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ButtonRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_button->m_font = font;
m_button->m_text.setTextFont(font);
// Reposition the text
m_button->setText(m_button->m_string);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ButtonRenderer::setTextColor(const sf::Color& color)
{
setTextColorNormal(color);

View File

@ -43,10 +43,12 @@ namespace tgui
m_callback.widgetType = "ChatBox";
m_draggableWidget = true;
m_panel->setBackgroundColor(sf::Color::Transparent);
m_renderer = std::make_shared<ChatBoxRenderer>(this);
reload();
setSize({200, 120});
setSize({200, 126});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -100,15 +102,11 @@ namespace tgui
void ChatBox::setPosition(const Layout2d& position)
{
sf::Vector2f oldPosition = getPosition();
Widget::setPosition(position);
getRenderer()->m_backgroundTexture.setPosition(getPosition());
m_panel->move(getPosition() - oldPosition);
if (m_scroll)
m_scroll->move(getPosition() - oldPosition);
updateRendering();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -119,7 +117,8 @@ namespace tgui
getRenderer()->m_backgroundTexture.setSize(getSize());
updateRendering();
updatePosition();
recalculateFullTextHeight();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -153,7 +152,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChatBox::addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, std::shared_ptr<sf::Font> font)
void ChatBox::addLine(const sf::String& text, const sf::Color& color, unsigned int textSize, const Font& font)
{
// Remove the top line if you exceed the maximum
if ((m_maxLines > 0) && (m_maxLines < m_panel->getWidgets().size() + 1))
@ -165,8 +164,10 @@ namespace tgui
label->setText(text);
m_panel->add(label);
if (font != nullptr)
label->setTextFont(font);
if (font.getFont())
label->setFont(font.getFont());
else
label->setFont(getFont());
recalculateFullTextHeight();
@ -232,18 +233,17 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChatBox::setTextFont(std::shared_ptr<sf::Font> font)
void ChatBox::setFont(const Font& font)
{
m_font = font;
m_panel->setGlobalFont(font);
Widget::setFont(font);
bool lineChanged = false;
for (auto& label : m_panel->getWidgets())
{
auto line = std::static_pointer_cast<Label>(label);
if (line->getTextFont() == nullptr)
if (line->getFont() == nullptr)
{
line->setTextFont(font);
line->setFont(font);
lineChanged = true;
}
}
@ -340,6 +340,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f ChatBox::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChatBox::mouseOnWidget(float x, float y)
{
// Pass the event to the scrollbar (if there is one)
@ -387,7 +394,7 @@ namespace tgui
void ChatBox::leftMouseReleased(float x, float y)
{
// If there is a scrollbar then pass it the event
if (m_scroll != nullptr)
if (m_scroll && getFont())
{
// Only pass the event when the scrollbar still thinks the mouse is down
if (m_scroll->m_mouseDown == true)
@ -408,7 +415,7 @@ namespace tgui
m_scroll->setValue(m_scroll->getValue()-1);
// Scroll down with the whole item height instead of with a single pixel
m_scroll->setValue(m_scroll->getValue() + m_textSize - (m_scroll->getValue() % m_textSize));
m_scroll->setValue(m_scroll->getValue() + getFont()->getLineSpacing(m_textSize) - (std::fmod(m_scroll->getValue(), getFont()->getLineSpacing(m_textSize))));
}
else if (m_scroll->getValue() == oldValue - 1) // Check if the scrollbar value was decremented (you have pressed on the up arrow)
{
@ -416,10 +423,10 @@ namespace tgui
m_scroll->setValue(m_scroll->getValue()+1);
// Scroll up with the whole item height instead of with a single pixel
if (m_scroll->getValue() % m_textSize > 0)
m_scroll->setValue(m_scroll->getValue() - (m_scroll->getValue() % m_textSize));
if (std::fmod(m_scroll->getValue(), getFont()->getLineSpacing(m_textSize)) > 0)
m_scroll->setValue(m_scroll->getValue() - std::fmod(m_scroll->getValue(), getFont()->getLineSpacing(m_textSize)));
else
m_scroll->setValue(m_scroll->getValue() - m_textSize);
m_scroll->setValue(m_scroll->getValue() - getFont()->getLineSpacing(m_textSize));
}
updateDisplayedText();
@ -485,7 +492,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChatBox::mouseWheelMoved(int delta, int, int)
void ChatBox::mouseWheelMoved(int delta, int x, int y)
{
// Only do something when there is a scrollbar
if (m_scroll != nullptr)
@ -496,11 +503,11 @@ namespace tgui
if (delta < 0)
{
// Scroll down
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * m_textSize));
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * getFont()->getLineSpacing(m_textSize)));
}
else // You are scrolling up
{
unsigned int change = static_cast<unsigned int>(delta) * m_textSize;
unsigned int change = static_cast<unsigned int>(delta) * getFont()->getLineSpacing(m_textSize);
// Scroll up
if (change < m_scroll->getValue())
@ -510,46 +517,13 @@ namespace tgui
}
updateDisplayedText();
mouseMoved(static_cast<float>(x), static_cast<float>(y));
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChatBox::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
setTextFont(m_parent->getGlobalFont());
m_panel->initialize(m_parent);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float ChatBox::getLineSpacing(const Label::Ptr& line)
{
// If a line spacing was manually set then just return that one
if (m_lineSpacing > 0)
return line->getSize().y + m_lineSpacing - line->getTextSize();
float lineSpacing = line->getSize().y;
if (line->getTextFont())
{
lineSpacing += line->getTextFont()->getLineSpacing(line->getTextSize()) - line->getTextSize();
lineSpacing = std::max(lineSpacing, sf::Text{"kg", *line->getTextFont(), line->getTextSize()}.getLocalBounds().height);
}
if (lineSpacing > line->getSize().y)
return lineSpacing;
else
return line->getSize().y + std::ceil(line->getTextSize() * 3.5f / 10.0f);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChatBox::recalculateFullTextHeight()
{
m_fullTextHeight = 0;
@ -565,13 +539,9 @@ namespace tgui
else
label->setMaximumTextWidth(m_panel->getSize().x);
m_fullTextHeight += getLineSpacing(label);
m_fullTextHeight += label->getSize().y;
}
// There should be no space below the bottom line
auto lastLine = std::static_pointer_cast<Label>(m_panel->getWidgets().back());
m_fullTextHeight -= getLineSpacing(lastLine) - lastLine->getSize().y;
// Set the maximum of the scrollbar when there is one
if (m_scroll != nullptr)
m_scroll->setMaximum(static_cast<unsigned int>(m_fullTextHeight));
@ -593,17 +563,12 @@ namespace tgui
{
auto label = std::static_pointer_cast<Label>(m_panel->getWidgets()[i]);
// Not every line has the same height
float positionFix = 0;
if ((label->getTextFont()) && (label->getTextFont()->getGlyph('k', label->getTextSize(), false).bounds.height > label->getSize().y))
positionFix = label->getTextFont()->getGlyph('k', label->getTextSize(), false).bounds.height - label->getSize().y;
label->setPosition({0, positionY + positionFix});
positionY += getLineSpacing(label);
label->setPosition({0, positionY});
positionY += label->getSize().y;
}
// Display the last lines when there is no scrollbar
if (m_scroll == nullptr)
if (!m_scroll)
{
if (positionY > getSize().y)
{

View File

@ -153,24 +153,15 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChildWindow::setGlobalFont(const std::string& filename)
void ChildWindow::setFont(const Font& font)
{
Container::setGlobalFont(filename);
Container::setFont(font);
m_closeButton->getRenderer()->setTextFont(getGlobalFont());
m_titleText.setTextFont(getGlobalFont());
setTitle(getTitle());
}
m_closeButton->setFont(getFont());
m_titleText.setFont(getFont());
m_titleText.setTextSize(findBestTextSize(getFont(), getRenderer()->m_titleBarHeight * 0.85f));
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChildWindow::setGlobalFont(std::shared_ptr<sf::Font> font)
{
Container::setGlobalFont(font);
m_closeButton->getRenderer()->setTextFont(getGlobalFont());
m_titleText.setTextFont(getGlobalFont());
setTitle(getTitle());
updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -475,15 +466,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChildWindow::initialize(Container *const parent)
{
Container::initialize(parent);
m_closeButton->initialize(this);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ChildWindow::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -494,7 +476,13 @@ namespace tgui
if (force)
{
if (getRenderer()->m_textureTitleBar.isLoaded())
{
getRenderer()->m_titleBarHeight = getRenderer()->m_textureTitleBar.getImageSize().y;
getRenderer()->m_textureTitleBar.setSize({getRenderer()->m_textureTitleBar.getSize().x, getRenderer()->m_titleBarHeight});
if (m_closeButton->getRenderer()->m_textureNormal.isLoaded())
m_closeButton->setSize(m_closeButton->getRenderer()->m_textureNormal.getImageSize());
}
}
}
else // Load white theme
@ -519,7 +507,7 @@ namespace tgui
}
// Set the size of the title text
m_titleText.setTextSize(static_cast<unsigned int>(getRenderer()->m_titleBarHeight * 0.8f));
m_titleText.setTextSize(findBestTextSize(getFont(), getRenderer()->m_titleBarHeight * 0.85f));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -646,13 +634,26 @@ namespace tgui
setTitleBarHeight(Deserializer::deserialize(ObjectConverter::Type::Number, value).getNumber());
else if (property == "closebutton")
{
if (m_childWindow->getTheme() == nullptr)
throw Exception{"Failed to load the internal list box, ComboBox has no connected theme to load the list box with"};
if (toLower(value) == "default")
if (value.empty() || toLower(value) == "default")
{
m_closeButtonClassName = "";
m_childWindow->m_closeButton = std::make_shared<Button>();
}
else
{
m_closeButtonClassName = Deserializer::deserialize(ObjectConverter::Type::String, value).getString();
/// TODO: Widget files do not contain themes yet. This means that child window cannot be loaded from one.
/// Temporarily load default close button in case it is attempted.
if (m_childWindow->getTheme() == nullptr)
{
m_childWindow->m_closeButton = std::make_shared<Button>();
return;
}
if (m_childWindow->getTheme() == nullptr)
throw Exception{"Failed to load the close button, ChildWindow has no connected theme to load the close button with"};
m_childWindow->m_closeButton = m_childWindow->getTheme()->internalLoad(
m_childWindow->getPrimaryLoadingParameter(),
Deserializer::deserialize(ObjectConverter::Type::String, value).getString()
@ -703,6 +704,31 @@ namespace tgui
else if (property == "titlebarheight")
setTitleBarHeight(value.getNumber());
}
else if (value.getType() == ObjectConverter::Type::String)
{
if (property == "closebutton")
{
m_closeButtonClassName = value.getString();
if (value.getString().isEmpty())
m_childWindow->m_closeButton = std::make_shared<Button>();
else
{
/// TODO: Widget files do not contain themes yet. This means that child window cannot be loaded from one.
/// Temporarily load default close button in case it is attempted.
if (m_childWindow->getTheme() == nullptr)
{
m_childWindow->m_closeButton = std::make_shared<Button>();
return;
}
if (m_childWindow->getTheme() == nullptr)
throw Exception{"Failed to load the close button, ChildWindow has no connected theme to load the close button with"};
m_childWindow->m_closeButton = m_childWindow->getTheme()->internalLoad(m_childWindow->getPrimaryLoadingParameter(), value.getString());
}
}
}
else
WidgetRenderer::setProperty(property, std::move(value));
}
@ -729,6 +755,8 @@ namespace tgui
return m_distanceToSide;
else if (property == "titlebarheight")
return m_titleBarHeight;
else if (property == "closebutton")
return m_closeButtonClassName;
else
return WidgetRenderer::getProperty(property);
}
@ -744,6 +772,9 @@ namespace tgui
else
pairs["TitleBarColor"] = m_titleBarColor;
if (!m_closeButtonClassName.isEmpty())
pairs["CloseButton"] = m_closeButtonClassName;
pairs["BackgroundColor"] = m_backgroundColor;
pairs["TitleColor"] = m_titleColor;
pairs["BorderColor"] = m_borderColor;
@ -764,14 +795,21 @@ namespace tgui
void ChildWindowRenderer::setTitleBarHeight(float height)
{
// Set the size of the close button
if (m_titleBarHeight)
{
m_childWindow->m_closeButton->setSize({m_childWindow->m_closeButton->getSize().x * (height / m_titleBarHeight),
m_childWindow->m_closeButton->getSize().y * (height / m_titleBarHeight)});
}
else
{
m_childWindow->m_closeButton->setSize({height * 0.8f, height * 0.8f});
}
m_titleBarHeight = height;
// Set the size of the close button
m_childWindow->m_closeButton->setSize({m_childWindow->m_closeButton->getSize().x * (height / m_titleBarHeight),
m_childWindow->m_closeButton->getSize().y * (height / m_titleBarHeight)});
// Set the size of the text in the title bar
m_childWindow->m_titleText.setTextSize(static_cast<unsigned int>(m_titleBarHeight * 0.8f));
m_childWindow->m_titleText.setTextSize(findBestTextSize(m_childWindow->getFont(), m_titleBarHeight * 0.85f));
m_textureTitleBar.setSize({m_childWindow->getSize().x + m_borders.left + m_borders.right, m_titleBarHeight});
@ -798,7 +836,7 @@ namespace tgui
void ChildWindowRenderer::setBorders(const Borders& borders)
{
m_borders = borders;
WidgetBorders::setBorders(borders);
m_textureTitleBar.setSize({m_childWindow->getSize().x + borders.left + borders.right, m_titleBarHeight});

View File

@ -47,7 +47,7 @@ namespace tgui
m_renderer = std::make_shared<ComboBoxRenderer>(this);
reload();
setSize({50, 24});
setSize({150, 24});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -108,14 +108,8 @@ namespace tgui
getRenderer()->m_textureArrowUpHover.setPosition(getRenderer()->m_textureArrowUpNormal.getPosition());
getRenderer()->m_textureArrowDownHover.setPosition(getRenderer()->m_textureArrowUpNormal.getPosition());
float textHeight;
if (m_font)
textHeight = sf::Text{"kg", *m_font, m_text.getTextSize()}.getLocalBounds().height;
else
textHeight = 0;
m_text.setPosition(getPosition().x + padding.left + (m_text.getTextSize() / 10.0f),
getPosition().y + padding.top + ((getSize().y - textHeight) / 2.0f));
m_text.setPosition(getPosition().x + padding.left,
getPosition().y + padding.top + (((getSize().y - padding.top - padding.bottom) - m_text.getSize().y) / 2.0f));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -146,7 +140,7 @@ namespace tgui
getRenderer()->m_textureArrowDownHover.setSize(getRenderer()->m_textureArrowUpNormal.getSize());
}
m_text.setTextSize(static_cast<unsigned int>(height * 0.8f));
m_text.setTextSize(m_listBox->getTextSize());
updatePosition();
}
@ -161,6 +155,19 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::setFont(const Font& font)
{
Widget::setFont(font);
m_text.setFont(font);
m_listBox->setFont(font);
m_text.setTextSize(m_listBox->getTextSize());
updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::setItemsToDisplay(std::size_t nrOfItemsInList)
{
m_nrOfItemsToDisplay = nrOfItemsInList;
@ -387,6 +394,21 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::setTextSize(unsigned int textSize)
{
m_listBox->setTextSize(textSize);
m_text.setTextSize(m_listBox->getTextSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int ComboBox::getTextSize() const
{
return m_listBox->getTextSize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::setOpacity(float opacity)
{
Widget::setOpacity(opacity);
@ -403,6 +425,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f ComboBox::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ComboBox::mouseOnWidget(float x, float y)
{
// Check if the mouse is on top of the combo box
@ -488,18 +517,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
m_text.initialize(parent);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBox::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -510,7 +527,7 @@ namespace tgui
if (force)
{
if (getRenderer()->m_backgroundTexture.isLoaded())
setSize(getRenderer()->m_backgroundTexture.getImageSize());
setSize(5 * getRenderer()->m_backgroundTexture.getImageSize().x, getRenderer()->m_backgroundTexture.getImageSize().y);
}
updateSize();
@ -1007,16 +1024,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBoxRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_comboBox->m_font = font;
m_comboBox->m_text.setTextFont(font);
m_comboBox->updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ComboBoxRenderer::setBorders(const Borders& borders)
{
WidgetBorders::setBorders(borders);

View File

@ -104,17 +104,39 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f EditBox::getFullSize() const
{
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBox::setFont(const Font& font)
{
Widget::setFont(font);
if (font.getFont())
{
m_textBeforeSelection.setFont(*font.getFont());
m_textSelection.setFont(*font.getFont());
m_textAfterSelection.setFont(*font.getFont());
m_textFull.setFont(*font.getFont());
m_defaultText.setFont(*font.getFont());
}
// Recalculate the text size and position
setText(m_text);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBox::setText(const sf::String& text)
{
// Check if the text is auto sized
if (m_textSize == 0)
{
// Calculate the text size
m_textFull.setString("kg");
m_textFull.setCharacterSize(static_cast<unsigned int>((getSize().y - getRenderer()->getScaledPadding().bottom - getRenderer()->getScaledPadding().top) * 0.75f));
m_textFull.setString(m_displayedText);
// Also adjust the character size of the other texts
m_textFull.setCharacterSize(findBestTextSize(getFont(), (getSize().y - getRenderer()->getScaledPadding().bottom - getRenderer()->getScaledPadding().top) * 0.85f));
m_textBeforeSelection.setCharacterSize(m_textFull.getCharacterSize());
m_textSelection.setCharacterSize(m_textFull.getCharacterSize());
m_textAfterSelection.setCharacterSize(m_textFull.getCharacterSize());
@ -122,11 +144,10 @@ namespace tgui
}
else // When the text has a fixed size
{
// Set the text size
m_textFull.setCharacterSize(m_textSize);
m_textBeforeSelection.setCharacterSize(m_textSize);
m_textSelection.setCharacterSize(m_textSize);
m_textAfterSelection.setCharacterSize(m_textSize);
m_textFull.setCharacterSize(m_textSize);
m_defaultText.setCharacterSize(m_textSize);
}
@ -408,6 +429,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f EditBox::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBox::leftMousePressed(float x, float y)
{
// Find the caret position
@ -1066,13 +1094,15 @@ namespace tgui
void EditBox::recalculateTextPositions()
{
float textX = getPosition().x;
float textY = getPosition().y;
Padding padding = getRenderer()->getScaledPadding();
textX += padding.left - m_textCropPosition;
textY += padding.top;
float textX = getPosition().x + padding.left - m_textCropPosition;
float textY = 0;
if (getFont())
{
textY = std::round((getPosition().y + padding.top - getTextVerticalCorrection(getFont(), getTextSize()))
+ ((getSize().y - padding.bottom - padding.top) - getFont()->getLineSpacing(getTextSize())) / 2.f);
}
// Check if the layout wasn't left
if (m_textAlignment != Alignment::Left)
@ -1093,14 +1123,9 @@ namespace tgui
float caretLeft = textX;
// Set the position of the text
sf::Text tempText(m_textFull);
tempText.setString("kg");
textY += (((getSize().y - padding.top - padding.bottom) - tempText.getLocalBounds().height) * 0.5f) - tempText.getLocalBounds().top;
// Set the text before the selection on the correct position
m_textBeforeSelection.setPosition(std::floor(textX + 0.5f), std::floor(textY + 0.5f));
m_defaultText.setPosition(std::floor(textX + 0.5f), std::floor(textY + 0.5f));
m_textBeforeSelection.setPosition(std::floor(textX + 0.5f), textY);
m_defaultText.setPosition(std::floor(textX + 0.5f), textY);
// Check if there is a selection
if (m_selChars != 0)
@ -1117,7 +1142,7 @@ namespace tgui
m_selectedTextBackground.setPosition(std::floor(textX + 0.5f), std::floor(getPosition().y + padding.top + 0.5f));
// Set the text selected text on the correct position
m_textSelection.setPosition(std::floor(textX + 0.5f), std::floor(textY + 0.5f));
m_textSelection.setPosition(std::floor(textX + 0.5f), textY);
// Watch out for kerning
if (m_displayedText.getSize() > m_textBeforeSelection.getString().getSize() + m_textSelection.getString().getSize())
@ -1125,7 +1150,7 @@ namespace tgui
// Set the text selected text on the correct position
textX += m_textSelection.findCharacterPos(m_textSelection.getString().getSize()).x - m_textSelection.getPosition().x;
m_textAfterSelection.setPosition(std::floor(textX + 0.5f), std::floor(textY + 0.5f));
m_textAfterSelection.setPosition(std::floor(textX + 0.5f), textY);
}
// Set the position of the caret
@ -1135,16 +1160,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBox::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBox::update(sf::Time elapsedTime)
{
Widget::update(elapsedTime);
@ -1389,25 +1404,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBoxRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_editBox->m_font = font;
if (font != nullptr)
{
m_editBox->m_textBeforeSelection.setFont(*font);
m_editBox->m_textSelection.setFont(*font);
m_editBox->m_textAfterSelection.setFont(*font);
m_editBox->m_textFull.setFont(*font);
m_editBox->m_defaultText.setFont(*font);
}
// Recalculate the text size and position
m_editBox->setText(m_editBox->m_text);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void EditBoxRenderer::setPadding(const Padding& padding)
{
WidgetPadding::setPadding(padding);

View File

@ -88,6 +88,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Knob::getFullSize() const
{
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Knob::setStartRotation(float startRotation)
{
while (startRotation >= 360)
@ -208,6 +216,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Knob::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Knob::mouseOnWidget(float x, float y)
{
// Check if the mouse is on top of the widget

View File

@ -67,8 +67,8 @@ namespace tgui
m_background.setPosition(getPosition());
m_text.setPosition(std::floor(getPosition().x + getRenderer()->getPadding().left - m_text.getLocalBounds().left + 0.5f),
std::floor(getPosition().y + getRenderer()->getPadding().top - m_text.getLocalBounds().top + 0.5f));
m_text.setPosition(std::round(getPosition().x + getRenderer()->getPadding().left),
std::floor(getPosition().y + getRenderer()->getPadding().top - getTextVerticalCorrection(getFont(), getTextSize(), m_text.getStyle())));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -87,9 +87,20 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::setText(const sf::String& string)
sf::Vector2f Label::getFullSize() const
{
m_string = string;
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::setFont(const Font& font)
{
Widget::setFont(font);
if (font.getFont())
m_text.setFont(*font.getFont());
rearrangeText();
updatePosition();
@ -97,14 +108,12 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::setTextFont(std::shared_ptr<sf::Font> font)
void Label::setText(const sf::String& string)
{
m_font = font;
m_string = string;
if (font != nullptr)
m_text.setFont(*font);
setText(getText());
rearrangeText();
updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -188,14 +197,9 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::initialize(Container *const parent)
sf::Vector2f Label::getWidgetOffset() const
{
bool autoSize = getAutoSize();
Widget::initialize(parent);
setAutoSize(autoSize);
if (!getFont() && m_parent->getGlobalFont())
setTextFont(m_parent->getGlobalFont());
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -226,6 +230,15 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::initialize(Container *const parent)
{
bool autoSize = getAutoSize();
Widget::initialize(parent);
setAutoSize(autoSize);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Label::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -258,105 +271,108 @@ namespace tgui
void Label::rearrangeText()
{
if (getFont() == nullptr)
if (!getFont())
return;
// Only rearrange the text when a maximum width was given
if (!m_autoSize || m_maximumTextWidth > 0)
// Find the maximum width of one line
float maxWidth = 0;
if (m_autoSize)
maxWidth = m_maximumTextWidth;
else if (getSize().x > getRenderer()->getPadding().left + getRenderer()->getPadding().right)
maxWidth = getSize().x - getRenderer()->getPadding().left - getRenderer()->getPadding().right;
m_text.setString("");
unsigned int index = 0;
unsigned int lineCount = 0;
float calculatedLabelWidth = 0;
bool bold = (m_text.getStyle() & sf::Text::Bold) != 0;
while (index < m_string.getSize())
{
// Find the maximum width of one line
float maxWidth = 0;
if (m_autoSize)
maxWidth = m_maximumTextWidth;
else if (getSize().x > getRenderer()->getPadding().left + getRenderer()->getPadding().right)
maxWidth = getSize().x - getRenderer()->getPadding().left - getRenderer()->getPadding().right;
lineCount++;
unsigned int oldIndex = index;
m_text.setString("");
unsigned int index = 0;
while (index < m_string.getSize())
float width = 0;
sf::Uint32 prevChar = 0;
for (std::size_t i = index; i < m_string.getSize(); ++i)
{
unsigned int oldIndex = index;
float width = 0;
sf::Uint32 prevChar = 0;
for (unsigned int i = index; i < m_string.getSize(); ++i)
float charWidth;
sf::Uint32 curChar = m_string[i];
if (curChar == '\n')
{
float charWidth;
sf::Uint32 curChar = m_string[i];
if (curChar == '\n')
{
index++;
break;
}
else if (curChar == '\t')
charWidth = static_cast<float>(getFont()->getGlyph(' ', m_text.getCharacterSize(), false).textureRect.width) * 4;
else
charWidth = static_cast<float>(getFont()->getGlyph(curChar, m_text.getCharacterSize(), false).textureRect.width);
float kerning = static_cast<float>(getFont()->getKerning(prevChar, curChar, m_text.getCharacterSize()));
if (width + charWidth + kerning <= maxWidth)
{
if (curChar == '\t')
charWidth = static_cast<float>(getFont()->getGlyph(' ', m_text.getCharacterSize(), false).advance) * 4;
else
charWidth = static_cast<float>(getFont()->getGlyph(curChar, m_text.getCharacterSize(), false).advance);
width += charWidth + kerning;
index++;
}
else
break;
prevChar = curChar;
}
// 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;
}
break;
}
if ((index < m_string.getSize()) && (m_string[index-1] != '\n'))
m_text.setString(m_text.getString() + m_string.substring(oldIndex, index - oldIndex) + "\n");
else if (curChar == '\t')
charWidth = static_cast<float>(getFont()->getGlyph(' ', m_text.getCharacterSize(), bold).textureRect.width) * 4;
else
m_text.setString(m_text.getString() + m_string.substring(oldIndex, index - oldIndex));
charWidth = static_cast<float>(getFont()->getGlyph(curChar, m_text.getCharacterSize(), bold).textureRect.width);
// If the next line starts with just a space, then the space need not be visible
if ((index < m_string.getSize()) && (m_string[index] == ' '))
float kerning = static_cast<float>(getFont()->getKerning(prevChar, curChar, m_text.getCharacterSize()));
if ((maxWidth == 0) || (width + charWidth + kerning <= maxWidth))
{
if ((index == 0) || (!isWhitespace(m_string[index-1])))
if (curChar == '\t')
width += (static_cast<float>(getFont()->getGlyph(' ', m_text.getCharacterSize(), bold).advance) * 4) + kerning;
else
width += static_cast<float>(getFont()->getGlyph(curChar, m_text.getCharacterSize(), 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])))
{
if (((index + 1 < m_string.getSize()) && (!isWhitespace(m_string[index + 1]))) || (index + 1 == m_string.getSize()))
index++;
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 ((index < m_string.getSize()) && (m_string[index-1] != '\n'))
m_text.setString(m_text.getString() + m_string.substring(oldIndex, index - oldIndex) + "\n");
else
m_text.setString(m_text.getString() + m_string.substring(oldIndex, index - oldIndex));
// 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++;
}
}
}
else // There is no maximum width, so the text should not be changed
m_text.setString(m_string);
// There is always at least one line
lineCount = std::max(1u, lineCount);
if (m_autoSize)
{
m_size = {m_text.getLocalBounds().width + getRenderer()->getPadding().left + getRenderer()->getPadding().right,
m_text.getLocalBounds().height + getRenderer()->getPadding().top + getRenderer()->getPadding().bottom};
m_size = {std::max(calculatedLabelWidth, maxWidth) + getRenderer()->getPadding().left + getRenderer()->getPadding().right,
(lineCount * getFont()->getLineSpacing(m_text.getCharacterSize())) + getRenderer()->getPadding().top + getRenderer()->getPadding().bottom};
m_background.setSize(getSize());
}

View File

@ -49,8 +49,8 @@ namespace tgui
m_renderer = std::make_shared<ListBoxRenderer>(this);
reload();
setSize({150, 150});
setItemHeight(20);
setSize({150, 154});
setItemHeight(22);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -62,6 +62,7 @@ namespace tgui
m_selectedItem {listBoxToCopy.m_selectedItem},
m_hoveringItem {listBoxToCopy.m_hoveringItem},
m_itemHeight {listBoxToCopy.m_itemHeight},
m_requestedTextSize {listBoxToCopy.m_requestedTextSize},
m_textSize {listBoxToCopy.m_textSize},
m_maxItems {listBoxToCopy.m_maxItems},
m_scroll {Scrollbar::copy(listBoxToCopy.m_scroll)},
@ -83,6 +84,7 @@ namespace tgui
std::swap(m_selectedItem, temp.m_selectedItem);
std::swap(m_hoveringItem, temp.m_hoveringItem);
std::swap(m_itemHeight, temp.m_itemHeight);
std::swap(m_requestedTextSize, temp.m_requestedTextSize);
std::swap(m_textSize, temp.m_textSize);
std::swap(m_maxItems, temp.m_maxItems);
std::swap(m_scroll, temp.m_scroll);
@ -114,11 +116,10 @@ namespace tgui
if (m_font != nullptr)
{
float textHeight = sf::Text{"kg", *m_font, m_textSize}.getLocalBounds().height;
for (std::size_t i = 0; i < m_items.size(); ++i)
{
m_items[i].setPosition({getPosition().x + (textHeight / 10.0f) + padding.left,
getPosition().y + static_cast<float>(i * m_itemHeight) + ((m_itemHeight - textHeight) / 2.0f) + padding.top});
m_items[i].setPosition({getPosition().x + padding.left,
getPosition().y + padding.top + (i * m_itemHeight) + ((m_itemHeight - m_items[i].getSize().y) / 2.0f)});
if ((m_scroll != nullptr) && (m_scroll->getLowValue() < m_scroll->getMaximum()))
m_items[i].setPosition({m_items[i].getPosition().x, m_items[i].getPosition().y - m_scroll->getValue()});
@ -158,6 +159,26 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::setFont(const Font& font)
{
Widget::setFont(font);
for (auto& item : m_items)
item.setFont(font);
// Recalculate the text size with the new font
if (m_requestedTextSize == 0)
{
m_textSize = findBestTextSize(getFont(), m_itemHeight * 0.85f);
for (auto& item : m_items)
item.setTextSize(m_textSize);
}
updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ListBox::addItem(const sf::String& itemName, const sf::String& id)
{
// Check if the item limit is reached (if there is one)
@ -176,11 +197,15 @@ namespace tgui
else // There is a scrollbar so tell it that another item was added
{
m_scroll->setMaximum(static_cast<unsigned int>((m_items.size() + 1) * m_itemHeight));
// Scroll down when auto-scrolling is enabled
if (m_autoScroll && (m_scroll->getLowValue() < m_scroll->getMaximum()))
m_scroll->setValue(m_scroll->getMaximum() - m_scroll->getLowValue());
}
// Create the new item
Label newItem;
newItem.setTextFont(m_font);
newItem.setFont(getFont());
newItem.setTextColor(getRenderer()->m_textColor);
newItem.setTextSize(m_textSize);
newItem.setText(itemName);
@ -478,16 +503,14 @@ namespace tgui
void ListBox::setItemHeight(unsigned int itemHeight)
{
// There is a minimum height
if (itemHeight < 10)
itemHeight = 10;
// Set the new heights
m_itemHeight = itemHeight;
m_textSize = static_cast<unsigned int>(itemHeight * 0.8f);
for (auto& item : m_items)
item.setTextSize(m_textSize);
if (m_requestedTextSize == 0)
{
m_textSize = findBestTextSize(getFont(), itemHeight * 0.85f);
for (auto& item : m_items)
item.setTextSize(m_textSize);
}
// Some items might be removed when there is no scrollbar
if (m_scroll == nullptr)
@ -514,6 +537,37 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int ListBox::getItemHeight() const
{
return m_itemHeight;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::setTextSize(unsigned int textSize)
{
m_requestedTextSize = textSize;
if (textSize)
m_textSize = textSize;
else
m_textSize = findBestTextSize(getFont(), m_itemHeight * 0.85f);
for (auto& item : m_items)
item.setTextSize(m_textSize);
updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int ListBox::getTextSize() const
{
return m_textSize;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::setMaximumItems(std::size_t maximumItems)
{
// Set the new limit
@ -537,6 +591,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::setAutoScroll(bool autoScroll)
{
m_autoScroll = autoScroll;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::setOpacity(float opacity)
{
Widget::setOpacity(opacity);
@ -549,6 +610,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f ListBox::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ListBox::mouseOnWidget(float x, float y)
{
// Pass the event to the scrollbar (if there is one)
@ -809,11 +877,11 @@ namespace tgui
if (delta < 0)
{
// Scroll down
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * (m_itemHeight / 2)));
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * m_itemHeight));
}
else // You are scrolling up
{
unsigned int change = static_cast<unsigned int>(delta) * (m_itemHeight / 2);
unsigned int change = static_cast<unsigned int>(delta) * m_itemHeight;
// Scroll up
if (change < m_scroll->getValue())
@ -871,16 +939,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBox::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -1257,18 +1315,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBoxRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_listBox->m_font = font;
for (auto& item : m_listBox->m_items)
item.setTextFont(font);
m_listBox->updatePosition();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ListBoxRenderer::setPadding(const Padding& padding)
{
WidgetPadding::setPadding(padding);

View File

@ -65,17 +65,14 @@ namespace tgui
if (!m_menus.empty())
{
Label tempText(m_menus[0].text);
tempText.setText("kg");
// Position the menus
sf::Vector2f pos = {getPosition().x, getPosition().y + (getSize().y - tempText.getSize().y) / 2.0f};
sf::Vector2f pos = getPosition();
for (unsigned int i = 0; i < m_menus.size(); ++i)
{
m_menus[i].text.setPosition({pos.x + getRenderer()->m_distanceToSide, pos.y});
m_menus[i].text.setPosition({pos.x + getRenderer()->m_distanceToSide, pos.y + ((getSize().y - m_menus[i].text.getSize().y) / 2.f)});
for (unsigned int j = 0; j < m_menus[i].menuItems.size(); ++j)
m_menus[i].menuItems[j].setPosition(pos.x + 2 * getRenderer()->m_distanceToSide, pos.y + (j+1)*getSize().y);
m_menus[i].menuItems[j].setPosition(pos.x + 2 * getRenderer()->m_distanceToSide, pos.y + (j+1)*getSize().y + ((getSize().y - m_menus[i].menuItems[j].getSize().y) / 2.f));
pos.x += m_menus[i].text.getSize().x + 2 * getRenderer()->m_distanceToSide;
}
@ -88,7 +85,24 @@ namespace tgui
{
Widget::setSize(size);
setTextSize(static_cast<unsigned int>(getSize().y * 0.75f));
setTextSize(findBestTextSize(getFont(), getSize().y * 0.85f));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MenuBar::setFont(const Font& font)
{
Widget::setFont(font);
for (unsigned int i = 0; i < m_menus.size(); ++i)
{
for (unsigned int j = 0; j < m_menus[i].menuItems.size(); ++j)
m_menus[i].menuItems[j].setFont(font);
m_menus[i].text.setFont(font);
}
setTextSize(findBestTextSize(getFont(), getSize().y * 0.85f));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -97,7 +111,7 @@ namespace tgui
{
Menu newMenu;
newMenu.text.setTextFont(m_font);
newMenu.text.setFont(m_font);
newMenu.text.setText(text);
newMenu.text.setTextColor(calcColorOpacity(getRenderer()->m_textColor, getOpacity()));
newMenu.text.setTextSize(m_textSize);
@ -112,10 +126,7 @@ namespace tgui
bool MenuBar::addMenuItem(const sf::String& menu, const sf::String& text)
{
Label tempText(m_menus[0].text);
tempText.setText("kg");
sf::Vector2f pos = {getPosition().x, getPosition().y + (getSize().y - tempText.getSize().y) / 2.0f};
sf::Vector2f pos = getPosition();
// Search for the menu
for (unsigned int i = 0; i < m_menus.size(); ++i)
@ -124,7 +135,7 @@ namespace tgui
if (m_menus[i].text.getText() == menu)
{
Label menuItem;
menuItem.setTextFont(m_font);
menuItem.setFont(m_font);
menuItem.setText(text);
menuItem.setTextColor(calcColorOpacity(getRenderer()->m_textColor, getOpacity()));
menuItem.setTextSize(m_textSize);
@ -132,7 +143,7 @@ namespace tgui
m_menus[i].menuItems.push_back(std::move(menuItem));
// Position the new menu item
m_menus[i].menuItems.back().setPosition({pos.x + 2 * getRenderer()->m_distanceToSide, pos.y + m_menus[i].menuItems.size() * getSize().y});
m_menus[i].menuItems.back().setPosition({pos.x + 2 * getRenderer()->m_distanceToSide, pos.y + m_menus[i].menuItems.size() * getSize().y + ((getSize().y - m_menus[i].menuItems.back().getSize().y) / 2.f)});
return true;
}
@ -445,9 +456,6 @@ namespace tgui
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
if (getSize().x == 0)
setSize(bindWidth(m_parent->shared_from_this()), m_size.y);
}
@ -687,23 +695,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MenuBarRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_menuBar->m_font = font;
for (unsigned int i = 0; i < m_menuBar->m_menus.size(); ++i)
{
for (unsigned int j = 0; j < m_menuBar->m_menus[i].menuItems.size(); ++j)
m_menuBar->m_menus[i].menuItems[j].setTextFont(font);
m_menuBar->m_menus[i].text.setTextFont(font);
}
m_menuBar->setTextSize(m_menuBar->m_textSize);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MenuBarRenderer::setDistanceToSide(float distanceToSide)
{
m_distanceToSide = distanceToSide;

View File

@ -99,6 +99,19 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBox::setFont(const Font& font)
{
ChildWindow::setFont(font);
m_label->setFont(font);
for (unsigned int i = 0; i < m_buttons.size(); ++i)
m_buttons[i]->setFont(font);
rearrange();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBox::setText(const sf::String& text)
{
m_label->setText(text);
@ -128,7 +141,7 @@ namespace tgui
if (!getTheme() || m_buttonClassName.empty())
button = std::make_shared<Button>();
else
getTheme()->internalLoad(getPrimaryLoadingParameter(), m_buttonClassName);
button = getTheme()->internalLoad(getPrimaryLoadingParameter(), m_buttonClassName);
button->setTextSize(m_textSize);
button->setText(caption);
@ -142,28 +155,23 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBox::initialize(Container *const parent)
{
std::shared_ptr<sf::Font> font = m_font;
ChildWindow::initialize(parent);
if (!font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBox::rearrange()
{
float buttonWidth = 120;
float buttonHeight = 24;
// Calculate the button size
float buttonWidth = 5.0f * m_textSize;
float buttonHeight = m_textSize * 10.0f / 8.0f;
for (unsigned int i = 0; i < m_buttons.size(); ++i)
if (getFont())
{
float width = sf::Text(m_buttons[i]->getText(), *getGlobalFont(), m_textSize).getLocalBounds().width;
if (buttonWidth < width * 10.0f / 9.0f)
buttonWidth = width * 10.0f / 9.0f;
buttonWidth = 5.0f * getFont()->getLineSpacing(m_textSize);
buttonHeight = getFont()->getLineSpacing(m_textSize) / 0.85f;
for (unsigned int i = 0; i < m_buttons.size(); ++i)
{
float width = sf::Text(m_buttons[i]->getText(), *getFont(), m_textSize).getLocalBounds().width;
if (buttonWidth < width * 10.0f / 9.0f)
buttonWidth = width * 10.0f / 9.0f;
}
}
// Calculate the space needed for the buttons
@ -231,7 +239,9 @@ namespace tgui
m_messageBox->m_primaryLoadingParameter,
Deserializer::deserialize(ObjectConverter::Type::String, value).getString()
);
m_messageBox->ChildWindow::operator=(*childWindow);
for (auto& pair : childWindow->getRenderer()->getPropertyValuePairs())
setProperty(pair.first, std::move(pair.second));
}
else
ChildWindowRenderer::setProperty(property, value);
@ -260,8 +270,12 @@ namespace tgui
throw Exception{"Failed to load scrollbar, ChatBox has no connected theme to load the scrollbar with"};
tgui::ChildWindow::Ptr childWindow = m_messageBox->getTheme()->internalLoad(m_messageBox->m_primaryLoadingParameter, value.getString());
m_messageBox->ChildWindow::operator=(*childWindow);
for (auto& pair : childWindow->getRenderer()->getPropertyValuePairs())
setProperty(pair.first, std::move(pair.second));
}
else
ChildWindowRenderer::setProperty(property, std::move(value));
}
else
ChildWindowRenderer::setProperty(property, std::move(value));
@ -290,19 +304,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBoxRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_messageBox->m_font = font;
m_messageBox->m_label->setTextFont(font);
for (unsigned int i = 0; i < m_messageBox->m_buttons.size(); ++i)
m_messageBox->m_buttons[i]->getRenderer()->setTextFont(font);
m_messageBox->rearrange();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBoxRenderer::setTextColor(const sf::Color& color)
{
m_messageBox->m_label->setTextColor(color);

View File

@ -43,13 +43,7 @@ namespace tgui
Picture::Picture(const std::string& filename, bool fullyClickable) :
Picture{}
{
m_fullyClickable = fullyClickable;
m_loadedFilename = getResourcePath() + filename;
// Try to load the texture from the file
m_texture.load(m_loadedFilename);
// Remember the size of the texture
setTexture(filename, fullyClickable);
setSize(m_texture.getImageSize());
}
@ -58,19 +52,48 @@ namespace tgui
Picture::Picture(const sf::Texture& texture) :
Picture{}
{
auto data = std::make_shared<TextureData>();
data->texture = texture;
m_texture.setTexture(data);
setTexture(texture);
setSize(m_texture.getImageSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Picture::Picture(const Texture& texture) :
Picture::Picture(const Texture& texture, bool fullyClickable) :
Picture{}
{
auto size = m_texture.getSize();
setTexture(texture, fullyClickable);
setSize(size);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Picture::setTexture(const std::string& filename, bool fullyClickable)
{
m_fullyClickable = fullyClickable;
m_loadedFilename = getResourcePath() + filename;
m_texture.load(m_loadedFilename);
m_texture.setSize(getSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Picture::setTexture(const sf::Texture& texture)
{
auto data = std::make_shared<TextureData>();
data->texture = texture;
m_texture.setTexture(data);
m_texture.setSize(getSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Picture::setTexture(const Texture& texture, bool fullyClickable)
{
m_fullyClickable = fullyClickable;
m_texture = texture;
setSize(m_texture.getSize());
m_texture.setSize(getSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -121,6 +121,25 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f ProgressBar::getFullSize() const
{
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBar::setFont(const Font& font)
{
Widget::setFont(font);
m_textBack.setFont(font);
m_textFront.setFont(font);
setText(getText());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBar::setMinimum(unsigned int minimum)
{
// Set the new minimum
@ -205,13 +224,17 @@ namespace tgui
// Check if the text is auto sized
if (m_textSize == 0)
{
// Calculate a possible text size
float size = getSize().y * 0.75f;
m_textBack.setTextSize(static_cast<unsigned int>(size));
unsigned int textSize;
if (getRenderer()->m_textureBack.isLoaded() && getRenderer()->m_textureFront.isLoaded())
textSize = findBestTextSize(getFont(), getRenderer()->m_textureFront.getSize().y * 0.85f);
else
textSize = findBestTextSize(getFont(), getSize().y * 0.85f);
// Make the text smaller when it is too width
if (m_textBack.getSize().x > (getSize().x * 0.8f))
m_textBack.setTextSize(static_cast<unsigned int>(size / (m_textBack.getSize().x / (getSize().x * 0.8f))));
m_textBack.setTextSize(textSize);
// Make the text smaller when it's too width
if (m_textBack.getSize().x > (getSize().x * 0.85f))
m_textBack.setTextSize(static_cast<unsigned int>(textSize * ((getSize().x * 0.85f) / m_textBack.getSize().x)));
}
else // When the text has a fixed size
{
@ -260,6 +283,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f ProgressBar::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBar::recalculateSize()
{
sf::Vector2f size;
@ -295,16 +325,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBar::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBar::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -315,7 +335,7 @@ namespace tgui
if (force)
{
// Use the size of the images when images were loaded
if (getRenderer()->m_textureBack.isLoaded())
if (getRenderer()->m_textureBack.isLoaded() && getRenderer()->m_textureFront.isLoaded())
{
setSize(getRenderer()->m_textureBack.getImageSize());
@ -479,17 +499,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBarRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_progressBar->m_font = font;
m_progressBar->m_textBack.setTextFont(font);
m_progressBar->m_textFront.setTextFont(font);
m_progressBar->setText(m_progressBar->getText());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ProgressBarRenderer::setTextColor(const sf::Color& color)
{
setTextColorBack(color);

View File

@ -31,7 +31,7 @@
namespace
{
const float textDistanceRatio = 1.25f;
const float textDistanceRatio = 0.25f;
}
namespace tgui
@ -73,7 +73,7 @@ namespace tgui
getRenderer()->m_textureCheckedHover.setPosition(getPosition());
getRenderer()->m_textureFocused.setPosition(getPosition());
m_text.setPosition(getPosition().x + getSize().x * textDistanceRatio,
m_text.setPosition(getPosition().x + getSize().x + getSize().y * textDistanceRatio,
getPosition().y + ((getSize().y - m_text.getSize().y) / 2.0f));
}
@ -112,7 +112,18 @@ namespace tgui
if (m_text.getText().isEmpty())
return getSize();
else
return {(getSize().x * textDistanceRatio) + m_text.getSize().x, getSize().y};
return {getSize().x + (getSize().y * textDistanceRatio) + m_text.getSize().x, getSize().y};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RadioButton::setFont(const Font& font)
{
Widget::setFont(font);
m_text.setFont(font.getFont());
// Recalculate the text position and size
setText(getText());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -155,7 +166,7 @@ namespace tgui
// Set the text size
if (m_textSize == 0)
m_text.setTextSize(static_cast<unsigned int>(getSize().y * 0.75f));
m_text.setTextSize(findBestTextSize(getFont(), getSize().y * 0.85f));
else
m_text.setTextSize(m_textSize);
@ -206,11 +217,11 @@ namespace tgui
if (m_allowTextClick)
{
// Check if the mouse is on top of the image or the small gap between image and text
if (sf::FloatRect{getPosition().x, getPosition().y, getSize().x * textDistanceRatio, getSize().y}.contains(x, y))
if (sf::FloatRect{getPosition().x, getPosition().y, getSize().x + getSize().y * textDistanceRatio, getSize().y}.contains(x, y))
return true;
// Check if the mouse is on top of the text
if (sf::FloatRect{getPosition().x, getPosition().y, m_text.getSize().x, m_text.getSize().y}.contains(x - (getSize().x * textDistanceRatio), y - ((getSize().y - m_text.getSize().y) / 2.0f)))
if (sf::FloatRect{getPosition().x, getPosition().y, m_text.getSize().x, m_text.getSize().y}.contains(x - (getSize().x + getSize().y * textDistanceRatio), y - ((getSize().y - m_text.getSize().y) / 2.0f)))
return true;
}
else // You are not allowed to click on the text
@ -260,16 +271,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RadioButton::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RadioButton::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -520,17 +521,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RadioButtonRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_radioButton->m_font = font;
m_radioButton->m_text.setTextFont(font);
// Recalculate the text position and size
m_radioButton->setText(m_radioButton->getText());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void RadioButtonRenderer::setTextColor(const sf::Color& color)
{
setTextColorNormal(color);

View File

@ -167,6 +167,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Slider::getFullSize() const
{
return {getSize().x + getRenderer()->getBorders().left + getRenderer()->getBorders().right,
getSize().y + getRenderer()->getBorders().top + getRenderer()->getBorders().bottom};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Slider::setMinimum(int minimum)
{
// Set the new minimum
@ -242,6 +250,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Slider::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider::mouseOnWidget(float x, float y)
{
// Check if the mouse is on top of the thumb

View File

@ -192,6 +192,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f SpinButton::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SpinButton::leftMousePressed(float x, float y)
{
ClickableWidget::leftMousePressed(x, y);
@ -294,6 +301,7 @@ namespace tgui
{
if (m_theme && primary != "")
{
getRenderer()->setSpaceBetweenArrows(0);
getRenderer()->setBorders({0, 0, 0, 0});
Widget::reload(primary, secondary, force);
@ -315,7 +323,7 @@ namespace tgui
getRenderer()->setArrowColorNormal({60, 60, 60});
getRenderer()->setArrowColorHover({0, 0, 0});
getRenderer()->setBorderColor({0, 0, 0});
getRenderer()->setSpaceBetweenArrows(0);
getRenderer()->setSpaceBetweenArrows(2);
getRenderer()->setArrowUpTexture({});
getRenderer()->setArrowDownTexture({});
getRenderer()->setArrowUpHoverTexture({});

View File

@ -67,12 +67,6 @@ namespace tgui
float positionX = getPosition().x;
float textHeight;
if (m_font)
textHeight = sf::Text{"kg", *m_font, getTextSize()}.getLocalBounds().height;
else
textHeight = 0;
auto textureNormalIt = getRenderer()->m_texturesNormal.begin();
auto textureSelectedIt = getRenderer()->m_texturesSelected.begin();
auto tabTextIt = m_tabTexts.begin();
@ -85,7 +79,7 @@ namespace tgui
}
tabTextIt->setPosition({positionX + getRenderer()->m_distanceToSide + ((*tabWidthIt - (2 * getRenderer()->m_distanceToSide) - tabTextIt->getSize().x) / 2.0f),
getPosition().y + ((m_tabHeight - textHeight) / 2.0f)});
getPosition().y + ((m_tabHeight - tabTextIt->getSize().y) / 2.0f)});
positionX += *tabWidthIt + ((getRenderer()->getBorders().left + getRenderer()->getBorders().right) / 2.0f);
}
@ -99,6 +93,20 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Tab::setFont(const Font& font)
{
Widget::setFont(font);
for (auto& tab : m_tabTexts)
tab.setFont(font);
// Recalculate the size when auto sizing
if (m_requestedTextSize == 0)
setTextSize(0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::size_t Tab::add(const sf::String& text, bool selectTab)
{
// Use the insert function to put the tab in the right place
@ -118,7 +126,7 @@ namespace tgui
// Create the new tab
Label newTab;
newTab.setTextFont(m_font);
newTab.setFont(m_font);
newTab.setTextColor(calcColorOpacity(getRenderer()->m_textColor, getOpacity()));
newTab.setTextSize(getTextSize());
newTab.setText(text);
@ -142,15 +150,9 @@ namespace tgui
selectedIt->setPosition({getPosition().x + getSize().x, getPosition().y});
}
float textHeight;
if (m_font)
textHeight = sf::Text{"kg", *m_font, getTextSize()}.getLocalBounds().height;
else
textHeight = 0;
// Set the correct size of the tab text
newTab.setPosition({getPosition().x + getSize().x + getRenderer()->m_distanceToSide + ((*tabWidthIt - (2 * getRenderer()->m_distanceToSide) - newTab.getSize().x) / 2.0f),
getPosition().y + ((m_tabHeight - textHeight) / 2.0f)});
getPosition().y + ((m_tabHeight - newTab.getSize().y) / 2.0f)});
// Add the tab
m_tabTexts.insert(m_tabTexts.begin() + index, std::move(newTab));
@ -327,9 +329,15 @@ namespace tgui
void Tab::setTextSize(unsigned int size)
{
if (m_textSize != size)
if ((size == 0) || (m_requestedTextSize != size))
{
m_textSize = size;
m_requestedTextSize = size;
if (size == 0)
m_textSize = findBestTextSize(getFont(), m_tabHeight * 0.85f);
else // An exact size was given
m_textSize = size;
for (auto& label : m_tabTexts)
label.setTextSize(getTextSize());
@ -341,10 +349,7 @@ namespace tgui
unsigned int Tab::getTextSize() const
{
if (m_textSize == 0)
return static_cast<unsigned int>(m_tabHeight * 0.75f);
else
return m_textSize;
return m_textSize;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -366,7 +371,7 @@ namespace tgui
}
// Recalculate the size when auto sizing
if (m_textSize == 0)
if (m_requestedTextSize == 0)
setTextSize(0);
}
@ -406,6 +411,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f Tab::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Tab::mouseOnWidget(float x, float y)
{
if (sf::FloatRect{getPosition().x, getPosition().y, getSize().x, getSize().y}.contains(x, y))
@ -473,16 +485,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Tab::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Tab::reload(const std::string& primary, const std::string& secondary, bool force)
{
if (m_theme && primary != "")
@ -704,18 +706,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TabRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_tab->m_font = font;
for (auto& tab : m_tab->m_tabTexts)
tab.setTextFont(font);
m_tab->recalculateTabsWidth();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TabRenderer::setTextColor(const sf::Color& color)
{
m_textColor = color;

View File

@ -50,7 +50,7 @@ namespace tgui
m_renderer = std::make_shared<TextBoxRenderer>(this);
reload();
setSize({360, 200});
setSize({360, 189});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -156,8 +156,8 @@ namespace tgui
{
getRenderer()->m_backgroundTexture.setPosition(getPosition());
sf::Text tempText{"kg", *m_font, getTextSize()};
float textShiftY = tempText.getLocalBounds().top;
sf::Text tempText{"", *m_font, getTextSize()};
float textShiftY = getTextVerticalCorrection(getFont(), getTextSize());
Padding padding = getRenderer()->getScaledPadding();
// Position the caret
@ -333,6 +333,24 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBox::setFont(const Font& font)
{
Widget::setFont(font);
if (font.getFont())
{
m_textBeforeSelection.setFont(*font.getFont());
m_textSelection1.setFont(*font.getFont());
m_textSelection2.setFont(*font.getFont());
m_textAfterSelection1.setFont(*font.getFont());
m_textAfterSelection2.setFont(*font.getFont());
}
setTextSize(getTextSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBox::setText(const sf::String& text)
{
m_text = text;
@ -353,10 +371,8 @@ namespace tgui
{
// Store the new text size
m_textSize = size;
// There is a minimum text size
if (m_textSize < 8)
m_textSize = 8;
if (m_textSize < 1)
m_textSize = 1;
// Change the text size
m_textBeforeSelection.setCharacterSize(m_textSize);
@ -441,6 +457,13 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Vector2f TextBox::getWidgetOffset() const
{
return {getRenderer()->getBorders().left, getRenderer()->getBorders().top};
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TextBox::mouseOnWidget(float x, float y)
{
// Pass the event to the scrollbar (if there is one)
@ -1247,11 +1270,11 @@ namespace tgui
if (delta < 0)
{
// Scroll down
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * (m_lineHeight / 2)));
m_scroll->setValue(m_scroll->getValue() + (static_cast<unsigned int>(-delta) * m_lineHeight));
}
else // You are scrolling up
{
unsigned int change = static_cast<unsigned int>(delta) * (m_lineHeight / 2);
unsigned int change = static_cast<unsigned int>(delta) * m_lineHeight;
// Scroll up
if (change < m_scroll->getValue())
@ -1699,16 +1722,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBox::initialize(Container *const parent)
{
Widget::initialize(parent);
if (!m_font && m_parent->getGlobalFont())
getRenderer()->setTextFont(m_parent->getGlobalFont());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBox::update(sf::Time elapsedTime)
{
Widget::update(elapsedTime);
@ -2047,24 +2060,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBoxRenderer::setTextFont(std::shared_ptr<sf::Font> font)
{
m_textBox->m_font = font;
if (font)
{
m_textBox->m_textBeforeSelection.setFont(*font);
m_textBox->m_textSelection1.setFont(*font);
m_textBox->m_textSelection2.setFont(*font);
m_textBox->m_textAfterSelection1.setFont(*font);
m_textBox->m_textAfterSelection2.setFont(*font);
}
m_textBox->setTextSize(m_textBox->getTextSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TextBoxRenderer::setPadding(const Padding& padding)
{
WidgetPadding::setPadding(padding);

View File

@ -2,8 +2,10 @@ set(TEST_SOURCES
main.cpp
Borders.cpp
Clipboard.cpp
Font.cpp
FileCompare.cpp
Layouts.cpp
Signal.cpp
Widget.cpp
Loading/Serializer.cpp
Loading/Deserializer.cpp

37
tests/Font.cpp Normal file
View File

@ -0,0 +1,37 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus's Graphical User Interface
// Copyright (C) 2012-2015 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 "Tests.hpp"
#include <TGUI/Font.hpp>
TEST_CASE("[Font]") {
sf::Font font1;
auto font2 = std::make_shared<sf::Font>();
REQUIRE(tgui::Font().getFont() == nullptr);
REQUIRE(tgui::Font(nullptr).getFont() == nullptr);
REQUIRE(tgui::Font(font1).getFont() != nullptr);
REQUIRE(tgui::Font(font2).getFont() == font2);
REQUIRE(tgui::Font("resources/DroidSansArmenian.ttf").getFont() != nullptr);
}

View File

@ -29,10 +29,10 @@ using Type = tgui::ObjectConverter::Type;
TEST_CASE("[Deserializer]") {
SECTION("deserialize font") {
/*
std::shared_ptr<sf::Font> font;
font = tgui::Deserializer::deserialize(tgui::ObjectConverter::Type::Font, "resources/DroidSansArmenian.ttf"
*/
REQUIRE(tgui::Deserializer::deserialize(tgui::ObjectConverter::Type::Font, "resources/DroidSansArmenian.ttf").getFont() != nullptr);
REQUIRE(tgui::Deserializer::deserialize(tgui::ObjectConverter::Type::Font, "\"resources/DroidSansArmenian.ttf\"").getFont() != nullptr);
REQUIRE(tgui::Deserializer::deserialize(tgui::ObjectConverter::Type::Font, "nullptr").getFont() == nullptr);
REQUIRE(tgui::Deserializer::deserialize(tgui::ObjectConverter::Type::Font, "null").getFont() == nullptr);
}
SECTION("deserialize color") {

View File

@ -31,11 +31,12 @@ TEST_CASE("[Serializer]") {
}
SECTION("serialize font") {
/*
auto font = std::make_shared<sf::Font>();
font->loadFromFile("resources/DroidSansArmenian.ttf");
REQUIRE(tgui::Serializer::serialize(font) == "FONT_PLACEHOLDER");
*/
REQUIRE(tgui::Serializer::serialize(font) == "null"); // Serializing fonts is not supported yet
font = nullptr;
REQUIRE(tgui::Serializer::serialize(font) == "null");
}
SECTION("serialize color") {

70
tests/Signal.cpp Normal file
View File

@ -0,0 +1,70 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus's Graphical User Interface
// Copyright (C) 2012-2015 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 "Tests.hpp"
#include <TGUI/Widgets/Button.hpp>
TEST_CASE("[Signal]") {
tgui::Widget::Ptr widget = std::make_shared<tgui::Button>();
SECTION("connect") {
unsigned int id = widget->connect("PositionChanged", [](){});
REQUIRE(widget->connect("SizeChanged", [](){}) == id+1);
REQUIRE(widget->connect("Focused", [](){}) == id+2);
REQUIRE(widget->connect("Unfocused", [](){}) == id+3);
REQUIRE(widget->connect("MouseEntered", [](){}) == id+4);
REQUIRE(widget->connect("MouseLeft", [](){}) == id+5);
REQUIRE(widget->connect("PositionChanged", [](sf::Vector2f){}) == id+6);
REQUIRE(widget->connect("SizeChanged", [](sf::Vector2f){}) == id+7);
tgui::Widget::Ptr widget2 = std::make_shared<tgui::Button>();
REQUIRE(widget->connect("PositionChanged", [](sf::Vector2f, sf::Vector2f){}, widget2->getPosition()) == id+8);
REQUIRE(widget->connect("SizeChanged", [](sf::Vector2f, sf::Vector2f){}, std::bind(&tgui::Widget::getSize, widget2)) == id+9);
REQUIRE_THROWS_AS(widget->connect("SomeWrongSignal", [](){}), tgui::Exception);
REQUIRE_THROWS_AS(widget->connect("PositionChanged", [](bool){}), tgui::Exception);
REQUIRE(widget->connect("Focused Unfocused MouseEntered MouseLeft", [](){}) == id+13);
REQUIRE(widget->connect("All", [](){}) > id+14);
}
SECTION("connectEx") {
unsigned int id = widget->connectEx("PositionChanged", [](const tgui::Callback&){});
REQUIRE(widget->connectEx("SizeChanged", [](const tgui::Callback&){}) == id+1);
REQUIRE(widget->connectEx("Focused", [](const tgui::Callback&){}) == id+2);
REQUIRE(widget->connectEx("Unfocused", [](const tgui::Callback&){}) == id+3);
REQUIRE(widget->connectEx("MouseEntered", [](const tgui::Callback&){}) == id+4);
REQUIRE(widget->connectEx("MouseLeft", [](const tgui::Callback&){}) == id+5);
tgui::Widget::Ptr widget2 = std::make_shared<tgui::Button>();
REQUIRE(widget->connectEx("PositionChanged", [](sf::Vector2f, const tgui::Callback&){}, widget2->getPosition()) == id+6);
REQUIRE(widget->connectEx("SizeChanged", [](sf::Vector2f, const tgui::Callback&){}, std::bind(&tgui::Widget::getSize, widget2)) == id+7);
REQUIRE_THROWS_AS(widget->connectEx("SomeWrongSignal", [](const tgui::Callback&){}), tgui::Exception);
REQUIRE(widget->connectEx("Focused Unfocused MouseEntered MouseLeft", [](const tgui::Callback&){}) == id+11);
REQUIRE(widget->connectEx("All", [](const tgui::Callback&){}) > id+12);
}
}

View File

@ -30,26 +30,6 @@
TEST_CASE("[Widget]") {
tgui::Widget::Ptr widget = std::make_shared<tgui::Button>();
SECTION("Signals") {
tgui::Widget::Ptr widget2 = std::make_shared<tgui::Button>();
REQUIRE_NOTHROW(widget->connect("PositionChanged", [](){}));
REQUIRE_NOTHROW(widget->connect("SizeChanged", [](){}));
REQUIRE_NOTHROW(widget->connect("Focused", [](){}));
REQUIRE_NOTHROW(widget->connect("Unfocused", [](){}));
REQUIRE_NOTHROW(widget->connect("MouseEntered", [](){}));
REQUIRE_NOTHROW(widget->connect("MouseLeft", [](){}));
REQUIRE_NOTHROW(widget->connect("PositionChanged", [](sf::Vector2f){}));
REQUIRE_NOTHROW(widget->connect("SizeChanged", [](sf::Vector2f){}));
REQUIRE_NOTHROW(widget->connect("PositionChanged", [](sf::Vector2f, sf::Vector2f){}, widget2->getPosition()));
REQUIRE_NOTHROW(widget->connect("SizeChanged", [](sf::Vector2f, sf::Vector2f){}, std::bind(&tgui::Widget::getSize, widget2)));
REQUIRE_THROWS_AS(widget->connect("SomeWrongSignal", [](){}), tgui::Exception);
REQUIRE_THROWS_AS(widget->connect("PositionChanged", [](bool){}), tgui::Exception);
}
SECTION("Visibile") {
REQUIRE(widget->isVisible());
widget->hide();
@ -57,7 +37,7 @@ TEST_CASE("[Widget]") {
widget->show();
REQUIRE(widget->isVisible());
}
SECTION("Enabled") {
REQUIRE(widget->isEnabled());
widget->disable();
@ -73,10 +53,11 @@ TEST_CASE("[Widget]") {
REQUIRE(widget->getParent() == nullptr);
panel1->add(widget);
REQUIRE(widget->getParent() == panel1.get());
widget->initialize(panel2.get());
panel1->remove(widget);
panel2->add(widget);
REQUIRE(widget->getParent() == panel2.get());
}
SECTION("Opacity") {
REQUIRE(widget->getOpacity() == 1.F);

View File

@ -38,7 +38,7 @@ TEST_CASE("[ChatBox]") {
font->loadFromFile("resources/DroidSansArmenian.ttf");
auto parent = std::make_shared<tgui::GuiContainer>();
parent->setGlobalFont("resources/DroidSansArmenian.ttf");
parent->setFont("resources/DroidSansArmenian.ttf");
parent->add(chatBox);
REQUIRE(chatBox->getLineAmount() == 0);

View File

@ -54,6 +54,7 @@ TEST_CASE("[ChildWindow]") {
REQUIRE_NOTHROW(renderer->setProperty("Borders", "(1, 2, 3, 4)"));
REQUIRE_NOTHROW(renderer->setProperty("DistanceToSide", "2"));
REQUIRE_NOTHROW(renderer->setProperty("TitleBarHeight", "25"));
REQUIRE_NOTHROW(renderer->setProperty("CloseButton", "default"));
}
SECTION("set object property") {
@ -64,6 +65,7 @@ TEST_CASE("[ChildWindow]") {
REQUIRE_NOTHROW(renderer->setProperty("Borders", tgui::Borders{1, 2, 3, 4}));
REQUIRE_NOTHROW(renderer->setProperty("DistanceToSide", 2));
REQUIRE_NOTHROW(renderer->setProperty("TitleBarHeight", 25));
REQUIRE_NOTHROW(renderer->setProperty("CloseButton", tgui::ObjectConverter("")));
}
SECTION("functions") {
@ -95,6 +97,7 @@ TEST_CASE("[ChildWindow]") {
REQUIRE(renderer->getProperty("Borders").getBorders() == tgui::Borders(1, 2, 3, 4));
REQUIRE(renderer->getProperty("DistanceToSide").getNumber() == 2);
REQUIRE(renderer->getProperty("TitleBarHeight").getNumber() == 25);
REQUIRE(renderer->getProperty("CloseButton").getString() == "");
}
SECTION("textured") {

View File

@ -93,7 +93,7 @@ MessageBox {
ProgressBar {
BackImage : "Black.png" Part(180, 64, 90, 40) Middle(20, 0, 50, 40);
FrontImage : "Black.png" Part(180, 108, 82, 32) Middle(16, 0, 50, 32);
FrontImage : "Black.png" Part(180, 108, 90, 32) Middle(16, 0, 50, 32);
TextColorBack : rgb(190, 190, 190);
TextColorFront : rgb(250, 250, 250);
}
@ -137,7 +137,6 @@ Tab {
SelectedImage : "Black.png" Part(0, 32, 60, 32) Middle(16, 0, 28, 32);
TextColor : rgb(190, 190, 190);
SelectedTextColor : rgb(255, 255, 255);
BorderColor : rgb( 0, 0, 0);
DistanceToSide : 8;
}
@ -151,7 +150,7 @@ TextBox {
Scrollbar : "Scrollbar";
}
Tooltip {
Label.Tooltip {
TextColor : rgb(190, 190, 190);
BackgroundColor : rgb( 80, 80, 80);
BorderColor : rgb( 0, 0, 0);

View File

@ -93,7 +93,7 @@ MessageBox {
ProgressBar {
BackImage : "Black.png" Part(180, 64, 90, 40) Middle(20, 0, 50, 40);
FrontImage : "Black.png" Part(180, 108, 82, 32) Middle(16, 0, 50, 32);
FrontImage : "Black.png" Part(180, 108, 90, 32) Middle(16, 0, 50, 32);
TextColorBack : rgb(190, 190, 190);
TextColorFront : rgb(250, 250, 250);
}
@ -137,7 +137,6 @@ Tab {
SelectedImage : "Black.png" Part(0, 32, 60, 32) Middle(16, 0, 28, 32);
TextColor : rgb(190, 190, 190);
SelectedTextColor : rgb(255, 255, 255);
BorderColor : rgb( 0, 0, 0);
DistanceToSide : 8;
}