File: RoundButton.h

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (115 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2018 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#pragma once

#include "../JuceLibraryCode/JuceHeader.h"

//==============================================================================
/*
*/
class RoundButton : public juce::ToggleButton
{
public:
    RoundButton() {}

    ~RoundButton() {}

    void paint (juce::Graphics& g) override
    {
        auto bounds = getLocalBounds();
        juce::Rectangle<float> buttonArea;
        if (isCircularShape)
        {
            const float boxSize = bounds.getWidth() >= bounds.getHeight()
                                      ? bounds.getHeight() * 0.8f
                                      : bounds.getWidth() * 0.8f;
            buttonArea = juce::Rectangle<float> ((bounds.getWidth() - boxSize) * 0.5f,
                                                 (bounds.getHeight() - boxSize) * 0.5f,
                                                 boxSize,
                                                 boxSize);
        }
        else
        {
            buttonArea = juce::Rectangle<float> (bounds.getX(),
                                                 bounds.getY(),
                                                 bounds.getWidth(),
                                                 bounds.getHeight());
            buttonArea.reduce (0.5f, 0.4f);
        }

        const bool isButtonDown = isMouseButtonDown();
        const bool isMouseOverButton = isMouseOver();

        const bool ticked = getToggleState();

        if (isButtonDown)
            buttonArea.reduce (0.8f, 0.8f);
        else if (isMouseOverButton)
            buttonArea.reduce (0.4f, 0.4f);

        g.setColour (findColour (juce::ToggleButton::tickColourId)
                         .withMultipliedAlpha (ticked              ? 1.0f
                                               : isMouseOverButton ? 0.7f
                                                                   : 0.5f));

        isCircularShape == true ? g.drawEllipse (buttonArea, 1.0f)
                                : g.drawRoundedRectangle (buttonArea, 10.0f, 1.0f);

        buttonArea.reduce (1.5f, 1.5f);
        g.setColour (findColour (juce::ToggleButton::tickColourId)
                         .withMultipliedAlpha (ticked              ? 1.0f
                                               : isMouseOverButton ? 0.5f
                                                                   : 0.2f));

        isCircularShape == true ? g.fillEllipse (buttonArea)
                                : g.fillRoundedRectangle (buttonArea, 10.0f);

        auto currentFont =
            juce::FontOptions (getLookAndFeel().getTypefaceForFont (juce::FontOptions (
                                   static_cast<float> (buttonArea.getHeight()) * scaleFontSize,
                                   1)))
                .withHeight (buttonArea.getHeight() * scaleFontSize);
        g.setFont (currentFont);
        g.setColour (findColour (getToggleState() ? juce::TextButton::textColourOnId
                                                  : juce::TextButton::textColourOffId)
                         .withMultipliedAlpha (isEnabled() ? 1.0f : 0.5f));

        g.setColour (ticked ? findColour (juce::ResizableWindow::backgroundColourId)
                            : findColour (juce::ToggleButton::tickColourId)
                                  .withMultipliedAlpha (isMouseOverButton ? 0.7f : 0.5f));

        g.drawText (getButtonText(), getLocalBounds(), juce::Justification::centred);
    }

    void resized() override {}

    void setScaleFontSize (const float newScale) { scaleFontSize = newScale; }

    void setCircularShape (bool shouldBeCircularShape) { isCircularShape = shouldBeCircularShape; }

private:
    bool isCircularShape = true;
    float scaleFontSize = 1.0f;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RoundButton)
};