Add remaining files
[juce-lv2.git] / juce / source / src / gui / graphics / drawables / juce_DrawableRectangle.cpp
blob769d5ec8f1a7a71b5d973f0af82612ca10e60ec4
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_DrawableRectangle.h"
31 #include "juce_DrawableComposite.h"
32 #include "../../components/positioning/juce_RelativeCoordinatePositioner.h"
35 //==============================================================================
36 DrawableRectangle::DrawableRectangle()
40 DrawableRectangle::DrawableRectangle (const DrawableRectangle& other)
41 : DrawableShape (other),
42 bounds (other.bounds),
43 cornerSize (other.cornerSize)
47 DrawableRectangle::~DrawableRectangle()
51 Drawable* DrawableRectangle::createCopy() const
53 return new DrawableRectangle (*this);
56 //==============================================================================
57 void DrawableRectangle::setRectangle (const RelativeParallelogram& newBounds)
59 if (bounds != newBounds)
61 bounds = newBounds;
62 rebuildPath();
66 void DrawableRectangle::setCornerSize (const RelativePoint& newSize)
68 if (cornerSize != newSize)
70 cornerSize = newSize;
71 rebuildPath();
75 void DrawableRectangle::rebuildPath()
77 if (bounds.isDynamic() || cornerSize.isDynamic())
79 Drawable::Positioner<DrawableRectangle>* const p = new Drawable::Positioner<DrawableRectangle> (*this);
80 setPositioner (p);
81 p->apply();
83 else
85 setPositioner (0);
86 recalculateCoordinates (0);
90 bool DrawableRectangle::registerCoordinates (RelativeCoordinatePositionerBase& pos)
92 bool ok = pos.addPoint (bounds.topLeft);
93 ok = pos.addPoint (bounds.topRight) && ok;
94 ok = pos.addPoint (bounds.bottomLeft) && ok;
95 return pos.addPoint (cornerSize) && ok;
98 void DrawableRectangle::recalculateCoordinates (Expression::Scope* scope)
100 Point<float> points[3];
101 bounds.resolveThreePoints (points, scope);
103 const float cornerSizeX = (float) cornerSize.x.resolve (scope);
104 const float cornerSizeY = (float) cornerSize.y.resolve (scope);
106 const float w = Line<float> (points[0], points[1]).getLength();
107 const float h = Line<float> (points[0], points[2]).getLength();
109 Path newPath;
111 if (cornerSizeX > 0 && cornerSizeY > 0)
112 newPath.addRoundedRectangle (0, 0, w, h, cornerSizeX, cornerSizeY);
113 else
114 newPath.addRectangle (0, 0, w, h);
116 newPath.applyTransform (AffineTransform::fromTargetPoints (0, 0, points[0].getX(), points[0].getY(),
117 w, 0, points[1].getX(), points[1].getY(),
118 0, h, points[2].getX(), points[2].getY()));
120 if (path != newPath)
122 path.swapWithPath (newPath);
123 pathChanged();
127 //==============================================================================
128 const Identifier DrawableRectangle::valueTreeType ("Rectangle");
129 const Identifier DrawableRectangle::ValueTreeWrapper::topLeft ("topLeft");
130 const Identifier DrawableRectangle::ValueTreeWrapper::topRight ("topRight");
131 const Identifier DrawableRectangle::ValueTreeWrapper::bottomLeft ("bottomLeft");
132 const Identifier DrawableRectangle::ValueTreeWrapper::cornerSize ("cornerSize");
134 //==============================================================================
135 DrawableRectangle::ValueTreeWrapper::ValueTreeWrapper (const ValueTree& state_)
136 : FillAndStrokeState (state_)
138 jassert (state.hasType (valueTreeType));
141 RelativeParallelogram DrawableRectangle::ValueTreeWrapper::getRectangle() const
143 return RelativeParallelogram (state.getProperty (topLeft, "0, 0"),
144 state.getProperty (topRight, "100, 0"),
145 state.getProperty (bottomLeft, "0, 100"));
148 void DrawableRectangle::ValueTreeWrapper::setRectangle (const RelativeParallelogram& newBounds, UndoManager* undoManager)
150 state.setProperty (topLeft, newBounds.topLeft.toString(), undoManager);
151 state.setProperty (topRight, newBounds.topRight.toString(), undoManager);
152 state.setProperty (bottomLeft, newBounds.bottomLeft.toString(), undoManager);
155 void DrawableRectangle::ValueTreeWrapper::setCornerSize (const RelativePoint& newSize, UndoManager* undoManager)
157 state.setProperty (cornerSize, newSize.toString(), undoManager);
160 RelativePoint DrawableRectangle::ValueTreeWrapper::getCornerSize() const
162 return RelativePoint (state [cornerSize]);
165 Value DrawableRectangle::ValueTreeWrapper::getCornerSizeValue (UndoManager* undoManager) const
167 return state.getPropertyAsValue (cornerSize, undoManager);
170 //==============================================================================
171 void DrawableRectangle::refreshFromValueTree (const ValueTree& tree, ComponentBuilder& builder)
173 ValueTreeWrapper v (tree);
174 setComponentID (v.getID());
176 refreshFillTypes (v, builder.getImageProvider());
177 setStrokeType (v.getStrokeType());
178 setRectangle (v.getRectangle());
179 setCornerSize (v.getCornerSize());
182 ValueTree DrawableRectangle::createValueTree (ComponentBuilder::ImageProvider* imageProvider) const
184 ValueTree tree (valueTreeType);
185 ValueTreeWrapper v (tree);
187 v.setID (getComponentID());
188 writeTo (v, imageProvider, nullptr);
189 v.setRectangle (bounds, nullptr);
190 v.setCornerSize (cornerSize, nullptr);
192 return tree;
195 END_JUCE_NAMESPACE