Skip to content

Commit

Permalink
[render] EventDrawer prepared to request to block event propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 9, 2025
1 parent a6a6ea3 commit 628c83a
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 15 deletions.
24 changes: 12 additions & 12 deletions tests/render/000-static-asserts/drawers.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ void drawersStaticAsserts()
static_assert(
!EventDrawerConcept<PlainDrawer<RendererTypePD>>,
"PlainDrawer does satisfy the EventDrawerConcept");
// static_assert(
// !EventDrawerConcept<const PlainDrawer<RendererTypePD>>,
// "const PlainDrawer does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<const PlainDrawer<RendererTypePD>>,
"const PlainDrawer does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<PlainDrawer<RendererTypePD>&>,
"PlainDrawer& does satisfy the EventDrawerConcept");
// static_assert(
// !EventDrawerConcept<const PlainDrawer<RendererTypePD>&>,
// "const PlainDrawer& does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<const PlainDrawer<RendererTypePD>&>,
"const PlainDrawer& does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<PlainDrawer<RendererTypePD>&&>,
"PlainDrawer&& does satisfy the EventDrawerConcept");
Expand Down Expand Up @@ -174,15 +174,15 @@ void drawersStaticAsserts()
static_assert(
!EventDrawerConcept<TextDrawer<RendererTypeTD>>,
"TextDrawer does satisfy the EventDrawerConcept");
// static_assert(
// !EventDrawerConcept<const TextDrawer<RendererTypeTD>>,
// "const TextDrawer does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<const TextDrawer<RendererTypeTD>>,
"const TextDrawer does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<TextDrawer<RendererTypeTD>&>,
"TextDrawer& does satisfy the EventDrawerConcept");
// static_assert(
// !EventDrawerConcept<const TextDrawer<RendererTypeTD>&>,
// "const TextDrawer& does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<const TextDrawer<RendererTypeTD>&>,
"const TextDrawer& does satisfy the EventDrawerConcept");
static_assert(
!EventDrawerConcept<TextDrawer<RendererTypeTD>&&>,
"TextDrawer&& does satisfy the EventDrawerConcept");
Expand Down
2 changes: 1 addition & 1 deletion vclib/render/include/vclib/imgui/imgui_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
namespace vcl::imgui {

template<typename DerivedRenderer>
class ImguiDrawer : public EventDrawer<DerivedRenderer>
class ImguiDrawer : public EventDrawer<DerivedRenderer, true>
{
protected:
using DRT = DerivedRenderer;
Expand Down
37 changes: 36 additions & 1 deletion vclib/render/include/vclib/render/concepts/event_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
namespace vcl {

template<typename T>
concept EventDrawerConcept = DrawerConcept<T> && requires (T&& obj) {
concept CantBlockEventDrawerConcept = DrawerConcept<T> && requires (T&& obj) {
requires RemoveRef<T>::CAN_BLOCK_EVENT_PROPAGATION == false;

// non const requirements
requires IsConst<T> || requires {
{ obj.onKeyPress(Key::Enum(), KeyModifiers()) } -> std::same_as<void>;
Expand All @@ -56,6 +58,39 @@ concept EventDrawerConcept = DrawerConcept<T> && requires (T&& obj) {
};
};

template<typename T>
concept CanBlockEventDrawerConcept = DrawerConcept<T> && requires (T&& obj) {
requires RemoveRef<T>::CAN_BLOCK_EVENT_PROPAGATION == true;

// non const requirements
requires IsConst<T> || requires {
{ obj.onKeyPress(Key::Enum(), KeyModifiers()) } -> std::same_as<bool>;
{ obj.onKeyRelease(Key::Enum(), KeyModifiers()) } -> std::same_as<bool>;
{
obj.onMouseMove(double(), double(), KeyModifiers())
} -> std::same_as<bool>;
{
obj.onMousePress(
MouseButton::Enum(), double(), double(), KeyModifiers())
} -> std::same_as<bool>;
{
obj.onMouseRelease(
MouseButton::Enum(), double(), double(), KeyModifiers())
} -> std::same_as<bool>;
{
obj.onMouseDoubleClick(
MouseButton::Enum(), double(), double(), KeyModifiers())
} -> std::same_as<bool>;
{
obj.onMouseScroll(double(), double(), KeyModifiers())
} -> std::same_as<bool>;
};
};

template<typename T>
concept EventDrawerConcept =
CanBlockEventDrawerConcept<T> || CantBlockEventDrawerConcept<T>;

} // namespace vcl

#endif // CONCEPTS_EVENT_DRAWER_H
111 changes: 110 additions & 1 deletion vclib/render/include/vclib/render/drawers/event_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,44 @@

namespace vcl {

/**
* @brief The EventDrawer class is a base class for all drawers that handle
* events.
*
* The EventDrawer class is a CRTP class that is templated on the derived
* renderer class. It provides the interface for handling events produced by the
* DerivedRenderer class.
*
* The EventDrawer class is specialized for two cases:
* - CAN_BLOCK_EVENT_PROPAGATION = false: The drawer cannot request to block the
* event propagation. All the event functions do not return any value. @see
* EventDrawer<DerivedRenderer, false>.
* - CAN_BLOCK_EVENT_PROPAGATION = true: The drawer can request to block the
* event propagation. All the event functions return a boolean value that
* indicates if the event propagation is blocked. @see
* EventDrawer<DerivedRenderer, true>.
*
* @tparam DerivedRenderer The type of the derived renderer class.
* @tparam CAN_BLOCK_EVENT_PROPAGATION: A boolean flag that indicates if the
* drawer can request to block the event propagation.
*/
template<typename, bool CAN_BLOCK_EVENT_PROPAGATION = false>
class EventDrawer
{
};

/**
* @brief Default specialization of the EventDrawer class for the case where the
* drawer cannot request to block the event propagation.
*
* @tparam DerivedRenderer The type of the derived renderer class.
*/
template<typename DerivedRenderer>
class EventDrawer : public PlainDrawer<DerivedRenderer>
class EventDrawer<DerivedRenderer, false> : public PlainDrawer<DerivedRenderer>
{
public:
static const bool CAN_BLOCK_EVENT_PROPAGATION = false;

EventDrawer() = default;

EventDrawer(uint, uint) {}
Expand Down Expand Up @@ -77,6 +111,81 @@ class EventDrawer : public PlainDrawer<DerivedRenderer>
}
};

/**
* @brief Specialization of the EventDrawer class for the case where the drawer
* can request to block the event propagation.
*
* A drawer that inherits from this class can request to block the event
* propagation, and (if requested) the event will not be propagated to the other
* drawer arguments of the Renderer class.
*
* All the event functions of this class return a boolean value. If the function
* returns true, the DerivedRenderer will block event propagation to other
* drawers; otherwise, the event is propagated to the other drawers.
*
* @tparam DerivedRenderer The type of the derived renderer class.
*/
template<typename DerivedRenderer>
class EventDrawer<DerivedRenderer, true> : public PlainDrawer<DerivedRenderer>
{
public:
static const bool CAN_BLOCK_EVENT_PROPAGATION = true;

EventDrawer() = default;

EventDrawer(uint, uint) {}

virtual bool onKeyPress(Key::Enum key, const KeyModifiers& modifiers)
{
return false;
}

virtual bool onKeyRelease(Key::Enum key, const KeyModifiers& modifiers)
{
return false;
}

virtual bool onMouseMove(double x, double y, const KeyModifiers& modifiers)
{
return false;
}

virtual bool onMousePress(
MouseButton::Enum button,
double x,
double y,
const KeyModifiers& modifiers)
{
return false;
}

virtual bool onMouseRelease(
MouseButton::Enum button,
double x,
double y,
const KeyModifiers& modifiers)
{
return false;
}

virtual bool onMouseDoubleClick(
MouseButton::Enum button,
double x,
double y,
const KeyModifiers& modifiers)
{
return false;
}

virtual bool onMouseScroll(
double x,
double y,
const KeyModifiers& modifiers)
{
return false;
}
};

} // namespace vcl

#endif // EVENT_DRAWER_H

0 comments on commit 628c83a

Please sign in to comment.