JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_LiveConstantEditor.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR && ! defined (DOXYGEN)
27
28//==============================================================================
33{
34 int64 parseInt (String);
35 double parseDouble (const String&);
36 String intToString (int, bool preferHex);
37 String intToString (int64, bool preferHex);
38
39 template <typename Type>
40 static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
41 inline void setFromString (char& v, const String& s) { v = (char) parseInt (s); }
42 inline void setFromString (unsigned char& v, const String& s) { v = (unsigned char) parseInt (s); }
43 inline void setFromString (short& v, const String& s) { v = (short) parseInt (s); }
44 inline void setFromString (unsigned short& v, const String& s) { v = (unsigned short) parseInt (s); }
45 inline void setFromString (int& v, const String& s) { v = (int) parseInt (s); }
46 inline void setFromString (unsigned int& v, const String& s) { v = (unsigned int) parseInt (s); }
47 inline void setFromString (long& v, const String& s) { v = (long) parseInt (s); }
48 inline void setFromString (unsigned long& v, const String& s) { v = (unsigned long) parseInt (s); }
49 inline void setFromString (int64& v, const String& s) { v = (int64) parseInt (s); }
50 inline void setFromString (uint64& v, const String& s) { v = (uint64) parseInt (s); }
51 inline void setFromString (double& v, const String& s) { v = parseDouble (s); }
52 inline void setFromString (float& v, const String& s) { v = (float) parseDouble (s); }
53 inline void setFromString (bool& v, const String& s) { v = (s == "true"); }
54 inline void setFromString (String& v, const String& s) { v = s; }
55 inline void setFromString (Colour& v, const String& s) { v = Colour ((uint32) parseInt (s)); }
56
57 template <typename Type>
58 inline String getAsString (const Type& v, bool) { return String (v); }
59 inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); }
60 inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); }
61 inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); }
62 inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); }
63 inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); }
64 inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); }
65 inline String getAsString (bool v, bool) { return v ? "true" : "false"; }
66 inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
67 inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
68 inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); }
69
70 template <typename Type> struct isStringType { enum { value = 0 }; };
71 template <> struct isStringType<String> { enum { value = 1 }; };
72
73 template <typename Type>
74 inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); }
75 inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; }
76 inline String getAsCode (const String& v, bool) { return CppTokeniserFunctions::addEscapeChars (v).quoted(); }
77 inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); }
78
79 template <typename Type>
80 inline const char* castToCharPointer (const Type&) { return ""; }
81 inline const char* castToCharPointer (const String& s) { return s.toRawUTF8(); }
82
84
85 //==============================================================================
86 struct JUCE_API LiveValueBase
87 {
88 LiveValueBase (const char* file, int line);
89 virtual ~LiveValueBase();
90
91 virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0;
92 virtual String getStringValue (bool preferHex) const = 0;
93 virtual String getCodeValue (bool preferHex) const = 0;
94 virtual void setStringValue (const String&) = 0;
95 virtual String getOriginalStringValue (bool preferHex) const = 0;
96 virtual bool isString() const = 0;
97
98 String name, sourceFile;
99 int sourceLine;
100
101 JUCE_DECLARE_NON_COPYABLE (LiveValueBase)
102 };
103
104 //==============================================================================
105 struct JUCE_API LivePropertyEditorBase : public Component
106 {
107 LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
108
109 void paint (Graphics&) override;
110 void resized() override;
111
112 void applyNewValue (const String&);
113 void selectOriginalValue();
114 void findOriginalValueInCode();
115
116 LiveValueBase& value;
117 Label name;
118 TextEditor valueEditor;
119 TextButton resetButton { "reset" };
120 CodeDocument& document;
121 CPlusPlusCodeTokeniser tokeniser;
122 CodeEditorComponent sourceEditor;
123 CodeDocument::Position valueStart, valueEnd;
125 bool wasHex = false;
126
127 JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase)
128 };
129
130 //==============================================================================
135
136 template <typename Type> struct CustomEditor { static Component* create (LivePropertyEditorBase&) { return nullptr; } };
137 template <> struct CustomEditor<char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
138 template <> struct CustomEditor<unsigned char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
139 template <> struct CustomEditor<int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
140 template <> struct CustomEditor<unsigned int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
141 template <> struct CustomEditor<short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
142 template <> struct CustomEditor<unsigned short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
143 template <> struct CustomEditor<int64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
144 template <> struct CustomEditor<uint64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
145 template <> struct CustomEditor<float> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
146 template <> struct CustomEditor<double> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
147 template <> struct CustomEditor<Colour> { static Component* create (LivePropertyEditorBase& e) { return createColourEditor (e); } };
148 template <> struct CustomEditor<bool> { static Component* create (LivePropertyEditorBase& e) { return createBoolSlider (e); } };
149
150 template <typename Type>
152 {
153 template <typename ValueType>
154 LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
155 {
156 customComp.reset (CustomEditor<Type>::create (*this));
157 addAndMakeVisible (customComp.get());
158 }
159 };
160
161 //==============================================================================
162 template <typename Type>
163 struct LiveValue : public LiveValueBase
164 {
165 LiveValue (const char* file, int line, const Type& initialValue)
166 : LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
167 {}
168
169 operator Type() const noexcept { return value; }
170 Type get() const noexcept { return value; }
171 operator const char*() const { return castToCharPointer (value); }
172
173 LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc) override
174 {
175 return new LivePropertyEditor<Type> (*this, doc);
176 }
177
178 String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); }
179 String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); }
180 String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); }
181 void setStringValue (const String& s) override { setFromString (value, s); }
182 bool isString() const override { return isStringType<Type>::value; }
183
184 Type value, originalValue;
185
186 JUCE_DECLARE_NON_COPYABLE (LiveValue)
187 };
188
189 //==============================================================================
190 class JUCE_API ValueList : private AsyncUpdater,
191 private DeletedAtShutdown
192 {
193 public:
194 ValueList();
195 ~ValueList() override;
196
197 JUCE_DECLARE_SINGLETON (ValueList, false)
198
199 template <typename Type>
200 LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
201 {
202 const ScopedLock sl (lock);
203 using ValueType = LiveValue<Type>;
204
205 for (auto* v : values)
206 if (v->sourceLine == line && v->sourceFile == file)
207 return *static_cast<ValueType*> (v);
208
209 auto v = new ValueType (file, line, initialValue);
210 addValue (v);
211 return *v;
212 }
213
214 private:
215 OwnedArray<LiveValueBase> values;
216 OwnedArray<CodeDocument> documents;
217 Array<File> documentFiles;
218 class EditorWindow;
219 Component::SafePointer<EditorWindow> editorWindow;
220 CriticalSection lock;
221
222 CodeDocument& getDocument (const File&);
223 void addValue (LiveValueBase*);
224 void handleAsyncUpdate() override;
225 };
226
227 template <typename Type>
228 inline LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
229 {
230 // If you hit this assertion then the __FILE__ macro is providing a
231 // relative path instead of an absolute path. On Windows this will be
232 // a path relative to the build directory rather than the currently
233 // running application. To fix this you must compile with the /FC flag.
235
236 return ValueList::getInstance()->getValue (file, line, initialValue);
237 }
238
239 inline LiveValue<String>& getValue (const char* file, int line, const char* initialValue)
240 {
241 return getValue (file, line, String (initialValue));
242 }
243
244} // namespace juce::LiveConstantEditor
245
246#endif
247
248//==============================================================================
249#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
296 #define JUCE_LIVE_CONSTANT(initialValue) \
297 (juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue).get())
298#else
299 #define JUCE_LIVE_CONSTANT(initialValue) \
300 (initialValue)
301#endif
static bool isAbsolutePath(StringRef path)
Returns true if the string seems to be a fully-specified absolute path.
String paddedLeft(juce_wchar padCharacter, int minimumLength) const
Returns a copy of this string with the specified character repeatedly added to its beginning until th...
String quoted(juce_wchar quoteCharacter='"') const
Adds quotation marks around a string.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
auto & get(ProcessorChain< Processors... > &chain) noexcept
Non-member equivalent of ProcessorChain::get which avoids awkward member template syntax.
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
Macro to generate the appropriate methods and boilerplate for a singleton class.
typedef char
T lock(T... args)
typedef float
CriticalSection::ScopedLockType ScopedLock
Automatically locks and unlocks a CriticalSection object.
unsigned long long uint64
A platform-independent 64-bit unsigned integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.
static String addEscapeChars(const String &s)
Takes a string and returns a version of it where standard C++ escape sequences have been used to repl...
typedef long