1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 #pragma once
27 
28 
29 //==============================================================================
30 class IconButton  : public Button
31 {
32 public:
IconButton(String buttonName,Image imageToDisplay)33     IconButton (String buttonName, Image imageToDisplay)
34         : Button (buttonName),
35           iconImage (imageToDisplay)
36     {
37         setTooltip (buttonName);
38     }
39 
IconButton(String buttonName,Path pathToDisplay)40     IconButton (String buttonName, Path pathToDisplay)
41         : Button (buttonName),
42           iconPath (pathToDisplay),
43           iconImage (createImageFromPath (iconPath))
44     {
45         setTooltip (buttonName);
46     }
47 
setImage(Image newImage)48     void setImage (Image newImage)
49     {
50         iconImage = newImage;
51         repaint();
52     }
53 
setPath(Path newPath)54     void setPath (Path newPath)
55     {
56         iconImage = createImageFromPath (newPath);
57         repaint();
58     }
59 
setBackgroundColour(Colour backgroundColourToUse)60     void setBackgroundColour (Colour backgroundColourToUse)
61     {
62         backgroundColour = backgroundColourToUse;
63         usingNonDefaultBackgroundColour = true;
64     }
65 
setIconInset(int newIconInset)66     void setIconInset (int newIconInset)
67     {
68         iconInset = newIconInset;
69         repaint();
70     }
71 
paintButton(Graphics & g,bool isMouseOverButton,bool isButtonDown)72     void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
73     {
74         float alpha = 1.0f;
75 
76         if (! isEnabled())
77         {
78             isMouseOverButton = false;
79             isButtonDown = false;
80 
81             alpha = 0.2f;
82         }
83 
84         auto fill = isButtonDown ? backgroundColour.darker (0.5f)
85                                  : isMouseOverButton ? backgroundColour.darker (0.2f)
86                                                      : backgroundColour;
87 
88         auto bounds = getLocalBounds();
89 
90         if (isButtonDown)
91             bounds.reduce (2, 2);
92 
93         Path ellipse;
94         ellipse.addEllipse (bounds.toFloat());
95         g.reduceClipRegion (ellipse);
96 
97         g.setColour (fill.withAlpha (alpha));
98         g.fillAll();
99 
100         g.setOpacity (alpha);
101         g.drawImage (iconImage, bounds.reduced (iconInset).toFloat(), RectanglePlacement::fillDestination, false);
102     }
103 
104 private:
lookAndFeelChanged()105     void lookAndFeelChanged() override
106     {
107         if (! usingNonDefaultBackgroundColour)
108             backgroundColour = findColour (defaultButtonBackgroundColourId);
109 
110         if (iconPath != Path())
111             iconImage = createImageFromPath (iconPath);
112 
113         repaint();
114     }
115 
createImageFromPath(Path path)116     Image createImageFromPath (Path path)
117     {
118         Image image (Image::ARGB, 250, 250, true);
119         Graphics g (image);
120 
121         g.setColour (findColour (defaultIconColourId));
122 
123         g.fillPath (path, RectanglePlacement (RectanglePlacement::centred)
124                             .getTransformToFit (path.getBounds(), image.getBounds().toFloat()));
125 
126         return image;
127     }
128 
129     Path iconPath;
130     Image iconImage;
131     Colour backgroundColour { findColour (defaultButtonBackgroundColourId) };
132     bool usingNonDefaultBackgroundColour = false;
133     int iconInset = 2;
134 
135     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (IconButton)
136 };
137