LMMS
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
26namespace juce
27{
28
29#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR && ! defined (DOXYGEN)
30
31//==============================================================================
35namespace LiveConstantEditor
36{
37 int64 parseInt (String);
38 double parseDouble (const String&);
39 String intToString (int, bool preferHex);
40 String intToString (int64, bool preferHex);
41
42 template <typename Type>
43 static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
44 inline void setFromString (char& v, const String& s) { v = (char) parseInt (s); }
45 inline void setFromString (unsigned char& v, const String& s) { v = (unsigned char) parseInt (s); }
46 inline void setFromString (short& v, const String& s) { v = (short) parseInt (s); }
47 inline void setFromString (unsigned short& v, const String& s) { v = (unsigned short) parseInt (s); }
48 inline void setFromString (int& v, const String& s) { v = (int) parseInt (s); }
49 inline void setFromString (unsigned int& v, const String& s) { v = (unsigned int) parseInt (s); }
50 inline void setFromString (long& v, const String& s) { v = (long) parseInt (s); }
51 inline void setFromString (unsigned long& v, const String& s) { v = (unsigned long) parseInt (s); }
52 inline void setFromString (int64& v, const String& s) { v = (int64) parseInt (s); }
53 inline void setFromString (uint64& v, const String& s) { v = (uint64) parseInt (s); }
54 inline void setFromString (double& v, const String& s) { v = parseDouble (s); }
55 inline void setFromString (float& v, const String& s) { v = (float) parseDouble (s); }
56 inline void setFromString (bool& v, const String& s) { v = (s == "true"); }
57 inline void setFromString (String& v, const String& s) { v = s; }
58 inline void setFromString (Colour& v, const String& s) { v = Colour ((uint32) parseInt (s)); }
59
60 template <typename Type>
61 inline String getAsString (const Type& v, bool) { return String (v); }
62 inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); }
63 inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); }
64 inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); }
65 inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); }
66 inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); }
67 inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); }
68 inline String getAsString (bool v, bool) { return v ? "true" : "false"; }
69 inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
70 inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
71 inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); }
72
73 template <typename Type> struct isStringType { enum { value = 0 }; };
74 template <> struct isStringType<String> { enum { value = 1 }; };
75
76 template <typename Type>
77 inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); }
78 inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; }
79 inline String getAsCode (const String& v, bool) { return CppTokeniserFunctions::addEscapeChars(v).quoted(); }
80 inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); }
81
82 template <typename Type>
83 inline const char* castToCharPointer (const Type&) { return ""; }
84 inline const char* castToCharPointer (const String& s) { return s.toRawUTF8(); }
85
86 struct LivePropertyEditorBase;
87
88 //==============================================================================
89 struct JUCE_API LiveValueBase
90 {
91 LiveValueBase (const char* file, int line);
92 virtual ~LiveValueBase();
93
94 virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0;
95 virtual String getStringValue (bool preferHex) const = 0;
96 virtual String getCodeValue (bool preferHex) const = 0;
97 virtual void setStringValue (const String&) = 0;
98 virtual String getOriginalStringValue (bool preferHex) const = 0;
99 virtual bool isString() const = 0;
100
101 String name, sourceFile;
102 int sourceLine;
103
104 JUCE_DECLARE_NON_COPYABLE (LiveValueBase)
105 };
106
107 //==============================================================================
108 struct JUCE_API LivePropertyEditorBase : public Component
109 {
110 LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
111
112 void paint (Graphics&) override;
113 void resized() override;
114
115 void applyNewValue (const String&);
116 void selectOriginalValue();
117 void findOriginalValueInCode();
118
119 LiveValueBase& value;
120 Label name;
121 TextEditor valueEditor;
122 TextButton resetButton { "reset" };
123 CodeDocument& document;
124 CPlusPlusCodeTokeniser tokeniser;
125 CodeEditorComponent sourceEditor;
126 CodeDocument::Position valueStart, valueEnd;
127 std::unique_ptr<Component> customComp;
128 bool wasHex = false;
129
130 JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase)
131 };
132
133 //==============================================================================
134 Component* createColourEditor (LivePropertyEditorBase&);
135 Component* createIntegerSlider (LivePropertyEditorBase&);
136 Component* createFloatSlider (LivePropertyEditorBase&);
137 Component* createBoolSlider (LivePropertyEditorBase&);
138
139 template <typename Type> struct CustomEditor { static Component* create (LivePropertyEditorBase&) { return nullptr; } };
140 template <> struct CustomEditor<char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
141 template <> struct CustomEditor<unsigned char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
142 template <> struct CustomEditor<int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
143 template <> struct CustomEditor<unsigned int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
144 template <> struct CustomEditor<short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
145 template <> struct CustomEditor<unsigned short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
146 template <> struct CustomEditor<int64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
147 template <> struct CustomEditor<uint64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
148 template <> struct CustomEditor<float> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
149 template <> struct CustomEditor<double> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
150 template <> struct CustomEditor<Colour> { static Component* create (LivePropertyEditorBase& e) { return createColourEditor (e); } };
151 template <> struct CustomEditor<bool> { static Component* create (LivePropertyEditorBase& e) { return createBoolSlider (e); } };
152
153 template <typename Type>
154 struct LivePropertyEditor : public LivePropertyEditorBase
155 {
156 template <typename ValueType>
157 LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
158 {
159 customComp.reset (CustomEditor<Type>::create (*this));
160 addAndMakeVisible (customComp.get());
161 }
162 };
163
164 //==============================================================================
165 template <typename Type>
166 struct LiveValue : public LiveValueBase
167 {
168 LiveValue (const char* file, int line, const Type& initialValue)
169 : LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
170 {}
171
172 operator Type() const noexcept { return value; }
173 Type get() const noexcept { return value; }
174 operator const char*() const { return castToCharPointer (value); }
175
176 LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc) override
177 {
178 return new LivePropertyEditor<Type> (*this, doc);
179 }
180
181 String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); }
182 String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); }
183 String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); }
184 void setStringValue (const String& s) override { setFromString (value, s); }
185 bool isString() const override { return isStringType<Type>::value; }
186
187 Type value, originalValue;
188
189 JUCE_DECLARE_NON_COPYABLE (LiveValue)
190 };
191
192 //==============================================================================
193 class JUCE_API ValueList : private AsyncUpdater,
194 private DeletedAtShutdown
195 {
196 public:
197 ValueList();
198 ~ValueList() override;
199
200 JUCE_DECLARE_SINGLETON (ValueList, false)
201
202 template <typename Type>
203 LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
204 {
205 const ScopedLock sl (lock);
206 using ValueType = LiveValue<Type>;
207
208 for (auto* v : values)
209 if (v->sourceLine == line && v->sourceFile == file)
210 return *static_cast<ValueType*> (v);
211
212 auto v = new ValueType (file, line, initialValue);
213 addValue (v);
214 return *v;
215 }
216
217 private:
218 OwnedArray<LiveValueBase> values;
219 OwnedArray<CodeDocument> documents;
220 Array<File> documentFiles;
221 class EditorWindow;
222 Component::SafePointer<EditorWindow> editorWindow;
223 CriticalSection lock;
224
225 CodeDocument& getDocument (const File&);
226 void addValue (LiveValueBase*);
227 void handleAsyncUpdate() override;
228 };
229
230 template <typename Type>
231 inline LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
232 {
233 // If you hit this assertion then the __FILE__ macro is providing a
234 // relative path instead of an absolute path. On Windows this will be
235 // a path relative to the build directory rather than the currently
236 // running application. To fix this you must compile with the /FC flag.
238
239 return ValueList::getInstance()->getValue (file, line, initialValue);
240 }
241
242 inline LiveValue<String>& getValue (const char* file, int line, const char* initialValue)
243 {
244 return getValue (file, line, String (initialValue));
245 }
246}
247
248#endif
249
250//==============================================================================
251#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
298 #define JUCE_LIVE_CONSTANT(initialValue) \
299 (juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue).get())
300#else
301 #define JUCE_LIVE_CONSTANT(initialValue) \
302 (initialValue)
303#endif
304
305} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
int64_t int64
Definition basics.h:91
uint64_t uint64
Definition basics.h:92
uint32_t uint32
Definition basics.h:90
static bool isAbsolutePath(StringRef path)
Definition File.cpp:406
static String toHexString(int number)
Definition String.cpp:1830
String paddedLeft(water_uchar padCharacter, int minimumLength) const
Definition String.cpp:1042
String quoted(water_uchar quoteCharacter='"') const
Definition String.cpp:1509
* e
Definition inflate.c:1404
unsigned v[N_MAX]
Definition inflate.c:1584
unsigned d
Definition inflate.c:940
unsigned s
Definition inflate.c:1555
static PuglViewHint int value
Definition pugl.h:1708
static const char * name
Definition pugl.h:1582
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_DECLARE_SINGLETON(Classname, doNotRecreateAfterDeletion)
Definition juce_Singleton.h:184
#define JUCE_API
Definition juce_StandardHeader.h:152
auto & get(ProcessorChain< Processors... > &chain) noexcept
Definition juce_ProcessorChain.h:133
Definition carla_juce.cpp:31
CriticalSection::ScopedLockType ScopedLock
Definition juce_CriticalSection.h:186
Type
Definition Lv2Ports.h:60
typedef int(UZ_EXP MsgFn)()
struct zdirent * file
Definition win32.c:1500
#define const
Definition zconf.h:137