Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. Drawable::Drawable()
  18. {
  19. setInterceptsMouseClicks (false, false);
  20. setPaintingIsUnclipped (true);
  21. }
  22. Drawable::Drawable (const Drawable& other)
  23. : Component (other.getName())
  24. {
  25. setComponentID (other.getComponentID());
  26. }
  27. Drawable::~Drawable()
  28. {
  29. }
  30. //==============================================================================
  31. void Drawable::draw (Graphics& g, float opacity, const AffineTransform& transform) const
  32. {
  33. const_cast <Drawable*> (this)->nonConstDraw (g, opacity, transform);
  34. }
  35. void Drawable::nonConstDraw (Graphics& g, float opacity, const AffineTransform& transform)
  36. {
  37. Graphics::ScopedSaveState ss (g);
  38. g.addTransform (AffineTransform::translation ((float) -(originRelativeToComponent.x),
  39. (float) -(originRelativeToComponent.y))
  40. .followedBy (getTransform())
  41. .followedBy (transform));
  42. if (! g.isClipEmpty())
  43. {
  44. if (opacity < 1.0f)
  45. {
  46. g.beginTransparencyLayer (opacity);
  47. paintEntireComponent (g, true);
  48. g.endTransparencyLayer();
  49. }
  50. else
  51. {
  52. paintEntireComponent (g, true);
  53. }
  54. }
  55. }
  56. void Drawable::drawAt (Graphics& g, float x, float y, float opacity) const
  57. {
  58. draw (g, opacity, AffineTransform::translation (x, y));
  59. }
  60. void Drawable::drawWithin (Graphics& g, const Rectangle<float>& destArea,
  61. RectanglePlacement placement, float opacity) const
  62. {
  63. draw (g, opacity, placement.getTransformToFit (getDrawableBounds(), destArea));
  64. }
  65. //==============================================================================
  66. DrawableComposite* Drawable::getParent() const
  67. {
  68. return dynamic_cast <DrawableComposite*> (getParentComponent());
  69. }
  70. void Drawable::transformContextToCorrectOrigin (Graphics& g)
  71. {
  72. g.setOrigin (originRelativeToComponent);
  73. }
  74. void Drawable::parentHierarchyChanged()
  75. {
  76. setBoundsToEnclose (getDrawableBounds());
  77. }
  78. void Drawable::setBoundsToEnclose (const Rectangle<float>& area)
  79. {
  80. Drawable* const parent = getParent();
  81. Point<int> parentOrigin;
  82. if (parent != nullptr)
  83. parentOrigin = parent->originRelativeToComponent;
  84. const Rectangle<int> newBounds (area.getSmallestIntegerContainer() + parentOrigin);
  85. originRelativeToComponent = parentOrigin - newBounds.getPosition();
  86. setBounds (newBounds);
  87. }
  88. //==============================================================================
  89. void Drawable::setOriginWithOriginalSize (Point<float> originWithinParent)
  90. {
  91. setTransform (AffineTransform::translation (originWithinParent.x, originWithinParent.y));
  92. }
  93. void Drawable::setTransformToFit (const Rectangle<float>& area, RectanglePlacement placement)
  94. {
  95. if (! area.isEmpty())
  96. setTransform (placement.getTransformToFit (getDrawableBounds(), area));
  97. }
  98. //==============================================================================
  99. Drawable* Drawable::createFromImageData (const void* data, const size_t numBytes)
  100. {
  101. Drawable* result = nullptr;
  102. Image image (ImageFileFormat::loadFrom (data, numBytes));
  103. if (image.isValid())
  104. {
  105. DrawableImage* const di = new DrawableImage();
  106. di->setImage (image);
  107. result = di;
  108. }
  109. else
  110. {
  111. const String asString (String::createStringFromData (data, (int) numBytes));
  112. XmlDocument doc (asString);
  113. ScopedPointer <XmlElement> outer (doc.getDocumentElement (true));
  114. if (outer != nullptr && outer->hasTagName ("svg"))
  115. {
  116. ScopedPointer <XmlElement> svg (doc.getDocumentElement());
  117. if (svg != nullptr)
  118. result = Drawable::createFromSVG (*svg);
  119. }
  120. }
  121. return result;
  122. }
  123. Drawable* Drawable::createFromImageDataStream (InputStream& dataSource)
  124. {
  125. MemoryOutputStream mo;
  126. mo << dataSource;
  127. return createFromImageData (mo.getData(), mo.getDataSize());
  128. }
  129. Drawable* Drawable::createFromImageFile (const File& file)
  130. {
  131. FileInputStream fin (file);
  132. return fin.openedOk() ? createFromImageDataStream (fin) : nullptr;
  133. }
  134. //==============================================================================
  135. template <class DrawableClass>
  136. class DrawableTypeHandler : public ComponentBuilder::TypeHandler
  137. {
  138. public:
  139. DrawableTypeHandler()
  140. : ComponentBuilder::TypeHandler (DrawableClass::valueTreeType)
  141. {
  142. }
  143. Component* addNewComponentFromState (const ValueTree& state, Component* parent)
  144. {
  145. DrawableClass* const d = new DrawableClass();
  146. if (parent != nullptr)
  147. parent->addAndMakeVisible (d);
  148. updateComponentFromState (d, state);
  149. return d;
  150. }
  151. void updateComponentFromState (Component* component, const ValueTree& state)
  152. {
  153. DrawableClass* const d = dynamic_cast <DrawableClass*> (component);
  154. jassert (d != nullptr);
  155. d->refreshFromValueTree (state, *this->getBuilder());
  156. }
  157. };
  158. void Drawable::registerDrawableTypeHandlers (ComponentBuilder& builder)
  159. {
  160. builder.registerTypeHandler (new DrawableTypeHandler <DrawablePath>());
  161. builder.registerTypeHandler (new DrawableTypeHandler <DrawableComposite>());
  162. builder.registerTypeHandler (new DrawableTypeHandler <DrawableRectangle>());
  163. builder.registerTypeHandler (new DrawableTypeHandler <DrawableImage>());
  164. builder.registerTypeHandler (new DrawableTypeHandler <DrawableText>());
  165. }
  166. Drawable* Drawable::createFromValueTree (const ValueTree& tree, ComponentBuilder::ImageProvider* imageProvider)
  167. {
  168. ComponentBuilder builder (tree);
  169. builder.setImageProvider (imageProvider);
  170. registerDrawableTypeHandlers (builder);
  171. ScopedPointer<Component> comp (builder.createComponent());
  172. Drawable* const d = dynamic_cast<Drawable*> (static_cast <Component*> (comp));
  173. if (d != nullptr)
  174. comp.release();
  175. return d;
  176. }
  177. //==============================================================================
  178. Drawable::ValueTreeWrapperBase::ValueTreeWrapperBase (const ValueTree& state_)
  179. : state (state_)
  180. {
  181. }
  182. String Drawable::ValueTreeWrapperBase::getID() const
  183. {
  184. return state [ComponentBuilder::idProperty];
  185. }
  186. void Drawable::ValueTreeWrapperBase::setID (const String& newID)
  187. {
  188. if (newID.isEmpty())
  189. state.removeProperty (ComponentBuilder::idProperty, nullptr);
  190. else
  191. state.setProperty (ComponentBuilder::idProperty, newID, nullptr);
  192. }