From c04bf47efd9aec9c8e8ef3391a0ef23d36320688 Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Sun, 14 Jul 2024 02:09:24 -0400 Subject: [PATCH] refactor popup menu auto-column num method --- Source/LumatoneEditorStyleCommon.h | 46 +++++++++++++++++++++++++++++ Source/components/RangedControl.cpp | 35 +--------------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/Source/LumatoneEditorStyleCommon.h b/Source/LumatoneEditorStyleCommon.h index 73f841d..59be398 100644 --- a/Source/LumatoneEditorStyleCommon.h +++ b/Source/LumatoneEditorStyleCommon.h @@ -14,6 +14,8 @@ #include #include "./lumatone_editor_library/graphics/lumatone_assets.h" +#include "./lumatone_editor_library/common/math.h" + //================================================================== // // STATIC HELPERS @@ -709,3 +711,47 @@ class TextButtonMouseHighlight : public TextButton } } }; + +static void findIdealComboBoxNumColumns(juce::ComboBox* box, int numItems) +{ + double sqNumItems = sqrt((double)numItems); + int maxColumns = (int)(sqNumItems); + + if (maxColumns != sqNumItems) + { + // get prime factors + auto factors = getFactors(numItems); + + // find largest exponent + int maxF = 1; + int maxExp = 1; + for (int i = 0; i < juce::jmin(factors.size(), 100); i++) + { + int iExp = factors[i]; + if (iExp >= maxExp) + { + maxExp = iExp; + maxF = (int)pow(PRIMES[i], maxExp); + } + } + + if (maxExp > 4) + maxColumns = 8; + else if (maxExp > 2) + maxColumns = 4; + else if (maxExp > 1) + maxColumns = 2; + else + { + if (maxF > 8) + { + maxColumns = sqrt(maxF); + } + else + maxColumns = 1; + } + } + + + box->getProperties().set(LumatoneEditorStyleIDs::popupMenuMaxColumns, juce::var(maxColumns)); +} diff --git a/Source/components/RangedControl.cpp b/Source/components/RangedControl.cpp index 9bc6377..4557778 100644 --- a/Source/components/RangedControl.cpp +++ b/Source/components/RangedControl.cpp @@ -85,40 +85,7 @@ void RangedControl::setRange(int min, int max) box->addItem(juce::String(i), id++); } - int numItems = range.getEnd() - range.getStart() + 1; - double sqNumItems = sqrt((double)numItems); - int maxColumns = (int)(sqNumItems); - - if (maxColumns != sqNumItems) - { - // get prime factors - auto factors = getFactors(numItems); - - // find largest exponent - int maxF = 1; - int maxExp = 1; - for (int i = 0; i < jmin(factors.size(), 100); i++) - { - int iExp = factors[i]; - if (iExp > maxExp) - { - maxExp = iExp; - maxF = (int)pow(PRIMES[i], maxExp); - } - } - - if (maxExp > 4) - maxColumns = 8; - else if (maxExp > 2) - maxColumns = 4; - else if (maxExp > 1) - maxColumns = 2; - else - maxColumns = 1; - } - - - box->getProperties().set(LumatoneEditorStyleIDs::popupMenuMaxColumns, juce::var(maxColumns)); + findIdealComboBoxNumColumns(box.get(), range.getEnd() - range.getStart() + 1); } else if (slider)