Skip to content

Commit

Permalink
Added new GrowHorizontalLayout and GrowVerticalLayout widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Dec 9, 2024
1 parent c26dd6e commit a45fe43
Show file tree
Hide file tree
Showing 17 changed files with 925 additions and 64 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
TGUI 1.7 (TBD)
---------------

- New widgets: GrowHorizontalLayout and GrowVerticalLayout


TGUI 1.6.1 (8 October 2024)
----------------------------

Expand Down
2 changes: 2 additions & 0 deletions include/TGUI/AllWidgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <TGUI/Widgets/FileDialog.hpp>
#include <TGUI/Widgets/Group.hpp>
#include <TGUI/Widgets/Grid.hpp>
#include <TGUI/Widgets/GrowHorizontalLayout.hpp>
#include <TGUI/Widgets/GrowVerticalLayout.hpp>
#include <TGUI/Widgets/HorizontalLayout.hpp>
#include <TGUI/Widgets/HorizontalWrap.hpp>
#include <TGUI/Widgets/Knob.hpp>
Expand Down
4 changes: 4 additions & 0 deletions include/TGUI/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ TGUI_MODULE_EXPORT namespace tgui
#if TGUI_HAS_WINDOW_BACKEND_SFML
// This constructor has to be explicit or it will cause MSVC to no longer compile code that performs sf::String + std::string
explicit String(const sf::String& str)
#if SFML_VERSION_MAJOR >= 3
: m_string{str.toUtf32()}
#else
: m_string{reinterpret_cast<const char32_t*>(str.toUtf32().c_str())}
#endif
{
}

Expand Down
144 changes: 144 additions & 0 deletions include/TGUI/Widgets/GrowHorizontalLayout.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2024 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_GROW_HORIZONTAL_LAYOUT_HPP
#define TGUI_GROW_HORIZONTAL_LAYOUT_HPP

#include <TGUI/Widgets/BoxLayoutRatios.hpp>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_MODULE_EXPORT namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Container that automatically positions children beside each other.
///
/// The width of child widgets isn't altered, while the height is changed to match that of the horizontal layout.
/// With each widget that gets added, the width of the horizontal layout increases to fit the new widget.
///
/// If you want the layout to have a fixed size and want children resized to fill the area then check HorizontalLayout instead.
///
/// @since TGUI 1.7
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API GrowHorizontalLayout : public BoxLayout
{
public:
using Ptr = std::shared_ptr<GrowHorizontalLayout>; //!< Shared widget pointer
using ConstPtr = std::shared_ptr<const GrowHorizontalLayout>; //!< Shared constant widget pointer

static constexpr const char StaticWidgetType[] = "GrowHorizontalLayout"; //!< Type name of the widget

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/// @brief Constructor
/// @param typeName Type of the widget
/// @param initRenderer Should the renderer be initialized? Should be true unless a derived class initializes it.
/// @see create
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowHorizontalLayout(const char* typeName = StaticWidgetType, bool initRenderer = true);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Copy constructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowHorizontalLayout(const GrowHorizontalLayout&);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Move constructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowHorizontalLayout(GrowHorizontalLayout&&) noexcept;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Overload of copy assignment operator
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowHorizontalLayout& operator=(const GrowHorizontalLayout&);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Move assignment
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowHorizontalLayout& operator=(GrowHorizontalLayout&&) noexcept;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Destructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~GrowHorizontalLayout() = default;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Creates a new horizontal layout widget
///
/// @param height Height of the horizontal layout
///
/// @return The new horizontal layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static GrowHorizontalLayout::Ptr create(const Layout& width = {RelativeValue(1)});

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Makes a copy of another layout
///
/// @param layout The other layout
///
/// @return The new layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static GrowHorizontalLayout::Ptr copy(const GrowHorizontalLayout::ConstPtr& layout);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the space available for widgets inside the container
/// @return Size of the container
///
/// @warning The width returned by this function is always 0. This is to prevent child widgets from depending on the
/// width of the horizontal layout (which isn't allowed as the width of the layout depends on its children).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD Vector2f getInnerSize() const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Removes all widgets that were added to the container
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void removeAllWidgets() override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @brief Repositions and resize the widgets
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateWidgets() override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @brief Makes a copy of the widget
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD Widget::Ptr clone() const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

std::vector<Layout> m_widgetLayouts;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif // TGUI_GROW_HORIZONTAL_LAYOUT_HPP
144 changes: 144 additions & 0 deletions include/TGUI/Widgets/GrowVerticalLayout.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2024 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_GROW_VERTICAL_LAYOUT_HPP
#define TGUI_GROW_VERTICAL_LAYOUT_HPP

#include <TGUI/Widgets/BoxLayoutRatios.hpp>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

TGUI_MODULE_EXPORT namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Container that automatically positions children below each other.
///
/// The height of child widgets isn't altered, while the width is changed to match that of the vertical layout.
/// With each widget that gets added, the height of the vertical layout increases to fit the new widget.
///
/// If you want the layout to have a fixed size and want children resized to fill the area then check VerticalLayout instead.
///
/// @since TGUI 1.7
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API GrowVerticalLayout : public BoxLayout
{
public:
using Ptr = std::shared_ptr<GrowVerticalLayout>; //!< Shared widget pointer
using ConstPtr = std::shared_ptr<const GrowVerticalLayout>; //!< Shared constant widget pointer

static constexpr const char StaticWidgetType[] = "GrowVerticalLayout"; //!< Type name of the widget

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/// @brief Constructor
/// @param typeName Type of the widget
/// @param initRenderer Should the renderer be initialized? Should be true unless a derived class initializes it.
/// @see create
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowVerticalLayout(const char* typeName = StaticWidgetType, bool initRenderer = true);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Copy constructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowVerticalLayout(const GrowVerticalLayout&);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Move constructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowVerticalLayout(GrowVerticalLayout&&) noexcept;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Overload of copy assignment operator
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowVerticalLayout& operator=(const GrowVerticalLayout&);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Move assignment
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GrowVerticalLayout& operator=(GrowVerticalLayout&&) noexcept;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Destructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~GrowVerticalLayout() = default;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Creates a new vertical layout widget
///
/// @param width Width of the vertical layout
///
/// @return The new vertical layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static GrowVerticalLayout::Ptr create(const Layout& width = {RelativeValue(1)});

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Makes a copy of another layout
///
/// @param layout The other layout
///
/// @return The new layout
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static GrowVerticalLayout::Ptr copy(const GrowVerticalLayout::ConstPtr& layout);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns the space available for widgets inside the container
/// @return Size of the container
///
/// @warning The height returned by this function is always 0. This is to prevent child widgets from depending on the
/// height of the vertical layout (which isn't allowed as the height of the layout depends on its children).
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD Vector2f getInnerSize() const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Removes all widgets that were added to the container
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void removeAllWidgets() override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @brief Repositions and resize the widgets
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void updateWidgets() override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @brief Makes a copy of the widget
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD Widget::Ptr clone() const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

std::vector<Layout> m_widgetLayouts;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#endif // TGUI_GROW_VERTICAL_LAYOUT_HPP
3 changes: 3 additions & 0 deletions include/TGUI/Widgets/HorizontalLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ TGUI_MODULE_EXPORT namespace tgui
/// @brief Container that automatically resizes children to fit the entire available space between children.
///
/// The children are positioned side by side.
///
/// If you don't want the width of child widgets to be altered and instead want the horizontal layout to grow in size as new
/// widgets are added, then check out the GrowHorizontalLayout instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API HorizontalLayout : public BoxLayoutRatios
{
Expand Down
3 changes: 3 additions & 0 deletions include/TGUI/Widgets/VerticalLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ TGUI_MODULE_EXPORT namespace tgui
/// @brief Container that automatically resizes children to fit the entire available space between children
///
/// The children are stacked vertically.
///
/// If you don't want the height of child widgets to be altered and instead want the vertical layout to grow in size as new
/// widgets are added, then check out the GrowVerticalLayout instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API VerticalLayout : public BoxLayoutRatios
{
Expand Down
Loading

0 comments on commit a45fe43

Please sign in to comment.