Audio plugin host https://kx.studio/carla
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.

467 lines
14KB

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