diff --git a/changelog.txt b/changelog.txt index dfded9f2..3ac669cc 100644 --- a/changelog.txt +++ b/changelog.txt @@ -9,6 +9,7 @@ TGUI 0.8.5 (TBD) - Added functions to ListView to change existing items - Support typing tabs in TextBox (if tab usage is disabled in gui) - Added function to signals to temporarily disable callbacks +- Added addition and subtraction operators to Outline - ChildWindow can now have a different border color in focused state - Added function to select item in TreeView - EditBox::setInputValidator now returns false when regex was invalid diff --git a/include/TGUI/Outline.hpp b/include/TGUI/Outline.hpp index 94dc7923..ead20c5d 100644 --- a/include/TGUI/Outline.hpp +++ b/include/TGUI/Outline.hpp @@ -190,6 +190,61 @@ namespace tgui } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Adds two outlines together (e.g. to add padding and borders) + /// + /// @param other The outline to add together with this instance + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TGUI_CONSTEXPR Outline operator+(const Outline& other) const + { + return {getLeft() + other.getLeft(), + getTop() + other.getTop(), + getRight() + other.getRight(), + getBottom() + other.getBottom()}; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Subtracts two outlines from each other + /// + /// @param other The outline to subtract from this instance + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TGUI_CONSTEXPR Outline operator-(const Outline& other) const + { + return {getLeft() - other.getLeft(), + getTop() - other.getTop(), + getRight() - other.getRight(), + getBottom() - other.getBottom()}; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Adds an outline to this instance (e.g. to add padding and borders) + /// + /// @param other The outline to add to this instance + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TGUI_CONSTEXPR Outline& operator+=(const Outline& other) + { + m_left = getLeft() + other.getLeft(); + m_top = getTop() + other.getTop(); + m_right = getRight() + other.getRight(); + m_bottom = getBottom() + other.getBottom(); + return *this; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Subtracts an outline from this instance + /// + /// @param other The outline to subtract from this instance + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + TGUI_CONSTEXPR Outline& operator-=(const Outline& other) + { + m_left = getLeft() - other.getLeft(); + m_top = getTop() - other.getTop(); + m_right = getRight() - other.getRight(); + m_bottom = getBottom() - other.getBottom(); + return *this; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @internal /// @brief Update the size to which the outline depends on if its values are relative diff --git a/tests/Outline.cpp b/tests/Outline.cpp index a34176f3..7a8624a6 100644 --- a/tests/Outline.cpp +++ b/tests/Outline.cpp @@ -130,6 +130,53 @@ TEST_CASE("[Outline]") REQUIRE(!(outline1 != outline4)); REQUIRE(!(outline4 != outline4)); } + + SECTION("+") + { + REQUIRE(outline1 + outline3 == tgui::Outline{7, 9, 11, 13}); + REQUIRE(outline4 + outline4 == tgui::Outline{2, 4, 6, 8}); + + REQUIRE(outline1 + outline3 == outline3 + outline1); + } + + SECTION("-") + { + REQUIRE(outline1 - outline3 == tgui::Outline{-5, -5, -5, -5}); + REQUIRE(outline3 - outline1 == tgui::Outline{5, 5, 5, 5}); + REQUIRE(outline4 - outline4 == tgui::Outline{0, 0, 0, 0}); + } + + SECTION("+=") + { + outline1 += outline3; + REQUIRE(outline1 == tgui::Outline{7, 9, 11, 13}); + +#if defined(__GNUC__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wself-assign-overloaded" +#endif + outline4 += outline4; +#if defined(__GNUC__) + #pragma GCC diagnostic pop +#endif + REQUIRE(outline4 == tgui::Outline{2, 4, 6, 8}); + } + + SECTION("-=") + { + outline1 -= outline3; + REQUIRE(outline1 == tgui::Outline{-5, -5, -5, -5}); + +#if defined(__GNUC__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wself-assign-overloaded" +#endif + outline4 -= outline4; +#if defined(__GNUC__) + #pragma GCC diagnostic pop +#endif + REQUIRE(outline4 == tgui::Outline{0, 0, 0, 0}); + } } SECTION("toString")