Skip to content

Commit

Permalink
GlyphButton can have a pair of glyphs (#104)
Browse files Browse the repository at this point in the history
Fun!
  • Loading branch information
baconpaul authored Aug 2, 2024
1 parent 8edbbc5 commit e4e29e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/sst/jucegui/components/GlyphButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef INCLUDE_SST_JUCEGUI_COMPONENTS_GLYPHBUTTON_H
#define INCLUDE_SST_JUCEGUI_COMPONENTS_GLYPHBUTTON_H

#include <optional>
#include <juce_gui_basics/juce_gui_basics.h>

#include <sst/jucegui/style/StyleAndSettingsConsumer.h>
Expand All @@ -39,6 +40,8 @@ struct GlyphButton : public CallbackButtonComponent<GlyphButton>,
public EditableComponentBase<GlyphButton>
{
GlyphButton(GlyphPainter::GlyphType type);
GlyphButton(GlyphPainter::GlyphType type, GlyphPainter::GlyphType secondGlyph,
int glyphZeroWidth); // side by side
~GlyphButton();

struct Styles : base_styles::PushButton, base_styles::BaseLabel
Expand All @@ -56,6 +59,8 @@ struct GlyphButton : public CallbackButtonComponent<GlyphButton>,
int glyphButtonPad{0};
void paint(juce::Graphics &g) override;
GlyphPainter::GlyphType glyph;
std::optional<GlyphPainter::GlyphType> secondGlyph{std::nullopt};
int glyphZeroWidth{0};

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(GlyphButton)
};
Expand Down
36 changes: 35 additions & 1 deletion src/sst/jucegui/components/GlyphButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,27 @@ GlyphButton::GlyphButton(GlyphPainter::GlyphType t)
{
}

GlyphButton::GlyphButton(GlyphPainter::GlyphType t, GlyphPainter::GlyphType snd, int g)
: style::StyleConsumer(Styles::styleClass), glyph(t), secondGlyph(snd), glyphZeroWidth(g)
{
}

GlyphButton::~GlyphButton() {}

juce::Rectangle<int> squareCenter(const juce::Rectangle<int> &b)
{
if (b.getWidth() > b.getHeight())
{
auto d = b.getWidth() - b.getHeight();
return b.withTrimmedLeft(d / 2).withTrimmedRight(d / 2);
}
else
{
auto d = b.getHeight() - b.getWidth();
return b.withTrimmedTop(d / 2).withTrimmedBottom(d / 2);
}
}

void GlyphButton::paint(juce::Graphics &g)
{
paintButtonBG(this, g);
Expand All @@ -37,6 +56,21 @@ void GlyphButton::paint(juce::Graphics &g)
col = getColour(Styles::labelcolor_hover);
else
col = getColour(Styles::labelcolor);
GlyphPainter::paintGlyph(g, getLocalBounds().reduced(glyphButtonPad), glyph, col);
if (secondGlyph.has_value())
{
if (glyphZeroWidth <= 0)
glyphZeroWidth = getWidth() / 2;

auto b1 = getLocalBounds().withWidth(glyphZeroWidth);
auto b2 = getLocalBounds().withTrimmedLeft(glyphZeroWidth);
GlyphPainter::paintGlyph(g, squareCenter(b1), glyph, col);
GlyphPainter::paintGlyph(g, squareCenter(b2), *secondGlyph, col);
}
else
{
auto b = squareCenter(getLocalBounds().reduced(glyphButtonPad));

GlyphPainter::paintGlyph(g, b, glyph, col);
}
}
} // namespace sst::jucegui::components

0 comments on commit e4e29e7

Please sign in to comment.