From 365d87ae11a8ae919f26cd5ea6d7849b92f6bd63 Mon Sep 17 00:00:00 2001 From: attila Date: Mon, 9 Oct 2023 09:46:12 +0200 Subject: [PATCH] Drawable: Fix transformations by recalculating them on bounds change --- .../drawables/juce_Drawable.cpp | 1 - .../juce_gui_basics/drawables/juce_Drawable.h | 34 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/juce_gui_basics/drawables/juce_Drawable.cpp b/modules/juce_gui_basics/drawables/juce_Drawable.cpp index d609a0e706..299122135b 100644 --- a/modules/juce_gui_basics/drawables/juce_Drawable.cpp +++ b/modules/juce_gui_basics/drawables/juce_Drawable.cpp @@ -141,7 +141,6 @@ void Drawable::setBoundsToEnclose (Rectangle area) auto newBounds = smallestIntegerContainer + parentOrigin; originRelativeToComponent = -smallestIntegerContainer.getPosition(); setBounds (newBounds); - updateTransform(); } //============================================================================== diff --git a/modules/juce_gui_basics/drawables/juce_Drawable.h b/modules/juce_gui_basics/drawables/juce_Drawable.h index 6d3ca08184..f288a9f270 100644 --- a/modules/juce_gui_basics/drawables/juce_Drawable.h +++ b/modules/juce_gui_basics/drawables/juce_Drawable.h @@ -26,6 +26,33 @@ namespace juce { +#ifndef DOXYGEN +namespace detail +{ +class BoundsChangeListener final : private ComponentListener +{ +public: + BoundsChangeListener (Component& c, std::function cb) + : callback (std::move (cb)), + componentListenerGuard { [comp = &c, this] { comp->removeComponentListener (this); } } + { + jassert (callback != nullptr); + + c.addComponentListener (this); + } + +private: + void componentMovedOrResized (Component&, bool, bool) override + { + callback(); + } + + std::function callback; + ErasedScopeGuard componentListenerGuard; +}; +} // namespace detail +#endif + //============================================================================== /** The base class for objects which can draw themselves, e.g. polygons, images, etc. @@ -220,11 +247,16 @@ protected: AffineTransform drawableTransform; void nonConstDraw (Graphics&, float opacity, const AffineTransform&); - void updateTransform(); Drawable (const Drawable&); Drawable& operator= (const Drawable&); JUCE_LEAK_DETECTOR (Drawable) + + +private: + void updateTransform(); + + detail::BoundsChangeListener boundsChangeListener { *this, [this] { updateTransform(); } }; }; } // namespace juce