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.

1612 lines
50KB

  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 (int i = 0; i < points.size(); ++i)
  306. {
  307. const PathPoint* const p = points.getUnchecked(i);
  308. switch (p->type)
  309. {
  310. case Path::Iterator::startNewSubPath:
  311. r << pathVariable << ".startNewSubPath (" << positionToPairOfValues (p->pos[0], layout) << ");\n";
  312. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  313. break;
  314. case Path::Iterator::lineTo:
  315. r << pathVariable << ".lineTo (" << positionToPairOfValues (p->pos[0], layout) << ");\n";
  316. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  317. break;
  318. case Path::Iterator::quadraticTo:
  319. r << pathVariable << ".quadraticTo (" << positionToPairOfValues (p->pos[0], layout)
  320. << ", " << positionToPairOfValues (p->pos[1], layout) << ");\n";
  321. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  322. somePointsAreRelative = somePointsAreRelative || ! p->pos[1].rect.isPositionAbsolute();
  323. break;
  324. case Path::Iterator::cubicTo:
  325. r << pathVariable << ".cubicTo (" << positionToPairOfValues (p->pos[0], layout)
  326. << ", " << positionToPairOfValues (p->pos[1], layout)
  327. << ", " << positionToPairOfValues (p->pos[2], layout) << ");\n";
  328. somePointsAreRelative = somePointsAreRelative || ! p->pos[0].rect.isPositionAbsolute();
  329. somePointsAreRelative = somePointsAreRelative || ! p->pos[1].rect.isPositionAbsolute();
  330. somePointsAreRelative = somePointsAreRelative || ! p->pos[2].rect.isPositionAbsolute();
  331. break;
  332. case Path::Iterator::closePath:
  333. r << pathVariable << ".closeSubPath();\n";
  334. break;
  335. default:
  336. jassertfalse;
  337. break;
  338. }
  339. }
  340. r << '\n';
  341. if (somePointsAreRelative)
  342. code.getCallbackCode (String(), "void", "resized()", false)
  343. << pathVariable << ".clear();\n" << r;
  344. else
  345. code.constructorCode << r;
  346. if (! fillType.isInvisible())
  347. {
  348. fillType.fillInGeneratedCode (code, paintMethodCode);
  349. paintMethodCode << "g.fillPath (" << pathVariable << ");\n";
  350. }
  351. if (isStrokePresent && ! strokeType.isInvisible())
  352. {
  353. String s;
  354. strokeType.fill.fillInGeneratedCode (code, s);
  355. s << "g.strokePath (" << pathVariable << ", " << strokeType.getPathStrokeCode() << ");\n";
  356. paintMethodCode += s;
  357. }
  358. paintMethodCode += "\n";
  359. }
  360. //==============================================================================
  361. XmlElement* PaintElementPath::createXml() const
  362. {
  363. XmlElement* e = new XmlElement (getTagName());
  364. position.applyToXml (*e);
  365. addColourAttributes (e);
  366. e->setAttribute ("nonZeroWinding", nonZeroWinding);
  367. e->addTextElement (pathToString());
  368. return e;
  369. }
  370. bool PaintElementPath::loadFromXml (const XmlElement& xml)
  371. {
  372. if (xml.hasTagName (getTagName()))
  373. {
  374. position.restoreFromXml (xml, position);
  375. loadColourAttributes (xml);
  376. nonZeroWinding = xml.getBoolAttribute ("nonZeroWinding", true);
  377. restorePathFromString (xml.getAllSubText().trim());
  378. return true;
  379. }
  380. jassertfalse;
  381. return false;
  382. }
  383. //==============================================================================
  384. void PaintElementPath::createSiblingComponents()
  385. {
  386. ColouredElement::createSiblingComponents();
  387. for (int i = 0; i < points.size(); ++i)
  388. {
  389. switch (points.getUnchecked(i)->type)
  390. {
  391. case Path::Iterator::startNewSubPath:
  392. siblingComponents.add (new PathPointComponent (this, i, 0));
  393. break;
  394. case Path::Iterator::lineTo:
  395. siblingComponents.add (new PathPointComponent (this, i, 0));
  396. break;
  397. case Path::Iterator::quadraticTo:
  398. siblingComponents.add (new PathPointComponent (this, i, 0));
  399. siblingComponents.add (new PathPointComponent (this, i, 1));
  400. break;
  401. case Path::Iterator::cubicTo:
  402. siblingComponents.add (new PathPointComponent (this, i, 0));
  403. siblingComponents.add (new PathPointComponent (this, i, 1));
  404. siblingComponents.add (new PathPointComponent (this, i, 2));
  405. break;
  406. case Path::Iterator::closePath:
  407. break;
  408. default:
  409. jassertfalse; break;
  410. }
  411. }
  412. for (int i = 0; i < siblingComponents.size(); ++i)
  413. {
  414. getParentComponent()->addAndMakeVisible (siblingComponents.getUnchecked(i));
  415. siblingComponents.getUnchecked(i)->updatePosition();
  416. }
  417. }
  418. String PaintElementPath::pathToString() const
  419. {
  420. String s;
  421. for (int i = 0; i < points.size(); ++i)
  422. {
  423. const PathPoint* const p = points.getUnchecked(i);
  424. switch (p->type)
  425. {
  426. case Path::Iterator::startNewSubPath:
  427. s << "s " << p->pos[0].toString() << ' ';
  428. break;
  429. case Path::Iterator::lineTo:
  430. s << "l " << p->pos[0].toString() << ' ';
  431. break;
  432. case Path::Iterator::quadraticTo:
  433. s << "q " << p->pos[0].toString()
  434. << ' ' << p->pos[1].toString() << ' ';
  435. break;
  436. case Path::Iterator::cubicTo:
  437. s << "c " << p->pos[0].toString()
  438. << ' ' << p->pos[1].toString() << ' '
  439. << ' ' << p->pos[2].toString() << ' ';
  440. break;
  441. case Path::Iterator::closePath:
  442. s << "x ";
  443. break;
  444. default:
  445. jassertfalse; break;
  446. }
  447. }
  448. return s.trimEnd();
  449. }
  450. void PaintElementPath::restorePathFromString (const String& s)
  451. {
  452. points.clear();
  453. StringArray tokens;
  454. tokens.addTokens (s, false);
  455. tokens.trim();
  456. tokens.removeEmptyStrings();
  457. for (int i = 0; i < tokens.size(); ++i)
  458. {
  459. ScopedPointer<PathPoint> p (new PathPoint (this));
  460. if (tokens[i] == "s")
  461. {
  462. p->type = Path::Iterator::startNewSubPath;
  463. p->pos [0] = RelativePositionedRectangle();
  464. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  465. i += 2;
  466. }
  467. else if (tokens[i] == "l")
  468. {
  469. p->type = Path::Iterator::lineTo;
  470. p->pos [0] = RelativePositionedRectangle();
  471. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  472. i += 2;
  473. }
  474. else if (tokens[i] == "q")
  475. {
  476. p->type = Path::Iterator::quadraticTo;
  477. p->pos [0] = RelativePositionedRectangle();
  478. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  479. p->pos [1] = RelativePositionedRectangle();
  480. p->pos [1].rect = PositionedRectangle (tokens [i + 3] + " " + tokens [i + 4]);
  481. i += 4;
  482. }
  483. else if (tokens[i] == "c")
  484. {
  485. p->type = Path::Iterator::cubicTo;
  486. p->pos [0] = RelativePositionedRectangle();
  487. p->pos [0].rect = PositionedRectangle (tokens [i + 1] + " " + tokens [i + 2]);
  488. p->pos [1] = RelativePositionedRectangle();
  489. p->pos [1].rect = PositionedRectangle (tokens [i + 3] + " " + tokens [i + 4]);
  490. p->pos [2] = RelativePositionedRectangle();
  491. p->pos [2].rect = PositionedRectangle (tokens [i + 5] + " " + tokens [i + 6]);
  492. i += 6;
  493. }
  494. else if (tokens[i] == "x")
  495. {
  496. p->type = Path::Iterator::closePath;
  497. }
  498. else
  499. continue;
  500. points.add (p.release());
  501. }
  502. }
  503. void PaintElementPath::setToPath (const Path& newPath)
  504. {
  505. points.clear();
  506. Path::Iterator i (newPath);
  507. while (i.next())
  508. {
  509. ScopedPointer<PathPoint> p (new PathPoint (this));
  510. p->type = i.elementType;
  511. if (i.elementType == Path::Iterator::startNewSubPath)
  512. {
  513. p->pos [0].rect.setX (i.x1);
  514. p->pos [0].rect.setY (i.y1);
  515. }
  516. else if (i.elementType == Path::Iterator::lineTo)
  517. {
  518. p->pos [0].rect.setX (i.x1);
  519. p->pos [0].rect.setY (i.y1);
  520. }
  521. else if (i.elementType == Path::Iterator::quadraticTo)
  522. {
  523. p->pos [0].rect.setX (i.x1);
  524. p->pos [0].rect.setY (i.y1);
  525. p->pos [1].rect.setX (i.x2);
  526. p->pos [1].rect.setY (i.y2);
  527. }
  528. else if (i.elementType == Path::Iterator::cubicTo)
  529. {
  530. p->pos [0].rect.setX (i.x1);
  531. p->pos [0].rect.setY (i.y1);
  532. p->pos [1].rect.setX (i.x2);
  533. p->pos [1].rect.setY (i.y2);
  534. p->pos [2].rect.setX (i.x3);
  535. p->pos [2].rect.setY (i.y3);
  536. }
  537. else if (i.elementType == Path::Iterator::closePath)
  538. {
  539. }
  540. else
  541. {
  542. continue;
  543. }
  544. points.add (p.release());
  545. }
  546. }
  547. void PaintElementPath::updateStoredPath (const ComponentLayout* layout, const Rectangle<int>& relativeTo) const
  548. {
  549. if (lastPathBounds != relativeTo && ! relativeTo.isEmpty())
  550. {
  551. lastPathBounds = relativeTo;
  552. path.clear();
  553. for (int i = 0; i < points.size(); ++i)
  554. {
  555. const PathPoint* const p = points.getUnchecked(i);
  556. switch (p->type)
  557. {
  558. case Path::Iterator::startNewSubPath:
  559. path.startNewSubPath (p->pos[0].toXY (relativeTo, layout));
  560. break;
  561. case Path::Iterator::lineTo:
  562. path.lineTo (p->pos[0].toXY (relativeTo, layout));
  563. break;
  564. case Path::Iterator::quadraticTo:
  565. path.quadraticTo (p->pos[0].toXY (relativeTo, layout),
  566. p->pos[1].toXY (relativeTo, layout));
  567. break;
  568. case Path::Iterator::cubicTo:
  569. path.cubicTo (p->pos[0].toXY (relativeTo, layout),
  570. p->pos[1].toXY (relativeTo, layout),
  571. p->pos[2].toXY (relativeTo, layout));
  572. break;
  573. case Path::Iterator::closePath:
  574. path.closeSubPath();
  575. break;
  576. default:
  577. jassertfalse; break;
  578. }
  579. }
  580. }
  581. }
  582. //==============================================================================
  583. class ChangeWindingAction : public PaintElementUndoableAction <PaintElementPath>
  584. {
  585. public:
  586. ChangeWindingAction (PaintElementPath* const path, const bool newValue_)
  587. : PaintElementUndoableAction <PaintElementPath> (path),
  588. newValue (newValue_),
  589. oldValue (path->isNonZeroWinding())
  590. {
  591. }
  592. bool perform()
  593. {
  594. showCorrectTab();
  595. getElement()->setNonZeroWinding (newValue, false);
  596. return true;
  597. }
  598. bool undo()
  599. {
  600. showCorrectTab();
  601. getElement()->setNonZeroWinding (oldValue, false);
  602. return true;
  603. }
  604. private:
  605. bool newValue, oldValue;
  606. };
  607. void PaintElementPath::setNonZeroWinding (const bool nonZero, const bool undoable)
  608. {
  609. if (nonZero != nonZeroWinding)
  610. {
  611. if (undoable)
  612. {
  613. perform (new ChangeWindingAction (this, nonZero), "Change path winding rule");
  614. }
  615. else
  616. {
  617. nonZeroWinding = nonZero;
  618. changed();
  619. }
  620. }
  621. }
  622. bool PaintElementPath::isSubpathClosed (int index) const
  623. {
  624. for (int i = index + 1; i < points.size(); ++i)
  625. {
  626. if (points.getUnchecked (i)->type == Path::Iterator::closePath)
  627. return true;
  628. if (points.getUnchecked (i)->type == Path::Iterator::startNewSubPath)
  629. break;
  630. }
  631. return false;
  632. }
  633. //==============================================================================
  634. void PaintElementPath::setSubpathClosed (int index, const bool closed, const bool undoable)
  635. {
  636. if (closed != isSubpathClosed (index))
  637. {
  638. for (int i = index + 1; i < points.size(); ++i)
  639. {
  640. PathPoint* p = points.getUnchecked (i);
  641. if (p->type == Path::Iterator::closePath)
  642. {
  643. jassert (! closed);
  644. deletePoint (i, undoable);
  645. return;
  646. }
  647. if (p->type == Path::Iterator::startNewSubPath)
  648. {
  649. jassert (closed);
  650. PathPoint* pp = addPoint (i - 1, undoable);
  651. PathPoint p2 (*pp);
  652. p2.type = Path::Iterator::closePath;
  653. perform (new ChangePointAction (pp, p2), "Close subpath");
  654. return;
  655. }
  656. }
  657. jassert (closed);
  658. PathPoint* p = addPoint (points.size() - 1, undoable);
  659. PathPoint p2 (*p);
  660. p2.type = Path::Iterator::closePath;
  661. perform (new ChangePointAction (p, p2), "Close subpath");
  662. }
  663. }
  664. //==============================================================================
  665. class AddPointAction : public PaintElementUndoableAction <PaintElementPath>
  666. {
  667. public:
  668. AddPointAction (PaintElementPath* path, int pointIndexToAddItAfter_)
  669. : PaintElementUndoableAction <PaintElementPath> (path),
  670. indexAdded (-1),
  671. pointIndexToAddItAfter (pointIndexToAddItAfter_)
  672. {
  673. }
  674. bool perform()
  675. {
  676. showCorrectTab();
  677. PaintElementPath* const path = getElement();
  678. jassert (path != nullptr);
  679. PathPoint* const p = path->addPoint (pointIndexToAddItAfter, false);
  680. jassert (p != nullptr);
  681. indexAdded = path->indexOfPoint (p);
  682. jassert (indexAdded >= 0);
  683. return true;
  684. }
  685. bool undo()
  686. {
  687. showCorrectTab();
  688. PaintElementPath* const path = getElement();
  689. jassert (path != nullptr);
  690. path->deletePoint (indexAdded, false);
  691. return true;
  692. }
  693. int indexAdded;
  694. private:
  695. int pointIndexToAddItAfter;
  696. };
  697. PathPoint* PaintElementPath::addPoint (int pointIndexToAddItAfter, const bool undoable)
  698. {
  699. if (undoable)
  700. {
  701. AddPointAction* action = new AddPointAction (this, pointIndexToAddItAfter);
  702. perform (action, "Add path point");
  703. return points [action->indexAdded];
  704. }
  705. double x1 = 20.0, y1 = 20.0, x2, y2;
  706. ComponentLayout* layout = getDocument()->getComponentLayout();
  707. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  708. if (points [pointIndexToAddItAfter] != nullptr)
  709. points [pointIndexToAddItAfter]->pos [points [pointIndexToAddItAfter]->getNumPoints() - 1].getXY (x1, y1, area, layout);
  710. else if (points[0] != nullptr)
  711. points[0]->pos[0].getXY (x1, y1, area, layout);
  712. x2 = x1 + 50.0;
  713. y2 = y1 + 50.0;
  714. if (points [pointIndexToAddItAfter + 1] != nullptr)
  715. {
  716. if (points [pointIndexToAddItAfter + 1]->type == Path::Iterator::closePath
  717. || points [pointIndexToAddItAfter + 1]->type == Path::Iterator::startNewSubPath)
  718. {
  719. int i = pointIndexToAddItAfter;
  720. while (i > 0)
  721. if (points [--i]->type == Path::Iterator::startNewSubPath)
  722. break;
  723. if (i != pointIndexToAddItAfter)
  724. points [i]->pos[0].getXY (x2, y2, area, layout);
  725. }
  726. else
  727. {
  728. points [pointIndexToAddItAfter + 1]->pos[0].getXY (x2, y2, area, layout);
  729. }
  730. }
  731. else
  732. {
  733. int i = pointIndexToAddItAfter + 1;
  734. while (i > 0)
  735. if (points [--i]->type == Path::Iterator::startNewSubPath)
  736. break;
  737. points[i]->pos[0].getXY (x2, y2, area, layout);
  738. }
  739. PathPoint* const p = new PathPoint (this);
  740. p->type = Path::Iterator::lineTo;
  741. p->pos[0].rect.setX ((x1 + x2) * 0.5f);
  742. p->pos[0].rect.setY ((y1 + y2) * 0.5f);
  743. points.insert (pointIndexToAddItAfter + 1, p);
  744. pointListChanged();
  745. return p;
  746. }
  747. //==============================================================================
  748. class DeletePointAction : public PaintElementUndoableAction <PaintElementPath>
  749. {
  750. public:
  751. DeletePointAction (PaintElementPath* const path, const int indexToRemove_)
  752. : PaintElementUndoableAction <PaintElementPath> (path),
  753. indexToRemove (indexToRemove_),
  754. oldValue (*path->getPoint (indexToRemove))
  755. {
  756. }
  757. bool perform()
  758. {
  759. showCorrectTab();
  760. PaintElementPath* const path = getElement();
  761. jassert (path != nullptr);
  762. path->deletePoint (indexToRemove, false);
  763. return path != nullptr;
  764. }
  765. bool undo()
  766. {
  767. showCorrectTab();
  768. PaintElementPath* const path = getElement();
  769. jassert (path != nullptr);
  770. PathPoint* p = path->addPoint (indexToRemove - 1, false);
  771. *p = oldValue;
  772. return path != nullptr;
  773. }
  774. int indexToRemove;
  775. private:
  776. PathPoint oldValue;
  777. };
  778. void PaintElementPath::deletePoint (int pointIndex, const bool undoable)
  779. {
  780. if (undoable)
  781. {
  782. perform (new DeletePointAction (this, pointIndex), "Delete path point");
  783. }
  784. else
  785. {
  786. PathPoint* const p = points [pointIndex];
  787. if (p != nullptr && pointIndex > 0)
  788. {
  789. owner->getSelectedPoints().deselect (p);
  790. owner->getSelectedPoints().changed (true);
  791. points.remove (pointIndex);
  792. pointListChanged();
  793. }
  794. }
  795. }
  796. //==============================================================================
  797. bool PaintElementPath::getPoint (int index, int pointNumber, double& x, double& y, const Rectangle<int>& parentArea) const
  798. {
  799. const PathPoint* const p = points [index];
  800. if (p == nullptr)
  801. {
  802. x = y = 0;
  803. return false;
  804. }
  805. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  806. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  807. p->pos [pointNumber].getXY (x, y, parentArea, getDocument()->getComponentLayout());
  808. return true;
  809. }
  810. int PaintElementPath::findSegmentAtXY (int x, int y) const
  811. {
  812. double x1, y1, x2, y2, x3, y3, lastX = 0.0, lastY = 0.0, subPathStartX = 0.0, subPathStartY = 0.0;
  813. ComponentLayout* const layout = getDocument()->getComponentLayout();
  814. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  815. int subpathStartIndex = 0;
  816. float thickness = 10.0f;
  817. if (isStrokePresent)
  818. thickness = jmax (thickness, strokeType.stroke.getStrokeThickness());
  819. for (int i = 0; i < points.size(); ++i)
  820. {
  821. Path segmentPath;
  822. PathPoint* const p = points.getUnchecked (i);
  823. switch (p->type)
  824. {
  825. case Path::Iterator::startNewSubPath:
  826. p->pos[0].getXY (lastX, lastY, area, layout);
  827. subPathStartX = lastX;
  828. subPathStartY = lastY;
  829. subpathStartIndex = i;
  830. break;
  831. case Path::Iterator::lineTo:
  832. p->pos[0].getXY (x1, y1, area, layout);
  833. segmentPath.addLineSegment (Line<float> ((float) lastX, (float) lastY, (float) x1, (float) y1), thickness);
  834. if (segmentPath.contains ((float) x, (float) y))
  835. return i;
  836. lastX = x1;
  837. lastY = y1;
  838. break;
  839. case Path::Iterator::quadraticTo:
  840. p->pos[0].getXY (x1, y1, area, layout);
  841. p->pos[1].getXY (x2, y2, area, layout);
  842. segmentPath.startNewSubPath ((float) lastX, (float) lastY);
  843. segmentPath.quadraticTo ((float) x1, (float) y1, (float) x2, (float) y2);
  844. PathStrokeType (thickness).createStrokedPath (segmentPath, segmentPath);
  845. if (segmentPath.contains ((float) x, (float) y))
  846. return i;
  847. lastX = x2;
  848. lastY = y2;
  849. break;
  850. case Path::Iterator::cubicTo:
  851. p->pos[0].getXY (x1, y1, area, layout);
  852. p->pos[1].getXY (x2, y2, area, layout);
  853. p->pos[2].getXY (x3, y3, area, layout);
  854. segmentPath.startNewSubPath ((float) lastX, (float) lastY);
  855. segmentPath.cubicTo ((float) x1, (float) y1, (float) x2, (float) y2, (float) x3, (float) y3);
  856. PathStrokeType (thickness).createStrokedPath (segmentPath, segmentPath);
  857. if (segmentPath.contains ((float) x, (float) y))
  858. return i;
  859. lastX = x3;
  860. lastY = y3;
  861. break;
  862. case Path::Iterator::closePath:
  863. segmentPath.addLineSegment (Line<float> ((float) lastX, (float) lastY, (float) subPathStartX, (float) subPathStartY), thickness);
  864. if (segmentPath.contains ((float) x, (float) y))
  865. return subpathStartIndex;
  866. lastX = subPathStartX;
  867. lastY = subPathStartY;
  868. break;
  869. default:
  870. jassertfalse; break;
  871. }
  872. }
  873. return -1;
  874. }
  875. //==============================================================================
  876. void PaintElementPath::movePoint (int index, int pointNumber,
  877. double newX, double newY,
  878. const Rectangle<int>& parentArea,
  879. const bool undoable)
  880. {
  881. if (PathPoint* const p = points [index])
  882. {
  883. PathPoint newPoint (*p);
  884. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  885. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  886. RelativePositionedRectangle& pr = newPoint.pos [pointNumber];
  887. double x, y, w, h;
  888. pr.getRectangleDouble (x, y, w, h, parentArea, getDocument()->getComponentLayout());
  889. pr.updateFrom (newX, newY, w, h, parentArea, getDocument()->getComponentLayout());
  890. if (undoable)
  891. {
  892. perform (new ChangePointAction (p, index, newPoint), "Move path point");
  893. }
  894. else
  895. {
  896. *p = newPoint;
  897. changed();
  898. }
  899. }
  900. }
  901. RelativePositionedRectangle PaintElementPath::getPoint (int index, int pointNumber) const
  902. {
  903. if (PathPoint* const p = points [index])
  904. {
  905. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  906. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  907. return p->pos [pointNumber];
  908. }
  909. jassertfalse;
  910. return RelativePositionedRectangle();
  911. }
  912. void PaintElementPath::setPoint (int index, int pointNumber, const RelativePositionedRectangle& newPos, const bool undoable)
  913. {
  914. if (PathPoint* const p = points [index])
  915. {
  916. PathPoint newPoint (*p);
  917. jassert (pointNumber < 3 || p->type == Path::Iterator::cubicTo);
  918. jassert (pointNumber < 2 || p->type == Path::Iterator::cubicTo || p->type == Path::Iterator::quadraticTo);
  919. if (newPoint.pos [pointNumber] != newPos)
  920. {
  921. newPoint.pos [pointNumber] = newPos;
  922. if (undoable)
  923. {
  924. perform (new ChangePointAction (p, index, newPoint), "Change path point position");
  925. }
  926. else
  927. {
  928. *p = newPoint;
  929. changed();
  930. }
  931. }
  932. }
  933. else
  934. {
  935. jassertfalse;
  936. }
  937. }
  938. //==============================================================================
  939. class PathPointTypeProperty : public ChoicePropertyComponent,
  940. public ChangeListener
  941. {
  942. public:
  943. PathPointTypeProperty (PaintElementPath* const owner_,
  944. const int index_)
  945. : ChoicePropertyComponent ("point type"),
  946. owner (owner_),
  947. index (index_)
  948. {
  949. choices.add ("Start of sub-path");
  950. choices.add ("Line");
  951. choices.add ("Quadratic");
  952. choices.add ("Cubic");
  953. owner->getDocument()->addChangeListener (this);
  954. }
  955. ~PathPointTypeProperty()
  956. {
  957. owner->getDocument()->removeChangeListener (this);
  958. }
  959. void setIndex (int newIndex)
  960. {
  961. Path::Iterator::PathElementType type = Path::Iterator::startNewSubPath;
  962. switch (newIndex)
  963. {
  964. case 0: type = Path::Iterator::startNewSubPath; break;
  965. case 1: type = Path::Iterator::lineTo; break;
  966. case 2: type = Path::Iterator::quadraticTo; break;
  967. case 3: type = Path::Iterator::cubicTo; break;
  968. default: jassertfalse; break;
  969. }
  970. const Rectangle<int> area (((PaintRoutineEditor*) owner->getParentComponent())->getComponentArea());
  971. owner->getPoint (index)->changePointType (type, area, true);
  972. }
  973. int getIndex() const
  974. {
  975. const PathPoint* const p = owner->getPoint (index);
  976. jassert (p != nullptr);
  977. switch (p->type)
  978. {
  979. case Path::Iterator::startNewSubPath: return 0;
  980. case Path::Iterator::lineTo: return 1;
  981. case Path::Iterator::quadraticTo: return 2;
  982. case Path::Iterator::cubicTo: return 3;
  983. case Path::Iterator::closePath: break;
  984. default: jassertfalse; break;
  985. }
  986. return 0;
  987. }
  988. void changeListenerCallback (ChangeBroadcaster*)
  989. {
  990. refresh();
  991. }
  992. private:
  993. PaintElementPath* const owner;
  994. const int index;
  995. };
  996. //==============================================================================
  997. class PathPointPositionProperty : public PositionPropertyBase
  998. {
  999. public:
  1000. PathPointPositionProperty (PaintElementPath* const owner_,
  1001. const int index_, const int pointNumber_,
  1002. const String& name,
  1003. ComponentPositionDimension dimension_)
  1004. : PositionPropertyBase (owner_, name, dimension_, false, false,
  1005. owner_->getDocument()->getComponentLayout()),
  1006. owner (owner_),
  1007. index (index_),
  1008. pointNumber (pointNumber_)
  1009. {
  1010. owner->getDocument()->addChangeListener (this);
  1011. }
  1012. ~PathPointPositionProperty()
  1013. {
  1014. owner->getDocument()->removeChangeListener (this);
  1015. }
  1016. void setPosition (const RelativePositionedRectangle& newPos)
  1017. {
  1018. owner->setPoint (index, pointNumber, newPos, true);
  1019. }
  1020. RelativePositionedRectangle getPosition() const
  1021. {
  1022. return owner->getPoint (index, pointNumber);
  1023. }
  1024. private:
  1025. PaintElementPath* const owner;
  1026. const int index, pointNumber;
  1027. };
  1028. //==============================================================================
  1029. class PathPointClosedProperty : public ChoicePropertyComponent,
  1030. private ChangeListener
  1031. {
  1032. public:
  1033. PathPointClosedProperty (PaintElementPath* const owner_, const int index_)
  1034. : ChoicePropertyComponent ("openness"),
  1035. owner (owner_),
  1036. index (index_)
  1037. {
  1038. owner->getDocument()->addChangeListener (this);
  1039. choices.add ("Subpath is closed");
  1040. choices.add ("Subpath is open-ended");
  1041. }
  1042. ~PathPointClosedProperty()
  1043. {
  1044. owner->getDocument()->removeChangeListener (this);
  1045. }
  1046. void changeListenerCallback (ChangeBroadcaster*)
  1047. {
  1048. refresh();
  1049. }
  1050. void setIndex (int newIndex)
  1051. {
  1052. owner->setSubpathClosed (index, newIndex == 0, true);
  1053. }
  1054. int getIndex() const
  1055. {
  1056. return owner->isSubpathClosed (index) ? 0 : 1;
  1057. }
  1058. private:
  1059. PaintElementPath* const owner;
  1060. const int index;
  1061. };
  1062. //==============================================================================
  1063. class AddNewPointProperty : public ButtonPropertyComponent
  1064. {
  1065. public:
  1066. AddNewPointProperty (PaintElementPath* const owner_, const int index_)
  1067. : ButtonPropertyComponent ("new point", false),
  1068. owner (owner_),
  1069. index (index_)
  1070. {
  1071. }
  1072. void buttonClicked()
  1073. {
  1074. owner->addPoint (index, true);
  1075. }
  1076. String getButtonText() const { return "Add new point"; }
  1077. private:
  1078. PaintElementPath* const owner;
  1079. const int index;
  1080. };
  1081. //==============================================================================
  1082. PathPoint::PathPoint (PaintElementPath* const owner_)
  1083. : owner (owner_)
  1084. {
  1085. }
  1086. PathPoint::PathPoint (const PathPoint& other)
  1087. : owner (other.owner),
  1088. type (other.type)
  1089. {
  1090. pos [0] = other.pos [0];
  1091. pos [1] = other.pos [1];
  1092. pos [2] = other.pos [2];
  1093. }
  1094. PathPoint& PathPoint::operator= (const PathPoint& other)
  1095. {
  1096. owner = other.owner;
  1097. type = other.type;
  1098. pos [0] = other.pos [0];
  1099. pos [1] = other.pos [1];
  1100. pos [2] = other.pos [2];
  1101. return *this;
  1102. }
  1103. PathPoint::~PathPoint()
  1104. {
  1105. }
  1106. int PathPoint::getNumPoints() const
  1107. {
  1108. if (type == Path::Iterator::cubicTo) return 3;
  1109. if (type == Path::Iterator::quadraticTo) return 2;
  1110. if (type == Path::Iterator::closePath) return 0;
  1111. return 1;
  1112. }
  1113. PathPoint PathPoint::withChangedPointType (const Path::Iterator::PathElementType newType,
  1114. const Rectangle<int>& parentArea) const
  1115. {
  1116. PathPoint p (*this);
  1117. if (newType != p.type)
  1118. {
  1119. int oldNumPoints = getNumPoints();
  1120. p.type = newType;
  1121. int numPoints = p.getNumPoints();
  1122. if (numPoints != oldNumPoints)
  1123. {
  1124. double lastX, lastY;
  1125. double x, y, w, h;
  1126. p.pos [numPoints - 1] = p.pos [oldNumPoints - 1];
  1127. p.pos [numPoints - 1].getRectangleDouble (x, y, w, h, parentArea, owner->getDocument()->getComponentLayout());
  1128. const int index = owner->points.indexOf (this);
  1129. if (PathPoint* lastPoint = owner->points [index - 1])
  1130. {
  1131. lastPoint->pos [lastPoint->getNumPoints() - 1]
  1132. .getRectangleDouble (lastX, lastY, w, h, parentArea, owner->getDocument()->getComponentLayout());
  1133. }
  1134. else
  1135. {
  1136. jassertfalse;
  1137. lastX = x;
  1138. lastY = y;
  1139. }
  1140. for (int i = 0; i < numPoints - 1; ++i)
  1141. {
  1142. p.pos[i] = p.pos [numPoints - 1];
  1143. p.pos[i].updateFrom (lastX + (x - lastX) * (i + 1) / numPoints,
  1144. lastY + (y - lastY) * (i + 1) / numPoints,
  1145. w, h,
  1146. parentArea,
  1147. owner->getDocument()->getComponentLayout());
  1148. }
  1149. }
  1150. }
  1151. return p;
  1152. }
  1153. void PathPoint::changePointType (const Path::Iterator::PathElementType newType,
  1154. const Rectangle<int>& parentArea, const bool undoable)
  1155. {
  1156. if (newType != type)
  1157. {
  1158. if (undoable)
  1159. {
  1160. owner->perform (new ChangePointAction (this, withChangedPointType (newType, parentArea)),
  1161. "Change path point type");
  1162. }
  1163. else
  1164. {
  1165. *this = withChangedPointType (newType, parentArea);
  1166. owner->pointListChanged();
  1167. }
  1168. }
  1169. }
  1170. void PathPoint::getEditableProperties (Array<PropertyComponent*>& props)
  1171. {
  1172. const int index = owner->points.indexOf (this);
  1173. jassert (index >= 0);
  1174. switch (type)
  1175. {
  1176. case Path::Iterator::startNewSubPath:
  1177. props.add (new PathPointPositionProperty (owner, index, 0, "x", PositionPropertyBase::componentX));
  1178. props.add (new PathPointPositionProperty (owner, index, 0, "y", PositionPropertyBase::componentY));
  1179. props.add (new PathPointClosedProperty (owner, index));
  1180. props.add (new AddNewPointProperty (owner, index));
  1181. break;
  1182. case Path::Iterator::lineTo:
  1183. props.add (new PathPointTypeProperty (owner, index));
  1184. props.add (new PathPointPositionProperty (owner, index, 0, "x", PositionPropertyBase::componentX));
  1185. props.add (new PathPointPositionProperty (owner, index, 0, "y", PositionPropertyBase::componentY));
  1186. props.add (new AddNewPointProperty (owner, index));
  1187. break;
  1188. case Path::Iterator::quadraticTo:
  1189. props.add (new PathPointTypeProperty (owner, index));
  1190. props.add (new PathPointPositionProperty (owner, index, 0, "control pt x", PositionPropertyBase::componentX));
  1191. props.add (new PathPointPositionProperty (owner, index, 0, "control pt y", PositionPropertyBase::componentY));
  1192. props.add (new PathPointPositionProperty (owner, index, 1, "x", PositionPropertyBase::componentX));
  1193. props.add (new PathPointPositionProperty (owner, index, 1, "y", PositionPropertyBase::componentY));
  1194. props.add (new AddNewPointProperty (owner, index));
  1195. break;
  1196. case Path::Iterator::cubicTo:
  1197. props.add (new PathPointTypeProperty (owner, index));
  1198. props.add (new PathPointPositionProperty (owner, index, 0, "control pt1 x", PositionPropertyBase::componentX));
  1199. props.add (new PathPointPositionProperty (owner, index, 0, "control pt1 y", PositionPropertyBase::componentY));
  1200. props.add (new PathPointPositionProperty (owner, index, 1, "control pt2 x", PositionPropertyBase::componentX));
  1201. props.add (new PathPointPositionProperty (owner, index, 1, "control pt2 y", PositionPropertyBase::componentY));
  1202. props.add (new PathPointPositionProperty (owner, index, 2, "x", PositionPropertyBase::componentX));
  1203. props.add (new PathPointPositionProperty (owner, index, 2, "y", PositionPropertyBase::componentY));
  1204. props.add (new AddNewPointProperty (owner, index));
  1205. break;
  1206. case Path::Iterator::closePath:
  1207. break;
  1208. default:
  1209. jassertfalse;
  1210. break;
  1211. }
  1212. }
  1213. void PathPoint::deleteFromPath()
  1214. {
  1215. owner->deletePoint (owner->points.indexOf (this), true);
  1216. }
  1217. //==============================================================================
  1218. PathPointComponent::PathPointComponent (PaintElementPath* const path_,
  1219. const int index_,
  1220. const int pointNumber_)
  1221. : ElementSiblingComponent (path_),
  1222. path (path_),
  1223. routine (path_->getOwner()),
  1224. index (index_),
  1225. pointNumber (pointNumber_),
  1226. selected (false)
  1227. {
  1228. setSize (11, 11);
  1229. setRepaintsOnMouseActivity (true);
  1230. selected = routine->getSelectedPoints().isSelected (path_->points [index]);
  1231. routine->getSelectedPoints().addChangeListener (this);
  1232. }
  1233. PathPointComponent::~PathPointComponent()
  1234. {
  1235. routine->getSelectedPoints().removeChangeListener (this);
  1236. }
  1237. void PathPointComponent::updatePosition()
  1238. {
  1239. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  1240. jassert (getParentComponent() != nullptr);
  1241. double x, y;
  1242. path->getPoint (index, pointNumber, x, y, area);
  1243. setCentrePosition (roundToInt (x),
  1244. roundToInt (y));
  1245. }
  1246. void PathPointComponent::showPopupMenu()
  1247. {
  1248. }
  1249. void PathPointComponent::paint (Graphics& g)
  1250. {
  1251. if (isMouseOverOrDragging())
  1252. g.fillAll (Colours::red);
  1253. if (selected)
  1254. {
  1255. g.setColour (Colours::red);
  1256. g.drawRect (getLocalBounds());
  1257. }
  1258. g.setColour (Colours::white);
  1259. g.fillRect (getWidth() / 2 - 3, getHeight() / 2 - 3, 7, 7);
  1260. g.setColour (Colours::black);
  1261. if (pointNumber < path->getPoint (index)->getNumPoints() - 1)
  1262. g.drawRect (getWidth() / 2 - 2, getHeight() / 2 - 2, 5, 5);
  1263. else
  1264. g.fillRect (getWidth() / 2 - 2, getHeight() / 2 - 2, 5, 5);
  1265. }
  1266. void PathPointComponent::mouseDown (const MouseEvent& e)
  1267. {
  1268. dragging = false;
  1269. if (e.mods.isPopupMenu())
  1270. {
  1271. showPopupMenu();
  1272. return; // this may be deleted now..
  1273. }
  1274. dragX = getX() + getWidth() / 2;
  1275. dragY = getY() + getHeight() / 2;
  1276. mouseDownSelectStatus = routine->getSelectedPoints().addToSelectionOnMouseDown (path->points [index], e.mods);
  1277. owner->getDocument()->beginTransaction();
  1278. }
  1279. void PathPointComponent::mouseDrag (const MouseEvent& e)
  1280. {
  1281. if (! e.mods.isPopupMenu())
  1282. {
  1283. if (selected && ! dragging)
  1284. dragging = e.mouseWasDraggedSinceMouseDown();
  1285. if (dragging)
  1286. {
  1287. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  1288. int x = dragX + e.getDistanceFromDragStartX() - area.getX();
  1289. int y = dragY + e.getDistanceFromDragStartY() - area.getY();
  1290. if (JucerDocument* const document = owner->getDocument())
  1291. {
  1292. x = document->snapPosition (x);
  1293. y = document->snapPosition (y);
  1294. }
  1295. owner->getDocument()->getUndoManager().undoCurrentTransactionOnly();
  1296. path->movePoint (index, pointNumber, x + area.getX(), y + area.getY(), area, true);
  1297. }
  1298. }
  1299. }
  1300. void PathPointComponent::mouseUp (const MouseEvent& e)
  1301. {
  1302. routine->getSelectedPoints().addToSelectionOnMouseUp (path->points [index],
  1303. e.mods, dragging,
  1304. mouseDownSelectStatus);
  1305. }
  1306. void PathPointComponent::changeListenerCallback (ChangeBroadcaster* source)
  1307. {
  1308. ElementSiblingComponent::changeListenerCallback (source);
  1309. const bool nowSelected = routine->getSelectedPoints().isSelected (path->points [index]);
  1310. if (nowSelected != selected)
  1311. {
  1312. selected = nowSelected;
  1313. repaint();
  1314. if (Component* parent = getParentComponent())
  1315. parent->repaint();
  1316. }
  1317. }