2024-01-27 13:57:18 +00:00
|
|
|
#pragma once
|
|
|
|
#include <JuceHeader.h>
|
|
|
|
|
|
|
|
class DoubleTextBox : public juce::TextEditor {
|
|
|
|
public:
|
|
|
|
DoubleTextBox(double minValue, double maxValue) : minValue(minValue), maxValue(maxValue) {
|
|
|
|
setText(juce::String(minValue, 2), false);
|
|
|
|
setMultiLine(false);
|
|
|
|
setJustification(juce::Justification::centred);
|
|
|
|
setFont(juce::Font(15.0f, juce::Font::plain));
|
|
|
|
onTextChange = [this]() {
|
|
|
|
setText(getText(), false);
|
|
|
|
};
|
2024-04-26 20:21:59 +00:00
|
|
|
juce::Desktop::getInstance().addGlobalMouseListener(this);
|
2024-01-27 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double getValue() {
|
|
|
|
return getText().getDoubleValue();
|
|
|
|
}
|
|
|
|
|
2024-04-21 18:07:15 +00:00
|
|
|
void setValue(double value, bool sendChangeMessage = true, int digits = 2) {
|
|
|
|
setText(juce::String(value, (size_t)(digits)), sendChangeMessage);
|
2024-01-27 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setText(const juce::String& newText, bool sendChangeMessage = true) {
|
|
|
|
// remove all non-digits
|
|
|
|
juce::String text = newText.retainCharacters("0123456789.-");
|
|
|
|
|
|
|
|
// only keep first decimal point
|
|
|
|
int firstDecimal = text.indexOfChar('.');
|
|
|
|
if (firstDecimal != -1) {
|
|
|
|
juce::String remainder = text.substring(firstDecimal + 1);
|
|
|
|
remainder = remainder.retainCharacters("0123456789");
|
|
|
|
text = text.substring(0, firstDecimal + 1) + remainder;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only keep negative sign at beginning
|
|
|
|
if (text.contains("-")) {
|
|
|
|
juce::String remainder = text.substring(1);
|
2024-04-26 19:34:50 +00:00
|
|
|
remainder = remainder.retainCharacters("0123456789.");
|
2024-01-27 13:57:18 +00:00
|
|
|
text = text.substring(0, 1) + remainder;
|
|
|
|
}
|
|
|
|
|
|
|
|
double value = text.getDoubleValue();
|
|
|
|
if (value < minValue || value > maxValue) {
|
|
|
|
text = juce::String(prevValue);
|
|
|
|
} else {
|
|
|
|
prevValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
juce::TextEditor::setText(text, sendChangeMessage);
|
2024-05-11 22:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouseDown(const juce::MouseEvent& e) override {
|
|
|
|
if (getScreenBounds().contains(e.getScreenPosition())) {
|
|
|
|
// Delegate mouse clicks inside the editor to the TextEditor
|
|
|
|
// class so as to not break its functionality.
|
|
|
|
juce::TextEditor::mouseDown(e);
|
|
|
|
} else {
|
|
|
|
// Lose focus when mouse clicks occur outside the editor.
|
|
|
|
giveAwayKeyboardFocus();
|
|
|
|
}
|
2024-01-27 13:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:21:59 +00:00
|
|
|
~DoubleTextBox() override {
|
|
|
|
juce::Desktop::getInstance().removeGlobalMouseListener(this);
|
|
|
|
}
|
2024-01-27 13:57:18 +00:00
|
|
|
|
|
|
|
private:
|
2024-05-11 22:10:54 +00:00
|
|
|
double minValue;
|
|
|
|
double maxValue;
|
2024-01-27 13:57:18 +00:00
|
|
|
double prevValue = minValue;
|
|
|
|
};
|