From 7bde0b54deb4fb49432313c6694903223b62234f Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Tue, 4 Jun 2024 23:23:33 -0400 Subject: [PATCH] auto-size inc/dec slider text entry box size --- Source/LumatoneEditorLookAndFeel.h | 53 +++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/Source/LumatoneEditorLookAndFeel.h b/Source/LumatoneEditorLookAndFeel.h index b618e99..6602762 100644 --- a/Source/LumatoneEditorLookAndFeel.h +++ b/Source/LumatoneEditorLookAndFeel.h @@ -59,6 +59,11 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 return getAppFont(LumatoneEditorFont::GothamNarrowMedium, height); } + juce::Font getSliderTextBoxFont(float height=12.0f) const + { + return getAppFont(LumatoneEditorFont::GothamNarrowMedium, height); + } + float getRoundedRectCornerToAppHeightRatio() const { return 0.00555556f; @@ -264,7 +269,8 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 void drawLabel(Graphics& g, Label& l) override { - Path roundedBounds = getConnectedRoundedRectPath(l.getBounds().toFloat(), l.getHeight() * comboBoxRoundedCornerScalar, 0); + auto labelBounds = l.getBounds().toFloat(); + Path roundedBounds = getConnectedRoundedRectPath(labelBounds, l.getHeight() * comboBoxRoundedCornerScalar, 0); g.setColour(l.findColour(Label::ColourIds::backgroundColourId)); g.fillPath(roundedBounds); @@ -647,13 +653,13 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 Label* createSliderTextBox(Slider& sld) override { + Label* label = new Label(sld.getName() + "_ValueLabel"); + label->setText(String(sld.getValue()), dontSendNotification); + label->setJustificationType(Justification::centred); + label->setFont(getSliderTextBoxFont()); + if (sld.getSliderStyle() >= Slider::SliderStyle::Rotary && sld.getSliderStyle() < Slider::SliderStyle::IncDecButtons) { - Label* label = new Label(sld.getName() + "_ValueLabel"); - label->setText(String(sld.getValue()), dontSendNotification); - label->setJustificationType(Justification::centred); - label->setFont(getAppFont(LumatoneEditorFont::GothamNarrowMedium)); - Colour textColour = (sld.isEnabled()) ? findColour(LumatoneEditorColourIDs::DescriptionText) : findColour(LumatoneEditorColourIDs::InactiveText); @@ -670,13 +676,13 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 } else if (sld.getSliderStyle() == Slider::SliderStyle::IncDecButtons) { - Label* label = new Label(sld.getName() + "_ValueLabel"); - label->setText(String(sld.getValue()), dontSendNotification); - label->setJustificationType(Justification::centred); - label->setFont(getAppFont(LumatoneEditorFont::GothamNarrowMedium)); - + float fontHeightScalar = 1.0f; if (sld.getProperties().contains(LumatoneEditorStyleIDs::fontHeightScalar)) - label->getProperties().set(LumatoneEditorStyleIDs::fontHeightScalar, sld.getProperties()[LumatoneEditorStyleIDs::fontHeightScalar]); + { + auto scalarProperty = sld.getProperties()[LumatoneEditorStyleIDs::fontHeightScalar]; + fontHeightScalar = (float)scalarProperty; + label->getProperties().set(LumatoneEditorStyleIDs::fontHeightScalar, scalarProperty); + } Colour backgroundColour = findColour(LumatoneEditorColourIDs::ControlBoxBackground); Colour textColour = findColour(LumatoneEditorColourIDs::DescriptionText); @@ -691,6 +697,28 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 label->setColour(Label::ColourIds::backgroundWhenEditingColourId, backgroundColour); label->setColour(Label::ColourIds::textColourId, textColour); + auto sliderRange = sld.getRange(); + juce::String minValue = juce::String(roundToInt(sliderRange.getEnd())); + juce::String maxValue = juce::String(roundToInt(sliderRange.getStart())); + juce::String longestValue = (minValue.length() > maxValue.length()) ? minValue : maxValue; + + for (int n = 0; n < sld.getNumDecimalPlacesToDisplay(); n++) + { + longestValue += "_"; + } + + // extra margin + longestValue += "_"; + + float fontHeight = sld.getHeight() * fontHeightScalar * 0.8f; + + juce::Font textBoxFont = getSliderTextBoxFont(fontHeight); + + int labelMaxWidth = textBoxFont.getStringWidth(longestValue); + + sld.setTextBoxStyle(sld.getTextBoxPosition(), false, labelMaxWidth, roundToInt(fontHeight)); + + return label; } else @@ -1299,6 +1327,7 @@ class LumatoneEditorLookAndFeel : public LookAndFeel_V4 setColour(LumatoneEditorColourIDs::LightBackground, Colour(0xff272b2e)); setColour(LumatoneEditorColourIDs::ControlAreaHeader, Colour(0xff272b2e)); setColour(LumatoneEditorColourIDs::ControlAreaBackground, Colour(0xff2d3135)); + setColour(LumatoneEditorColourIDs::ColourPaletteBackground, Colour(0xff292b2d)); setColour(LumatoneEditorColourIDs::ActiveText, Colours::white); setColour(LumatoneEditorColourIDs::InactiveText, Colour(0xffb1b1b1)); setColour(LumatoneEditorColourIDs::DescriptionText, Colour(0xffcbcbcb));