elanhickler

Untitled

Jan 1st, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. class SVGObject : Drawable
  2. {
  3. public:
  4.   SVGObject() {}
  5.  
  6.   SVGObject(const String & svg)
  7.   {
  8.     if (svg.isEmpty())
  9.       return;
  10.  
  11.     ScopedPointer<XmlElement> xml(XmlDocument::parse(svg));
  12.     drawable = createFromSVG(*xml);
  13.     svgBounds = static_cast<DrawableComposite*>(drawable.get())->getContentArea();
  14.   }
  15.   ~SVGObject() = default;
  16.  
  17.   Drawable* createCopy() const override
  18.   {
  19.     return static_cast<DrawableComposite*>(drawable.get())->createCopy();
  20.   }
  21.  
  22.   void draw(Graphics & g, const juce::Rectangle<float> & rectangle, float opacity = 1.f) const
  23.   {
  24.     juce::AffineTransform xform = RectanglePlacement(RectanglePlacement::centred).getTransformToFit(svgBounds, rectangle);
  25.     drawable->draw(g, opacity, xform);
  26.   }
  27.  
  28.   void drawWithColor(Graphics & g, const juce::Rectangle<float> & rectangle, const Colour & colorToReplace, const Colour & newColor, float opacity = 1.f)
  29.   {
  30.     juce::AffineTransform xform = RectanglePlacement(RectanglePlacement::centred).getTransformToFit(svgBounds, rectangle);
  31.     drawable->replaceColour(colorToReplace, newColor);
  32.     drawable->draw(g, opacity, xform);
  33.     drawable->replaceColour(newColor, colorToReplace);
  34.   }
  35.  
  36.   void drawWithRotation(Graphics & g, const juce::Rectangle<float> & rectangle, float rotationDegrees, float opacity = 1.f) const
  37.   {
  38.     juce::AffineTransform xform = RectanglePlacement(RectanglePlacement::centred).getTransformToFit(svgBounds, rectangle)
  39.       .rotated(juce::degreesToRadians(rotationDegrees), rectangle.getCentreX(), rectangle.getCentreY());
  40.  
  41.     drawable->draw(g, opacity, xform);
  42.   }
  43.  
  44.   Rectangle<float> getDrawableBounds() const override { return svgBounds; }
  45.  
  46.   Path getOutlineAsPath() const override { jassertfalse;  return {}; };
  47.  
  48.   bool isValid()
  49.   {
  50.     return drawable != nullptr;
  51.   }
  52.  
  53. protected:
  54.   juce::Rectangle<float> svgBounds;
  55.   ScopedPointer<Drawable> drawable;
  56. };
Advertisement
Add Comment
Please, Sign In to add comment