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.

223 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../../jucer_Headers.h"
  19. #include "jucer_DrawableObjectComponent.h"
  20. #include "jucer_DrawableEditor.h"
  21. #include "../jucer_JucerTreeViewBase.h"
  22. #include "jucer_DrawableTreeViewItem.h"
  23. //==============================================================================
  24. class RightHandPanel : public Component
  25. {
  26. public:
  27. RightHandPanel (DrawableEditor& editor_)
  28. : editor (editor_)
  29. {
  30. setOpaque (true);
  31. addAndMakeVisible (tree = new TreeView());
  32. tree->setRootItemVisible (true);
  33. tree->setMultiSelectEnabled (true);
  34. tree->setDefaultOpenness (true);
  35. tree->setColour (TreeView::backgroundColourId, Colours::white);
  36. tree->setIndentSize (15);
  37. tree->setRootItem (DrawableTreeViewItem::createItemForNode (editor, editor.getDocument().getRootDrawableNode()));
  38. }
  39. ~RightHandPanel()
  40. {
  41. tree->deleteRootItem();
  42. deleteAllChildren();
  43. }
  44. void paint (Graphics& g)
  45. {
  46. g.fillAll (Colour::greyLevel (0.92f));
  47. }
  48. void resized()
  49. {
  50. tree->setSize (getWidth(), getHeight());
  51. }
  52. private:
  53. DrawableEditor& editor;
  54. TreeView* tree;
  55. };
  56. //==============================================================================
  57. DrawableEditor::DrawableEditor (OpenDocumentManager::Document* document,
  58. Project* project_,
  59. DrawableDocument* drawableDocument_)
  60. : DocumentEditorComponent (document),
  61. project (project_),
  62. drawableDocument (drawableDocument_)
  63. {
  64. jassert (drawableDocument_ != 0);
  65. setOpaque (true);
  66. addAndMakeVisible (rightHandPanel = new RightHandPanel (*this));
  67. Canvas* canvas = new Canvas (*this);
  68. addAndMakeVisible (viewport = new Viewport());
  69. viewport->setViewedComponent (canvas);
  70. canvas->createRootObject();
  71. }
  72. DrawableEditor::~DrawableEditor()
  73. {
  74. deleteAllChildren();
  75. }
  76. int64 DrawableEditor::getHashForNode (const ValueTree& node)
  77. {
  78. return node ["id"].toString().hashCode64();
  79. }
  80. void DrawableEditor::paint (Graphics& g)
  81. {
  82. g.fillAll (Colours::white);
  83. }
  84. void DrawableEditor::resized()
  85. {
  86. rightHandPanel->setBounds (getWidth() - 200, 0, 200, getHeight());
  87. viewport->setBounds (0, 0, rightHandPanel->getX(), getHeight());
  88. getCanvas()->updateSize();
  89. }
  90. //==============================================================================
  91. DrawableEditor::Canvas::Canvas (DrawableEditor& editor_)
  92. : editor (editor_), border (40)
  93. {
  94. origin.setXY (50, 50);
  95. }
  96. DrawableEditor::Canvas::~Canvas()
  97. {
  98. rootObject = 0;
  99. }
  100. void DrawableEditor::Canvas::createRootObject()
  101. {
  102. addAndMakeVisible (rootObject = DrawableObjectComponent::create (editor.getDocument().getRootDrawableNode(),
  103. editor, 0));
  104. rootObject->drawableOriginRelativeToParentTopLeft = origin;
  105. rootObject->reloadFromValueTree();
  106. }
  107. void DrawableEditor::Canvas::paint (Graphics& g)
  108. {
  109. /* g.setColour (Colours::lightgrey);
  110. g.fillRect (0, border.getTop() - 1, getWidth(), 1);
  111. g.fillRect (0, getHeight() - border.getBottom(), getWidth(), 1);
  112. g.fillRect (border.getLeft() - 1, 0, 1, getHeight());
  113. g.fillRect (getWidth() - border.getRight(), 0, 1, getHeight());*/
  114. g.setColour (Colours::grey);
  115. g.fillRect (0, origin.getY(), getWidth(), 1);
  116. g.fillRect (origin.getX(), 0, 1, getHeight());
  117. }
  118. void DrawableEditor::Canvas::mouseDown (const MouseEvent& e)
  119. {
  120. lasso = 0;
  121. if (e.mods.isPopupMenu())
  122. {
  123. PopupMenu m;
  124. m.addItem (1, "New Rectangle");
  125. m.addItem (2, "New Circle");
  126. switch (m.show())
  127. {
  128. case 1:
  129. editor.getDocument().addRectangle();
  130. break;
  131. case 2:
  132. editor.getDocument().addCircle();
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. else
  139. {
  140. addAndMakeVisible (lasso = new LassoComponent <int64>());
  141. lasso->beginLasso (e, this);
  142. }
  143. }
  144. void DrawableEditor::Canvas::mouseDrag (const MouseEvent& e)
  145. {
  146. if (lasso != 0)
  147. lasso->dragLasso (e);
  148. }
  149. void DrawableEditor::Canvas::mouseUp (const MouseEvent& e)
  150. {
  151. if (lasso != 0)
  152. {
  153. lasso->endLasso();
  154. lasso = 0;
  155. }
  156. }
  157. void DrawableEditor::Canvas::findLassoItemsInArea (Array <int64>& itemsFound, int x, int y, int width, int height)
  158. {
  159. for (int i = getNumChildComponents(); --i >= 0;)
  160. {
  161. DrawableObjectComponent* d = dynamic_cast <DrawableObjectComponent*> (getChildComponent(i));
  162. if (d != 0)
  163. d->findLassoItemsInArea (itemsFound, Rectangle<int> (x, y, width, height));
  164. }
  165. }
  166. SelectedItemSet <int64>& DrawableEditor::Canvas::getLassoSelection()
  167. {
  168. return editor.selectedItems;
  169. }
  170. void DrawableEditor::Canvas::updateSize()
  171. {
  172. Rectangle<int> r (rootObject->getBounds());
  173. setSize (jmax (editor.viewport->getMaximumVisibleWidth(), r.getRight()),
  174. jmax (editor.viewport->getMaximumVisibleHeight(), r.getBottom()));
  175. }
  176. void DrawableEditor::Canvas::childBoundsChanged (Component* child)
  177. {
  178. if (child == rootObject)
  179. updateSize();
  180. }