|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- DrawableShape::DrawableShape()
- : strokeType (0.0f),
- mainFill (Colours::black),
- strokeFill (Colours::black)
- {
- }
-
- DrawableShape::DrawableShape (const DrawableShape& other)
- : Drawable (other),
- strokeType (other.strokeType),
- mainFill (other.mainFill),
- strokeFill (other.strokeFill)
- {
- }
-
- DrawableShape::~DrawableShape()
- {
- }
-
- //==============================================================================
- class DrawableShape::RelativePositioner : public RelativeCoordinatePositionerBase
- {
- public:
- RelativePositioner (DrawableShape& comp, const DrawableShape::RelativeFillType& f, bool isMain)
- : RelativeCoordinatePositionerBase (comp),
- owner (comp),
- fill (f),
- isMainFill (isMain)
- {
- }
-
- bool registerCoordinates() override
- {
- bool ok = addPoint (fill.gradientPoint1);
- ok = addPoint (fill.gradientPoint2) && ok;
- return addPoint (fill.gradientPoint3) && ok;
- }
-
- void applyToComponentBounds() override
- {
- ComponentScope scope (owner);
- if (isMainFill ? owner.mainFill.recalculateCoords (&scope)
- : owner.strokeFill.recalculateCoords (&scope))
- owner.repaint();
- }
-
- void applyNewBounds (const Rectangle<int>&) override
- {
- jassertfalse; // drawables can't be resized directly!
- }
-
- private:
- DrawableShape& owner;
- const DrawableShape::RelativeFillType fill;
- const bool isMainFill;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RelativePositioner)
- };
-
- void DrawableShape::setFill (const FillType& newFill)
- {
- setFill (RelativeFillType (newFill));
- }
-
- void DrawableShape::setStrokeFill (const FillType& newFill)
- {
- setStrokeFill (RelativeFillType (newFill));
- }
-
- void DrawableShape::setFillInternal (RelativeFillType& fill, const RelativeFillType& newFill,
- ScopedPointer<RelativeCoordinatePositionerBase>& pos)
- {
- if (fill != newFill)
- {
- fill = newFill;
- pos = nullptr;
-
- if (fill.isDynamic())
- {
- pos = new RelativePositioner (*this, fill, true);
- pos->apply();
- }
- else
- {
- fill.recalculateCoords (nullptr);
- }
-
- repaint();
- }
- }
-
- void DrawableShape::setFill (const RelativeFillType& newFill)
- {
- setFillInternal (mainFill, newFill, mainFillPositioner);
- }
-
- void DrawableShape::setStrokeFill (const RelativeFillType& newFill)
- {
- setFillInternal (strokeFill, newFill, strokeFillPositioner);
- }
-
- void DrawableShape::setStrokeType (const PathStrokeType& newStrokeType)
- {
- if (strokeType != newStrokeType)
- {
- strokeType = newStrokeType;
- strokeChanged();
- }
- }
-
- void DrawableShape::setStrokeThickness (const float newThickness)
- {
- setStrokeType (PathStrokeType (newThickness, strokeType.getJointStyle(), strokeType.getEndStyle()));
- }
-
- bool DrawableShape::isStrokeVisible() const noexcept
- {
- return strokeType.getStrokeThickness() > 0.0f && ! strokeFill.fill.isInvisible();
- }
-
- void DrawableShape::refreshFillTypes (const FillAndStrokeState& newState, ComponentBuilder::ImageProvider* imageProvider)
- {
- setFill (newState.getFill (FillAndStrokeState::fill, imageProvider));
- setStrokeFill (newState.getFill (FillAndStrokeState::stroke, imageProvider));
- }
-
- void DrawableShape::writeTo (FillAndStrokeState& state, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
- {
- state.setFill (FillAndStrokeState::fill, mainFill, imageProvider, undoManager);
- state.setFill (FillAndStrokeState::stroke, strokeFill, imageProvider, undoManager);
- state.setStrokeType (strokeType, undoManager);
- }
-
- //==============================================================================
- void DrawableShape::paint (Graphics& g)
- {
- transformContextToCorrectOrigin (g);
-
- g.setFillType (mainFill.fill);
- g.fillPath (path);
-
- if (isStrokeVisible())
- {
- g.setFillType (strokeFill.fill);
- g.fillPath (strokePath);
- }
- }
-
- void DrawableShape::pathChanged()
- {
- strokeChanged();
- }
-
- void DrawableShape::strokeChanged()
- {
- strokePath.clear();
- strokeType.createStrokedPath (strokePath, path, AffineTransform::identity, 4.0f);
-
- setBoundsToEnclose (getDrawableBounds());
- repaint();
- }
-
- Rectangle<float> DrawableShape::getDrawableBounds() const
- {
- if (isStrokeVisible())
- return strokePath.getBounds();
-
- return path.getBounds();
- }
-
- bool DrawableShape::hitTest (int x, int y)
- {
- bool allowsClicksOnThisComponent, allowsClicksOnChildComponents;
- getInterceptsMouseClicks (allowsClicksOnThisComponent, allowsClicksOnChildComponents);
-
- if (! allowsClicksOnThisComponent)
- return false;
-
- const float globalX = (float) (x - originRelativeToComponent.x);
- const float globalY = (float) (y - originRelativeToComponent.y);
-
- return path.contains (globalX, globalY)
- || (isStrokeVisible() && strokePath.contains (globalX, globalY));
- }
-
- //==============================================================================
- DrawableShape::RelativeFillType::RelativeFillType()
- {
- }
-
- DrawableShape::RelativeFillType::RelativeFillType (const FillType& fill_)
- : fill (fill_)
- {
- if (fill.isGradient())
- {
- const ColourGradient& g = *fill.gradient;
-
- gradientPoint1 = g.point1.transformedBy (fill.transform);
- gradientPoint2 = g.point2.transformedBy (fill.transform);
- gradientPoint3 = Point<float> (g.point1.x + g.point2.y - g.point1.y,
- g.point1.y + g.point1.x - g.point2.x)
- .transformedBy (fill.transform);
- fill.transform = AffineTransform::identity;
- }
- }
-
- DrawableShape::RelativeFillType::RelativeFillType (const RelativeFillType& other)
- : fill (other.fill),
- gradientPoint1 (other.gradientPoint1),
- gradientPoint2 (other.gradientPoint2),
- gradientPoint3 (other.gradientPoint3)
- {
- }
-
- DrawableShape::RelativeFillType& DrawableShape::RelativeFillType::operator= (const RelativeFillType& other)
- {
- fill = other.fill;
- gradientPoint1 = other.gradientPoint1;
- gradientPoint2 = other.gradientPoint2;
- gradientPoint3 = other.gradientPoint3;
- return *this;
- }
-
- bool DrawableShape::RelativeFillType::operator== (const RelativeFillType& other) const
- {
- return fill == other.fill
- && ((! fill.isGradient())
- || (gradientPoint1 == other.gradientPoint1
- && gradientPoint2 == other.gradientPoint2
- && gradientPoint3 == other.gradientPoint3));
- }
-
- bool DrawableShape::RelativeFillType::operator!= (const RelativeFillType& other) const
- {
- return ! operator== (other);
- }
-
- bool DrawableShape::RelativeFillType::recalculateCoords (Expression::Scope* scope)
- {
- if (fill.isGradient())
- {
- const Point<float> g1 (gradientPoint1.resolve (scope));
- const Point<float> g2 (gradientPoint2.resolve (scope));
- AffineTransform t;
-
- ColourGradient& g = *fill.gradient;
-
- if (g.isRadial)
- {
- const Point<float> g3 (gradientPoint3.resolve (scope));
- const Point<float> g3Source (g1.x + g2.y - g1.y,
- g1.y + g1.x - g2.x);
-
- t = AffineTransform::fromTargetPoints (g1.x, g1.y, g1.x, g1.y,
- g2.x, g2.y, g2.x, g2.y,
- g3Source.x, g3Source.y, g3.x, g3.y);
- }
-
- if (g.point1 != g1 || g.point2 != g2 || fill.transform != t)
- {
- g.point1 = g1;
- g.point2 = g2;
- fill.transform = t;
- return true;
- }
- }
-
- return false;
- }
-
- bool DrawableShape::RelativeFillType::isDynamic() const
- {
- return gradientPoint1.isDynamic() || gradientPoint2.isDynamic() || gradientPoint3.isDynamic();
- }
-
- void DrawableShape::RelativeFillType::writeTo (ValueTree& v, ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager) const
- {
- if (fill.isColour())
- {
- v.setProperty (FillAndStrokeState::type, "solid", undoManager);
- v.setProperty (FillAndStrokeState::colour, String::toHexString ((int) fill.colour.getARGB()), undoManager);
- }
- else if (fill.isGradient())
- {
- v.setProperty (FillAndStrokeState::type, "gradient", undoManager);
- v.setProperty (FillAndStrokeState::gradientPoint1, gradientPoint1.toString(), undoManager);
- v.setProperty (FillAndStrokeState::gradientPoint2, gradientPoint2.toString(), undoManager);
- v.setProperty (FillAndStrokeState::gradientPoint3, gradientPoint3.toString(), undoManager);
-
- const ColourGradient& cg = *fill.gradient;
- v.setProperty (FillAndStrokeState::radial, cg.isRadial, undoManager);
-
- String s;
- for (int i = 0; i < cg.getNumColours(); ++i)
- s << ' ' << cg.getColourPosition (i)
- << ' ' << String::toHexString ((int) cg.getColour(i).getARGB());
-
- v.setProperty (FillAndStrokeState::colours, s.trimStart(), undoManager);
- }
- else if (fill.isTiledImage())
- {
- v.setProperty (FillAndStrokeState::type, "image", undoManager);
-
- if (imageProvider != nullptr)
- v.setProperty (FillAndStrokeState::imageId, imageProvider->getIdentifierForImage (fill.image), undoManager);
-
- if (fill.getOpacity() < 1.0f)
- v.setProperty (FillAndStrokeState::imageOpacity, fill.getOpacity(), undoManager);
- else
- v.removeProperty (FillAndStrokeState::imageOpacity, undoManager);
- }
- else
- {
- jassertfalse;
- }
- }
-
- bool DrawableShape::RelativeFillType::readFrom (const ValueTree& v, ComponentBuilder::ImageProvider* imageProvider)
- {
- const String newType (v [FillAndStrokeState::type].toString());
-
- if (newType == "solid")
- {
- const String colourString (v [FillAndStrokeState::colour].toString());
- fill.setColour (colourString.isEmpty() ? Colours::black
- : Colour::fromString (colourString));
- return true;
- }
- else if (newType == "gradient")
- {
- ColourGradient g;
- g.isRadial = v [FillAndStrokeState::radial];
-
- StringArray colourSteps;
- colourSteps.addTokens (v [FillAndStrokeState::colours].toString(), false);
-
- for (int i = 0; i < colourSteps.size() / 2; ++i)
- g.addColour (colourSteps[i * 2].getDoubleValue(),
- Colour::fromString (colourSteps[i * 2 + 1]));
-
- fill.setGradient (g);
-
- gradientPoint1 = RelativePoint (v [FillAndStrokeState::gradientPoint1]);
- gradientPoint2 = RelativePoint (v [FillAndStrokeState::gradientPoint2]);
- gradientPoint3 = RelativePoint (v [FillAndStrokeState::gradientPoint3]);
- return true;
- }
- else if (newType == "image")
- {
- Image im;
- if (imageProvider != nullptr)
- im = imageProvider->getImageForIdentifier (v [FillAndStrokeState::imageId]);
-
- fill.setTiledImage (im, AffineTransform::identity);
- fill.setOpacity ((float) v.getProperty (FillAndStrokeState::imageOpacity, 1.0f));
- return true;
- }
-
- jassertfalse;
- return false;
- }
-
- //==============================================================================
- const Identifier DrawableShape::FillAndStrokeState::type ("type");
- const Identifier DrawableShape::FillAndStrokeState::colour ("colour");
- const Identifier DrawableShape::FillAndStrokeState::colours ("colours");
- const Identifier DrawableShape::FillAndStrokeState::fill ("Fill");
- const Identifier DrawableShape::FillAndStrokeState::stroke ("Stroke");
- const Identifier DrawableShape::FillAndStrokeState::path ("Path");
- const Identifier DrawableShape::FillAndStrokeState::jointStyle ("jointStyle");
- const Identifier DrawableShape::FillAndStrokeState::capStyle ("capStyle");
- const Identifier DrawableShape::FillAndStrokeState::strokeWidth ("strokeWidth");
- const Identifier DrawableShape::FillAndStrokeState::gradientPoint1 ("point1");
- const Identifier DrawableShape::FillAndStrokeState::gradientPoint2 ("point2");
- const Identifier DrawableShape::FillAndStrokeState::gradientPoint3 ("point3");
- const Identifier DrawableShape::FillAndStrokeState::radial ("radial");
- const Identifier DrawableShape::FillAndStrokeState::imageId ("imageId");
- const Identifier DrawableShape::FillAndStrokeState::imageOpacity ("imageOpacity");
-
- DrawableShape::FillAndStrokeState::FillAndStrokeState (const ValueTree& state_)
- : Drawable::ValueTreeWrapperBase (state_)
- {
- }
-
- DrawableShape::RelativeFillType DrawableShape::FillAndStrokeState::getFill (const Identifier& fillOrStrokeType, ComponentBuilder::ImageProvider* imageProvider) const
- {
- DrawableShape::RelativeFillType f;
- f.readFrom (state.getChildWithName (fillOrStrokeType), imageProvider);
- return f;
- }
-
- ValueTree DrawableShape::FillAndStrokeState::getFillState (const Identifier& fillOrStrokeType)
- {
- ValueTree v (state.getChildWithName (fillOrStrokeType));
- if (v.isValid())
- return v;
-
- setFill (fillOrStrokeType, FillType (Colours::black), nullptr, nullptr);
- return getFillState (fillOrStrokeType);
- }
-
- void DrawableShape::FillAndStrokeState::setFill (const Identifier& fillOrStrokeType, const RelativeFillType& newFill,
- ComponentBuilder::ImageProvider* imageProvider, UndoManager* undoManager)
- {
- ValueTree v (state.getOrCreateChildWithName (fillOrStrokeType, undoManager));
- newFill.writeTo (v, imageProvider, undoManager);
- }
-
- PathStrokeType DrawableShape::FillAndStrokeState::getStrokeType() const
- {
- const String jointStyleString (state [jointStyle].toString());
- const String capStyleString (state [capStyle].toString());
-
- return PathStrokeType (state [strokeWidth],
- jointStyleString == "curved" ? PathStrokeType::curved
- : (jointStyleString == "bevel" ? PathStrokeType::beveled
- : PathStrokeType::mitered),
- capStyleString == "square" ? PathStrokeType::square
- : (capStyleString == "round" ? PathStrokeType::rounded
- : PathStrokeType::butt));
- }
-
- void DrawableShape::FillAndStrokeState::setStrokeType (const PathStrokeType& newStrokeType, UndoManager* undoManager)
- {
- state.setProperty (strokeWidth, (double) newStrokeType.getStrokeThickness(), undoManager);
- state.setProperty (jointStyle, newStrokeType.getJointStyle() == PathStrokeType::mitered
- ? "miter" : (newStrokeType.getJointStyle() == PathStrokeType::curved ? "curved" : "bevel"), undoManager);
- state.setProperty (capStyle, newStrokeType.getEndStyle() == PathStrokeType::butt
- ? "butt" : (newStrokeType.getEndStyle() == PathStrokeType::square ? "square" : "round"), undoManager);
- }
-
- static bool replaceColourInFill (DrawableShape::RelativeFillType& fill, Colour original, Colour replacement)
- {
- if (fill.fill.colour == original && fill.fill.isColour())
- {
- fill = FillType (replacement);
- return true;
- }
-
- return false;
- }
-
- bool DrawableShape::replaceColour (Colour original, Colour replacement)
- {
- bool changed1 = replaceColourInFill (mainFill, original, replacement);
- bool changed2 = replaceColourInFill (strokeFill, original, replacement);
- return changed1 || changed2;
- }
|