forked from jcjones/SCHUA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.pde
More file actions
186 lines (159 loc) · 4.68 KB
/
UI.pde
File metadata and controls
186 lines (159 loc) · 4.68 KB
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
/*
* SCHUA: The SCHooling User-interactive Aquarium
* - Created for Paul Fishwick's CAP5805 "Simulation Concepts" course at the
* University of Florida in November 2005
* Copyright (c) 2005 James C. Jones <jcjones AT ufl DOT edu>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class Widget
{
protected String label;
int Xpos, Ypos, Width, Height;
PFont font;
int fontSz;
int padding = 5;
public boolean mouseIn(int mx, int my) {
return ( abs(mx-Xpos)<(Width/2) && abs(my-Ypos)<(Height/2) );
}
public void move(int x, int y) {
Xpos = x; Ypos = y;
}
public void resize(int w, int h) {
Width = w; Height = h;
}
public void update(int mx, int my) {
}
public void draw(int mx, int my) {
}
public void setLabel(String t) {
label = t;
}
public void setFont(PFont f, int s) {
font = f;
fontSz = s;
}
}
public class Label extends Widget
{
color TextColor;
int alignment;
public Label (int x, int y, int Wid, int Hig, color tc, int a)
{
Xpos = x; Ypos = y; Width = Wid; Height = Hig; TextColor = tc;
alignment = a;
label = "";
font = smallFont;
fontSz = 12;
}
public void draw(int mx, int my)
{
stroke(TextColor);
fill(TextColor);
textFont(font, fontSz);
textAlign(alignment);
text(label, Xpos+padding, Ypos+padding, Width-padding, Height-padding);
textAlign(CENTER);
}
}
public class Button extends Widget
{
color Base;
color Clicked;
color Mouseover;
public Button(int x, int y, int w, int h, color b, color c, color mo)
{
Xpos = x; Ypos = y; Width = w; Height = h; Base = b; Clicked = c; Mouseover = mo;
label = "";
font = smallFont;
fontSz = 12;
}
public void draw(int mx, int my)
{
if (mouseIn(mx,my)) {
if (currentlyActiveWidget == this ||
(currentlyActiveWidget == null && mousePressed && mouseButton == LEFT)) {
currentlyActiveWidget = this;
fill(Clicked);
} else {
fill(Mouseover);
}
} else {
fill(Base);
}
rect((float)Xpos-(Width/2), (float)Ypos-(Height/2), (float)Width, (float)Height);
fill(255,255,255);
textFont(font, fontSz);
text(label, Xpos-(Width/2)+(.6*padding), Ypos-(Height/2)+padding, Width-padding, Height-padding);
}
}
public class Slider extends Widget
{
float minValue;
float maxValue;
double Value;
int boxSize;
boolean isHorizontal;
Button button;
public Slider (int xi, int yi, int wi, int bw, int bh, double v, color b, color c, color mo, boolean hz) {
Xpos = xi;
Ypos = yi;
Height = bh;
Width = wi;
Value = v;
isHorizontal=hz;
button = new Button(0, 0, bw, bh, b, c, mo);
if (isHorizontal)
boxSize = bh/2;
else
boxSize = bw/2;
label = "";
font = smallFont;
fontSz = 12;
}
public void draw(int mx, int my)
{
stroke(255,255,255);
if (isHorizontal) {
line(Xpos, Ypos, Xpos+Width, Ypos);
button.move(Xpos+(int)(Width*Value), Ypos);
}
else {
line(Xpos, Ypos, Xpos, Ypos+Width);
button.move(Xpos, Ypos+(int)(Width*Value));
}
button.draw(mx, my);
if (currentlyActiveWidget == button)
currentlyActiveWidget = this;
stroke(0);
fill(255,255,255);
textFont(font, fontSz);
text(label, Xpos+padding, Ypos+padding, Width-padding, Height-padding);
}
public void update(int mx, int my) {
if (isHorizontal)
Value = (double)(mx-Xpos-boxSize)/Width;
else
Value = (double)(my-Ypos-boxSize)/Width;
if (Value > 1.0) Value = 1.0;
if (Value < 0.0) Value = 0.0;
}
public float getValue() {
return (float)Value;
}
}