1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-25 02:04:23 +00:00

Some improvements to JUCE_LIVE_CONSTANT: Matching original hex/decimal format, and added a reset button.

This commit is contained in:
jules 2013-12-06 18:27:41 +00:00
parent 1872ed68c2
commit 08c2fce9b5
3 changed files with 54 additions and 37 deletions

View file

@ -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);

View file

@ -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<int> left (r.removeFromLeft (jmax (200, r.getWidth() / 3)));
name.setBounds (left.removeFromTop (25));
Rectangle<int> 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<ColourSelector*> (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));
}

View file

@ -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 <typename Type>
static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
@ -56,22 +56,22 @@ namespace LiveConstantEditor
inline void setFromString (Colour& v, const String& s) { v = Colour ((int) parseInt (s)); }
template <typename Type>
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 <typename Type>
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 <typename Type>
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<Component> 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<Type> (*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)
};