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.

1608 lines
51KB

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