Skip to content

Commit

Permalink
Added method to Canvas to ignore mouse events and let them pass throu…
Browse files Browse the repository at this point in the history
…gh the canvas to the widgets behind it (for cases where the canvas is cleared with Color::Transparent to render something on top of a widget)
  • Loading branch information
texus committed Nov 23, 2023
1 parent 1905cce commit 260cdb3
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 81 deletions.
13 changes: 2 additions & 11 deletions include/TGUI/Backend/Renderer/GLES2/CanvasGLES2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#if !TGUI_BUILD_AS_CXX_MODULE
#include <TGUI/Backend/Renderer/BackendRenderTarget.hpp>
#include <TGUI/Widgets/ClickableWidget.hpp>
#include <TGUI/Widgets/CanvasBase.hpp>
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -61,7 +61,7 @@ TGUI_MODULE_EXPORT namespace tgui
/// glBindFramebuffer(GL_FRAMEBUFFER, 0); // Let further drawing happen on the window again
/// @endcode
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API CanvasGLES2 : public ClickableWidget
class TGUI_API CanvasGLES2 : public CanvasBase
{
public:

Expand Down Expand Up @@ -153,15 +153,6 @@ TGUI_MODULE_EXPORT namespace tgui
void draw(BackendRenderTarget& target, RenderStates states) const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget can gain focus
/// @return Can the widget be focused?
///
/// This function returns false for Canvas widgets.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool canGainFocus() const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Binds the framebuffer of the canvas
///
Expand Down
13 changes: 2 additions & 11 deletions include/TGUI/Backend/Renderer/OpenGL3/CanvasOpenGL3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#if !TGUI_BUILD_AS_CXX_MODULE
#include <TGUI/Backend/Renderer/BackendRenderTarget.hpp>
#include <TGUI/Widgets/ClickableWidget.hpp>
#include <TGUI/Widgets/CanvasBase.hpp>
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -61,7 +61,7 @@ TGUI_MODULE_EXPORT namespace tgui
/// glBindFramebuffer(GL_FRAMEBUFFER, 0); // Let further drawing happen on the window again
/// @endcode
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API CanvasOpenGL3 : public ClickableWidget
class TGUI_API CanvasOpenGL3 : public CanvasBase
{
public:

Expand Down Expand Up @@ -153,15 +153,6 @@ TGUI_MODULE_EXPORT namespace tgui
void draw(BackendRenderTarget& target, RenderStates states) const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget can gain focus
/// @return Can the widget be focused?
///
/// This function returns false for Canvas widgets.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool canGainFocus() const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Binds the framebuffer of the canvas
///
Expand Down
13 changes: 2 additions & 11 deletions include/TGUI/Backend/Renderer/SDL_Renderer/CanvasSDL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#if !TGUI_BUILD_AS_CXX_MODULE
#include <TGUI/Backend/Renderer/BackendRenderTarget.hpp>
#include <TGUI/Widgets/ClickableWidget.hpp>
#include <TGUI/Widgets/CanvasBase.hpp>
#endif

#include <TGUI/extlibs/IncludeSDL.hpp>
Expand Down Expand Up @@ -65,7 +65,7 @@ TGUI_MODULE_EXPORT namespace tgui
/// SDL_SetRenderTarget(renderer, nullptr); // Let further drawing happen on the window again
/// @endcode
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API CanvasSDL : public ClickableWidget
class TGUI_API CanvasSDL : public CanvasBase
{
public:

Expand Down Expand Up @@ -151,15 +151,6 @@ TGUI_MODULE_EXPORT namespace tgui
void draw(BackendRenderTarget& target, RenderStates states) const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget can gain focus
/// @return Can the widget be focused?
///
/// This function returns false for Canvas widgets.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool canGainFocus() const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Access the internal texture on which you can render
///
Expand Down
13 changes: 2 additions & 11 deletions include/TGUI/Backend/Renderer/SFML-Graphics/CanvasSFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#if !TGUI_BUILD_AS_CXX_MODULE
#include <TGUI/Backend/Renderer/BackendRenderTarget.hpp>
#include <TGUI/Widgets/ClickableWidget.hpp>
#include <TGUI/Widgets/CanvasBase.hpp>
#endif

#include <SFML/Graphics/RenderTexture.hpp>
Expand Down Expand Up @@ -66,7 +66,7 @@ TGUI_MODULE_EXPORT namespace tgui
/// canvas->display(); // Save what was drawn on the canvas
/// @endcode
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API CanvasSFML : public ClickableWidget
class TGUI_API CanvasSFML : public CanvasBase
{
public:

Expand Down Expand Up @@ -251,15 +251,6 @@ TGUI_MODULE_EXPORT namespace tgui
void draw(BackendRenderTarget& target, RenderStates states) const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget can gain focus
/// @return Can the widget be focused?
///
/// This function returns false for Canvas widgets.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool canGainFocus() const override;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Access the internal render texture that the canvas widget uses to draw on
///
Expand Down
105 changes: 105 additions & 0 deletions include/TGUI/Widgets/CanvasBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2023 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_CANVAS_BASE_HPP
#define TGUI_CANVAS_BASE_HPP

#include <TGUI/Widgets/ClickableWidget.hpp>

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

TGUI_MODULE_EXPORT namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// This class is used as base class for the backend-specific canvas widgets
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API CanvasBase : public ClickableWidget
{
public:

using Ptr = std::shared_ptr<CanvasBase>; //!< Shared widget pointer
using ConstPtr = std::shared_ptr<const CanvasBase>; //!< Shared constant widget pointer

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @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.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CanvasBase(const char* typeName, bool initRenderer);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Sets whether the widget should completely ignore mouse events and let them pass to the widgets behind it
///
/// @param ignore Should mouse events be ignored by this widget?
///
/// By default, mouse events are NOT ignored.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ignoreMouseEvents(bool ignore = true);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget is ignoring mouse events and letting them pass to the widgets behind it
///
/// @return Are mouse events ignored by this widget?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isIgnoringMouseEvents() const;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the widget can gain focus
/// @return Can the widget be focused?
///
/// This function returns false for Canvas widgets.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool canGainFocus() const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget
/// @return Is the mouse on top of the widget?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Saves the widget as a tree node in order to save it to a file
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Loads the widget from a tree of nodes
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:

bool m_ignoringMouseEvents = false;
};
}

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

#endif // TGUI_CANVAS_BASE_HPP
11 changes: 2 additions & 9 deletions src/Backend/Renderer/GLES2/CanvasGLES2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasGLES2::CanvasGLES2(const char* typeName, bool initRenderer) :
ClickableWidget{typeName, initRenderer}
CanvasBase{typeName, initRenderer}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererGLES2>(getBackend()->getRenderer()),
"CanvasGLES2 can only be created when using the GLES2 backend renderer");
Expand All @@ -52,7 +52,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasGLES2::CanvasGLES2(const CanvasGLES2& other) :
ClickableWidget{other}
CanvasBase{other}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererGLES2>(getBackend()->getRenderer()),
"CanvasGLES2 can only be used when using the GLES2 backend renderer");
Expand Down Expand Up @@ -160,13 +160,6 @@ namespace tgui

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

bool CanvasGLES2::canGainFocus() const
{
return false;
}

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

Widget::Ptr CanvasGLES2::clone() const
{
return std::make_shared<CanvasGLES2>(*this);
Expand Down
11 changes: 2 additions & 9 deletions src/Backend/Renderer/OpenGL3/CanvasOpenGL3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasOpenGL3::CanvasOpenGL3(const char* typeName, bool initRenderer) :
ClickableWidget{typeName, initRenderer}
CanvasBase{typeName, initRenderer}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererOpenGL3>(getBackend()->getRenderer()),
"CanvasOpenGL3 can only be created when using the OpenGL3 backend renderer");
Expand All @@ -52,7 +52,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasOpenGL3::CanvasOpenGL3(const CanvasOpenGL3& other) :
ClickableWidget{other}
CanvasBase{other}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererOpenGL3>(getBackend()->getRenderer()),
"CanvasOpenGL3 can only be used when using the OpenGL3 backend renderer");
Expand Down Expand Up @@ -160,13 +160,6 @@ namespace tgui

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

bool CanvasOpenGL3::canGainFocus() const
{
return false;
}

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

Widget::Ptr CanvasOpenGL3::clone() const
{
return std::make_shared<CanvasOpenGL3>(*this);
Expand Down
11 changes: 2 additions & 9 deletions src/Backend/Renderer/SDL_Renderer/CanvasSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasSDL::CanvasSDL(const char* typeName, bool initRenderer) :
ClickableWidget{typeName, initRenderer}
CanvasBase{typeName, initRenderer}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererSDL>(getBackend()->getRenderer()),
"CanvasSDL can only be created when using the SDL_Renderer backend renderer");
Expand All @@ -53,7 +53,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasSDL::CanvasSDL(const CanvasSDL& other) :
ClickableWidget{other}
CanvasBase{other}
{
TGUI_ASSERT(isBackendSet() && getBackend()->hasRenderer() && std::dynamic_pointer_cast<BackendRendererSDL>(getBackend()->getRenderer()),
"CanvasSDL can only be used when using the SDL_Renderer backend renderer");
Expand Down Expand Up @@ -154,13 +154,6 @@ namespace tgui

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

bool CanvasSDL::canGainFocus() const
{
return false;
}

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

Widget::Ptr CanvasSDL::clone() const
{
return std::make_shared<CanvasSDL>(*this);
Expand Down
13 changes: 3 additions & 10 deletions src/Backend/Renderer/SFML-Graphics/CanvasSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

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

CanvasSFML::CanvasSFML(const CanvasSFML& other) :
ClickableWidget {other},
CanvasBase{other},
m_usedTextureSize{other.m_usedTextureSize}
{
setSize(other.getSize());
Expand All @@ -58,7 +58,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CanvasSFML::CanvasSFML(CanvasSFML&& other) noexcept :
ClickableWidget {std::move(other)},
CanvasBase{std::move(other)},
m_usedTextureSize{std::move(other.m_usedTextureSize)}
{
// sf::RenderTexture does not support move yet
Expand Down Expand Up @@ -283,13 +283,6 @@ namespace tgui

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

bool CanvasSFML::canGainFocus() const
{
return false;
}

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

Widget::Ptr CanvasSFML::clone() const
{
return std::make_shared<CanvasSFML>(*this);
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ set(TGUI_SRC
Widgets/BoxLayoutRatios.cpp
Widgets/Button.cpp
Widgets/ButtonBase.cpp
Widgets/CanvasBase.cpp
Widgets/ChatBox.cpp
Widgets/CheckBox.cpp
Widgets/ChildWindow.cpp
Expand Down
Loading

0 comments on commit 260cdb3

Please sign in to comment.