diff --git a/extras/Demo/Source/Demos/LiveConstantDemo.cpp b/extras/Demo/Source/Demos/LiveConstantDemo.cpp index 58370e5507..93cdab79db 100644 --- a/extras/Demo/Source/Demos/LiveConstantDemo.cpp +++ b/extras/Demo/Source/Demos/LiveConstantDemo.cpp @@ -36,7 +36,7 @@ struct LiveConstantDemoComponent : public Component g.fillAll (JUCE_LIVE_CONSTANT (Colour (0xffe5e7a7))); g.setColour (JUCE_LIVE_CONSTANT (Colours::red.withAlpha (0.2f))); - int blockWidth = JUCE_LIVE_CONSTANT (200); + int blockWidth = JUCE_LIVE_CONSTANT (0x120); int blockHeight = JUCE_LIVE_CONSTANT (200); g.fillRect ((getWidth() - blockWidth) / 2, (getHeight() - blockHeight) / 2, blockWidth, blockHeight); diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp index 78d6445370..7f41ed7c68 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp @@ -87,8 +87,8 @@ double parseDouble (const String& s) return s.retainCharacters ("0123456789.eE-").getDoubleValue(); } -String hexString (int v) { return "0x" + String::toHexString (v); } -String hexString (int64 v) { return "0x" + String::toHexString (v); } +String intToString (int v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); } +String intToString (int64 v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); } //============================================================================== LiveValueBase::LiveValueBase (const char* file, int line) @@ -103,11 +103,12 @@ LiveValueBase::~LiveValueBase() //============================================================================== LivePropertyEditorBase::LivePropertyEditorBase (LiveValueBase& v, CodeDocument& d) - : value (v), document (d), sourceEditor (document, &tokeniser) + : value (v), resetButton ("reset"), document (d), sourceEditor (document, &tokeniser), wasHex (false) { setSize (600, 100); addAndMakeVisible (&name); + addAndMakeVisible (&resetButton); addAndMakeVisible (&valueEditor); addAndMakeVisible (&sourceEditor); @@ -116,9 +117,10 @@ LivePropertyEditorBase::LivePropertyEditorBase (LiveValueBase& v, CodeDocument& name.setFont (13.0f); name.setText (v.name, dontSendNotification); - valueEditor.setText (v.getStringValue(), dontSendNotification); + valueEditor.setText (v.getStringValue (wasHex), dontSendNotification); valueEditor.addListener (this); sourceEditor.setReadOnly (true); + resetButton.addListener (this); } void LivePropertyEditorBase::paint (Graphics& g) @@ -133,7 +135,9 @@ void LivePropertyEditorBase::resized() Rectangle left (r.removeFromLeft (jmax (200, r.getWidth() / 3))); - name.setBounds (left.removeFromTop (25)); + Rectangle top (left.removeFromTop (25)); + resetButton.setBounds (top.removeFromRight (35).reduced (0, 3)); + name.setBounds (top); valueEditor.setBounds (left.removeFromTop (25)); left.removeFromTop (2); @@ -149,11 +153,16 @@ void LivePropertyEditorBase::textEditorTextChanged (TextEditor&) applyNewValue (valueEditor.getText()); } +void LivePropertyEditorBase::buttonClicked (Button*) +{ + applyNewValue (value.getOriginalStringValue (wasHex)); +} + void LivePropertyEditorBase::applyNewValue (const String& s) { value.setStringValue (s); - document.replaceSection (valueStart.getPosition(), valueEnd.getPosition(), value.getCodeValue()); + document.replaceSection (valueStart.getPosition(), valueEnd.getPosition(), value.getCodeValue (wasHex)); document.clearUndoHistory(); selectOriginalValue(); @@ -219,6 +228,8 @@ void LivePropertyEditorBase::findOriginalValueInCode() valueStart.setPositionMaintained (true); valueEnd.setPositionMaintained (true); + + wasHex = String (start, end).containsIgnoreCase ("0x"); } } } @@ -362,7 +373,7 @@ struct ColourEditorComp : public Component, Colour getColour() const { - return Colour ((int) parseInt (editor.value.getStringValue())); + return Colour ((int) parseInt (editor.value.getStringValue (false))); } void paint (Graphics& g) override @@ -387,7 +398,7 @@ struct ColourEditorComp : public Component, void changeListenerCallback (ChangeBroadcaster* source) override { if (ColourSelector* cs = dynamic_cast (source)) - editor.applyNewValue (getAsString (cs->getCurrentColour())); + editor.applyNewValue (getAsString (cs->getCurrentColour(), true)); repaint(); } @@ -416,8 +427,8 @@ public: void updateRange() { - double v = isFloat ? parseDouble (editor.value.getStringValue()) - : (double) parseInt (editor.value.getStringValue()); + double v = isFloat ? parseDouble (editor.value.getStringValue (false)) + : (double) parseInt (editor.value.getStringValue (false)); double range = isFloat ? 10 : 100; @@ -432,8 +443,8 @@ private: void sliderValueChanged (Slider*) { - editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue()) - : getAsString ((int64) slider.getValue())); + editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex) + : getAsString ((int64) slider.getValue(), editor.wasHex)); } diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h index f8e7b4b14f..f4714fe109 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.h @@ -35,8 +35,8 @@ namespace LiveConstantEditor { int64 parseInt (String); double parseDouble (const String&); - String hexString (int); - String hexString (int64); + String intToString (int, bool preferHex); + String intToString (int64, bool preferHex); template static void setFromString (Type& v, const String& s) { v = static_cast (s); } @@ -56,22 +56,22 @@ namespace LiveConstantEditor inline void setFromString (Colour& v, const String& s) { v = Colour ((int) parseInt (s)); } template - inline String getAsString (const Type& v) { return String (v); } - inline String getAsString (char v) { return hexString ((int) v); } - inline String getAsString (unsigned char v) { return hexString ((int) v); } - inline String getAsString (short v) { return hexString ((int) v); } - inline String getAsString (unsigned short v) { return hexString ((int) v); } - inline String getAsString (int v) { return hexString ((int) v); } - inline String getAsString (unsigned int v) { return hexString ((int) v); } - inline String getAsString (int64 v) { return hexString ((int64) v); } - inline String getAsString (uint64 v) { return hexString ((int64) v); } - inline String getAsString (Colour v) { return hexString ((int) v.getARGB()); } + inline String getAsString (const Type& v, bool) { return String (v); } + inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); } + inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); } + inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); } + inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); } template - inline String getAsCode (Type& value) { return getAsString (value); } - inline String getAsCode (Colour value) { return "Colour (" + hexString ((int) value.getARGB()) + ")"; } - inline String getAsCode (const String& v) { return "\"" + v + "\""; } - inline String getAsCode (const char* value) { return getAsCode (String (value)); } + inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); } + inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; } + inline String getAsCode (const String& v, bool) { return "\"" + v + "\""; } + inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); } template inline const char* castToCharPointer (const Type&) { return ""; } @@ -86,9 +86,10 @@ namespace LiveConstantEditor virtual ~LiveValueBase(); virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0; - virtual String getStringValue() const = 0; - virtual String getCodeValue() const = 0; + virtual String getStringValue (bool preferHex) const = 0; + virtual String getCodeValue (bool preferHex) const = 0; virtual void setStringValue (const String&) = 0; + virtual String getOriginalStringValue (bool preferHex) const = 0; String name, sourceFile; int sourceLine; @@ -98,13 +99,15 @@ namespace LiveConstantEditor //============================================================================== struct JUCE_API LivePropertyEditorBase : public Component, - private TextEditor::Listener + private TextEditor::Listener, + private ButtonListener { LivePropertyEditorBase (LiveValueBase&, CodeDocument&); void paint (Graphics&) override; void resized() override; void textEditorTextChanged (TextEditor&) override; + void buttonClicked (Button*) override; void applyNewValue (const String&); void selectOriginalValue(); @@ -113,11 +116,13 @@ namespace LiveConstantEditor LiveValueBase& value; Label name; TextEditor valueEditor; + TextButton resetButton; CodeDocument& document; CPlusPlusCodeTokeniser tokeniser; CodeEditorComponent sourceEditor; CodeDocument::Position valueStart, valueEnd; ScopedPointer customComp; + bool wasHex; JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase) }; @@ -155,7 +160,7 @@ namespace LiveConstantEditor struct LiveValue : public LiveValueBase { LiveValue (const char* file, int line, const Type& initialValue) - : LiveValueBase (file, line), value (initialValue) + : LiveValueBase (file, line), value (initialValue), originalValue (initialValue) {} operator Type() const noexcept { return value; } @@ -166,11 +171,12 @@ namespace LiveConstantEditor return new LivePropertyEditor (*this, doc); } - String getStringValue() const override { return getAsString (value); } - String getCodeValue() const override { return getAsCode (value); } - void setStringValue (const String& s) override { setFromString (value, s); } + String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); } + String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); } + String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); } + void setStringValue (const String& s) override { setFromString (value, s); } - Type value; + Type value, originalValue; JUCE_DECLARE_NON_COPYABLE (LiveValue) };