From a99422efeef09ec07c4f7a67d07da8c078d55cd6 Mon Sep 17 00:00:00 2001 From: attila Date: Thu, 14 Jul 2022 19:12:41 +0200 Subject: [PATCH] Drawable: Add setDrawableTransform() and use it in SVGParser Using this new function a previous bug is avoided where transforms were applied differently to drawable paths and text elements. --- .../juce_gui_basics/drawables/juce_Drawable.cpp | 15 +++++++++++++++ modules/juce_gui_basics/drawables/juce_Drawable.h | 14 ++++++++++++++ .../juce_gui_basics/drawables/juce_SVGParser.cpp | 4 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/modules/juce_gui_basics/drawables/juce_Drawable.cpp b/modules/juce_gui_basics/drawables/juce_Drawable.cpp index 2f21435058..1a902e6a5d 100644 --- a/modules/juce_gui_basics/drawables/juce_Drawable.cpp +++ b/modules/juce_gui_basics/drawables/juce_Drawable.cpp @@ -140,6 +140,7 @@ void Drawable::setBoundsToEnclose (Rectangle area) auto newBounds = area.getSmallestIntegerContainer() + parentOrigin; originRelativeToComponent = parentOrigin - newBounds.getPosition(); setBounds (newBounds); + updateTransform(); } //============================================================================== @@ -154,6 +155,20 @@ bool Drawable::replaceColour (Colour original, Colour replacement) return changed; } +void Drawable::setDrawableTransform (const AffineTransform& transform) +{ + drawableTransform = transform; + updateTransform(); +} + +void Drawable::updateTransform() +{ + const auto transformationOrigin = originRelativeToComponent + getPosition(); + setTransform (AffineTransform::translation (transformationOrigin * (-1)) + .followedBy (drawableTransform) + .followedBy (AffineTransform::translation (transformationOrigin))); +} + //============================================================================== void Drawable::setOriginWithOriginalSize (Point originWithinParent) { diff --git a/modules/juce_gui_basics/drawables/juce_Drawable.h b/modules/juce_gui_basics/drawables/juce_Drawable.h index 9215a96319..566c59be1a 100644 --- a/modules/juce_gui_basics/drawables/juce_Drawable.h +++ b/modules/juce_gui_basics/drawables/juce_Drawable.h @@ -186,6 +186,18 @@ public: */ virtual bool replaceColour (Colour originalColour, Colour replacementColour); + /** Sets a transformation that applies to the same coordinate system in which the rest of the + draw calls are made. You almost certainly want to call this function when working with + Drawables as opposed to Component::setTransform(). + + The reason for this is that the origin of a Drawable is not the same as the point returned + by Component::getPosition() but has an additional offset internal to the Drawable class. + + Using setDrawableTransform() will take this internal offset into account when applying the + transform to the Component base. + */ + void setDrawableTransform (const AffineTransform& transform); + protected: //============================================================================== friend class DrawableComposite; @@ -202,8 +214,10 @@ protected: Point originRelativeToComponent; std::unique_ptr drawableClipPath; + AffineTransform drawableTransform; void nonConstDraw (Graphics&, float opacity, const AffineTransform&); + void updateTransform(); Drawable (const Drawable&); Drawable& operator= (const Drawable&); diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index 7b4d83cc2c..c96a86227a 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -1093,9 +1093,9 @@ private: dt->setFont (font, true); if (additonalTransform != nullptr) - dt->setTransform (transform.followedBy (*additonalTransform)); + dt->setDrawableTransform (transform.followedBy (*additonalTransform)); else - dt->setTransform (transform); + dt->setDrawableTransform (transform); dt->setColour (parseColour (xml, "fill", Colours::black) .withMultipliedAlpha (parseSafeFloat (getStyleAttribute (xml, "fill-opacity", "1"))));