Skip to content

Commit

Permalink
refactor popup menu auto-column num method
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Jul 14, 2024
1 parent 040246c commit c04bf47
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
46 changes: 46 additions & 0 deletions Source/LumatoneEditorStyleCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <JuceHeader.h>
#include "./lumatone_editor_library/graphics/lumatone_assets.h"

#include "./lumatone_editor_library/common/math.h"

//==================================================================
//
// STATIC HELPERS
Expand Down Expand Up @@ -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));
}
35 changes: 1 addition & 34 deletions Source/components/RangedControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c04bf47

Please sign in to comment.