The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1638 lines
51KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../../jucer_Headers.h"
  20. #include "jucer_PaintElementPath.h"
  21. #include "../properties/jucer_PositionPropertyBase.h"
  22. #include "jucer_PaintElementUndoableAction.h"
  23. #include "../jucer_UtilityFunctions.h"
  24. //==============================================================================
  25. class ChangePointAction : public PaintElementUndoableAction <PaintElementPath>
  26. {
  27. public:
  28. ChangePointAction (PathPoint* const point,
  29. const int pointIndex,
  30. const PathPoint& newValue_)
  31. : PaintElementUndoableAction <PaintElementPath> (point->owner),
  32. index (pointIndex),
  33. newValue (newValue_),
  34. oldValue (*point)
  35. {
  36. }
  37. ChangePointAction (PathPoint* const point,
  38. const PathPoint& newValue_)
  39. : PaintElementUndoableAction <PaintElementPath> (point->owner),
  40. index (point->owner->indexOfPoint (point)),
  41. newValue (newValue_),
  42. oldValue (*point)
  43. {
  44. }
  45. bool perform()
  46. {
  47. return changeTo (newValue);
  48. }
  49. bool undo()
  50. {
  51. return changeTo (oldValue);
  52. }
  53. private:
  54. const int index;
  55. PathPoint newValue, oldValue;
  56. PathPoint* getPoint() const
  57. {
  58. PathPoint* p = getElement()->getPoint (index);
  59. jassert (p != nullptr);
  60. return p;
  61. }
  62. bool changeTo (const PathPoint& value) const
  63. {
  64. showCorrectTab();
  65. PaintElementPath* const path = getElement();
  66. jassert (path != nullptr);
  67. PathPoint* const p = path->getPoint (index);
  68. jassert (p != nullptr);
  69. const bool typeChanged = (p->type != value.type);
  70. *p = value;
  71. p->owner = path;
  72. if (typeChanged)
  73. path->pointListChanged();
  74. path->changed();
  75. return true;
  76. }
  77. };
  78. //==============================================================================
  79. class PathWindingModeProperty : public ChoicePropertyComponent,
  80. public ChangeListener
  81. {
  82. public:
  83. PathWindingModeProperty (PaintElementPath* const owner_)
  84. : ChoicePropertyComponent ("winding rule"),
  85. owner (owner_)
  86. {
  87. choices.add ("Non-zero winding");
  88. choices.add ("Even/odd winding");
  89. owner->getDocument()->addChangeListener (this);
  90. }
  91. ~PathWindingModeProperty()
  92. {
  93. owner->getDocument()->removeChangeListener (this);
  94. }
  95. void setIndex (int newIndex) { owner->setNonZeroWinding (newIndex == 0, true); }
  96. int getIndex() const { return owner->isNonZeroWinding() ? 0 : 1; }
  97. void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
  98. private:
  99. PaintElementPath* const owner;
  100. };
  101. //==============================================================================
  102. PaintElementPath::PaintElementPath (PaintRoutine* pr)
  103. : ColouredElement (pr, "Path", true, true),
  104. nonZeroWinding (true)
  105. {
  106. }
  107. PaintElementPath::~PaintElementPath()
  108. {
  109. }
  110. static int randomPos (int size)
  111. {
  112. return size / 4 + Random::getSystemRandom().nextInt (size / 4) - size / 8;
  113. }
  114. void PaintElementPath::setInitialBounds (int w, int h)
  115. {
  116. String s;
  117. int x = randomPos (w);
  118. int y = randomPos (h);
  119. s << "s "
  120. << x << " " << y << " l "
  121. << (x + 30) << " " << (y + 50) << " l "
  122. << (x - 30) << " " << (y + 50) << " x";
  123. restorePathFromString (s);
  124. }
  125. //==============================================================================
  126. int PaintElementPath::getBorderSize() const
  127. {
  128. return isStrokePresent ? 1 + roundFloatToInt (strokeType.stroke.getStrokeThickness())
  129. : 0;
  130. }
  131. Rectangle<int> PaintElementPath::getCurrentBounds (const Rectangle<int>& parentArea) const
  132. {
  133. updateStoredPath (getDocument()->getComponentLayout(), parentArea);
  134. Rectangle<float> r (path.getBounds());
  135. const int borderSize = getBorderSize();
  136. return Rectangle<int> ((int) r.getX() - borderSize,
  137. (int) r.getY() - borderSize,
  138. (int) r.getWidth() + borderSize * 2,
  139. (int) r.getHeight() + borderSize * 2);
  140. }
  141. void PaintElementPath::setCurrentBounds (const Rectangle<int>& b,
  142. const Rectangle<int>& parentArea,
  143. const bool /*undoable*/)
  144. {
  145. Rectangle<int> newBounds (b);
  146. newBounds.setSize (jmax (1, newBounds.getWidth()),
  147. jmax (1, newBounds.getHeight()));
  148. const Rectangle<int> current (getCurrentBounds (parentArea));
  149. if (newBounds != current)
  150. {
  151. const int borderSize = getBorderSize();
  152. const int dx = newBounds.getX() - current.getX();
  153. const int dy = newBounds.getY() - current.getY();
  154. const double scaleStartX = current.getX() + borderSize;
  155. const double scaleStartY = current.getY() + borderSize;
  156. const double scaleX = (newBounds.getWidth() - borderSize * 2) / (double) (current.getWidth() - borderSize * 2);
  157. const double scaleY = (newBounds.getHeight() - borderSize * 2) / (double) (current.getHeight() - borderSize * 2);
  158. for (int i = 0; i < points.size(); ++i)
  159. {
  160. PathPoint* const destPoint = points.getUnchecked(i);
  161. PathPoint p (*destPoint);
  162. for (int j = p.getNumPoints(); --j >= 0;)
  163. rescalePoint (p.pos[j], dx, dy,
  164. scaleX, scaleY,
  165. scaleStartX, scaleStartY,
  166. parentArea);
  167. perform (new ChangePointAction (destPoint, i, p), "Move path");
  168. }
  169. }
  170. }
  171. void PaintElementPath::rescalePoint (RelativePositionedRectangle& pos, int dx, int dy,
  172. double scaleX, double scaleY,
  173. double scaleStartX, double scaleStartY,
  174. const Rectangle<int>& parentArea) const
  175. {
  176. double x, y, w, h;
  177. pos.getRectangleDouble (x, y, w, h, parentArea, getDocument()->getComponentLayout());
  178. x = (x - scaleStartX) * scaleX + scaleStartX + dx;
  179. y = (y - scaleStartY) * scaleY + scaleStartY + dy;
  180. pos.updateFrom (x, y, w, h, parentArea, getDocument()->getComponentLayout());
  181. }
  182. //==============================================================================
  183. static void drawArrow (Graphics& g, const Point<float> p1, const Point<float> p2)
  184. {
  185. g.drawArrow (Line<float> (p1.x, p1.y, (p1.x + p2.x) * 0.5f, (p1.y + p2.y) * 0.5f), 1.0f, 8.0f, 10.0f);
  186. g.drawLine (p1.x + (p2.x - p1.x) * 0.49f, p1.y + (p2.y - p1.y) * 0.49f, p2.x, p2.y);
  187. }
  188. void PaintElementPath::draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  189. {
  190. updateStoredPath (layout, parentArea);
  191. path.setUsingNonZeroWinding (nonZeroWinding);
  192. fillType.setFillType (g, getDocument(), parentArea);
  193. g.fillPath (path);
  194. if (isStrokePresent)
  195. {
  196. strokeType.fill.setFillType (g, getDocument(), parentArea);
  197. g.strokePath (path, getStrokeType().stroke);
  198. }
  199. }
  200. void PaintElementPath::drawExtraEditorGraphics (Graphics& g, const Rectangle<int>& relativeTo)
  201. {
  202. ComponentLayout* layout = getDocument()->getComponentLayout();
  203. for (int i = 0; i < points.size(); ++i)
  204. {
  205. PathPoint* const p = points.getUnchecked (i);
  206. const int numPoints = p->getNumPoints();
  207. if (numPoints > 0)
  208. {
  209. if (owner->getSelectedPoints().isSelected (p))
  210. {
  211. g.setColour (Colours::red);
  212. Point<float> p1, p2;
  213. if (numPoints > 2)
  214. {
  215. p1 = p->pos[1].toXY (relativeTo, layout);
  216. p2 = p->pos[2].toXY (relativeTo, layout);
  217. drawArrow (g, p1, p2);
  218. }
  219. if (numPoints > 1)
  220. {
  221. p1 = p->pos[0].toXY (relativeTo, layout);
  222. p2 = p->pos[1].toXY (relativeTo, layout);
  223. drawArrow (g, p1, p2);
  224. }
  225. p2 = p->pos[0].toXY (relativeTo, layout);
  226. if (const PathPoint* const nextPoint = points [i - 1])
  227. {
  228. p1 = nextPoint->pos [nextPoint->getNumPoints() - 1].toXY (relativeTo, layout);
  229. drawArrow (g, p1, p2);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. void PaintElementPath::resized()
  236. {
  237. ColouredElement::resized();
  238. }
  239. void PaintElementPath::parentSizeChanged()
  240. {
  241. repaint();
  242. }
  243. //==============================================================================
  244. void PaintElementPath::mouseDown (const MouseEvent& e)
  245. {
  246. if (e.mods.isPopupMenu() || ! owner->getSelectedElements().isSelected (this))
  247. mouseDownOnSegment = -1;
  248. else
  249. mouseDownOnSegment = findSegmentAtXY (getX() + e.x, getY() + e.y);
  250. if (points [mouseDownOnSegment] != nullptr)
  251. mouseDownSelectSegmentStatus = owner->getSelectedPoints().addToSelectionOnMouseDown (points [mouseDownOnSegment], e.mods);
  252. else
  253. ColouredElement::mouseDown (e);
  254. }
  255. void PaintElementPath::mouseDrag (const MouseEvent& e)
  256. {
  257. if (mouseDownOnSegment < 0)
  258. ColouredElement::mouseDrag (e);
  259. }
  260. void PaintElementPath::mouseUp (const MouseEvent& e)
  261. {
  262. if (points [mouseDownOnSegment] == 0)
  263. ColouredElement::mouseUp (e);
  264. else
  265. owner->getSelectedPoints().addToSelectionOnMouseUp (points [mouseDownOnSegment],
  266. e.mods, false, mouseDownSelectSegmentStatus);
  267. }
  268. //==============================================================================
  269. void PaintElementPath::changed()
  270. {
  271. ColouredElement::changed();
  272. lastPathBounds = Rectangle<int>();
  273. }
  274. void PaintElementPath::pointListChanged()
  275. {
  276. changed();
  277. siblingComponentsChanged();
  278. }
  279. //==============================================================================
  280. void PaintElementPath::getEditableProperties (Array <PropertyComponent*>& props)
  281. {
  282. props.add (new PathWindingModeProperty (this));
  283. getColourSpecificProperties (props);
  284. }
  285. //==============================================================================
  286. static String positionToPairOfValues (const RelativePositionedRectangle& position,
  287. const ComponentLayout* layout)
  288. {
  289. String x, y, w, h;
  290. positionToCode (position, layout, x, y, w, h);
  291. return castToFloat (x) + ", " + castToFloat (y);
  292. }
  293. void PaintElementPath::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  294. {
  295. if (fillType.isInvisible() && (strokeType.isInvisible() || ! isStrokePresent))
  296. return;
  297. const String pathVariable ("internalPath" + String (code.getUniqueSuffix()));
  298. const ComponentLayout* layout = code.document->getComponentLayout();
  299. code.privateMemberDeclarations
  300. << "Path " << pathVariable << ";\n";
  301. String r;
  302. bool somePointsAreRelative = false;
  303. if (! nonZeroWinding)
  304. r << pathVariable << ".setUsingNonZeroWinding (false);\n";
  305. for (auto* p : points)
  306. {
  307. switch (p->type)
  308. {
  309. case Path::Iterator::startNewSubPath:
  310. r << pathVariable << ".startNewSubPath (" << positionToPairOfValues (p->pos[0], layout) << ");\n";
  311. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  312. break;
  313. case Path::Iterator::lineTo:
  314. r << pathVariable << ".lineTo (" << positionToPairOfValues (p->pos[0], layout) << ");\n";
  315. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  316. break;
  317. case Path::Iterator::quadraticTo:
  318. r << pathVariable << ".quadraticTo (" << positionToPairOfValues (p->pos[0], layout)
  319. << ", " << positionToPairOfValues (p->pos[1], layout) << ");\n";
  320. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  321. somePointsAreRelative = somePointsAreRelative || ! p->pos[1].rect.isPositionAbsolute();
  322. break;
  323. case Path::Iterator::cubicTo:
  324. r << pathVariable << ".cubicTo (" << positionToPairOfValues (p->pos[0], layout)
  325. << ", " << positionToPairOfValues (p->pos[1], layout)
  326. << ", " << positionToPairOfValues (p->pos[2], layout) << ");\n";
  327. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  328. somePointsAreRelative = somePointsAreRelative || ! p->pos[1].rect.isPositionAbsolute();
  329. somePointsAreRelative = somePointsAreRelative || ! p->pos[2].rect.isPositionAbsolute();
  330. break;
  331. case Path::Iterator::closePath:
  332. r << pathVariable << ".closeSubPath();\n";
  333. break;
  334. default:
  335. jassertfalse;
  336. break;
  337. }
  338. }
  339. r << '\n';
  340. if (somePointsAreRelative)
  341. code.getCallbackCode (String(), "void", "resized()", false)
  342. << pathVariable << ".clear();\n" << r;
  343. else
  344. code.constructorCode << r;
  345. String s;
  346. s << "{\n"
  347. << " float x = 0, y = 0;\n";
  348. if (! fillType.isInvisible())
  349. s << " " << fillType.generateVariablesCode ("fill");
  350. if (isStrokePresent && ! strokeType.isInvisible())
  351. s << " " << strokeType.fill.generateVariablesCode ("stroke");
  352. s << " //[UserPaintCustomArguments] Customize the painting arguments here..\n"
  353. << customPaintCode
  354. << " //[/UserPaintCustomArguments]\n";
  355. RelativePositionedRectangle zero;
  356. if (! fillType.isInvisible())
  357. {
  358. s << " ";
  359. fillType.fillInGeneratedCode ("fill", zero, code, s);
  360. s << " g.fillPath (" << pathVariable << ", AffineTransform::translation(x, y));\n";
  361. }
  362. if (isStrokePresent && ! strokeType.isInvisible())
  363. {
  364. s << " ";
  365. strokeType.fill.fillInGeneratedCode ("stroke", zero, code, s);
  366. s << " g.strokePath (" << pathVariable << ", " << strokeType.getPathStrokeCode() << ", AffineTransform::translation(x, y));\n";
  367. }
  368. s << "}\n\n";
  369. paintMethodCode += s;
  370. }
  371. void PaintElementPath::applyCustomPaintSnippets (StringArray& snippets)
  372. {
  373. customPaintCode.clear();
  374. if (! snippets.isEmpty() && (! fillType.isInvisible() || (isStrokePresent && ! strokeType.isInvisible())))
  375. {
  376. customPaintCode = snippets[0];
  377. snippets.remove(0);
  378. }
  379. }
  380. //==============================================================================
  381. XmlElement* PaintElementPath::createXml() const
  382. {
  383. XmlElement* e = new XmlElement (getTagName());
  384. position.applyToXml (*e);
  385. addColourAttributes (e);
  386. e->setAttribute ("nonZeroWinding", nonZeroWinding);
  387. e->addTextElement (pathToString());
  388. return e;
  389. }
  390. bool PaintElementPath::loadFromXml (const XmlElement& xml)
  391. {
  392. if (xml.hasTagName (getTagName()))
  393. {
  394. position.restoreFromXml (xml, position);
  395. loadColourAttributes (xml);
  396. nonZeroWinding = xml.getBoolAttribute ("nonZeroWinding", true);
  397. restorePathFromString (xml.getAllSubText().trim());
  398. return true;
  399. }
  400. jassertfalse;
  401. return false;
  402. }
  403. //==============================================================================
  404. void PaintElementPath::createSiblingComponents()
  405. {
  406. ColouredElement::createSiblingComponents();
  407. for (int i = 0; i < points.size(); ++i)
  408. {
  409. switch (points.getUnchecked(i)->type)
  410. {
  411. case Path::Iterator::startNewSubPath:
  412. siblingComponents.add (new PathPointComponent (this, i, 0));
  413. break;
  414. case Path::Iterator::lineTo:
  415. siblingComponents.add (new PathPointComponent (this, i, 0));
  416. break;
  417. case Path::Iterator::quadraticTo:
  418. siblingComponents.add (new PathPointComponent (this, i, 0));
  419. siblingComponents.add (new PathPointComponent (this, i, 1));
  420. break;
  421. case Path::Iterator::cubicTo:
  422. siblingComponents.add (new PathPointComponent (this, i, 0));
  423. siblingComponents.add (new PathPointComponent (this, i, 1));
  424. siblingComponents.add (new PathPointComponent (this, i, 2));
  425. break;
  426. case Path::Iterator::closePath:
  427. break;
  428. default:
  429. jassertfalse; break;
  430. }
  431. }
  432. for (int i = 0; i < siblingComponents.size(); ++i)
  433. {
  434. getParentComponent()->addAndMakeVisible (siblingComponents.getUnchecked(i));
  435. siblingComponents.getUnchecked(i)->updatePosition();
  436. }
  437. }
  438. String PaintElementPath::pathToString() const
  439. {
  440. String s;
  441. for (int i = 0; i < points.size(); ++i)
  442. {
  443. const PathPoint* const p = points.getUnchecked(i);
  444. switch (p->type)
  445. {
  446. case Path::Iterator::startNewSubPath:
  447. s << "s " << p->pos[0].toString() << ' ';
  448. break;
  449. case Path::Iterator::lineTo:
  450. s << "l " << p->pos[0].toString() << ' ';
  451. break;
  452. case Path::Iterator::quadraticTo:
  453. s << "q " << p->pos[0].toString()
  454. << ' ' << p->pos[1].toString() << ' ';
  455. break;
  456. case Path::Iterator::cubicTo:
  457. s << "c " << p->pos[0].toString()
  458. << ' ' << p->pos[1].toString() << ' '
  459. << ' ' << p->pos[2].toString() << ' ';
  460. break;
  461. case Path::Iterator::closePath:
  462. s << "x ";
  463. break;
  464. default:
  465. jassertfalse; break;
  466. }
  467. }
  468. return s.trimEnd();
  469. }
  470. void PaintElementPath::restorePathFromString (const String& s)
  471. {
  472. points.clear();
  473. StringArray tokens;
  474. tokens.addTokens (s, false);
  475. tokens.trim();
  476. tokens.removeEmptyStrings();
  477. for (int i = 0; i < tokens.size(); ++i)
  478. {
  479. ScopedPointer<PathPoint> p (new PathPoint (this));
  480. if (tokens[i] == "s")
  481. {
  482. p->type = Path::Iterator::startNewSubPath;
  483. p->pos [0] = RelativePositionedRectangle();
  484. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  485. i += 2;
  486. }
  487. else if (tokens[i] == "l")
  488. {
  489. p->type = Path::Iterator::lineTo;
  490. p->pos [0] = RelativePositionedRectangle();
  491. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  492. i += 2;
  493. }
  494. else if (tokens[i] == "q")
  495. {
  496. p->type = Path::Iterator::quadraticTo;
  497. p->pos [0] = RelativePositionedRectangle();
  498. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  499. p->pos [1] = RelativePositionedRectangle();
  500. p->pos [1].rect = PositionedRectangle (tokens [i + 3] + " " + tokens [i + 4]);
  501. i += 4;
  502. }
  503. else if (tokens[i] == "c")
  504. {
  505. p->type = Path::Iterator::cubicTo;
  506. p->pos [0] = RelativePositionedRectangle();
  507. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  508. p->pos [1] = RelativePositionedRectangle();
  509. p->pos [1].rect = PositionedRectangle (tokens [i + 3] + " " + tokens [i + 4]);
  510. p->pos [2] = RelativePositionedRectangle();
  511. p->pos [2].rect = PositionedRectangle (tokens [i + 5] + " " + tokens [i + 6]);
  512. i += 6;
  513. }
  514. else if (tokens[i] == "x")
  515. {
  516. p->type = Path::Iterator::closePath;
  517. }
  518. else
  519. continue;
  520. points.add (p.release());
  521. }
  522. }
  523. void PaintElementPath::setToPath (const Path& newPath)
  524. {
  525. points.clear();
  526. Path::Iterator i (newPath);
  527. while (i.next())
  528. {
  529. ScopedPointer<PathPoint> p (new PathPoint (this));
  530. p->type = i.elementType;
  531. if (i.elementType == Path::Iterator::startNewSubPath)
  532. {
  533. p->pos [0].rect.setX (i.x1);
  534. p->pos [0].rect.setY (i.y1);
  535. }
  536. else if (i.elementType == Path::Iterator::lineTo)
  537. {
  538. p->pos [0].rect.setX (i.x1);
  539. p->pos [0].rect.setY (i.y1);
  540. }
  541. else if (i.elementType == Path::Iterator::quadraticTo)
  542. {
  543. p->pos [0].rect.setX (i.x1);
  544. p->pos [0].rect.setY (i.y1);
  545. p->pos [1].rect.setX (i.x2);
  546. p->pos [1].rect.setY (i.y2);
  547. }
  548. else if (i.elementType == Path::Iterator::cubicTo)
  549. {
  550. p->pos [0].rect.setX (i.x1);
  551. p->pos [0].rect.setY (i.y1);
  552. p->pos [1].rect.setX (i.x2);
  553. p->pos [1].rect.setY (i.y2);
  554. p->pos [2].rect.setX (i.x3);
  555. p->pos [2].rect.setY (i.y3);
  556. }
  557. else if (i.elementType == Path::Iterator::closePath)
  558. {
  559. }
  560. else
  561. {
  562. continue;
  563. }
  564. points.add (p.release());
  565. }
  566. }
  567. void PaintElementPath::updateStoredPath (const ComponentLayout* layout, const Rectangle<int>& relativeTo) const
  568. {
  569. if (lastPathBounds != relativeTo && ! relativeTo.isEmpty())
  570. {
  571. lastPathBounds = relativeTo;
  572. path.clear();
  573. for (int i = 0; i < points.size(); ++i)
  574. {
  575. const PathPoint* const p = points.getUnchecked(i);
  576. switch (p->type)
  577. {
  578. case Path::Iterator::startNewSubPath:
  579. path.startNewSubPath (p->pos[0].toXY (relativeTo, layout));
  580. break;
  581. case Path::Iterator::lineTo:
  582. path.lineTo (p->pos[0].toXY (relativeTo, layout));
  583. break;
  584. case Path::Iterator::quadraticTo:
  585. path.quadraticTo (p->pos[0].toXY (relativeTo, layout),
  586. p->pos[1].toXY (relativeTo, layout));
  587. break;
  588. case Path::Iterator::cubicTo:
  589. path.cubicTo (p->pos[0].toXY (relativeTo, layout),
  590. p->pos[1].toXY (relativeTo, layout),
  591. p->pos[2].toXY (relativeTo, layout));
  592. break;
  593. case Path::Iterator::closePath:
  594. path.closeSubPath();
  595. break;
  596. default:
  597. jassertfalse; break;
  598. }
  599. }
  600. }
  601. }
  602. //==============================================================================
  603. class ChangeWindingAction : public PaintElementUndoableAction <PaintElementPath>
  604. {
  605. public:
  606. ChangeWindingAction (PaintElementPath* const path, const bool newValue_)
  607. : PaintElementUndoableAction <PaintElementPath> (path),
  608. newValue (newValue_),
  609. oldValue (path->isNonZeroWinding())
  610. {
  611. }
  612. bool perform()
  613. {
  614. showCorrectTab();
  615. getElement()->setNonZeroWinding (newValue, false);
  616. return true;
  617. }
  618. bool undo()
  619. {
  620. showCorrectTab();
  621. getElement()->setNonZeroWinding (oldValue, false);
  622. return true;
  623. }
  624. private:
  625. bool newValue, oldValue;
  626. };
  627. void PaintElementPath::setNonZeroWinding (const bool nonZero, const bool undoable)
  628. {
  629. if (nonZero != nonZeroWinding)
  630. {
  631. if (undoable)
  632. {
  633. perform (new ChangeWindingAction (this, nonZero), "Change path winding rule");
  634. }
  635. else
  636. {
  637. nonZeroWinding = nonZero;
  638. changed();
  639. }
  640. }
  641. }
  642. bool PaintElementPath::isSubpathClosed (int index) const
  643. {
  644. for (int i = index + 1; i < points.size(); ++i)
  645. {
  646. if (points.getUnchecked (i)->type == Path::Iterator::closePath)
  647. return true;
  648. if (points.getUnchecked (i)->type == Path::Iterator::startNewSubPath)
  649. break;
  650. }
  651. return false;
  652. }
  653. //==============================================================================
  654. void PaintElementPath::setSubpathClosed (int index, const bool closed, const bool undoable)
  655. {
  656. if (closed != isSubpathClosed (index))
  657. {
  658. for (int i = index + 1; i < points.size(); ++i)
  659. {
  660. PathPoint* p = points.getUnchecked (i);
  661. if (p->type == Path::Iterator::closePath)
  662. {
  663. jassert (! closed);
  664. deletePoint (i, undoable);
  665. return;
  666. }
  667. if (p->type == Path::Iterator::startNewSubPath)
  668. {
  669. jassert (closed);
  670. PathPoint* pp = addPoint (i - 1, undoable);
  671. PathPoint p2 (*pp);
  672. p2.type = Path::Iterator::closePath;
  673. perform (new ChangePointAction (pp, p2), "Close subpath");
  674. return;
  675. }
  676. }
  677. jassert (closed);
  678. PathPoint* p = addPoint (points.size() - 1, undoable);
  679. PathPoint p2 (*p);
  680. p2.type = Path::Iterator::closePath;
  681. perform (new ChangePointAction (p, p2), "Close subpath");
  682. }
  683. }
  684. //==============================================================================
  685. class AddPointAction : public PaintElementUndoableAction <PaintElementPath>
  686. {
  687. public:
  688. AddPointAction (PaintElementPath* path, int pointIndexToAddItAfter_)
  689. : PaintElementUndoableAction <PaintElementPath> (path),
  690. indexAdded (-1),
  691. pointIndexToAddItAfter (pointIndexToAddItAfter_)
  692. {
  693. }
  694. bool perform()
  695. {
  696. showCorrectTab();
  697. PaintElementPath* const path = getElement();
  698. jassert (path != nullptr);
  699. PathPoint* const p = path->addPoint (pointIndexToAddItAfter, false);
  700. jassert (p != nullptr);
  701. indexAdded = path->indexOfPoint (p);
  702. jassert (indexAdded >= 0);
  703. return true;
  704. }
  705. bool undo()
  706. {
  707. showCorrectTab();
  708. PaintElementPath* const path = getElement();
  709. jassert (path != nullptr);
  710. path->deletePoint (indexAdded, false);
  711. return true;
  712. }
  713. int indexAdded;
  714. private:
  715. int pointIndexToAddItAfter;
  716. };
  717. PathPoint* PaintElementPath::addPoint (int pointIndexToAddItAfter, const bool undoable)
  718. {
  719. if (undoable)
  720. {
  721. AddPointAction* action = new AddPointAction (this, pointIndexToAddItAfter);
  722. perform (action, "Add path point");
  723. return points [action->indexAdded];
  724. }
  725. double x1 = 20.0, y1 = 20.0, x2, y2;
  726. ComponentLayout* layout = getDocument()->getComponentLayout();
  727. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  728. if (points [pointIndexToAddItAfter] != nullptr)
  729. points [pointIndexToAddItAfter]->pos [points [pointIndexToAddItAfter]->getNumPoints() - 1].getXY (x1, y1, area, layout);
  730. else if (points[0] != nullptr)
  731. points[0]->pos[0].getXY (x1, y1, area, layout);
  732. x2 = x1 + 50.0;
  733. y2 = y1 + 50.0;
  734. if (points [pointIndexToAddItAfter + 1] != nullptr)
  735. {
  736. if (points [pointIndexToAddItAfter + 1]->type == Path::Iterator::closePath
  737. || points [pointIndexToAddItAfter + 1]->type == Path::Iterator::startNewSubPath)
  738. {
  739. int i = pointIndexToAddItAfter;
  740. while (i > 0)
  741. if (points [--i]->type == Path::Iterator::startNewSubPath)
  742. break;
  743. if (i != pointIndexToAddItAfter)
  744. points [i]->pos[0].getXY (x2, y2, area, layout);
  745. }
  746. else
  747. {
  748. points [pointIndexToAddItAfter + 1]->pos[0].getXY (x2, y2, area, layout);
  749. }
  750. }
  751. else
  752. {
  753. int i = pointIndexToAddItAfter + 1;
  754. while (i > 0)
  755. if (points [--i]->type == Path::Iterator::startNewSubPath)
  756. break;
  757. points[i]->pos[0].getXY (x2, y2, area, layout);
  758. }
  759. PathPoint* const p = new PathPoint (this);
  760. p->type = Path::Iterator::lineTo;
  761. p->pos[0].rect.setX ((x1 + x2) * 0.5f);
  762. p->pos[0].rect.setY ((y1 + y2) * 0.5f);
  763. points.insert (pointIndexToAddItAfter + 1, p);
  764. pointListChanged();
  765. return p;
  766. }
  767. //==============================================================================
  768. class DeletePointAction : public PaintElementUndoableAction <PaintElementPath>
  769. {
  770. public:
  771. DeletePointAction (PaintElementPath* const path, const int indexToRemove_)
  772. : PaintElementUndoableAction <PaintElementPath> (path),
  773. indexToRemove (indexToRemove_),
  774. oldValue (*path->getPoint (indexToRemove))
  775. {
  776. }
  777. bool perform()
  778. {
  779. showCorrectTab();
  780. PaintElementPath* const path = getElement();
  781. jassert (path != nullptr);
  782. path->deletePoint (indexToRemove, false);
  783. return path != nullptr;
  784. }
  785. bool undo()
  786. {
  787. showCorrectTab();
  788. PaintElementPath* const path = getElement();
  789. jassert (path != nullptr);
  790. PathPoint* p = path->addPoint (indexToRemove - 1, false);
  791. *p = oldValue;
  792. return path != nullptr;
  793. }
  794. int indexToRemove;
  795. private:
  796. PathPoint oldValue;
  797. };
  798. void PaintElementPath::deletePoint (int pointIndex, const bool undoable)
  799. {
  800. if (undoable)
  801. {
  802. perform (new DeletePointAction (this, pointIndex), "Delete path point");
  803. }
  804. else
  805. {
  806. PathPoint* const p = points [pointIndex];
  807. if (p != nullptr && pointIndex > 0)
  808. {
  809. owner->getSelectedPoints().deselect (p);
  810. owner->getSelectedPoints().changed (true);
  811. points.remove (pointIndex);
  812. pointListChanged();
  813. }
  814. }
  815. }
  816. //==============================================================================
  817. bool PaintElementPath::getPoint (int index, int pointNumber, double& x, double& y, const Rectangle<int>& parentArea) const
  818. {
  819. const PathPoint* const p = points [index];
  820. if (p == nullptr)
  821. {
  822. x = y = 0;
  823. return false;
  824. }
  825. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  826. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  827. p->pos [pointNumber].getXY (x, y, parentArea, getDocument()->getComponentLayout());
  828. return true;
  829. }
  830. int PaintElementPath::findSegmentAtXY (int x, int y) const
  831. {
  832. double x1, y1, x2, y2, x3, y3, lastX = 0.0, lastY = 0.0, subPathStartX = 0.0, subPathStartY = 0.0;
  833. ComponentLayout* const layout = getDocument()->getComponentLayout();
  834. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  835. int subpathStartIndex = 0;
  836. float thickness = 10.0f;
  837. if (isStrokePresent)
  838. thickness = jmax (thickness, strokeType.stroke.getStrokeThickness());
  839. for (int i = 0; i < points.size(); ++i)
  840. {
  841. Path segmentPath;
  842. PathPoint* const p = points.getUnchecked (i);
  843. switch (p->type)
  844. {
  845. case Path::Iterator::startNewSubPath:
  846. p->pos[0].getXY (lastX, lastY, area, layout);
  847. subPathStartX = lastX;
  848. subPathStartY = lastY;
  849. subpathStartIndex = i;
  850. break;
  851. case Path::Iterator::lineTo:
  852. p->pos[0].getXY (x1, y1, area, layout);
  853. segmentPath.addLineSegment (Line<float> ((float) lastX, (float) lastY, (float) x1, (float) y1), thickness);
  854. if (segmentPath.contains ((float) x, (float) y))
  855. return i;
  856. lastX = x1;
  857. lastY = y1;
  858. break;
  859. case Path::Iterator::quadraticTo:
  860. p->pos[0].getXY (x1, y1, area, layout);
  861. p->pos[1].getXY (x2, y2, area, layout);
  862. segmentPath.startNewSubPath ((float) lastX, (float) lastY);
  863. segmentPath.quadraticTo ((float) x1, (float) y1, (float) x2, (float) y2);
  864. PathStrokeType (thickness).createStrokedPath (segmentPath, segmentPath);
  865. if (segmentPath.contains ((float) x, (float) y))
  866. return i;
  867. lastX = x2;
  868. lastY = y2;
  869. break;
  870. case Path::Iterator::cubicTo:
  871. p->pos[0].getXY (x1, y1, area, layout);
  872. p->pos[1].getXY (x2, y2, area, layout);
  873. p->pos[2].getXY (x3, y3, area, layout);
  874. segmentPath.startNewSubPath ((float) lastX, (float) lastY);
  875. segmentPath.cubicTo ((float) x1, (float) y1, (float) x2, (float) y2, (float) x3, (float) y3);
  876. PathStrokeType (thickness).createStrokedPath (segmentPath, segmentPath);
  877. if (segmentPath.contains ((float) x, (float) y))
  878. return i;
  879. lastX = x3;
  880. lastY = y3;
  881. break;
  882. case Path::Iterator::closePath:
  883. segmentPath.addLineSegment (Line<float> ((float) lastX, (float) lastY, (float) subPathStartX, (float) subPathStartY), thickness);
  884. if (segmentPath.contains ((float) x, (float) y))
  885. return subpathStartIndex;
  886. lastX = subPathStartX;
  887. lastY = subPathStartY;
  888. break;
  889. default:
  890. jassertfalse; break;
  891. }
  892. }
  893. return -1;
  894. }
  895. //==============================================================================
  896. void PaintElementPath::movePoint (int index, int pointNumber,
  897. double newX, double newY,
  898. const Rectangle<int>& parentArea,
  899. const bool undoable)
  900. {
  901. if (PathPoint* const p = points [index])
  902. {
  903. PathPoint newPoint (*p);
  904. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  905. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  906. RelativePositionedRectangle& pr = newPoint.pos [pointNumber];
  907. double x, y, w, h;
  908. pr.getRectangleDouble (x, y, w, h, parentArea, getDocument()->getComponentLayout());
  909. pr.updateFrom (newX, newY, w, h, parentArea, getDocument()->getComponentLayout());
  910. if (undoable)
  911. {
  912. perform (new ChangePointAction (p, index, newPoint), "Move path point");
  913. }
  914. else
  915. {
  916. *p = newPoint;
  917. changed();
  918. }
  919. }
  920. }
  921. RelativePositionedRectangle PaintElementPath::getPoint (int index, int pointNumber) const
  922. {
  923. if (PathPoint* const p = points [index])
  924. {
  925. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  926. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  927. return p->pos [pointNumber];
  928. }
  929. jassertfalse;
  930. return RelativePositionedRectangle();
  931. }
  932. void PaintElementPath::setPoint (int index, int pointNumber, const RelativePositionedRectangle& newPos, const bool undoable)
  933. {
  934. if (PathPoint* const p = points [index])
  935. {
  936. PathPoint newPoint (*p);
  937. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  938. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  939. if (newPoint.pos [pointNumber] != newPos)
  940. {
  941. newPoint.pos [pointNumber] = newPos;
  942. if (undoable)
  943. {
  944. perform (new ChangePointAction (p, index, newPoint), "Change path point position");
  945. }
  946. else
  947. {
  948. *p = newPoint;
  949. changed();
  950. }
  951. }
  952. }
  953. else
  954. {
  955. jassertfalse;
  956. }
  957. }
  958. //==============================================================================
  959. class PathPointTypeProperty : public ChoicePropertyComponent,
  960. public ChangeListener
  961. {
  962. public:
  963. PathPointTypeProperty (PaintElementPath* const owner_,
  964. const int index_)
  965. : ChoicePropertyComponent ("point type"),
  966. owner (owner_),
  967. index (index_)
  968. {
  969. choices.add ("Start of sub-path");
  970. choices.add ("Line");
  971. choices.add ("Quadratic");
  972. choices.add ("Cubic");
  973. owner->getDocument()->addChangeListener (this);
  974. }
  975. ~PathPointTypeProperty()
  976. {
  977. owner->getDocument()->removeChangeListener (this);
  978. }
  979. void setIndex (int newIndex)
  980. {
  981. Path::Iterator::PathElementType type = Path::Iterator::startNewSubPath;
  982. switch (newIndex)
  983. {
  984. case 0: type = Path::Iterator::startNewSubPath; break;
  985. case 1: type = Path::Iterator::lineTo; break;
  986. case 2: type = Path::Iterator::quadraticTo; break;
  987. case 3: type = Path::Iterator::cubicTo; break;
  988. default: jassertfalse; break;
  989. }
  990. const Rectangle<int> area (((PaintRoutineEditor*) owner->getParentComponent())->getComponentArea());
  991. owner->getPoint (index)->changePointType (type, area, true);
  992. }
  993. int getIndex() const
  994. {
  995. const PathPoint* const p = owner->getPoint (index);
  996. jassert (p != nullptr);
  997. switch (p->type)
  998. {
  999. case Path::Iterator::startNewSubPath: return 0;
  1000. case Path::Iterator::lineTo: return 1;
  1001. case Path::Iterator::quadraticTo: return 2;
  1002. case Path::Iterator::cubicTo: return 3;
  1003. case Path::Iterator::closePath: break;
  1004. default: jassertfalse; break;
  1005. }
  1006. return 0;
  1007. }
  1008. void changeListenerCallback (ChangeBroadcaster*)
  1009. {
  1010. refresh();
  1011. }
  1012. private:
  1013. PaintElementPath* const owner;
  1014. const int index;
  1015. };
  1016. //==============================================================================
  1017. class PathPointPositionProperty : public PositionPropertyBase
  1018. {
  1019. public:
  1020. PathPointPositionProperty (PaintElementPath* const owner_,
  1021. const int index_, const int pointNumber_,
  1022. const String& name,
  1023. ComponentPositionDimension dimension_)
  1024. : PositionPropertyBase (owner_, name, dimension_, false, false,
  1025. owner_->getDocument()->getComponentLayout()),
  1026. owner (owner_),
  1027. index (index_),
  1028. pointNumber (pointNumber_)
  1029. {
  1030. owner->getDocument()->addChangeListener (this);
  1031. }
  1032. ~PathPointPositionProperty()
  1033. {
  1034. owner->getDocument()->removeChangeListener (this);
  1035. }
  1036. void setPosition (const RelativePositionedRectangle& newPos)
  1037. {
  1038. owner->setPoint (index, pointNumber, newPos, true);
  1039. }
  1040. RelativePositionedRectangle getPosition() const
  1041. {
  1042. return owner->getPoint (index, pointNumber);
  1043. }
  1044. private:
  1045. PaintElementPath* const owner;
  1046. const int index, pointNumber;
  1047. };
  1048. //==============================================================================
  1049. class PathPointClosedProperty : public ChoicePropertyComponent,
  1050. private ChangeListener
  1051. {
  1052. public:
  1053. PathPointClosedProperty (PaintElementPath* const owner_, const int index_)
  1054. : ChoicePropertyComponent ("openness"),
  1055. owner (owner_),
  1056. index (index_)
  1057. {
  1058. owner->getDocument()->addChangeListener (this);
  1059. choices.add ("Subpath is closed");
  1060. choices.add ("Subpath is open-ended");
  1061. }
  1062. ~PathPointClosedProperty()
  1063. {
  1064. owner->getDocument()->removeChangeListener (this);
  1065. }
  1066. void changeListenerCallback (ChangeBroadcaster*)
  1067. {
  1068. refresh();
  1069. }
  1070. void setIndex (int newIndex)
  1071. {
  1072. owner->setSubpathClosed (index, newIndex == 0, true);
  1073. }
  1074. int getIndex() const
  1075. {
  1076. return owner->isSubpathClosed (index) ? 0 : 1;
  1077. }
  1078. private:
  1079. PaintElementPath* const owner;
  1080. const int index;
  1081. };
  1082. //==============================================================================
  1083. class AddNewPointProperty : public ButtonPropertyComponent
  1084. {
  1085. public:
  1086. AddNewPointProperty (PaintElementPath* const owner_, const int index_)
  1087. : ButtonPropertyComponent ("new point", false),
  1088. owner (owner_),
  1089. index (index_)
  1090. {
  1091. }
  1092. void buttonClicked()
  1093. {
  1094. owner->addPoint (index, true);
  1095. }
  1096. String getButtonText() const { return "Add new point"; }
  1097. private:
  1098. PaintElementPath* const owner;
  1099. const int index;
  1100. };
  1101. //==============================================================================
  1102. PathPoint::PathPoint (PaintElementPath* const owner_)
  1103. : owner (owner_)
  1104. {
  1105. }
  1106. PathPoint::PathPoint (const PathPoint& other)
  1107. : owner (other.owner),
  1108. type (other.type)
  1109. {
  1110. pos [0] = other.pos [0];
  1111. pos [1] = other.pos [1];
  1112. pos [2] = other.pos [2];
  1113. }
  1114. PathPoint& PathPoint::operator= (const PathPoint& other)
  1115. {
  1116. owner = other.owner;
  1117. type = other.type;
  1118. pos [0] = other.pos [0];
  1119. pos [1] = other.pos [1];
  1120. pos [2] = other.pos [2];
  1121. return *this;
  1122. }
  1123. PathPoint::~PathPoint()
  1124. {
  1125. }
  1126. int PathPoint::getNumPoints() const
  1127. {
  1128. if (type == Path::Iterator::cubicTo) return 3;
  1129. if (type == Path::Iterator::quadraticTo) return 2;
  1130. if (type == Path::Iterator::closePath) return 0;
  1131. return 1;
  1132. }
  1133. PathPoint PathPoint::withChangedPointType (const Path::Iterator::PathElementType newType,
  1134. const Rectangle<int>& parentArea) const
  1135. {
  1136. PathPoint p (*this);
  1137. if (newType != p.type)
  1138. {
  1139. int oldNumPoints = getNumPoints();
  1140. p.type = newType;
  1141. int numPoints = p.getNumPoints();
  1142. if (numPoints != oldNumPoints)
  1143. {
  1144. double lastX, lastY;
  1145. double x, y, w, h;
  1146. p.pos [numPoints - 1] = p.pos [oldNumPoints - 1];
  1147. p.pos [numPoints - 1].getRectangleDouble (x, y, w, h, parentArea, owner->getDocument()->getComponentLayout());
  1148. const int index = owner->points.indexOf (this);
  1149. if (PathPoint* lastPoint = owner->points [index - 1])
  1150. {
  1151. lastPoint->pos [lastPoint->getNumPoints() - 1]
  1152. .getRectangleDouble (lastX, lastY, w, h, parentArea, owner->getDocument()->getComponentLayout());
  1153. }
  1154. else
  1155. {
  1156. jassertfalse;
  1157. lastX = x;
  1158. lastY = y;
  1159. }
  1160. for (int i = 0; i < numPoints - 1; ++i)
  1161. {
  1162. p.pos[i] = p.pos [numPoints - 1];
  1163. p.pos[i].updateFrom (lastX + (x - lastX) * (i + 1) / numPoints,
  1164. lastY + (y - lastY) * (i + 1) / numPoints,
  1165. w, h,
  1166. parentArea,
  1167. owner->getDocument()->getComponentLayout());
  1168. }
  1169. }
  1170. }
  1171. return p;
  1172. }
  1173. void PathPoint::changePointType (const Path::Iterator::PathElementType newType,
  1174. const Rectangle<int>& parentArea, const bool undoable)
  1175. {
  1176. if (newType != type)
  1177. {
  1178. if (undoable)
  1179. {
  1180. owner->perform (new ChangePointAction (this, withChangedPointType (newType, parentArea)),
  1181. "Change path point type");
  1182. }
  1183. else
  1184. {
  1185. *this = withChangedPointType (newType, parentArea);
  1186. owner->pointListChanged();
  1187. }
  1188. }
  1189. }
  1190. void PathPoint::getEditableProperties (Array<PropertyComponent*>& props)
  1191. {
  1192. const int index = owner->points.indexOf (this);
  1193. jassert (index >= 0);
  1194. switch (type)
  1195. {
  1196. case Path::Iterator::startNewSubPath:
  1197. props.add (new PathPointPositionProperty (owner, index, 0, "x", PositionPropertyBase::componentX));
  1198. props.add (new PathPointPositionProperty (owner, index, 0, "y", PositionPropertyBase::componentY));
  1199. props.add (new PathPointClosedProperty (owner, index));
  1200. props.add (new AddNewPointProperty (owner, index));
  1201. break;
  1202. case Path::Iterator::lineTo:
  1203. props.add (new PathPointTypeProperty (owner, index));
  1204. props.add (new PathPointPositionProperty (owner, index, 0, "x", PositionPropertyBase::componentX));
  1205. props.add (new PathPointPositionProperty (owner, index, 0, "y", PositionPropertyBase::componentY));
  1206. props.add (new AddNewPointProperty (owner, index));
  1207. break;
  1208. case Path::Iterator::quadraticTo:
  1209. props.add (new PathPointTypeProperty (owner, index));
  1210. props.add (new PathPointPositionProperty (owner, index, 0, "control pt x", PositionPropertyBase::componentX));
  1211. props.add (new PathPointPositionProperty (owner, index, 0, "control pt y", PositionPropertyBase::componentY));
  1212. props.add (new PathPointPositionProperty (owner, index, 1, "x", PositionPropertyBase::componentX));
  1213. props.add (new PathPointPositionProperty (owner, index, 1, "y", PositionPropertyBase::componentY));
  1214. props.add (new AddNewPointProperty (owner, index));
  1215. break;
  1216. case Path::Iterator::cubicTo:
  1217. props.add (new PathPointTypeProperty (owner, index));
  1218. props.add (new PathPointPositionProperty (owner, index, 0, "control pt1 x", PositionPropertyBase::componentX));
  1219. props.add (new PathPointPositionProperty (owner, index, 0, "control pt1 y", PositionPropertyBase::componentY));
  1220. props.add (new PathPointPositionProperty (owner, index, 1, "control pt2 x", PositionPropertyBase::componentX));
  1221. props.add (new PathPointPositionProperty (owner, index, 1, "control pt2 y", PositionPropertyBase::componentY));
  1222. props.add (new PathPointPositionProperty (owner, index, 2, "x", PositionPropertyBase::componentX));
  1223. props.add (new PathPointPositionProperty (owner, index, 2, "y", PositionPropertyBase::componentY));
  1224. props.add (new AddNewPointProperty (owner, index));
  1225. break;
  1226. case Path::Iterator::closePath:
  1227. break;
  1228. default:
  1229. jassertfalse;
  1230. break;
  1231. }
  1232. }
  1233. void PathPoint::deleteFromPath()
  1234. {
  1235. owner->deletePoint (owner->points.indexOf (this), true);
  1236. }
  1237. //==============================================================================
  1238. PathPointComponent::PathPointComponent (PaintElementPath* const path_,
  1239. const int index_,
  1240. const int pointNumber_)
  1241. : ElementSiblingComponent (path_),
  1242. path (path_),
  1243. routine (path_->getOwner()),
  1244. index (index_),
  1245. pointNumber (pointNumber_),
  1246. selected (false)
  1247. {
  1248. setSize (11, 11);
  1249. setRepaintsOnMouseActivity (true);
  1250. selected = routine->getSelectedPoints().isSelected (path_->points [index]);
  1251. routine->getSelectedPoints().addChangeListener (this);
  1252. }
  1253. PathPointComponent::~PathPointComponent()
  1254. {
  1255. routine->getSelectedPoints().removeChangeListener (this);
  1256. }
  1257. void PathPointComponent::updatePosition()
  1258. {
  1259. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  1260. jassert (getParentComponent() != nullptr);
  1261. double x, y;
  1262. path->getPoint (index, pointNumber, x, y, area);
  1263. setCentrePosition (roundToInt (x),
  1264. roundToInt (y));
  1265. }
  1266. void PathPointComponent::showPopupMenu()
  1267. {
  1268. }
  1269. void PathPointComponent::paint (Graphics& g)
  1270. {
  1271. if (isMouseOverOrDragging())
  1272. g.fillAll (Colours::red);
  1273. if (selected)
  1274. {
  1275. g.setColour (Colours::red);
  1276. g.drawRect (getLocalBounds());
  1277. }
  1278. g.setColour (Colours::white);
  1279. g.fillRect (getWidth() / 2 - 3, getHeight() / 2 - 3, 7, 7);
  1280. g.setColour (Colours::black);
  1281. if (pointNumber < path->getPoint (index)->getNumPoints() - 1)
  1282. g.drawRect (getWidth() / 2 - 2, getHeight() / 2 - 2, 5, 5);
  1283. else
  1284. g.fillRect (getWidth() / 2 - 2, getHeight() / 2 - 2, 5, 5);
  1285. }
  1286. void PathPointComponent::mouseDown (const MouseEvent& e)
  1287. {
  1288. dragging = false;
  1289. if (e.mods.isPopupMenu())
  1290. {
  1291. showPopupMenu();
  1292. return; // this may be deleted now..
  1293. }
  1294. dragX = getX() + getWidth() / 2;
  1295. dragY = getY() + getHeight() / 2;
  1296. mouseDownSelectStatus = routine->getSelectedPoints().addToSelectionOnMouseDown (path->points [index], e.mods);
  1297. owner->getDocument()->beginTransaction();
  1298. }
  1299. void PathPointComponent::mouseDrag (const MouseEvent& e)
  1300. {
  1301. if (! e.mods.isPopupMenu())
  1302. {
  1303. if (selected && ! dragging)
  1304. dragging = e.mouseWasDraggedSinceMouseDown();
  1305. if (dragging)
  1306. {
  1307. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  1308. int x = dragX + e.getDistanceFromDragStartX() - area.getX();
  1309. int y = dragY + e.getDistanceFromDragStartY() - area.getY();
  1310. if (JucerDocument* const document = owner->getDocument())
  1311. {
  1312. x = document->snapPosition (x);
  1313. y = document->snapPosition (y);
  1314. }
  1315. owner->getDocument()->getUndoManager().undoCurrentTransactionOnly();
  1316. path->movePoint (index, pointNumber, x + area.getX(), y + area.getY(), area, true);
  1317. }
  1318. }
  1319. }
  1320. void PathPointComponent::mouseUp (const MouseEvent& e)
  1321. {
  1322. routine->getSelectedPoints().addToSelectionOnMouseUp (path->points [index],
  1323. e.mods, dragging,
  1324. mouseDownSelectStatus);
  1325. }
  1326. void PathPointComponent::changeListenerCallback (ChangeBroadcaster* source)
  1327. {
  1328. ElementSiblingComponent::changeListenerCallback (source);
  1329. const bool nowSelected = routine->getSelectedPoints().isSelected (path->points [index]);
  1330. if (nowSelected != selected)
  1331. {
  1332. selected = nowSelected;
  1333. repaint();
  1334. if (Component* parent = getParentComponent())
  1335. parent->repaint();
  1336. }
  1337. }