forked from FionaWright/High5ive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW_RadioButton.pde
287 lines (260 loc) · 7.67 KB
/
W_RadioButton.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
284
285
286
287
/**
* A. Robertson
*
* Represents a group of radio buttons in the user interface.
*/
class RadioButtonGroupTypeUI extends WidgetGroupType {
public RadioButtonGroupTypeUI() {
super();
}
/**
* A. Robertson
*
* Adds a radio button as a member to the group and sets up event handling for it.
*
* @param rb The radio button to add to the group.
*/
public void addMember(RadioButtonUI rb) {
Members.add(rb);
rb.getOnClickEvent().addHandler(e -> memberClicked(e));
}
/**
* A. Robertson
*
* Handles the click event of a radio button by unchecking all other members of the group.
*
* @param e The event information associated with the click event.
*/
private void memberClicked(EventInfoType e) {
for (Widget member : Members) {
RadioButtonUI rb = (RadioButtonUI)member;
if (rb != e.Widget) {
rb.setChecked(false);
}
}
}
}
/**
* A. Robertson
*
* Represents a radio button user interface element that can be clicked to toggle its state.
*/
class RadioButtonUI extends Widget implements IClickable {
private EventType<EventInfoType> m_onClickEvent;
private EventType<EventInfoType> m_onCheckedEvent;
private LabelUI m_label;
protected boolean m_checked;
private boolean m_uncheckable;
private color m_checkedColour = DEFAULT_RADIOBUTTON_CHECKED_COLOUR;
private boolean m_drawCircle = true;
/**
* A. Robertson
*
* Initialises a radio button element.
*
* @param posX The x-coordinate of the top-left corner of the radio button.
* @param posY The y-coordinate of the top-left corner of the radio button.
* @param scaleX The horizontal scale of the radio button.
* @param scaleY The vertical scale of the radio button.
* @param label The label text to display next to the radio button.
*/
public RadioButtonUI(int posX, int posY, int scaleX, int scaleY, String label) {
super(posX, posY, scaleX, scaleY);
m_label = new LabelUI(posX + scaleY, posY, scaleX - scaleY, scaleY, label);
m_label.setForegroundColour(DEFAULT_TEXT_COLOUR_OUTSIDE);
m_onCheckedEvent = new EventType<EventInfoType>();
m_onClickEvent = new EventType<EventInfoType>();
m_onClickEvent.addHandler(e -> {
RadioButtonUI box = (RadioButtonUI)e.Widget;
if (!box.getChecked()) {
m_onCheckedEvent.raise(e);
box.setChecked(true);
} else if (m_uncheckable) {
box.setChecked(false);
} else {
box.setChecked(true);
}
}
);
}
/**
* A. Robertson
*
* Draws the radio button on the screen.
*/
@ Override
public void draw() {
super.draw();
if (m_drawCircle) {
ellipseMode(RADIUS);
fill(color(m_checked ? m_checkedColour : m_backgroundColour));
circle(m_pos.x + m_scale.y / 2, m_pos.y + m_scale.y / 2, m_scale.y / 2);
}
m_label.draw();
}
/**
* A. Robertson
*
* Returns the event type for click events.
*
* @return The event type for click events.
*/
public EventType<EventInfoType> getOnClickEvent() {
return m_onClickEvent;
}
/**
* A. Robertson
*
* Sets whether the radio button can be unchecked.
*
* @param uncheckable True if the radio button can be unchecked, false otherwise.
*/
public EventType<EventInfoType> getOnCheckedEvent() {
return m_onCheckedEvent;
}
/**
* A. Robertson
*
* Sets whether the radio button can be checked.
*
* @param uncheckable True if the radio button cant be checked, false otherwise.
*/
public void setUncheckable(boolean uncheckable) {
m_uncheckable = uncheckable;
}
/**
* A. Robertson
*
* Checks the radio button, triggering any associated events.
*/
public void check() {
m_onClickEvent.raise(new EventInfoType((int)m_pos.x, (int)m_pos.y, this));
}
/**
* A. Robertson
*
* Sets the checked state of the radio button.
*
* @param checked True to check the radio button, false to uncheck it.
*/
public void setChecked(boolean checked) {
m_checked = checked;
}
/**
* A. Robertson
*
* Returns whether the radio button is checked.
*
* @return True if the radio button is checked, false otherwise.
*/
public boolean getChecked() {
return m_checked;
}
/**
* A. Robertson
*
* Sets the text of the label displayed next to the radio button.
*
* @param text The text to set.
*/
public void setText(String text) {
m_label.setText(text);
}
/**
* A.Robertson
*
* Sets the button text size.
*
* @param textSize The size of the text.
* @throws IllegalArgumentException when the size argument is negative.
*/
public void setTextSize(int textSize) {
m_label.setTextSize(textSize);
}
/**
* A. Robertson
*
* Gets the label component displayed next to the radio button.
*
* @return The label component.
*/
public LabelUI getLabel() {
return m_label;
}
/**
* A. Robertson
*
* Sets whether to draw the radio button.
*
* @param enabled True to draw, false otherwise.
*/
public void setDrawCircle(boolean enabled) {
m_drawCircle = enabled;
}
}
/**
* F. Wright
*
* Extends the radio button element with an image changes depending on its state.
*/
class RadioImageButtonUI extends RadioButtonUI {
public PImage m_enabledImage;
public PImage m_disabledImage;
/**
* F. Wright
*
* Initialises a radio button element with an image.
*
* @param posX The x-coordinate of the top-left corner of the radio button.
* @param posY The y-coordinate of the top-left corner of the radio button.
* @param scaleX The horizontal scale of the radio button.
* @param scaleY The vertical scale of the radio button.
* @param label The label text to display next to the radio button.
* @param enImg The image to display when the radio button is enabled.
* @param disenImg The image to display when the radio button is disabled.
*/
public RadioImageButtonUI(int posX, int posY, int scaleX, int scaleY, String label, PImage enImg, PImage disenImg) {
super(posX, posY, scaleX, scaleY, label);
m_enabledImage = enImg;
m_disabledImage = disenImg;
setDrawCircle(false);
}
/**
* F. Wright
*
* Initialises a radio button element with an image, using the image's file path.
* as opposed to a PImage variable
*
* @param posX The x-coordinate of the top-left corner of the radio button.
* @param posY The y-coordinate of the top-left corner of the radio button.
* @param scaleX The horizontal scale of the radio button.
* @param scaleY The vertical scale of the radio button.
* @param label The label text to display next to the radio button.
* @param enImgPath The path to image to display when the radio button is enabled.
* @param disenImgPath The path to the image to display when the radio button is disabled.
*/
public RadioImageButtonUI(int posX, int posY, int scaleX, int scaleY, String label, String enImgPath, String disenImgPath) {
super(posX, posY, scaleX, scaleY, label);
m_enabledImage = loadImage(enImgPath);
m_disabledImage = loadImage(disenImgPath);
setDrawCircle(false);
}
/**
* F. Wright
*
* Draws the radio button with images on the screen.
*/
@Override
public void draw() {
super.draw();
fill(m_backgroundColour);
int scaleOffset = (int)((m_scale.x * 1.1f - m_scale.x) * 0.5f);
rect(m_pos.x - scaleOffset, m_pos.y - scaleOffset, m_scale.x * 1.1f, m_scale.y * 1.1f, DEFAULT_WIDGET_ROUNDNESS_1);
if (m_checked)
image(m_enabledImage, m_pos.x, m_pos.y, m_scale.x, m_scale.y);
else
image(m_disabledImage, m_pos.x, m_pos.y, m_scale.x, m_scale.y);
}
}
// Code authorship
// A. Robertson, Created a radiobutton widget, 12pm 04/03/24