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.

499 lines
15KB

  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. namespace juce
  20. {
  21. #if JUCE_ENABLE_LIVE_CONSTANT_EDITOR
  22. namespace LiveConstantEditor
  23. {
  24. //==============================================================================
  25. class AllComponentRepainter : private Timer,
  26. private DeletedAtShutdown
  27. {
  28. public:
  29. AllComponentRepainter() {}
  30. ~AllComponentRepainter() { clearSingletonInstance(); }
  31. JUCE_DECLARE_SINGLETON (AllComponentRepainter, false)
  32. void trigger()
  33. {
  34. if (! isTimerRunning())
  35. startTimer (100);
  36. }
  37. private:
  38. void timerCallback() override
  39. {
  40. stopTimer();
  41. Array<Component*> alreadyDone;
  42. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  43. if (auto* c = TopLevelWindow::getTopLevelWindow(i))
  44. repaintAndResizeAllComps (c, alreadyDone);
  45. auto& desktop = Desktop::getInstance();
  46. for (int i = desktop.getNumComponents(); --i >= 0;)
  47. if (auto* c = desktop.getComponent(i))
  48. repaintAndResizeAllComps (c, alreadyDone);
  49. }
  50. static void repaintAndResizeAllComps (Component::SafePointer<Component> c,
  51. Array<Component*>& alreadyDone)
  52. {
  53. if (c->isVisible() && ! alreadyDone.contains (c))
  54. {
  55. c->repaint();
  56. c->resized();
  57. for (int i = c->getNumChildComponents(); --i >= 0;)
  58. {
  59. if (auto* child = c->getChildComponent(i))
  60. {
  61. repaintAndResizeAllComps (child, alreadyDone);
  62. alreadyDone.add (child);
  63. }
  64. if (c == nullptr)
  65. break;
  66. }
  67. }
  68. }
  69. };
  70. JUCE_IMPLEMENT_SINGLETON (AllComponentRepainter)
  71. JUCE_IMPLEMENT_SINGLETON (ValueList)
  72. //==============================================================================
  73. int64 parseInt (String s)
  74. {
  75. s = s.trimStart();
  76. if (s.startsWithChar ('-'))
  77. return -parseInt (s.substring (1));
  78. if (s.startsWith ("0x"))
  79. return s.substring(2).getHexValue64();
  80. return s.getLargeIntValue();
  81. }
  82. double parseDouble (const String& s)
  83. {
  84. return s.retainCharacters ("0123456789.eE-").getDoubleValue();
  85. }
  86. String intToString (int v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); }
  87. String intToString (int64 v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); }
  88. //==============================================================================
  89. LiveValueBase::LiveValueBase (const char* file, int line)
  90. : sourceFile (file), sourceLine (line)
  91. {
  92. name = File (sourceFile).getFileName() + " : " + String (sourceLine);
  93. }
  94. LiveValueBase::~LiveValueBase()
  95. {
  96. }
  97. //==============================================================================
  98. LivePropertyEditorBase::LivePropertyEditorBase (LiveValueBase& v, CodeDocument& d)
  99. : value (v), document (d), sourceEditor (document, &tokeniser)
  100. {
  101. setSize (600, 100);
  102. addAndMakeVisible (name);
  103. addAndMakeVisible (resetButton);
  104. addAndMakeVisible (valueEditor);
  105. addAndMakeVisible (sourceEditor);
  106. findOriginalValueInCode();
  107. selectOriginalValue();
  108. name.setFont (13.0f);
  109. name.setText (v.name, dontSendNotification);
  110. valueEditor.setMultiLine (v.isString());
  111. valueEditor.setReturnKeyStartsNewLine (v.isString());
  112. valueEditor.setText (v.getStringValue (wasHex), dontSendNotification);
  113. valueEditor.onTextChange = [this] { applyNewValue (valueEditor.getText()); };
  114. sourceEditor.setReadOnly (true);
  115. sourceEditor.setFont (sourceEditor.getFont().withHeight (13.0f));
  116. resetButton.onClick = [this] { applyNewValue (value.getOriginalStringValue (wasHex)); };
  117. }
  118. void LivePropertyEditorBase::paint (Graphics& g)
  119. {
  120. g.setColour (Colours::white);
  121. g.fillRect (getLocalBounds().removeFromBottom (1));
  122. }
  123. void LivePropertyEditorBase::resized()
  124. {
  125. auto r = getLocalBounds().reduced (0, 3).withTrimmedBottom (1);
  126. auto left = r.removeFromLeft (jmax (200, r.getWidth() / 3));
  127. auto top = left.removeFromTop (25);
  128. resetButton.setBounds (top.removeFromRight (35).reduced (0, 3));
  129. name.setBounds (top);
  130. if (customComp != nullptr)
  131. {
  132. valueEditor.setBounds (left.removeFromTop (25));
  133. left.removeFromTop (2);
  134. customComp->setBounds (left);
  135. }
  136. else
  137. {
  138. valueEditor.setBounds (left);
  139. }
  140. r.removeFromLeft (4);
  141. sourceEditor.setBounds (r);
  142. }
  143. void LivePropertyEditorBase::applyNewValue (const String& s)
  144. {
  145. value.setStringValue (s);
  146. document.replaceSection (valueStart.getPosition(), valueEnd.getPosition(), value.getCodeValue (wasHex));
  147. document.clearUndoHistory();
  148. selectOriginalValue();
  149. valueEditor.setText (s, dontSendNotification);
  150. AllComponentRepainter::getInstance()->trigger();
  151. }
  152. void LivePropertyEditorBase::selectOriginalValue()
  153. {
  154. sourceEditor.selectRegion (valueStart, valueEnd);
  155. }
  156. void LivePropertyEditorBase::findOriginalValueInCode()
  157. {
  158. CodeDocument::Position pos (document, value.sourceLine, 0);
  159. auto line = pos.getLineText();
  160. auto p = line.getCharPointer();
  161. p = CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT"));
  162. if (p.isEmpty())
  163. {
  164. // Not sure how this would happen - some kind of mix-up between source code and line numbers..
  165. jassertfalse;
  166. return;
  167. }
  168. p += (int) (sizeof ("JUCE_LIVE_CONSTANT") - 1);
  169. p = p.findEndOfWhitespace();
  170. if (! CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")).isEmpty())
  171. {
  172. // Aargh! You've added two JUCE_LIVE_CONSTANT macros on the same line!
  173. // They're identified by their line number, so you must make sure each
  174. // one goes on a separate line!
  175. jassertfalse;
  176. }
  177. if (p.getAndAdvance() == '(')
  178. {
  179. auto start = p, end = p;
  180. int depth = 1;
  181. while (! end.isEmpty())
  182. {
  183. auto c = end.getAndAdvance();
  184. if (c == '(') ++depth;
  185. if (c == ')') --depth;
  186. if (depth == 0)
  187. {
  188. --end;
  189. break;
  190. }
  191. }
  192. if (end > start)
  193. {
  194. valueStart = CodeDocument::Position (document, value.sourceLine, (int) (start - line.getCharPointer()));
  195. valueEnd = CodeDocument::Position (document, value.sourceLine, (int) (end - line.getCharPointer()));
  196. valueStart.setPositionMaintained (true);
  197. valueEnd.setPositionMaintained (true);
  198. wasHex = String (start, end).containsIgnoreCase ("0x");
  199. }
  200. }
  201. }
  202. //==============================================================================
  203. class ValueListHolderComponent : public Component
  204. {
  205. public:
  206. ValueListHolderComponent (ValueList& l) : valueList (l)
  207. {
  208. setVisible (true);
  209. }
  210. void addItem (int width, LiveValueBase& v, CodeDocument& doc)
  211. {
  212. addAndMakeVisible (editors.add (v.createPropertyComponent (doc)));
  213. layout (width);
  214. }
  215. void layout (int width)
  216. {
  217. setSize (width, editors.size() * itemHeight);
  218. resized();
  219. }
  220. void resized() override
  221. {
  222. Rectangle<int> r (getLocalBounds().reduced (2, 0));
  223. for (int i = 0; i < editors.size(); ++i)
  224. editors.getUnchecked(i)->setBounds (r.removeFromTop (itemHeight));
  225. }
  226. enum { itemHeight = 120 };
  227. ValueList& valueList;
  228. OwnedArray<LivePropertyEditorBase> editors;
  229. };
  230. //==============================================================================
  231. class ValueList::EditorWindow : public DocumentWindow,
  232. private DeletedAtShutdown
  233. {
  234. public:
  235. EditorWindow (ValueList& list)
  236. : DocumentWindow ("Live Values", Colours::lightgrey, DocumentWindow::closeButton)
  237. {
  238. setLookAndFeel (&lookAndFeel);
  239. setUsingNativeTitleBar (true);
  240. viewport.setViewedComponent (new ValueListHolderComponent (list), true);
  241. viewport.setSize (700, 600);
  242. viewport.setScrollBarsShown (true, false);
  243. setContentNonOwned (&viewport, true);
  244. setResizable (true, false);
  245. setResizeLimits (500, 400, 10000, 10000);
  246. centreWithSize (getWidth(), getHeight());
  247. setVisible (true);
  248. }
  249. ~EditorWindow()
  250. {
  251. setLookAndFeel (nullptr);
  252. }
  253. void closeButtonPressed() override
  254. {
  255. setVisible (false);
  256. }
  257. void updateItems (ValueList& list)
  258. {
  259. if (auto* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
  260. {
  261. while (l->getNumChildComponents() < list.values.size())
  262. {
  263. if (auto* v = list.values [l->getNumChildComponents()])
  264. l->addItem (viewport.getMaximumVisibleWidth(), *v, list.getDocument (v->sourceFile));
  265. else
  266. break;
  267. }
  268. setVisible (true);
  269. }
  270. }
  271. void resized() override
  272. {
  273. DocumentWindow::resized();
  274. if (auto* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
  275. l->layout (viewport.getMaximumVisibleWidth());
  276. }
  277. Viewport viewport;
  278. LookAndFeel_V3 lookAndFeel;
  279. };
  280. //==============================================================================
  281. ValueList::ValueList() {}
  282. ValueList::~ValueList() { clearSingletonInstance(); }
  283. void ValueList::addValue (LiveValueBase* v)
  284. {
  285. values.add (v);
  286. triggerAsyncUpdate();
  287. }
  288. void ValueList::handleAsyncUpdate()
  289. {
  290. if (editorWindow == nullptr)
  291. editorWindow = new EditorWindow (*this);
  292. editorWindow->updateItems (*this);
  293. }
  294. CodeDocument& ValueList::getDocument (const File& file)
  295. {
  296. const int index = documentFiles.indexOf (file.getFullPathName());
  297. if (index >= 0)
  298. return *documents.getUnchecked (index);
  299. auto* doc = documents.add (new CodeDocument());
  300. documentFiles.add (file);
  301. doc->replaceAllContent (file.loadFileAsString());
  302. doc->clearUndoHistory();
  303. return *doc;
  304. }
  305. //==============================================================================
  306. struct ColourEditorComp : public Component,
  307. private ChangeListener
  308. {
  309. ColourEditorComp (LivePropertyEditorBase& e) : editor (e)
  310. {
  311. setMouseCursor (MouseCursor::PointingHandCursor);
  312. }
  313. Colour getColour() const
  314. {
  315. return Colour ((uint32) parseInt (editor.value.getStringValue (false)));
  316. }
  317. void paint (Graphics& g) override
  318. {
  319. g.fillCheckerBoard (getLocalBounds().toFloat(), 6.0f, 6.0f,
  320. Colour (0xffdddddd).overlaidWith (getColour()),
  321. Colour (0xffffffff).overlaidWith (getColour()));
  322. }
  323. void mouseDown (const MouseEvent&) override
  324. {
  325. auto* colourSelector = new ColourSelector();
  326. colourSelector->setName ("Colour");
  327. colourSelector->setCurrentColour (getColour());
  328. colourSelector->addChangeListener (this);
  329. colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  330. colourSelector->setSize (300, 400);
  331. CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr);
  332. }
  333. void changeListenerCallback (ChangeBroadcaster* source) override
  334. {
  335. if (auto* cs = dynamic_cast<ColourSelector*> (source))
  336. editor.applyNewValue (getAsString (cs->getCurrentColour(), true));
  337. repaint();
  338. }
  339. LivePropertyEditorBase& editor;
  340. };
  341. Component* createColourEditor (LivePropertyEditorBase& editor)
  342. {
  343. return new ColourEditorComp (editor);
  344. }
  345. //==============================================================================
  346. struct SliderComp : public Component
  347. {
  348. SliderComp (LivePropertyEditorBase& e, bool useFloat)
  349. : editor (e), isFloat (useFloat)
  350. {
  351. slider.setTextBoxStyle (Slider::NoTextBox, true, 0, 0);
  352. addAndMakeVisible (slider);
  353. updateRange();
  354. slider.onDragEnd = [this] { updateRange(); };
  355. slider.onValueChange = [this]
  356. {
  357. editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex)
  358. : getAsString ((int64) slider.getValue(), editor.wasHex));
  359. };
  360. }
  361. virtual void updateRange()
  362. {
  363. double v = isFloat ? parseDouble (editor.value.getStringValue (false))
  364. : (double) parseInt (editor.value.getStringValue (false));
  365. double range = isFloat ? 10 : 100;
  366. slider.setRange (v - range, v + range);
  367. slider.setValue (v, dontSendNotification);
  368. }
  369. void resized() override
  370. {
  371. slider.setBounds (getLocalBounds().removeFromTop (25));
  372. }
  373. LivePropertyEditorBase& editor;
  374. Slider slider;
  375. bool isFloat;
  376. };
  377. //==============================================================================
  378. struct BoolSliderComp : public SliderComp
  379. {
  380. BoolSliderComp (LivePropertyEditorBase& e)
  381. : SliderComp (e, false)
  382. {
  383. slider.onValueChange = [this] { editor.applyNewValue (slider.getValue() > 0.5 ? "true" : "false"); };
  384. }
  385. void updateRange() override
  386. {
  387. slider.setRange (0.0, 1.0, dontSendNotification);
  388. slider.setValue (editor.value.getStringValue (false) == "true", dontSendNotification);
  389. }
  390. };
  391. Component* createIntegerSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, false); }
  392. Component* createFloatSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, true); }
  393. Component* createBoolSlider (LivePropertyEditorBase& editor) { return new BoolSliderComp (editor); }
  394. }
  395. #endif
  396. } // namespace juce