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.

491 lines
15KB

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