-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.pde
285 lines (247 loc) · 7.12 KB
/
gui.pde
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
// -*- mode:java; c-basic-offset: 4; indent-tabs-mode: nil -*-
// Copyright (c) 2009 Jeremy English <[email protected]>
// Permission to use, copy, modify, distribute, and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. No representations are made
// about the suitability of this software for any purpose. It is
// provided "as is" without express or implied warranty.
// Created: 23-January-2009
class Pos{
private int x,y;
Pos(int x, int y){
this.x = x;
this.y = y;
}
int getX(){
return x;
}
int getY(){
return y;
}
}
class GuiFont{
private PFont pf;
private color c;
private int sz;
GuiFont(PFont pf, color c, int sz){
this.pf = pf;
this.c = c;
this.sz = sz;
}
void makeActive(){
textFont(pf,sz);
}
color getColor(){
return c;
}
int getSize(){
return sz;
}
PFont getFont(){
return pf;
}
}
interface ClickEvent {
void onClick(Object tag);
}
interface Drawable{
void draw();
}
interface MouseMove{
void onMove();
}
interface MouseDrag{
void onDrag();
}
interface GuiElement extends Drawable, ClickEvent, MouseMove, MouseDrag{
}
class Button implements GuiElement{
private String caption;
private Pos p;
private ClickEvent ce;
private int fs;
private color c;
private float w, h;
private int leftPad;
private int topPad;
private GuiFont gf;
private int clickTime;
private boolean lastOnButton;
private int onButtonTime;
private Object tag;
Button(String caption, Pos p, ClickEvent ce, GuiFont gf, color c){
this.caption = caption;
this.p = p;
this.ce = ce;
this.fs = gf.getSize();
this.c = c;
gf.makeActive();
this.w = textWidth(caption);
this.leftPad = round(w/10);
this.w = this.w + (2 * leftPad);
this.h = fs;
this.topPad = round(fs/10);
this.h = this.h + (2 * topPad);
this.gf = gf;
this.clickTime = 0;
this.lastOnButton = false;
this.onButtonTime = 0;
}
void draw(){
textAlign(LEFT);
gf.makeActive();
pushMatrix();
translate(p.getX(), p.getY());
int dct = millis() - clickTime;
int dob = millis() - onButtonTime;
int sOffset = round(h/10);
smooth();
strokeWeight(2);
strokeJoin(ROUND);
if (dob < 125){ //The mouse has just came over the button.
//Scale everthing up by the padding amount
//Draw the shadow
float nw = w + (2 * sOffset);
float nh = h + (2 * sOffset);
int nOffset = round(nh/10);
noStroke();
fill(0,0,0,50);
rect(nOffset-sOffset, nOffset-sOffset, nw, nh);
//Draw a rectangle
stroke(0);
fill(c);
rect(-sOffset,-sOffset, nw, nh);
//Draw the highlight
PImage img = createImage(round(nw), round(nh), ARGB);
for(int i=0; i < img.pixels.length; i++) {
img.pixels[i] = color(0xff, 0xff, 0xff, 100 - max(0,i % img.width));
}
image(img, -sOffset,-sOffset);
//Draw the text on top
fill(gf.getColor());
text(caption, leftPad, topPad + (fs - topPad));
}
else if (dct < 250){ //Has not been 1/4 of a second since the click. Draw the pushed down button
//Draw a rectangle
fill(c);
rect(sOffset,sOffset, w, h);
//Draw the highlight
PImage img = createImage(round(w), round(h), ARGB);
for(int i=0; i < img.pixels.length; i++) {
img.pixels[i] = color(0xff, 0xff, 0xff, 100 - max(0,i % img.width));
}
image(img, sOffset, sOffset);
//Draw the text on top
fill(gf.getColor());
text(caption, leftPad + sOffset, topPad + (fs - topPad) + sOffset);
}
else { //Draw the default button
//Draw the shadow
noStroke();
fill(0,0,0,50);
rect(sOffset, sOffset, w, h);
//Draw a rectangle
stroke(0);
fill(c);
rect(0,0, w, h);
//Draw the highlight
PImage img = createImage(round(w), round(h), ARGB);
for(int i=0; i < img.pixels.length; i++) {
img.pixels[i] = color(0xff, 0xff, 0xff, 100 - max(0,i % img.width));
}
image(img, 0,0);
//Draw the text on top
fill(gf.getColor());
text(caption, leftPad, topPad + (fs - topPad));
}
popMatrix();
}
private boolean mouseOnButton(){
int mx = mouseX;
int my = mouseY;
return (p.getX() < mx && mx < (p.getX() + w) && p.getY() < my && my < (p.getY() + h));
}
void onClick(Object tag){
if (mouseOnButton()){
clickTime = millis();
ce.onClick(tag);
}
}
void onMove(){
boolean ob = mouseOnButton();
if (ob && !lastOnButton){
onButtonTime = millis();
}
lastOnButton = ob;
}
void onDrag(){}
}
class Slider implements GuiElement{
private int x;
private int y;
private int w;
private int h;
private int value;
private int sx; //slider x
private int sy; //slider y
private int sw; //slider width
private int sh; //slider height;
private color c;
private boolean grabbed;
private float stepper;
//Value should be in the range of 0 to 100. 0 being all the way to the left.
Slider(int x, int y, int width, int height, int value, color c){
this.x = x;
this.y = y;
this.w = width;
this.h = height;
this.value = value;
this.sw = round(width / 10);
this.sh = height;
this.sy = y;
this.c = c;
grabbed = false;
stepper = (this.w - sw) / 100.0;
}
private void set_sx(){
sx = round((value * stepper) + x);
}
void draw(){
fill(0);
//Paint the end peices
rect(x,y,1,sh);
rect(w + x,y,1,sh);
//paint the slot for the slider
rect(x,y + (h/2),w,1);
//paint the slider
fill(c);
set_sx();
rect(sx,sy,sw,sh);
}
void onClick(Object tag){
int mx = mouseX;
int my = mouseY;
grabbed = false;
if (mousePressed && mouseButton == LEFT){
if (sx < mx && mx < (sx + sw) && my > sy && my < (sy + sh)){
grabbed = true;
}
}
}
void onMove(){}
void onDrag(){
int mx = mouseX;
if (grabbed){
if(x <= mx && mx <= ((w - sw) + x)){
int x1 = mx - x;
println(x1);
value = round(x1/stepper);
}
}
}
int getValue(){
return value;
}
}