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.

315 lines
16KB

  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 && ! DOXYGEN
  22. //==============================================================================
  23. /** You can safely ignore all the stuff in this namespace - it's a bunch of boilerplate
  24. code used to implement the JUCE_LIVE_CONSTANT functionality.
  25. */
  26. namespace LiveConstantEditor
  27. {
  28. int64 parseInt (String);
  29. double parseDouble (const String&);
  30. String intToString (int, bool preferHex);
  31. String intToString (int64, bool preferHex);
  32. template <typename Type>
  33. static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
  34. inline void setFromString (char& v, const String& s) { v = (char) parseInt (s); }
  35. inline void setFromString (unsigned char& v, const String& s) { v = (unsigned char) parseInt (s); }
  36. inline void setFromString (short& v, const String& s) { v = (short) parseInt (s); }
  37. inline void setFromString (unsigned short& v, const String& s) { v = (unsigned short) parseInt (s); }
  38. inline void setFromString (int& v, const String& s) { v = (int) parseInt (s); }
  39. inline void setFromString (unsigned int& v, const String& s) { v = (unsigned int) parseInt (s); }
  40. inline void setFromString (long& v, const String& s) { v = (long) parseInt (s); }
  41. inline void setFromString (unsigned long& v, const String& s) { v = (unsigned long) parseInt (s); }
  42. inline void setFromString (int64& v, const String& s) { v = (int64) parseInt (s); }
  43. inline void setFromString (uint64& v, const String& s) { v = (uint64) parseInt (s); }
  44. inline void setFromString (double& v, const String& s) { v = parseDouble (s); }
  45. inline void setFromString (float& v, const String& s) { v = (float) parseDouble (s); }
  46. inline void setFromString (bool& v, const String& s) { v = (s == "true"); }
  47. inline void setFromString (String& v, const String& s) { v = s; }
  48. inline void setFromString (Colour& v, const String& s) { v = Colour ((uint32) parseInt (s)); }
  49. template <typename Type>
  50. inline String getAsString (const Type& v, bool) { return String (v); }
  51. inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); }
  52. inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); }
  53. inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); }
  54. inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); }
  55. inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); }
  56. inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); }
  57. inline String getAsString (bool v, bool) { return v ? "true" : "false"; }
  58. inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
  59. inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
  60. inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); }
  61. template <typename Type> struct isStringType { enum { value = 0 }; };
  62. template <> struct isStringType<String> { enum { value = 1 }; };
  63. template <typename Type>
  64. inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); }
  65. inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; }
  66. inline String getAsCode (const String& v, bool) { return CppTokeniserFunctions::addEscapeChars(v).quoted(); }
  67. inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); }
  68. template <typename Type>
  69. inline const char* castToCharPointer (const Type&) { return ""; }
  70. inline const char* castToCharPointer (const String& s) { return s.toRawUTF8(); }
  71. struct LivePropertyEditorBase;
  72. //==============================================================================
  73. struct JUCE_API LiveValueBase
  74. {
  75. LiveValueBase (const char* file, int line);
  76. virtual ~LiveValueBase();
  77. virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0;
  78. virtual String getStringValue (bool preferHex) const = 0;
  79. virtual String getCodeValue (bool preferHex) const = 0;
  80. virtual void setStringValue (const String&) = 0;
  81. virtual String getOriginalStringValue (bool preferHex) const = 0;
  82. virtual bool isString() const = 0;
  83. String name, sourceFile;
  84. int sourceLine;
  85. JUCE_DECLARE_NON_COPYABLE (LiveValueBase)
  86. };
  87. //==============================================================================
  88. struct JUCE_API LivePropertyEditorBase : public Component,
  89. private TextEditor::Listener,
  90. private Button::Listener
  91. {
  92. LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
  93. void paint (Graphics&) override;
  94. void resized() override;
  95. void textEditorTextChanged (TextEditor&) override;
  96. void buttonClicked (Button*) override;
  97. void applyNewValue (const String&);
  98. void selectOriginalValue();
  99. void findOriginalValueInCode();
  100. LiveValueBase& value;
  101. Label name;
  102. TextEditor valueEditor;
  103. TextButton resetButton { "reset" };
  104. CodeDocument& document;
  105. CPlusPlusCodeTokeniser tokeniser;
  106. CodeEditorComponent sourceEditor;
  107. CodeDocument::Position valueStart, valueEnd;
  108. ScopedPointer<Component> customComp;
  109. bool wasHex = false;
  110. JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase)
  111. };
  112. //==============================================================================
  113. Component* createColourEditor (LivePropertyEditorBase&);
  114. Component* createIntegerSlider (LivePropertyEditorBase&);
  115. Component* createFloatSlider (LivePropertyEditorBase&);
  116. Component* createBoolSlider (LivePropertyEditorBase&);
  117. template <typename Type> struct CustomEditor { static Component* create (LivePropertyEditorBase&) { return nullptr; } };
  118. template<> struct CustomEditor<char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  119. template<> struct CustomEditor<unsigned char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  120. template<> struct CustomEditor<int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  121. template<> struct CustomEditor<unsigned int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  122. template<> struct CustomEditor<short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  123. template<> struct CustomEditor<unsigned short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  124. template<> struct CustomEditor<int64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  125. template<> struct CustomEditor<uint64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
  126. template<> struct CustomEditor<float> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
  127. template<> struct CustomEditor<double> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
  128. template<> struct CustomEditor<Colour> { static Component* create (LivePropertyEditorBase& e) { return createColourEditor (e); } };
  129. template<> struct CustomEditor<bool> { static Component* create (LivePropertyEditorBase& e) { return createBoolSlider (e); } };
  130. template <typename Type>
  131. struct LivePropertyEditor : public LivePropertyEditorBase
  132. {
  133. template <typename ValueType>
  134. LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
  135. {
  136. addAndMakeVisible (customComp = CustomEditor<Type>::create (*this));
  137. }
  138. };
  139. //==============================================================================
  140. template <typename Type>
  141. struct LiveValue : public LiveValueBase
  142. {
  143. LiveValue (const char* file, int line, const Type& initialValue)
  144. : LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
  145. {}
  146. operator Type() const noexcept { return value; }
  147. operator const char*() const { return castToCharPointer (value); }
  148. LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc) override
  149. {
  150. return new LivePropertyEditor<Type> (*this, doc);
  151. }
  152. String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); }
  153. String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); }
  154. String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); }
  155. void setStringValue (const String& s) override { setFromString (value, s); }
  156. bool isString() const override { return isStringType<Type>::value; }
  157. Type value, originalValue;
  158. JUCE_DECLARE_NON_COPYABLE (LiveValue)
  159. };
  160. //==============================================================================
  161. class JUCE_API ValueList : private AsyncUpdater,
  162. private DeletedAtShutdown
  163. {
  164. public:
  165. ValueList();
  166. ~ValueList();
  167. juce_DeclareSingleton (ValueList, false)
  168. template <typename Type>
  169. LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
  170. {
  171. const ScopedLock sl (lock);
  172. typedef LiveValue<Type> ValueType;
  173. for (int i = 0; i < values.size(); ++i)
  174. {
  175. LiveValueBase* v = values.getUnchecked(i);
  176. if (v->sourceLine == line && v->sourceFile == file)
  177. return *static_cast<ValueType*> (v);
  178. }
  179. ValueType* v = new ValueType (file, line, initialValue);
  180. addValue (v);
  181. return *v;
  182. }
  183. private:
  184. OwnedArray<LiveValueBase> values;
  185. OwnedArray<CodeDocument> documents;
  186. Array<File> documentFiles;
  187. class EditorWindow;
  188. friend class EditorWindow;
  189. friend struct ContainerDeletePolicy<EditorWindow>;
  190. Component::SafePointer<EditorWindow> editorWindow;
  191. CriticalSection lock;
  192. CodeDocument& getDocument (const File&);
  193. void addValue (LiveValueBase*);
  194. void handleAsyncUpdate() override;
  195. };
  196. template <typename Type>
  197. inline LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
  198. {
  199. // If you hit this assertion then the __FILE__ macro is providing a
  200. // relative path instead of an absolute path. On Windows this will be
  201. // a path relative to the build directory rather than the currently
  202. // running application. To fix this you must compile with the /FC flag.
  203. jassert (File::isAbsolutePath (file));
  204. return ValueList::getInstance()->getValue (file, line, initialValue);
  205. }
  206. inline LiveValue<String>& getValue (const char* file, int line, const char* initialValue)
  207. {
  208. return getValue (file, line, String (initialValue));
  209. }
  210. }
  211. #endif
  212. //==============================================================================
  213. #if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
  214. /**
  215. This macro wraps a primitive constant value in some cunning boilerplate code that allows
  216. its value to be interactively tweaked in a popup window while your application is running.
  217. In a release build, this macro disappears and is replaced by only the constant that it
  218. wraps, but if JUCE_ENABLE_LIVE_CONSTANT_EDITOR is enabled, it injects a class wrapper
  219. that automatically pops-up a window containing an editor that allows the value to be
  220. tweaked at run-time. The editor window will also force all visible components to be
  221. resized and repainted whenever a value is changed, so that if you use this to wrap
  222. a colour or layout parameter, you'll be able to immediately see the effects of changing it.
  223. The editor will also load the original source-file that contains each JUCE_LIVE_CONSTANT
  224. macro, and will display a preview of the modified source code as you adjust the values.
  225. Things to note:
  226. - Only one of these per line! The __FILE__ and __LINE__ macros are used to identify
  227. the value, so things will get confused if you have more than one per line
  228. - Obviously because it needs to load the source code based on the __FILE__ macro,
  229. it'll only work if the source files are stored locally in the same location as they
  230. were when you compiled the program.
  231. - It's only designed to cope with simple types: primitives, string literals, and
  232. the Colour class, so if you try using it for other classes or complex expressions,
  233. good luck!
  234. - The editor window will get popped up whenever a new value is used for the first
  235. time. You can close the window, but there's no way to get it back without restarting
  236. the app!
  237. e.g. in this example the colours, font size, and text used in the paint method can
  238. all be adjusted live:
  239. @code
  240. void MyComp::paint (Graphics& g) override
  241. {
  242. g.fillAll (JUCE_LIVE_CONSTANT (Colour (0xffddddff)));
  243. Colour fontColour = JUCE_LIVE_CONSTANT (Colour (0xff005500));
  244. float fontSize = JUCE_LIVE_CONSTANT (16.0f);
  245. g.setColour (fontColour);
  246. g.setFont (fontSize);
  247. g.drawFittedText (JUCE_LIVE_CONSTANT ("Hello world!"),
  248. getLocalBounds(), Justification::centred, 2);
  249. }
  250. @endcode
  251. */
  252. #define JUCE_LIVE_CONSTANT(initialValue) \
  253. (juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue))
  254. #else
  255. #define JUCE_LIVE_CONSTANT(initialValue) \
  256. (initialValue)
  257. #endif
  258. } // namespace juce