WE Core
Loading...
Searching...
No Matches
ButtonV2.h
Go to the documentation of this file.
1/*
2 * File: ButtonV2.h
3 *
4 * Version: 1.0.0
5 *
6 * Created: 19/03/2019
7 *
8 * This file is part of WECore.
9 *
10 * WECore is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * WECore is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with WECore. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#pragma once
26
27#include "../JuceLibraryCode/JuceHeader.h"
28
30
40 template <typename BASE>
41 class ButtonV2 : public BASE {
42
43 public:
44 ButtonV2() = default;
45 virtual ~ButtonV2() = default;
46
48 inline virtual void drawButtonBackground(juce::Graphics& g,
49 juce::Button& button,
50 const juce::Colour& backgroundColour,
51 bool isMouseOverButton,
52 bool isButtonDown) override;
53
54 inline virtual void drawButtonText(juce::Graphics& g,
55 juce::TextButton& textButton,
56 bool isMouseOverButton,
57 bool isButtonDown) override;
60 private:
61 static constexpr float _disabledDarker {0.7f};
62 };
63
64 template <typename BASE>
66 juce::Button& button,
67 const juce::Colour& /*backgroundColour*/,
68 bool /*isMouseOverButton*/,
69 bool /*isButtonDown*/) {
70 const int width {button.getWidth()};
71 const int height {button.getHeight()};
72
73 constexpr float indent {2.0f};
74 const int cornerSize {juce::jmin(juce::roundToInt(width * 0.4f),
75 juce::roundToInt(height * 0.4f))};
76
77 juce::Path p;
78 juce::PathStrokeType pStroke(1);
79
80 if (button.isEnabled()) {
81 if (button.getToggleState()) {
82 g.setColour(button.findColour(juce::TextButton::buttonOnColourId));
83 } else {
84 g.setColour(button.findColour(juce::TextButton::buttonColourId));
85 }
86 } else {
87 g.setColour(button.findColour(juce::TextButton::buttonColourId).darker(_disabledDarker));
88 }
89
90 p.addRoundedRectangle(indent,
91 indent,
92 width - 2 * indent,
93 height - 2 * indent,
94 static_cast<float>(cornerSize));
95
96 g.strokePath(p, pStroke);
97 }
98
99 template <typename BASE>
100 void ButtonV2<BASE>::drawButtonText(juce::Graphics& g,
101 juce::TextButton& textButton,
102 bool /*isMouseOverButton*/,
103 bool /*isButtonDown*/) {
104 if (textButton.isEnabled()) {
105 if (textButton.getToggleState()) {
106 g.setColour(textButton.findColour(juce::TextButton::textColourOnId));
107 } else {
108 g.setColour(textButton.findColour(juce::TextButton::textColourOffId));
109 }
110 } else {
111 g.setColour(textButton.findColour(juce::TextButton::textColourOffId).darker(_disabledDarker));
112 }
113
114 constexpr int MARGIN {0};
115
116 juce::Font font;
117 font.setTypefaceName(BASE::getTypefaceForFont(font)->getName());
118 g.setFont(font);
119
120 g.drawFittedText(textButton.getButtonText(),
121 MARGIN,
122 0,
123 textButton.getWidth() - 2 * MARGIN,
124 textButton.getHeight(),
125 juce::Justification::centred,
126 0);
127 }
128}
static constexpr float _disabledDarker
Definition ButtonV2.h:61
virtual void drawButtonBackground(juce::Graphics &g, juce::Button &button, const juce::Colour &backgroundColour, bool isMouseOverButton, bool isButtonDown) override
Definition ButtonV2.h:65
virtual void drawButtonText(juce::Graphics &g, juce::TextButton &textButton, bool isMouseOverButton, bool isButtonDown) override
Definition ButtonV2.h:100