1 /*
2  * Copyright (C) 2018-2019 Rob van den Berg <rghvdberg at gmail dot org>
3  *
4  * This file is part of CharacterCompressor
5  *
6  * Nnjas2 is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * CharacterCompressor is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with CharacterCompressor.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "NanoButton.hpp"
21 #include "Window.hpp"
22 
23 START_NAMESPACE_DISTRHO
24 
Button(Widget * parent,Callback * cb)25 Button::Button(Widget *parent, Callback *cb)
26     : NanoWidget(parent),
27       fCallback(cb),
28       buttonActive(false)
29 {
30     loadSharedResources();
31     fNanoFont = findFont(NANOVG_DEJAVU_SANS_TTF);
32     labelColor = Color(255, 255, 255);
33     backgroundColor = Color(32,32,32);
34     Label = "button";
35 }
36 
onNanoDisplay()37 void Button::onNanoDisplay()
38 {
39     auto w = getWidth();
40     auto h = getHeight();
41     auto margin = 1.0f;
42 
43     // Background
44     beginPath();
45     fillColor(backgroundColor);
46     strokeColor(borderColor);
47     rect(margin, margin, w - 2 * margin, h-2*margin);
48     fill();
49     stroke();
50     closePath();
51 
52     //Label
53     beginPath();
54     fontFaceId(fNanoFont);
55     fontSize(14);
56     fillColor(labelColor);
57     Rectangle<float> bounds;
58     textBounds(0, 0, Label.c_str(), NULL, bounds);
59     // float tw = bounds.getWidth();
60     // float th = bounds.getHeight();
61     float tx = w / 2.0f ;
62     float ty = h / 2.0f;
63     textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
64 
65     fillColor(255, 255, 255, 255);
66     text(tx, ty, Label.c_str(), NULL);
67     closePath();
68 }
69 
setLabel(std::string label)70 void Button::setLabel(std::string label)
71 {
72     Label = label;
73 }
74 
setLabelColor(const Color color)75 void Button::setLabelColor(const Color color)
76 {
77     labelColor = color;
78     borderColor = color;
79 }
setBackgroundColor(const Color color)80 void Button::setBackgroundColor(const Color color)
81 {
82     backgroundColor = color;
83 }
84 
onMouse(const MouseEvent & ev)85 bool Button::onMouse(const MouseEvent &ev)
86 {
87     if (ev.press & contains(ev.pos))
88     {
89         buttonActive = true;
90         setLabelColor(labelColor);
91         fCallback->buttonClicked(this, true);
92         return true;
93     }
94     else if (buttonActive)
95     {
96         buttonActive = false;
97         //setLabelColor(Color(128,128,128));
98         return true;
99     }
100 
101     return false;
102 }
103 
104 END_NAMESPACE_DISTRHO
105