tcr/java/src/game/gui/element/Toggle.java

52 lines
1.5 KiB
Java
Raw Normal View History

2025-03-11 14:09:49 +01:00
package game.gui.element;
import game.renderer.Drawing;
import game.util.Formatter;
import game.window.Button;
public class Toggle extends Element {
public static interface Callback {
void use(Toggle elem, boolean value);
}
private final Callback func;
private final boolean def;
private boolean value;
public Toggle(int x, int y, int w, int h, boolean def, boolean init, Callback callback, Formatter<Toggle> formatter) {
super(x, y, w, h, formatter);
this.func = callback;
this.def = def;
this.value = init;
this.formatText();
}
public Toggle(int x, int y, int w, int h, boolean def, boolean init, Callback callback, final String text) {
this(x, y, w, h, def, init, callback, new Formatter<Toggle>() {
public String use(Toggle elem) {
return String.format("%s: %s", text, elem.value ? "An" : "Aus");
}
});
}
public void mouse(Button btn, int x, int y, boolean ctrl, boolean shift) {
boolean prev = this.value;
if((this.value = (ctrl || (btn == Button.MOUSE_MIDDLE)) ? this.def : !this.value) != prev) {
// this.type = this.value != 0 ? ElemType.TOGGLE_ON : ElemType.TOGGLE_OFF;
// gui_update_style(this, 1);
this.r_dirty = true;
this.func.use(this, this.value);
this.formatText();
}
}
protected void drawBackground() {
if(this.value)
Drawing.drawGradient2Border(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, this.gm.style.brdr_top, this.gm.style.brdr_btm);
else
super.drawBackground();
}
}