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.

juce_LiveConstantEditor.cpp 14KB

10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.setMultiLine (v.isString());
  94. valueEditor.setReturnKeyStartsNewLine (v.isString());
  95. valueEditor.setText (v.getStringValue (wasHex), dontSendNotification);
  96. valueEditor.addListener (this);
  97. sourceEditor.setReadOnly (true);
  98. resetButton.addListener (this);
  99. }
  100. void LivePropertyEditorBase::paint (Graphics& g)
  101. {
  102. g.setColour (Colours::white);
  103. g.fillRect (getLocalBounds().removeFromBottom (1));
  104. }
  105. void LivePropertyEditorBase::resized()
  106. {
  107. Rectangle<int> r (getLocalBounds().reduced (0, 3).withTrimmedBottom (1));
  108. Rectangle<int> left (r.removeFromLeft (jmax (200, r.getWidth() / 3)));
  109. Rectangle<int> top (left.removeFromTop (25));
  110. resetButton.setBounds (top.removeFromRight (35).reduced (0, 3));
  111. name.setBounds (top);
  112. if (customComp != nullptr)
  113. {
  114. valueEditor.setBounds (left.removeFromTop (25));
  115. left.removeFromTop (2);
  116. customComp->setBounds (left);
  117. }
  118. else
  119. {
  120. valueEditor.setBounds (left);
  121. }
  122. r.removeFromLeft (4);
  123. sourceEditor.setBounds (r);
  124. }
  125. void LivePropertyEditorBase::textEditorTextChanged (TextEditor&)
  126. {
  127. applyNewValue (valueEditor.getText());
  128. }
  129. void LivePropertyEditorBase::buttonClicked (Button*)
  130. {
  131. applyNewValue (value.getOriginalStringValue (wasHex));
  132. }
  133. void LivePropertyEditorBase::applyNewValue (const String& s)
  134. {
  135. value.setStringValue (s);
  136. document.replaceSection (valueStart.getPosition(), valueEnd.getPosition(), value.getCodeValue (wasHex));
  137. document.clearUndoHistory();
  138. selectOriginalValue();
  139. valueEditor.setText (s, dontSendNotification);
  140. AllComponentRepainter::getInstance().trigger();
  141. }
  142. void LivePropertyEditorBase::selectOriginalValue()
  143. {
  144. sourceEditor.selectRegion (valueStart, valueEnd);
  145. }
  146. void LivePropertyEditorBase::findOriginalValueInCode()
  147. {
  148. CodeDocument::Position pos (document, value.sourceLine, 0);
  149. String line (pos.getLineText());
  150. String::CharPointerType p (line.getCharPointer());
  151. p = CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT"));
  152. if (p.isEmpty())
  153. {
  154. // Not sure how this would happen - some kind of mix-up between source code and line numbers..
  155. jassertfalse;
  156. return;
  157. }
  158. p += (int) (sizeof ("JUCE_LIVE_CONSTANT") - 1);
  159. p = p.findEndOfWhitespace();
  160. if (! CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")).isEmpty())
  161. {
  162. // Aargh! You've added two JUCE_LIVE_CONSTANT macros on the same line!
  163. // They're identified by their line number, so you must make sure each
  164. // one goes on a separate line!
  165. jassertfalse;
  166. }
  167. if (p.getAndAdvance() == '(')
  168. {
  169. String::CharPointerType start (p), end (p);
  170. int depth = 1;
  171. while (! end.isEmpty())
  172. {
  173. const juce_wchar c = end.getAndAdvance();
  174. if (c == '(') ++depth;
  175. if (c == ')') --depth;
  176. if (depth == 0)
  177. {
  178. --end;
  179. break;
  180. }
  181. }
  182. if (end > start)
  183. {
  184. valueStart = CodeDocument::Position (document, value.sourceLine, (int) (start - line.getCharPointer()));
  185. valueEnd = CodeDocument::Position (document, value.sourceLine, (int) (end - line.getCharPointer()));
  186. valueStart.setPositionMaintained (true);
  187. valueEnd.setPositionMaintained (true);
  188. wasHex = String (start, end).containsIgnoreCase ("0x");
  189. }
  190. }
  191. }
  192. //==============================================================================
  193. class ValueListHolderComponent : public Component
  194. {
  195. public:
  196. ValueListHolderComponent (ValueList& l) : valueList (l)
  197. {
  198. setVisible (true);
  199. }
  200. void addItem (int width, LiveValueBase& v, CodeDocument& doc)
  201. {
  202. addAndMakeVisible (editors.add (v.createPropertyComponent (doc)));
  203. layout (width);
  204. }
  205. void layout (int width)
  206. {
  207. setSize (width, editors.size() * itemHeight);
  208. resized();
  209. }
  210. void resized() override
  211. {
  212. Rectangle<int> r (getLocalBounds().reduced (2, 0));
  213. for (int i = 0; i < editors.size(); ++i)
  214. editors.getUnchecked(i)->setBounds (r.removeFromTop (itemHeight));
  215. }
  216. enum { itemHeight = 120 };
  217. ValueList& valueList;
  218. OwnedArray<LivePropertyEditorBase> editors;
  219. };
  220. //==============================================================================
  221. class ValueList::EditorWindow : public DocumentWindow,
  222. private DeletedAtShutdown
  223. {
  224. public:
  225. EditorWindow (ValueList& list)
  226. : DocumentWindow ("Live Values", Colours::lightgrey, DocumentWindow::closeButton)
  227. {
  228. setLookAndFeel (&lookAndFeel);
  229. setUsingNativeTitleBar (true);
  230. viewport.setViewedComponent (new ValueListHolderComponent (list), true);
  231. viewport.setSize (700, 600);
  232. viewport.setScrollBarsShown (true, false);
  233. setContentNonOwned (&viewport, true);
  234. setResizable (true, false);
  235. setResizeLimits (500, 400, 10000, 10000);
  236. centreWithSize (getWidth(), getHeight());
  237. setVisible (true);
  238. }
  239. void closeButtonPressed() override
  240. {
  241. setVisible (false);
  242. }
  243. void updateItems (ValueList& list)
  244. {
  245. if (ValueListHolderComponent* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
  246. {
  247. while (l->getNumChildComponents() < list.values.size())
  248. {
  249. if (LiveValueBase* v = list.values [l->getNumChildComponents()])
  250. l->addItem (viewport.getMaximumVisibleWidth(), *v, list.getDocument (v->sourceFile));
  251. else
  252. break;
  253. }
  254. setVisible (true);
  255. }
  256. }
  257. void resized() override
  258. {
  259. DocumentWindow::resized();
  260. if (ValueListHolderComponent* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
  261. l->layout (viewport.getMaximumVisibleWidth());
  262. }
  263. Viewport viewport;
  264. LookAndFeel_V3 lookAndFeel;
  265. };
  266. //==============================================================================
  267. ValueList::ValueList() {}
  268. ValueList::~ValueList() {}
  269. ValueList& ValueList::getInstance()
  270. {
  271. static ValueList* i = new ValueList();
  272. return *i;
  273. }
  274. void ValueList::addValue (LiveValueBase* v)
  275. {
  276. values.add (v);
  277. triggerAsyncUpdate();
  278. }
  279. void ValueList::handleAsyncUpdate()
  280. {
  281. if (editorWindow == nullptr)
  282. editorWindow = new EditorWindow (*this);
  283. editorWindow->updateItems (*this);
  284. }
  285. CodeDocument& ValueList::getDocument (const File& file)
  286. {
  287. const int index = documentFiles.indexOf (file.getFullPathName());
  288. if (index >= 0)
  289. return *documents.getUnchecked (index);
  290. CodeDocument* doc = documents.add (new CodeDocument());
  291. documentFiles.add (file);
  292. doc->replaceAllContent (file.loadFileAsString());
  293. doc->clearUndoHistory();
  294. return *doc;
  295. }
  296. //==============================================================================
  297. struct ColourEditorComp : public Component,
  298. private ChangeListener
  299. {
  300. ColourEditorComp (LivePropertyEditorBase& e) : editor (e)
  301. {
  302. setMouseCursor (MouseCursor::PointingHandCursor);
  303. }
  304. Colour getColour() const
  305. {
  306. return Colour ((uint32) parseInt (editor.value.getStringValue (false)));
  307. }
  308. void paint (Graphics& g) override
  309. {
  310. g.fillCheckerBoard (getLocalBounds(), 6, 6,
  311. Colour (0xffdddddd).overlaidWith (getColour()),
  312. Colour (0xffffffff).overlaidWith (getColour()));
  313. }
  314. void mouseDown (const MouseEvent&) override
  315. {
  316. ColourSelector* colourSelector = new ColourSelector();
  317. colourSelector->setName ("Colour");
  318. colourSelector->setCurrentColour (getColour());
  319. colourSelector->addChangeListener (this);
  320. colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
  321. colourSelector->setSize (300, 400);
  322. CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr);
  323. }
  324. void changeListenerCallback (ChangeBroadcaster* source) override
  325. {
  326. if (ColourSelector* cs = dynamic_cast<ColourSelector*> (source))
  327. editor.applyNewValue (getAsString (cs->getCurrentColour(), true));
  328. repaint();
  329. }
  330. LivePropertyEditorBase& editor;
  331. };
  332. Component* createColourEditor (LivePropertyEditorBase& editor)
  333. {
  334. return new ColourEditorComp (editor);
  335. }
  336. //==============================================================================
  337. class SliderComp : public Component,
  338. private Slider::Listener
  339. {
  340. public:
  341. SliderComp (LivePropertyEditorBase& e, bool useFloat)
  342. : editor (e), isFloat (useFloat)
  343. {
  344. slider.setTextBoxStyle (Slider::NoTextBox, true, 0, 0);
  345. addAndMakeVisible (slider);
  346. updateRange();
  347. slider.addListener (this);
  348. }
  349. void updateRange()
  350. {
  351. double v = isFloat ? parseDouble (editor.value.getStringValue (false))
  352. : (double) parseInt (editor.value.getStringValue (false));
  353. double range = isFloat ? 10 : 100;
  354. slider.setRange (v - range, v + range);
  355. slider.setValue (v, dontSendNotification);
  356. }
  357. private:
  358. LivePropertyEditorBase& editor;
  359. Slider slider;
  360. bool isFloat;
  361. void sliderValueChanged (Slider*)
  362. {
  363. editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex)
  364. : getAsString ((int64) slider.getValue(), editor.wasHex));
  365. }
  366. void sliderDragStarted (Slider*) {}
  367. void sliderDragEnded (Slider*) { updateRange(); }
  368. void resized()
  369. {
  370. slider.setBounds (getLocalBounds().removeFromTop (25));
  371. }
  372. };
  373. Component* createIntegerSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, false); }
  374. Component* createFloatSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, true); }
  375. }
  376. #endif