Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GlyphButton can have a pair of glyphs #104

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading