-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radio.pde
154 lines (134 loc) · 4.1 KB
/
Radio.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
//
// IceTimer - Graphical timer and match scheduler for pick-up ice hockey
// Copyright (C) 2014 Joe Cridge
//
// This file is part of IceTimer.
//
// IceTimer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// IceTimer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with IceTimer. If not, see <http://www.gnu.org/licenses/>.
//
// Joe Cridge, June 2014.
// <[email protected]>
//
class Radio {
final int DEACTIVATED = 0;
final int DESELECTED = 1;
final int SELECTED = 2;
int xPos, yPos, value, nOptions;
int[] states;
String[] options;
boolean isActive, changeMade;
color activeColour, inactiveColour;
PFont radioFont;
Radio(int xPos_, int yPos_, String[] options_, int startValIndex, color activeColour_, color inactiveColour_) {
xPos = xPos_;
yPos = yPos_;
options = options_;
nOptions = options.length;
// Set states of options given initial value
value = startValIndex;
states = new int[nOptions];
for (int i = 0; i < nOptions; i++) {
if (i == value) {
states[i] = SELECTED;
} else {
states[i] = DESELECTED;
}
}
activeColour = activeColour_;
inactiveColour = inactiveColour_;
isActive = true;
changeMade = false;
radioFont = loadFont("fonts/SquarishSansCTRegular-24.vlw");
}
void disableOption(int index) {
// Doesn't deal with option if it is currently selected
// To disable a selected option, first change the selection
if (states[index] == DESELECTED) {
states[index] = DEACTIVATED;
}
}
void display() {
textAlign(LEFT);
textFont(radioFont);
for (int i = 0; i < nOptions; i++) {
// Draw text labels
if (states[i] == DEACTIVATED || !isActive) {
fill(inactiveColour);
} else {
fill(activeColour);
}
text(options[i], xPos+19, yPos + i*29);
// Draw option circles
noFill();
strokeWeight(2);
if (isActive && states[i] != DEACTIVATED && mouseX >= xPos-12 && mouseX <= xPos+144 && mouseY >= (yPos-18+i*29) && mouseY < (yPos+11+i*29)) {
stroke(activeColour);
} else {
stroke(inactiveColour);
}
ellipse(xPos, yPos-7 + i*29, 14, 14);
// Fill and highlight selected circle
if (states[i] == SELECTED) {
if (isActive) {
fill(activeColour);
stroke(activeColour);
} else {
fill(inactiveColour);
stroke(inactiveColour);
}
ellipse(xPos, yPos-7 + i*29, 14, 14);
}
noStroke();
}
}
void enableAll() {
// Leaves selected option unchanged
for (int i = 0; i < nOptions; i++) {
if (states[i] == DEACTIVATED) {
states[i] = DESELECTED;
}
}
}
void enableOption(int index) {
// Does not automatically select the option
if (states[index] == DEACTIVATED) {
states[index] = DESELECTED;
}
}
int getSelectedIndex() {
return value;
}
void respond(int clickX, int clickY) {
for (int i = 0; i < nOptions; i++) {
if (isActive && states[i] != DEACTIVATED && clickX >= xPos-12 && clickX <= xPos+144 && clickY >= (yPos-18+i*29) && clickY < (yPos+11+i*29)) {
// If a selectable option is clicked, select it
setSelected(i);
}
}
}
void setActiveColour(color newColour) {
activeColour = newColour;
}
void setSelected(int newValueIndex) {
//Act only if new option can be selected
if (states[newValueIndex] == DESELECTED) {
// Deselect old option
states[value] = DESELECTED;
//Select new option
value = newValueIndex;
states[value] = SELECTED;
changeMade = true;
}
}
}