-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggle.java
More file actions
93 lines (74 loc) · 2.82 KB
/
Toggle.java
File metadata and controls
93 lines (74 loc) · 2.82 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
package uk.co.ctdev.uiobjects;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
/**
* A Toggle is an implementation of the Google Material Design Toggle widget. It has two states
* either on or off and can be used to represent this for anything with those values.
** <p>
* {@link ChangeListener.ChangeEvent} is fired when the progress bar knob is moved. Cancelling the event will move the knob to where it was
* previously.
* <p>
* The preferred height of a progress bar is determined by the larger of the knob and background. The preferred width of progress
* bar is 140, a relatively arbitrary size.
*/
public class Toggle extends Slider {
private boolean isOn;
public Toggle(boolean vertical, Skin skin) {
this(vertical, skin.get("default-" + (vertical ? "vertical" : "horizontal"), ToggleStyle.class));
}
public Toggle(boolean vertical, Skin skin, String styleName) {
this(vertical, skin.get(styleName, ToggleStyle.class));
}
public Toggle(boolean vertical, ToggleStyle style) {
super(0, 1, 1, vertical, style);
addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
isOn = Toggle.this.getValue() == 1;
}
});
}
/** Returns the toggle's style. Modifying the returned style may not have an effect until {@link #setStyle(ToggleStyle)} is
* called. */
public ToggleStyle getStyle(){
return (ToggleStyle)super.getStyle();
}
public void setStyle(ToggleStyle style){
if (style == null) throw new NullPointerException("style cannot be null");
if (!(style instanceof ToggleStyle)) throw new IllegalArgumentException("style must be a ToggleStyle.");
super.setStyle(style);
}
protected Drawable getKnobDrawable(){
ToggleStyle style = getStyle();
return isOn ? style.knobOn : style.knob;
}
public boolean isOn(){
return isOn;
}
public void setIsOn(boolean isOn){
if(isOn) {
this.setValue(1);
}else{
this.setValue(0);
}
this.isOn = isOn;
}
/** The style for a toggle, see {@link Toggle}.
*/
static public class ToggleStyle extends SliderStyle{
public Drawable knobOn;
public ToggleStyle () {
}
public ToggleStyle ( Drawable knobOn, Drawable background, Drawable knob) {
super(background, knob);
this.knobOn = knobOn;
}
public ToggleStyle (ToggleStyle style) {
super(style);
this.knobOn = style.knobOn;
}
}
}