26#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR && ! defined (DOXYGEN)
32namespace juce::LiveConstantEditor
34 int64 parseInt (String);
35 double parseDouble (
const String&);
36 String intToString (
int,
bool preferHex);
37 String intToString (int64,
bool preferHex);
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)); }
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); }
70 template <
typename Type>
struct isStringType {
enum { value = 0 }; };
71 template <>
struct isStringType<String> {
enum { value = 1 }; };
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); }
79 template <
typename Type>
80 inline const char* castToCharPointer (
const Type&) {
return ""; }
81 inline const char* castToCharPointer (
const String& s) {
return s.toRawUTF8(); }
83 struct LivePropertyEditorBase;
86 struct JUCE_API LiveValueBase
88 LiveValueBase (
const char* file,
int line);
89 virtual ~LiveValueBase();
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;
98 String name, sourceFile;
105 struct JUCE_API LivePropertyEditorBase :
public Component
107 LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
109 void paint (Graphics&)
override;
110 void resized()
override;
112 void applyNewValue (
const String&);
113 void selectOriginalValue();
114 void findOriginalValueInCode();
116 LiveValueBase& value;
118 TextEditor valueEditor;
119 TextButton resetButton {
"reset" };
120 CodeDocument& document;
121 CPlusPlusCodeTokeniser tokeniser;
122 CodeEditorComponent sourceEditor;
123 CodeDocument::Position valueStart, valueEnd;
131 Component* createColourEditor (LivePropertyEditorBase&);
132 Component* createIntegerSlider (LivePropertyEditorBase&);
133 Component* createFloatSlider (LivePropertyEditorBase&);
134 Component* createBoolSlider (LivePropertyEditorBase&);
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); } };
150 template <
typename Type>
151 struct LivePropertyEditor :
public LivePropertyEditorBase
153 template <
typename ValueType>
154 LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
156 customComp.reset (CustomEditor<Type>::create (*
this));
157 addAndMakeVisible (customComp.get());
162 template <
typename Type>
163 struct LiveValue :
public LiveValueBase
165 LiveValue (
const char* file,
int line,
const Type& initialValue)
166 : LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
169 operator Type() const noexcept {
return value; }
170 Type
get() const noexcept {
return value; }
171 operator const char*()
const {
return castToCharPointer (value); }
173 LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc)
override
175 return new LivePropertyEditor<Type> (*
this, doc);
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; }
184 Type value, originalValue;
190 class JUCE_API ValueList :
private AsyncUpdater,
191 private DeletedAtShutdown
195 ~ValueList()
override;
199 template <typename Type>
200 LiveValue<Type>& getValue (const
char* file,
int line, const Type& initialValue)
203 using ValueType = LiveValue<Type>;
205 for (
auto* v : values)
206 if (v->sourceLine == line && v->sourceFile == file)
207 return *static_cast<ValueType*> (v);
209 auto v =
new ValueType (file, line, initialValue);
215 OwnedArray<LiveValueBase> values;
216 OwnedArray<CodeDocument> documents;
217 Array<File> documentFiles;
219 Component::SafePointer<EditorWindow> editorWindow;
220 CriticalSection
lock;
222 CodeDocument& getDocument (
const File&);
223 void addValue (LiveValueBase*);
224 void handleAsyncUpdate()
override;
227 template <
typename Type>
228 inline LiveValue<Type>& getValue (
const char* file,
int line,
const Type& initialValue)
234 jassert (File::isAbsolutePath (file));
236 return ValueList::getInstance()->getValue (file, line, initialValue);
239 inline LiveValue<String>& getValue (
const char* file,
int line,
const char* initialValue)
241 return getValue (file, line, String (initialValue));
249#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
296 #define JUCE_LIVE_CONSTANT(initialValue) \
297 (juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue).get())
299 #define JUCE_LIVE_CONSTANT(initialValue) \
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.
CriticalSection::ScopedLockType ScopedLock
Automatically locks and unlocks a CriticalSection object.