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.

302 lines
15KB

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