diff --git a/examples/android/assets/widgets/Black.txt b/examples/android/assets/widgets/Black.txt index ae23713f..325d22c1 100644 --- a/examples/android/assets/widgets/Black.txt +++ b/examples/android/assets/widgets/Black.txt @@ -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); diff --git a/examples/android/jni/main.cpp b/examples/android/jni/main.cpp index f7a64170..99549737 100644 --- a/examples/android/jni/main.cpp +++ b/examples/android/jni/main.cpp @@ -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("widgets/Black.txt"); auto picLandscape = std::make_shared("Background-Landscape.png"); diff --git a/examples/full_example/FullExample.cpp b/examples/full_example/FullExample.cpp index 30b093e9..a6a2b472 100644 --- a/examples/full_example/FullExample.cpp +++ b/examples/full_example/FullExample.cpp @@ -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("../../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}); diff --git a/examples/login_screen/LoginScreen.cpp b/examples/login_screen/LoginScreen.cpp index d30e2a94..e09e2d52 100644 --- a/examples/login_screen/LoginScreen.cpp +++ b/examples/login_screen/LoginScreen.cpp @@ -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); diff --git a/examples/scalable/Scalable.cpp b/examples/scalable/Scalable.cpp index a033e3e6..678b5a1d 100644 --- a/examples/scalable/Scalable.cpp +++ b/examples/scalable/Scalable.cpp @@ -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("../Linux.jpg")); diff --git a/include/TGUI/BoxLayout.hpp b/include/TGUI/BoxLayout.hpp index 63680bce..128b3be6 100644 --- a/include/TGUI/BoxLayout.hpp +++ b/include/TGUI/BoxLayout.hpp @@ -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. /// diff --git a/include/TGUI/Container.hpp b/include/TGUI/Container.hpp index 12b916b1..440ea69f 100644 --- a/include/TGUI/Container.hpp +++ b/include/TGUI/Container.hpp @@ -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 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 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: diff --git a/include/TGUI/Font.hpp b/include/TGUI/Font.hpp new file mode 100644 index 00000000..279052fe --- /dev/null +++ b/include/TGUI/Font.hpp @@ -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 + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +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 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 getFont() const; + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + private: + + std::shared_ptr m_font; + }; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif // TGUI_TEXTURE_HPP diff --git a/include/TGUI/Global.hpp b/include/TGUI/Global.hpp index 07370f6f..9b8e92b6 100644 --- a/include/TGUI/Global.hpp +++ b/include/TGUI/Global.hpp @@ -36,6 +36,7 @@ #include #include #include +#include ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -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(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 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 font, float height, int fit = 0); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Gui.hpp b/include/TGUI/Gui.hpp index d6265726..1f466dad 100644 --- a/include/TGUI/Gui.hpp +++ b/include/TGUI/Gui.hpp @@ -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 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 getGlobalFont() const + std::shared_ptr getFont() const { - return m_container->getGlobalFont(); + return m_container->getFont(); } diff --git a/include/TGUI/Signal.hpp b/include/TGUI/Signal.hpp index c4d0d56e..ddb7174b 100644 --- a/include/TGUI/Signal.hpp +++ b/include/TGUI/Signal.hpp @@ -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.")}; + } + } + } } } diff --git a/include/TGUI/Widget.hpp b/include/TGUI/Widget.hpp index 963245ee..64df8aaa 100644 --- a/include/TGUI/Widget.hpp +++ b/include/TGUI/Widget.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -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 getFont() const - { - return m_font; - } + std::shared_ptr 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 clone(Widget* widget) = 0; - friend class Widget; - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + friend class Widget; }; diff --git a/include/TGUI/Widgets/Button.hpp b/include/TGUI/Widgets/Button.hpp index 8239b526..61c01454 100644 --- a/include/TGUI/Widgets/Button.hpp +++ b/include/TGUI/Widgets/Button.hpp @@ -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 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the color of the text. /// diff --git a/include/TGUI/Widgets/ChatBox.hpp b/include/TGUI/Widgets/ChatBox.hpp index 54c76e78..40bb621e 100644 --- a/include/TGUI/Widgets/ChatBox.hpp +++ b/include/TGUI/Widgets/ChatBox.hpp @@ -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 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 font); + void setFont(const Font& font); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -271,7 +270,7 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::shared_ptr 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: diff --git a/include/TGUI/Widgets/ChildWindow.hpp b/include/TGUI/Widgets/ChildWindow.hpp index 89a0c039..239611cc 100644 --- a/include/TGUI/Widgets/ChildWindow.hpp +++ b/include/TGUI/Widgets/ChildWindow.hpp @@ -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 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; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Widgets/ComboBox.hpp b/include/TGUI/Widgets/ComboBox.hpp index 095e9947..b4ab7b41 100644 --- a/include/TGUI/Widgets/ComboBox.hpp +++ b/include/TGUI/Widgets/ComboBox.hpp @@ -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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the size of the borders. /// diff --git a/include/TGUI/Widgets/EditBox.hpp b/include/TGUI/Widgets/EditBox.hpp index 0b5365aa..6e98fec4 100644 --- a/include/TGUI/Widgets/EditBox.hpp +++ b/include/TGUI/Widgets/EditBox.hpp @@ -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 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the padding of the edit box. /// diff --git a/include/TGUI/Widgets/Knob.hpp b/include/TGUI/Widgets/Knob.hpp index 0035790a..f2669b75 100644 --- a/include/TGUI/Widgets/Knob.hpp +++ b/include/TGUI/Widgets/Knob.hpp @@ -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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Widgets/Label.hpp b/include/TGUI/Widgets/Label.hpp index 8346666c..14ba7ca8 100644 --- a/include/TGUI/Widgets/Label.hpp +++ b/include/TGUI/Widgets/Label.hpp @@ -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 font); - - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - /// @brief Returns the font of the text. - /// - /// @return Pointer to the font that is currently being used. - /// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - std::shared_ptr 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 /// diff --git a/include/TGUI/Widgets/ListBox.hpp b/include/TGUI/Widgets/ListBox.hpp index 26663324..459eed99 100644 --- a/include/TGUI/Widgets/ListBox.hpp +++ b/include/TGUI/Widgets/ListBox.hpp @@ -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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the padding of the list box. /// diff --git a/include/TGUI/Widgets/MenuBar.hpp b/include/TGUI/Widgets/MenuBar.hpp index 5fe8e4a9..a60228da 100644 --- a/include/TGUI/Widgets/MenuBar.hpp +++ b/include/TGUI/Widgets/MenuBar.hpp @@ -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. /// diff --git a/include/TGUI/Widgets/MessageBox.hpp b/include/TGUI/Widgets/MessageBox.hpp index 3b55df49..1581412d 100644 --- a/include/TGUI/Widgets/MessageBox.hpp +++ b/include/TGUI/Widgets/MessageBox.hpp @@ -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 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the color of the text. /// diff --git a/include/TGUI/Widgets/Picture.hpp b/include/TGUI/Widgets/Picture.hpp index 056bb631..6b8a0d9a 100644 --- a/include/TGUI/Widgets/Picture.hpp +++ b/include/TGUI/Widgets/Picture.hpp @@ -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::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); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Widgets/ProgressBar.hpp b/include/TGUI/Widgets/ProgressBar.hpp index 81a4a094..ca4bc6a8 100644 --- a/include/TGUI/Widgets/ProgressBar.hpp +++ b/include/TGUI/Widgets/ProgressBar.hpp @@ -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 /// diff --git a/include/TGUI/Widgets/RadioButton.hpp b/include/TGUI/Widgets/RadioButton.hpp index 74d6b70d..2f5ae173 100644 --- a/include/TGUI/Widgets/RadioButton.hpp +++ b/include/TGUI/Widgets/RadioButton.hpp @@ -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 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the color of the text. /// diff --git a/include/TGUI/Widgets/Slider.hpp b/include/TGUI/Widgets/Slider.hpp index c51054c9..e2b380f7 100644 --- a/include/TGUI/Widgets/Slider.hpp +++ b/include/TGUI/Widgets/Slider.hpp @@ -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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Widgets/SpinButton.hpp b/include/TGUI/Widgets/SpinButton.hpp index 2390cd5a..f4f44d42 100644 --- a/include/TGUI/Widgets/SpinButton.hpp +++ b/include/TGUI/Widgets/SpinButton.hpp @@ -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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/Widgets/Tab.hpp b/include/TGUI/Widgets/Tab.hpp index 1a6e4a37..b2103343 100644 --- a/include/TGUI/Widgets/Tab.hpp +++ b/include/TGUI/Widgets/Tab.hpp @@ -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 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Set the text color that will be used inside the tabs. /// diff --git a/include/TGUI/Widgets/TextBox.hpp b/include/TGUI/Widgets/TextBox.hpp index 2cbe51bd..6b567490 100644 --- a/include/TGUI/Widgets/TextBox.hpp +++ b/include/TGUI/Widgets/TextBox.hpp @@ -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 m_lines = std::vector{""}; // 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 font); - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Changes the padding of the text box. /// diff --git a/src/TGUI/BoxLayout.cpp b/src/TGUI/BoxLayout.cpp index 2b9a0df7..97755d14 100644 --- a/src/TGUI/BoxLayout.cpp +++ b/src/TGUI/BoxLayout.cpp @@ -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); diff --git a/src/TGUI/CMakeLists.txt b/src/TGUI/CMakeLists.txt index 7c4bd031..fac73e7a 100644 --- a/src/TGUI/CMakeLists.txt +++ b/src/TGUI/CMakeLists.txt @@ -4,6 +4,7 @@ set(TGUI_SRC BoxLayout.cpp Clipboard.cpp Container.cpp + Font.cpp Global.cpp Gui.cpp HorizontalLayout.cpp diff --git a/src/TGUI/Container.cpp b/src/TGUI/Container.cpp index c2a367cc..73266b76 100644 --- a/src/TGUI/Container.cpp +++ b/src/TGUI/Container.cpp @@ -81,18 +81,12 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - void Container::setGlobalFont(const std::string& filename) + void Container::setFont(const Font& font) { - m_font = std::make_shared(); - if (!m_font->loadFromFile(getResourcePath() + filename)) - throw Exception{"Failed to load font '" + filename + "'."}; - } + Widget::setFont(font); - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - void Container::setGlobalFont(std::shared_ptr 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); diff --git a/src/TGUI/Font.cpp b/src/TGUI/Font.cpp new file mode 100644 index 00000000..2348135c --- /dev/null +++ b/src/TGUI/Font.cpp @@ -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 +#include + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +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 font) : + m_font{font} + { + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + Font::Font(const sf::Font& font) : + m_font{std::make_shared(font)} + { + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + std::shared_ptr Font::getFont() const + { + return m_font; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/TGUI/Global.cpp b/src/TGUI/Global.cpp index 74a6b6ec..14670fdc 100644 --- a/src/TGUI/Global.cpp +++ b/src/TGUI/Global.cpp @@ -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(color.a * alpha)}; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + float getTextVerticalCorrection(std::shared_ptr 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 font, float height, int fit) + { + if (!font) + return 0; + + if (height < 2) + return 1; + + std::vector textSizes(static_cast(height)); + for (std::size_t i = 0; i < static_cast(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; + } + } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } diff --git a/src/TGUI/HorizontalLayout.cpp b/src/TGUI/HorizontalLayout.cpp index dd55c207..287f1c97 100644 --- a/src/TGUI/HorizontalLayout.cpp +++ b/src/TGUI/HorizontalLayout.cpp @@ -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()); + } + } } } diff --git a/src/TGUI/Loading/Deserializer.cpp b/src/TGUI/Loading/Deserializer.cpp index a04140a2..a337069e 100644 --- a/src/TGUI/Loading/Deserializer.cpp +++ b/src/TGUI/Loading/Deserializer.cpp @@ -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()}; /// TODO: Support deserializing fonts + if (value == "null" || value == "nullptr") + return std::shared_ptr(); + + auto font = std::make_shared(); + font->loadFromFile(Deserializer::deserialize(ObjectConverter::Type::String, value).getString()); + return font; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/TGUI/Loading/Serializer.cpp b/src/TGUI/Loading/Serializer.cpp index 688017d5..8c5611fd 100644 --- a/src/TGUI/Loading/Serializer.cpp +++ b/src/TGUI/Loading/Serializer.cpp @@ -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"; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/TGUI/Loading/WidgetSaver.cpp b/src/TGUI/Loading/WidgetSaver.cpp index 4603b237..83fca91e 100644 --- a/src/TGUI/Loading/WidgetSaver.cpp +++ b/src/TGUI/Loading/WidgetSaver.cpp @@ -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()) { diff --git a/src/TGUI/VerticalLayout.cpp b/src/TGUI/VerticalLayout.cpp index ff7fd661..ff8613c3 100644 --- a/src/TGUI/VerticalLayout.cpp +++ b/src/TGUI/VerticalLayout.cpp @@ -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()); + } + } } } diff --git a/src/TGUI/Widget.cpp b/src/TGUI/Widget.cpp index d467c7ec..2c8afa06 100644 --- a/src/TGUI/Widget.cpp +++ b/src/TGUI/Widget.cpp @@ -383,6 +383,20 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void Widget::setFont(const Font& font) + { + m_font = font.getFont(); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + std::shared_ptr 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 WidgetRenderer::getPropertyValuePairs() const { - /// TODO: Font in every widget return std::map{}; } diff --git a/src/TGUI/Widgets/Button.cpp b/src/TGUI/Widgets/Button.cpp index 326c42d4..b6e68e1b 100644 --- a/src/TGUI/Widgets/Button.cpp +++ b/src/TGUI/Widgets/Button.cpp @@ -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(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(size * getSize().y * 0.8f / m_text.getSize().y)); + if (m_text.getSize().y > (getSize().y * 0.85f)) + m_text.setTextSize(static_cast(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(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(size * getSize().x * 0.8f / m_text.getSize().x)); + if (m_text.getSize().x > (getSize().x * 0.85f)) + m_text.setTextSize(static_cast(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 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); diff --git a/src/TGUI/Widgets/ChatBox.cpp b/src/TGUI/Widgets/ChatBox.cpp index dd297479..62320156 100644 --- a/src/TGUI/Widgets/ChatBox.cpp +++ b/src/TGUI/Widgets/ChatBox.cpp @@ -43,10 +43,12 @@ namespace tgui m_callback.widgetType = "ChatBox"; m_draggableWidget = true; + m_panel->setBackgroundColor(sf::Color::Transparent); + m_renderer = std::make_shared(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 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 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