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.

424 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../../Application/jucer_Headers.h"
  14. #include "../../Application/jucer_Application.h"
  15. #include "jucer_ComponentLayoutEditor.h"
  16. #include "../UI/jucer_JucerCommandIDs.h"
  17. #include "../jucer_ObjectTypes.h"
  18. #include "../Components/jucer_JucerComponentHandler.h"
  19. //==============================================================================
  20. class SubComponentHolderComp : public Component
  21. {
  22. public:
  23. SubComponentHolderComp (JucerDocument& doc,
  24. SnapGridPainter& g)
  25. : document (doc), grid (g),
  26. dontFillBackground (false)
  27. {
  28. setInterceptsMouseClicks (false, false);
  29. setWantsKeyboardFocus (false);
  30. setFocusContainer (true);
  31. }
  32. void paint (Graphics& g) override
  33. {
  34. if (! dontFillBackground)
  35. {
  36. if (PaintRoutine* const background = document.getPaintRoutine (0))
  37. {
  38. background->fillWithBackground (g, false);
  39. background->drawElements (g, getLocalBounds());
  40. grid.draw (g, background);
  41. }
  42. else
  43. {
  44. grid.draw (g, nullptr);
  45. }
  46. }
  47. }
  48. void resized() override
  49. {
  50. if (! getBounds().isEmpty())
  51. {
  52. int numTimesToTry = 10;
  53. while (--numTimesToTry >= 0)
  54. {
  55. bool anyCompsMoved = false;
  56. for (int i = 0; i < getNumChildComponents(); ++i)
  57. {
  58. Component* comp = getChildComponent (i);
  59. if (ComponentTypeHandler* const type = ComponentTypeHandler::getHandlerFor (*comp))
  60. {
  61. const Rectangle<int> newBounds (type->getComponentPosition (comp)
  62. .getRectangle (getLocalBounds(),
  63. document.getComponentLayout()));
  64. anyCompsMoved = anyCompsMoved || (comp->getBounds() != newBounds);
  65. comp->setBounds (newBounds);
  66. }
  67. }
  68. // repeat this loop until they've all stopped shuffling (might require a few
  69. // loops for all the relative positioned comps to settle down)
  70. if (! anyCompsMoved)
  71. break;
  72. }
  73. }
  74. }
  75. void moved() override
  76. {
  77. ((ComponentLayoutEditor*) getParentComponent())->updateOverlayPositions();
  78. }
  79. JucerDocument& document;
  80. SnapGridPainter& grid;
  81. bool dontFillBackground;
  82. };
  83. //==============================================================================
  84. ComponentLayoutEditor::ComponentLayoutEditor (JucerDocument& doc, ComponentLayout& cl)
  85. : document (doc), layout (cl), firstResize (true)
  86. {
  87. setWantsKeyboardFocus (true);
  88. addAndMakeVisible (subCompHolder = new SubComponentHolderComp (document, grid));
  89. refreshAllComponents();
  90. setSize (document.getInitialWidth(),
  91. document.getInitialHeight());
  92. }
  93. ComponentLayoutEditor::~ComponentLayoutEditor()
  94. {
  95. document.removeChangeListener (this);
  96. removeChildComponent (&lassoComp);
  97. deleteAllChildren();
  98. }
  99. //==============================================================================
  100. void ComponentLayoutEditor::visibilityChanged()
  101. {
  102. document.beginTransaction();
  103. if (isVisible())
  104. {
  105. refreshAllComponents();
  106. document.addChangeListener (this);
  107. }
  108. else
  109. {
  110. document.removeChangeListener (this);
  111. }
  112. }
  113. void ComponentLayoutEditor::changeListenerCallback (ChangeBroadcaster*)
  114. {
  115. refreshAllComponents();
  116. }
  117. void ComponentLayoutEditor::paint (Graphics&)
  118. {
  119. }
  120. void ComponentLayoutEditor::resized()
  121. {
  122. if (firstResize && getWidth() > 0 && getHeight() > 0)
  123. {
  124. firstResize = false;
  125. refreshAllComponents();
  126. }
  127. subCompHolder->setBounds (getComponentArea());
  128. updateOverlayPositions();
  129. }
  130. Rectangle<int> ComponentLayoutEditor::getComponentArea() const
  131. {
  132. const int editorEdgeGap = 4;
  133. if (document.isFixedSize())
  134. return Rectangle<int> ((getWidth() - document.getInitialWidth()) / 2,
  135. (getHeight() - document.getInitialHeight()) / 2,
  136. document.getInitialWidth(),
  137. document.getInitialHeight());
  138. return Rectangle<int> (editorEdgeGap, editorEdgeGap,
  139. getWidth() - editorEdgeGap * 2,
  140. getHeight() - editorEdgeGap * 2);
  141. }
  142. Image ComponentLayoutEditor::createComponentLayerSnapshot() const
  143. {
  144. ((SubComponentHolderComp*) subCompHolder)->dontFillBackground = true;
  145. Image im = subCompHolder->createComponentSnapshot (Rectangle<int> (0, 0, subCompHolder->getWidth(), subCompHolder->getHeight()));
  146. ((SubComponentHolderComp*) subCompHolder)->dontFillBackground = false;
  147. return im;
  148. }
  149. void ComponentLayoutEditor::updateOverlayPositions()
  150. {
  151. for (int i = getNumChildComponents(); --i >= 0;)
  152. if (ComponentOverlayComponent* const overlay = dynamic_cast<ComponentOverlayComponent*> (getChildComponent (i)))
  153. overlay->updateBoundsToMatchTarget();
  154. }
  155. void ComponentLayoutEditor::refreshAllComponents()
  156. {
  157. for (int i = getNumChildComponents(); --i >= 0;)
  158. {
  159. std::unique_ptr<ComponentOverlayComponent> overlay (dynamic_cast<ComponentOverlayComponent*> (getChildComponent (i)));
  160. if (overlay != nullptr && layout.containsComponent (overlay->target))
  161. overlay.release();
  162. }
  163. for (int i = subCompHolder->getNumChildComponents(); --i >= 0;)
  164. {
  165. Component* const comp = subCompHolder->getChildComponent (i);
  166. if (! layout.containsComponent (comp))
  167. subCompHolder->removeChildComponent (comp);
  168. }
  169. Component* lastComp = nullptr;
  170. Component* lastOverlay = nullptr;
  171. for (int i = layout.getNumComponents(); --i >= 0;)
  172. {
  173. auto c = layout.getComponent (i);
  174. jassert (c != nullptr);
  175. auto overlay = getOverlayCompFor (c);
  176. bool isNewOverlay = false;
  177. if (overlay == nullptr)
  178. {
  179. auto handler = ComponentTypeHandler::getHandlerFor (*c);
  180. jassert (handler != nullptr);
  181. overlay = handler->createOverlayComponent (c, layout);
  182. addAndMakeVisible (overlay);
  183. isNewOverlay = true;
  184. }
  185. if (lastOverlay != nullptr)
  186. overlay->toBehind (lastOverlay);
  187. else
  188. overlay->toFront (false);
  189. lastOverlay = overlay;
  190. subCompHolder->addAndMakeVisible (c);
  191. if (lastComp != nullptr)
  192. c->toBehind (lastComp);
  193. else
  194. c->toFront (false);
  195. lastComp = c;
  196. c->setWantsKeyboardFocus (false);
  197. c->setFocusContainer (true);
  198. if (isNewOverlay)
  199. overlay->updateBoundsToMatchTarget();
  200. }
  201. if (grid.updateFromDesign (document))
  202. subCompHolder->repaint();
  203. subCompHolder->setBounds (getComponentArea());
  204. subCompHolder->resized();
  205. }
  206. void ComponentLayoutEditor::mouseDown (const MouseEvent& e)
  207. {
  208. if (e.mods.isPopupMenu())
  209. {
  210. auto commandManager = &ProjucerApplication::getCommandManager();
  211. PopupMenu m;
  212. m.addCommandItem (commandManager, JucerCommandIDs::editCompLayout);
  213. m.addCommandItem (commandManager, JucerCommandIDs::editCompGraphics);
  214. m.addSeparator();
  215. for (int i = 0; i < ObjectTypes::numComponentTypes; ++i)
  216. m.addCommandItem (commandManager, JucerCommandIDs::newComponentBase + i);
  217. m.show();
  218. }
  219. else
  220. {
  221. addChildComponent (lassoComp);
  222. lassoComp.beginLasso (e, this);
  223. }
  224. }
  225. void ComponentLayoutEditor::mouseDrag (const MouseEvent& e)
  226. {
  227. lassoComp.toFront (false);
  228. lassoComp.dragLasso (e);
  229. }
  230. void ComponentLayoutEditor::mouseUp (const MouseEvent& e)
  231. {
  232. lassoComp.endLasso();
  233. removeChildComponent (&lassoComp);
  234. if (! (e.mouseWasDraggedSinceMouseDown() || e.mods.isAnyModifierKeyDown()))
  235. layout.getSelectedSet().deselectAll();
  236. }
  237. static void moveOrStretch (ComponentLayout& layout, int x, int y, bool snap, bool stretch)
  238. {
  239. if (stretch)
  240. layout.stretchSelectedComps (x, y, snap);
  241. else
  242. layout.moveSelectedComps (x, y, snap);
  243. }
  244. bool ComponentLayoutEditor::keyPressed (const KeyPress& key)
  245. {
  246. const bool snap = key.getModifiers().isAltDown();
  247. const bool stretch = key.getModifiers().isShiftDown();
  248. const int amount = snap ? document.getSnappingGridSize() + 1
  249. : 1;
  250. if (key.isKeyCode (KeyPress::rightKey))
  251. {
  252. moveOrStretch (layout, amount, 0, snap, stretch);
  253. }
  254. else if (key.isKeyCode (KeyPress::downKey))
  255. {
  256. moveOrStretch (layout, 0,amount, snap, stretch);
  257. }
  258. else if (key.isKeyCode (KeyPress::leftKey))
  259. {
  260. moveOrStretch (layout, -amount, 0, snap, stretch);
  261. }
  262. else if (key.isKeyCode (KeyPress::upKey))
  263. {
  264. moveOrStretch (layout, 0, -amount, snap, stretch);
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. return true;
  271. }
  272. bool ComponentLayoutEditor::isInterestedInFileDrag (const StringArray& filenames)
  273. {
  274. const File f (filenames [0]);
  275. return f.hasFileExtension (cppFileExtensions);
  276. }
  277. void ComponentLayoutEditor::filesDropped (const StringArray& filenames, int x, int y)
  278. {
  279. const File f (filenames [0]);
  280. if (JucerDocument::isValidJucerCppFile (f))
  281. {
  282. JucerComponentHandler jucerDocHandler;
  283. layout.getDocument()->beginTransaction();
  284. if (TestComponent* newOne = dynamic_cast<TestComponent*> (layout.addNewComponent (&jucerDocHandler,
  285. x - subCompHolder->getX(),
  286. y - subCompHolder->getY())))
  287. {
  288. JucerComponentHandler::setJucerComponentFile (*layout.getDocument(), newOne,
  289. f.getRelativePathFrom (document.getCppFile().getParentDirectory()));
  290. layout.getSelectedSet().selectOnly (newOne);
  291. }
  292. layout.getDocument()->beginTransaction();
  293. }
  294. }
  295. bool ComponentLayoutEditor::isInterestedInDragSource (const SourceDetails& dragSourceDetails)
  296. {
  297. if (dragSourceDetails.description != projectItemDragType)
  298. return false;
  299. OwnedArray<Project::Item> selectedNodes;
  300. ProjectContentComponent::getSelectedProjectItemsBeingDragged (dragSourceDetails, selectedNodes);
  301. return selectedNodes.size() > 0;
  302. }
  303. void ComponentLayoutEditor::itemDropped (const SourceDetails& dragSourceDetails)
  304. {
  305. OwnedArray <Project::Item> selectedNodes;
  306. ProjectContentComponent::getSelectedProjectItemsBeingDragged (dragSourceDetails, selectedNodes);
  307. StringArray filenames;
  308. for (int i = 0; i < selectedNodes.size(); ++i)
  309. if (selectedNodes.getUnchecked(i)->getFile().hasFileExtension (cppFileExtensions))
  310. filenames.add (selectedNodes.getUnchecked(i)->getFile().getFullPathName());
  311. filesDropped (filenames, dragSourceDetails.localPosition.x, dragSourceDetails.localPosition.y);
  312. }
  313. ComponentOverlayComponent* ComponentLayoutEditor::getOverlayCompFor (Component* compToFind) const
  314. {
  315. for (int i = getNumChildComponents(); --i >= 0;)
  316. {
  317. if (ComponentOverlayComponent* const overlay = dynamic_cast<ComponentOverlayComponent*> (getChildComponent (i)))
  318. if (overlay->target == compToFind)
  319. return overlay;
  320. }
  321. return nullptr;
  322. }
  323. void ComponentLayoutEditor::findLassoItemsInArea (Array <Component*>& results, const Rectangle<int>& area)
  324. {
  325. const Rectangle<int> lasso (area - subCompHolder->getPosition());
  326. for (int i = 0; i < subCompHolder->getNumChildComponents(); ++i)
  327. {
  328. Component* c = subCompHolder->getChildComponent (i);
  329. if (c->getBounds().intersects (lasso))
  330. results.add (c);
  331. }
  332. }
  333. SelectedItemSet <Component*>& ComponentLayoutEditor::getLassoSelection()
  334. {
  335. return layout.getSelectedSet();
  336. }