-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathofxButton.cpp
117 lines (94 loc) · 2.53 KB
/
ofxButton.cpp
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
116
117
#include "ofxButton.h"
ofxButton::ofxButton(){
value.setSerializable(false);
}
ofxButton::ofxButton(ofParameter<void> _bVal, float width, float height){
setup(_bVal, width, height);
}
ofxButton::~ofxButton(){
//
}
ofxButton* ofxButton::setup(ofParameter<void> _bVal, float width, float height){
parameter = _bVal;
value = false;
b.x = 0;
b.y = 0;
b.width = width;
b.height = height;
bGuiActive = false;
checkboxRect.set(1, 1, b.height - 2, b.height - 2);
registerMouseEvents();
value.addListener(this,&ofxButton::valueChanged);
return this;
}
ofxButton* ofxButton::setup(const std::string& toggleName, float width, float height){
setName(toggleName);
value = false;
b.x = 0;
b.y = 0;
b.width = width;
b.height = height;
bGuiActive = false;
checkboxRect.set(1, 1, b.height - 2, b.height - 2);
registerMouseEvents();
value.addListener(this,&ofxButton::valueChanged);
return this;
}
void ofxButton::generateDraw(){
bg.clear();
bg.setFillColor(thisBackgroundColor);
bg.rectangle(b);
fg.clear();
fg.setFilled(false);
fg.setStrokeWidth(1);
fg.setStrokeColor(thisFillColor);
fg.rectRounded(b.getPosition()+checkboxRect.getTopLeft()+glm::vec2{1,1},checkboxRect.width-2,checkboxRect.height-2, 5);
cross.clear();
cross.setStrokeColor(thisTextColor);
cross.setFillColor(thisFillColor);
cross.setStrokeWidth(2);
cross.setFilled(true);
cross.rectRounded(b.getPosition()+checkboxRect.getTopLeft()+glm::vec2{1,1},checkboxRect.width-2,checkboxRect.height-2, 5);
std::string name;
auto textX = b.x + textPadding + checkboxRect.width;
if(getTextBoundingBox(getName(), textX, 0).getMaxX() > b.getMaxX() - textPadding){
for(auto c: ofUTF8Iterator(getName())){
auto next = name;
ofUTF8Append(next, c);
if(getTextBoundingBox(next,textX,0).getMaxX() > b.getMaxX() - textPadding){
break;
}else{
name = next;
}
}
}else{
name = getName();
}
textMesh = getTextMesh(name, textX, getTextVCenteredInRect(b));
}
bool ofxButton::mouseReleased(ofMouseEventArgs & args){
bool attended = setValue(args.x, args.y, false);
bGuiActive = false;
if(attended){
return true;
}else{
return false;
}
}
bool ofxButton::mouseMoved(ofMouseEventArgs & args){
return ofxToggle::mouseMoved(args);
}
bool ofxButton::mousePressed(ofMouseEventArgs & args){
return ofxToggle::mousePressed(args);
}
bool ofxButton::mouseDragged(ofMouseEventArgs & args){
return ofxToggle::mouseDragged(args);
}
ofAbstractParameter & ofxButton::getParameter(){
return parameter;
}
void ofxButton::valueChanged(bool & v){
if(!v){
parameter.trigger();
}
}