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.h 15KB

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