-
Notifications
You must be signed in to change notification settings - Fork 19
/
ofGui.h
352 lines (279 loc) · 7.07 KB
/
ofGui.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma mark once
//#include "ofProperty.h"
#include "ofMain.h"
class ofBaseGui{
public:
ofBaseGui(){
bGuiActive = false;
}
virtual void mouseMoved(ofMouseEventArgs & args) = 0;
virtual void mousePressed(ofMouseEventArgs & args) = 0;
virtual void mouseDragged(ofMouseEventArgs & args) = 0;
virtual void mouseReleased(ofMouseEventArgs & args) = 0;
virtual void setValue(float mx, float my, bool bCheckBounds) = 0;
virtual void draw() = 0;
string name;
unsigned long currentFrame;
ofRectangle b;
bool bGuiActive;
};
class ofGuiCollection : public ofBaseGui{
public:
void setup(string collectionName, float x, float y){
name = collectionName;
b.x = x;
b.y = y;
header = 18;
spacing = 4;
currentY = header + spacing;
b.width = 200;
b.height = 100;
ofRegisterMouseEvents(this);
}
void add(ofBaseGui * element){
collection.push_back( element );
element->b.y = currentY;
element->b.x = 0;
currentY += element->b.height + spacing;
if( currentY >= b.height ){
b.height += 40;
}
}
void clear(){
collection.clear();
}
virtual void mouseMoved(ofMouseEventArgs & args){
ofMouseEventArgs a = args;
a.x -= b.x;
a.y -= b.y;
for(int k = 0; k < collection.size(); k++){
collection[k]->mouseMoved(a);
}
}
virtual void mousePressed(ofMouseEventArgs & args){
setValue(args.x, args.y, true);
if( bGuiActive ){
ofMouseEventArgs a = args;
a.x -= b.x;
a.y -= b.y;
for(int k = 0; k < collection.size(); k++){
collection[k]->mousePressed(a);
}
}
}
virtual void mouseDragged(ofMouseEventArgs & args){
setValue(args.x, args.y, false);
if( bGuiActive ){
ofMouseEventArgs a = args;
a.x -= b.x;
a.y -= b.y;
for(int k = 0; k < collection.size(); k++){
collection[k]->mouseDragged(a);
}
}
}
virtual void mouseReleased(ofMouseEventArgs & args){
bGuiActive = false;
for(int k = 0; k < collection.size(); k++){
ofMouseEventArgs a = args;
a.x -= b.x;
a.y -= b.y;
collection[k]->mouseReleased(a);
}
}
void draw(){
currentFrame = ofGetFrameNum();
ofPushStyle();
ofFill();
//ofSetColor(10, 10, 10, 170);
//ofRect(b);
ofSetColor(30, 30, 80);
ofRect(b.x, b.y, b.width, header);
ofSetColor(230);
ofDrawBitmapString(name, b.x + 4, b.y + 12);
ofPushMatrix();
ofTranslate(b.x, b.y, 0);
for(int k = 0; k < collection.size(); k++){
collection[k]->draw();
}
ofPopMatrix();
ofPopStyle();
}
protected:
void setValue(float mx, float my, bool bCheck){
if( ofGetFrameNum() - currentFrame > 1 ){
bGrabbed = false;
bGuiActive = false;
return;
}
if( bCheck ){
if( b.inside(mx, my) ){
bGuiActive = true;
if( my > b.y && my <= b.y + header ){
bGrabbed = true;
grabPt.set(mx-b.x, my-b.y);
}else{
bGrabbed = false;
}
}
}else if( bGrabbed ){
b.x = mx - grabPt.x;
b.y = my - grabPt.y;
}
}
ofPoint grabPt;
bool bGrabbed;
float currentY;
float spacing;
float header;
vector <ofBaseGui *> collection;
};
class ofSlider : public ofBaseGui{
friend class ofGuiCollection;
public:
void setup(string sliderName, double _val, double _min, double _max, bool _bInt = false, float width = 200, float height = 20){
name = sliderName;
val = _val;
min = _min;
max = _max;
b.x = 0;
b.y = 0;
b.width = width;
b.height = height;
currentFrame = 0;
bGuiActive = false;
bInt = _bInt;
}
virtual void mouseMoved(ofMouseEventArgs & args){
}
virtual void mousePressed(ofMouseEventArgs & args){
setValue(args.x, args.y, true);
}
virtual void mouseDragged(ofMouseEventArgs & args){
setValue(args.x, args.y, false);
}
virtual void mouseReleased(ofMouseEventArgs & args){
bGuiActive = false;
}
double getValue(){
if( bInt ){
return (int)val;
}
return val;
}
void draw(){
currentFrame = ofGetFrameNum();
ofFill();
ofSetColor(30, 30, 80);
ofRect(b);
float valAsPct = ofMap( val, min, max, 0, b.width, true );
ofEnableAlphaBlending();
ofSetColor(180, 180, 180);
ofRect(b.x+1, b.y+1, valAsPct-1, b.height-2);
ofSetColor(230, 230, 230, 255);
float stringY = b.y + 14;
ofDrawBitmapString(name, b.x + 4, stringY);
string valStr;
if( bInt ){
valStr = ofToString(val, 0);
}else{
valStr = ofToString(val, 2);
}
ofDrawBitmapString( valStr, (b.x + b.width) - 3 - valStr.length() * 8, stringY );
}
protected:
double val, min, max;
bool bInt;
void setValue(float mx, float my, bool bCheck){
if( ofGetFrameNum() - currentFrame > 1 ){
bGuiActive = false;
return;
}
if( bCheck ){
if( b.inside(mx, my) ){
bGuiActive = true;
}else{
bGuiActive = false;
}
}
if( bGuiActive ){
val = ofMap(mx, b.x, b.x + b.width, min, max, true);
}
}
};
class ofToggle : public ofBaseGui{
friend class ofGuiCollection;
public:
void setup(string toggleName, bool _bVal, float width = 200, float height = 20){
name = toggleName;
b.x = 0;
b.y = 0;
b.width = width;
b.height = height;
currentFrame = 0;
bGuiActive = false;
bVal = _bVal;
checkboxRect.width = b.height - 3;
checkboxRect.height = b.height - 3;
}
virtual void mouseMoved(ofMouseEventArgs & args){
}
virtual void mousePressed(ofMouseEventArgs & args){
setValue(args.x, args.y, true);
}
virtual void mouseDragged(ofMouseEventArgs & args){
}
virtual void mouseReleased(ofMouseEventArgs & args){
bGuiActive = false;
}
bool getValue(){
return bVal;
}
void draw(){
ofPushStyle();
currentFrame = ofGetFrameNum();
ofSetColor(30, 30, 80);
ofRect(b);
checkboxRect.x = b.x + 2;
checkboxRect.y = b.y + 1;
ofNoFill();
ofSetColor(180, 180, 180);
ofRect(checkboxRect);
if( bVal ){
ofFill();
ofSetColor(180, 180, 180);
ofRect(checkboxRect);
ofSetColor(30, 30, 80);
ofLine(checkboxRect.x, checkboxRect.y, checkboxRect.x + checkboxRect.width, checkboxRect.y + checkboxRect.height);
ofLine(checkboxRect.x, checkboxRect.y+ checkboxRect.height, checkboxRect.x + checkboxRect.width, checkboxRect.y);
}
ofSetColor(230, 230, 230, 255);
float stringY = b.y + 14;
ofDrawBitmapString(name, b.x + 4 + checkboxRect.width, stringY);
string valStr = ofToString(bVal, 0);
ofDrawBitmapString( valStr, (b.x + b.width) - 3 - valStr.length() * 8, stringY );
ofPopStyle();
}
protected:
bool bVal;
ofRectangle checkboxRect;
void setValue(float mx, float my, bool bCheck){
if( ofGetFrameNum() - currentFrame > 1 ){
bGuiActive = false;
return;
}
if( bCheck ){
if( b.inside(mx, my) ){
bGuiActive = true;
}else{
bGuiActive = false;
}
}
if( bGuiActive ){
bVal = !bVal;
}
}
};
class ofColorSlider : public ofBaseGui{
public:
};