Skip to content

Commit

Permalink
fix(grid panel): cache text bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
zsliu98 committed Sep 20, 2024
1 parent 3ee363f commit 2818970
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
15 changes: 6 additions & 9 deletions source/panel/curve_panel/background_panel/grid_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ namespace zlPanel {

void GridPanel::paint(juce::Graphics &g) {
g.fillAll(uiBase.getBackgroundColor());
const auto bound = getLocalBounds().toFloat();

g.setFont(uiBase.getFontSize() * zlInterface::FontLarge);
if (uiBase.getColourByIdx(zlInterface::gridColour).getFloatAlpha() <= 0.01f) {
return;
}
g.setColour(uiBase.getTextInactiveColor());
for (size_t i = 0; i < backgroundFreqs.size(); ++i) {
const auto x = backgroundFreqs[i] * bound.getWidth() + bound.getX();
const auto textBound = juce::Rectangle<float>(x - uiBase.getFontSize() * 3 - uiBase.getFontSize() * 0.125f,
bound.getBottom() - uiBase.getFontSize() * 2,
uiBase.getFontSize() * 3, uiBase.getFontSize() * 2);
g.drawText(backgroundFreqsNames[i], textBound, juce::Justification::bottomRight);
g.drawText(backgroundFreqsNames[i], textBounds[i], juce::Justification::bottomRight);
}
g.setColour(uiBase.getColourByIdx(zlInterface::gridColour));
g.fillRectList(rectList);
Expand All @@ -43,12 +37,15 @@ namespace zlPanel {
const auto thickness = uiBase.getFontSize() * 0.1f;
for (size_t i = 0; i < backgroundFreqs.size(); ++i) {
const auto x = backgroundFreqs[i] * bound.getWidth() + bound.getX();
rectList.add({x - thickness * .5f, bound.getY(),thickness, bound.getHeight()});
rectList.add({x - thickness * .5f, bound.getY(), thickness, bound.getHeight()});
textBounds[i] = juce::Rectangle<float>(x - uiBase.getFontSize() * 3 - uiBase.getFontSize() * 0.125f,
bound.getBottom() - uiBase.getFontSize() * 2,
uiBase.getFontSize() * 3, uiBase.getFontSize() * 2);
}

bound = bound.withSizeKeepingCentre(bound.getWidth(), bound.getHeight() - 2 * uiBase.getFontSize());

for (auto &d:backgroundDBs) {
for (auto &d: backgroundDBs) {
const auto y = d * bound.getHeight() + bound.getY();
rectList.add({bound.getX(), y - thickness * .5f, bound.getWidth(), thickness});
}
Expand Down
3 changes: 2 additions & 1 deletion source/panel/curve_panel/background_panel/grid_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace zlPanel {
0.59836837f, 0.68843178f, 0.80748914f, 0.89755255f, 0.98761596f
};

inline static const std::array<juce::String, 10> backgroundFreqsNames = {
static constexpr std::array<std::string, 10> backgroundFreqsNames = {
"20", "50", "100", "200", "500", "1k", "2k", "5k", "10k", "20k"
};

Expand All @@ -43,6 +43,7 @@ namespace zlPanel {
private:
zlInterface::UIBase &uiBase;
juce::RectangleList<float> rectList;
std::array<juce::Rectangle<float>, 10> textBounds;
};
}

Expand Down

0 comments on commit 2818970

Please sign in to comment.