Skip to content

Commit

Permalink
Setting and getting view of CanvasSFML was broken once the canvas siz…
Browse files Browse the repository at this point in the history
…e was reduced (because the render texture would have a larger size than the canvas)
  • Loading branch information
texus committed Sep 21, 2024
1 parent 746749e commit e15fad7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ TGUI 1.6 (TBD)
- Added addMultipleItems to ListBox and ComboBox
- Added getItemByIndex, getIndexById and getIdByIndex to ComboBox
- Fixed crash on exit when tool tip was visible
- Fixed wrong arrow sizes for horizontal spin button
- Fixed view not being usable in CanvasSFML


TGUI 1.5 (25 August 2024)
Expand Down
3 changes: 2 additions & 1 deletion include/TGUI/Backend/Renderer/SFML-Graphics/CanvasSFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @return The default view of the canvas
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD const sf::View& getDefaultView() const;
TGUI_NODISCARD sf::View getDefaultView() const;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Get the viewport of the currently applied view, applied to this canvas
Expand Down Expand Up @@ -247,6 +247,7 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected:

sf::View m_view;
sf::RenderTexture m_renderTexture;
Vector2u m_usedTextureSize;
};
Expand Down
48 changes: 41 additions & 7 deletions src/Backend/Renderer/SFML-Graphics/CanvasSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasSFML::CanvasSFML(const char* typeName, bool initRenderer) :
CanvasBase{typeName, initRenderer}
CanvasBase{typeName, initRenderer},
m_view{{{}, {1, 1}}}
{
}

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

CanvasSFML::CanvasSFML(const CanvasSFML& other) :
CanvasBase{other}
CanvasBase{other},
m_view {other.m_view}
{
setSize(other.getSize());
}
Expand All @@ -57,6 +59,7 @@ namespace tgui

CanvasSFML::CanvasSFML(CanvasSFML&& other) noexcept :
CanvasBase{std::move(other)},
m_view {std::move(other.m_view)},
#if SFML_VERSION_MAJOR >= 3
m_renderTexture{std::move(other.m_renderTexture)},
#endif
Expand All @@ -74,6 +77,7 @@ namespace tgui
if (this != &right)
{
ClickableWidget::operator=(right);
m_view = right.m_view;
m_usedTextureSize = right.m_usedTextureSize;
setSize(right.getSize());
}
Expand All @@ -87,11 +91,16 @@ namespace tgui
{
if (this != &right)
{
m_usedTextureSize = std::move(right.m_usedTextureSize);
ClickableWidget::operator=(std::move(right));
m_view = std::move(right.m_view);
m_usedTextureSize = std::move(right.m_usedTextureSize);

#if SFML_VERSION_MAJOR >= 3
m_renderTexture = std::move(right.m_renderTexture);
#else
// sf::RenderTexture does not support move yet
setSize(getSize());
#endif
}

return *this;
Expand Down Expand Up @@ -137,27 +146,52 @@ namespace tgui

m_usedTextureSize = newTextureSize;
}

setView(getDefaultView());
}

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

void CanvasSFML::setView(const sf::View& view)
{
m_renderTexture.setView(view);
m_view = view;

// The render texture might be larger than the canvas
sf::FloatRect viewport = view.getViewport();
if ((m_renderTexture.getSize().x > 0) && (m_renderTexture.getSize().y > 0))
{
const float scaleX = static_cast<float>(m_usedTextureSize.x) / static_cast<float>(m_renderTexture.getSize().x);
const float scaleY = static_cast<float>(m_usedTextureSize.y) / static_cast<float>(m_renderTexture.getSize().y);
#if SFML_VERSION_MAJOR >= 3
viewport.position.x *= scaleX;
viewport.position.y *= scaleY;
viewport.size.x *= scaleX;
viewport.size.y *= scaleY;
#else
viewport.left *= scaleX;
viewport.top *= scaleY;
viewport.width *= scaleX;
viewport.height *= scaleY;
#endif
}

sf::View internalView = view;
internalView.setViewport(viewport);
m_renderTexture.setView(internalView);
}

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

const sf::View& CanvasSFML::getView() const
{
return m_renderTexture.getView();
return m_view;
}

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

const sf::View& CanvasSFML::getDefaultView() const
sf::View CanvasSFML::getDefaultView() const
{
return m_renderTexture.getDefaultView();
return sf::View{{{}, {static_cast<float>(m_usedTextureSize.x), static_cast<float>(m_usedTextureSize.y)}}};
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion tests/Widgets/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ TEST_CASE("[CanvasSFML]")

SECTION("view")
{
canvas = tgui::CanvasSFML::create({200, 100});
canvas = tgui::CanvasSFML::create({300, 200});
canvas->setSize({50, 30});
canvas->setSize({200, 100});

REQUIRE(canvas->getView() == sf::View(sf::FloatRect{{0, 0}, {200, 100}}));
REQUIRE(canvas->getDefaultView() == sf::View(sf::FloatRect{{0, 0}, {200, 100}}));
Expand Down

0 comments on commit e15fad7

Please sign in to comment.