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.

258 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #ifndef __JUCER_DRAWABLEOBJECTCOMPONENT_JUCEHEADER__
  19. #define __JUCER_DRAWABLEOBJECTCOMPONENT_JUCEHEADER__
  20. #include "jucer_DrawableEditor.h"
  21. //==============================================================================
  22. class DrawableEditorCanvas : public EditorCanvasBase,
  23. public Timer
  24. {
  25. public:
  26. DrawableEditorCanvas (DrawableEditor& editor_)
  27. : editor (editor_)
  28. {
  29. initialise();
  30. editor.getDocument().getRoot().addListener (this);
  31. }
  32. ~DrawableEditorCanvas()
  33. {
  34. editor.getDocument().getRoot().removeListener (this);
  35. shutdown();
  36. }
  37. Component* createComponentHolder()
  38. {
  39. return new DrawableComponent (this);
  40. }
  41. void updateComponents()
  42. {
  43. DrawableDocument& doc = getEditor().getDocument();
  44. if (drawable == 0)
  45. {
  46. drawable = Drawable::createFromValueTree (doc.getRootDrawableNode().getState(), &doc);
  47. getComponentHolder()->repaint();
  48. }
  49. else
  50. {
  51. const Rectangle<float> damage (drawable->refreshFromValueTree (doc.getRootDrawableNode().getState(), &doc));
  52. getComponentHolder()->repaint (damage.getSmallestIntegerContainer());
  53. }
  54. startTimer (500);
  55. }
  56. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  57. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  58. void setCanvasWidth (int w) { getDocument().getCanvasWidth() = w; }
  59. void setCanvasHeight (int h) { getDocument().getCanvasHeight() = h; }
  60. MarkerListBase& getMarkerList (bool isX)
  61. {
  62. return getDocument().getMarkerList (isX);
  63. }
  64. const SelectedItems::ItemType findObjectIdAt (const Point<int>& position)
  65. {
  66. return String::empty;
  67. }
  68. void showPopupMenu (bool isClickOnSelectedObject)
  69. {
  70. PopupMenu m;
  71. if (isClickOnSelectedObject)
  72. {
  73. m.addCommandItem (commandManager, CommandIDs::toFront);
  74. m.addCommandItem (commandManager, CommandIDs::toBack);
  75. m.addSeparator();
  76. m.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  77. const int r = m.show();
  78. (void) r;
  79. }
  80. else
  81. {
  82. getDocument().addNewItemMenuItems (m);
  83. const int r = m.show();
  84. getDocument().performNewItemMenuItem (r);
  85. }
  86. }
  87. void objectDoubleClicked (const MouseEvent& e, const ValueTree& state)
  88. {
  89. }
  90. const ValueTree getObjectState (const String& objectId)
  91. {
  92. return ValueTree();
  93. }
  94. const Rectangle<int> getObjectPosition (const ValueTree& state)
  95. {
  96. return Rectangle<int>();//getDocument().getCoordsFor (state).resolve (getDocument());
  97. }
  98. RelativeRectangle getObjectCoords (const ValueTree& state)
  99. {
  100. return RelativeRectangle();
  101. // return getDocument().getCoordsFor (state);
  102. }
  103. SelectedItems& getSelection()
  104. {
  105. return editor.getSelection();
  106. }
  107. void deselectNonDraggableObjects()
  108. {
  109. }
  110. void findLassoItemsInArea (Array <SelectedItems::ItemType>& itemsFound, const Rectangle<int>& area)
  111. {
  112. }
  113. //==============================================================================
  114. class DragOperation : public EditorDragOperation
  115. {
  116. public:
  117. DragOperation (DrawableEditorCanvas* canvas_,
  118. const MouseEvent& e,
  119. Component* snapGuideParentComp_,
  120. const ResizableBorderComponent::Zone& zone_)
  121. : EditorDragOperation (canvas_, e, snapGuideParentComp_, zone_)
  122. {
  123. }
  124. ~DragOperation()
  125. {
  126. getUndoManager().beginNewTransaction();
  127. }
  128. protected:
  129. DrawableDocument& getDocument() throw() { return static_cast <DrawableEditorCanvas*> (canvas)->getDocument(); }
  130. int getCanvasWidth() { return getDocument().getCanvasWidth().getValue(); }
  131. int getCanvasHeight() { return getDocument().getCanvasHeight().getValue(); }
  132. UndoManager& getUndoManager() { return *getDocument().getUndoManager(); }
  133. const Rectangle<float> getObjectPosition (const ValueTree& state)
  134. {
  135. return Rectangle<float> ();
  136. }
  137. bool setObjectPosition (ValueTree& state, const Rectangle<float>& newBounds)
  138. {
  139. return false;
  140. }
  141. float getMarkerPosition (const ValueTree& marker, bool isX)
  142. {
  143. return 0;
  144. }
  145. };
  146. DragOperation* createDragOperation (const MouseEvent& e, Component* snapGuideParentComponent,
  147. const ResizableBorderComponent::Zone& zone)
  148. {
  149. DragOperation* d = new DragOperation (this, e, snapGuideParentComponent, zone);
  150. Array<ValueTree> selected, unselected;
  151. /*for (int i = getDocument().getNumComponents(); --i >= 0;)
  152. {
  153. const ValueTree v (getDocument().getComponent (i));
  154. if (editor.getSelection().isSelected (v [ComponentDocument::idProperty]))
  155. selected.add (v);
  156. else
  157. unselected.add (v);
  158. }*/
  159. d->initialise (selected, unselected);
  160. return d;
  161. }
  162. UndoManager& getUndoManager()
  163. {
  164. return *getDocument().getUndoManager();
  165. }
  166. DrawableEditor& getEditor() throw() { return editor; }
  167. DrawableDocument& getDocument() throw() { return editor.getDocument(); }
  168. void timerCallback()
  169. {
  170. stopTimer();
  171. if (! Component::isMouseButtonDownAnywhere())
  172. getUndoManager().beginNewTransaction();
  173. }
  174. //==============================================================================
  175. class DrawableComponent : public Component
  176. {
  177. public:
  178. DrawableComponent (DrawableEditorCanvas* canvas_)
  179. : canvas (canvas_)
  180. {
  181. setOpaque (true);
  182. }
  183. ~DrawableComponent()
  184. {
  185. }
  186. void updateDrawable()
  187. {
  188. repaint();
  189. }
  190. void paint (Graphics& g)
  191. {
  192. g.fillAll (Colours::white);
  193. canvas->drawable->draw (g, 1.0f);
  194. }
  195. private:
  196. DrawableEditorCanvas* canvas;
  197. DrawableEditor& getEditor() const { return canvas->getEditor(); }
  198. };
  199. ScopedPointer<Drawable> drawable;
  200. private:
  201. //==============================================================================
  202. DrawableEditor& editor;
  203. };
  204. #endif // __JUCER_DRAWABLEOBJECTCOMPONENT_JUCEHEADER__