tcr/java/src/client/gui/element/Toggle.java
2025-05-04 20:27:55 +02:00

57 lines
1.9 KiB
Java

package client.gui.element;
import client.gui.Formatter;
import client.renderer.Drawing;
import client.window.Button;
import game.util.Util;
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();
this.playSound();
}
}
protected void drawBackground() {
if(this.value) {
if(this.enabled)
Drawing.drawGradientBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, this.gm.style.fill_btm, this.gm.style.fill_top, 0xff000000, this.gm.style.brdr_top, this.gm.style.brdr_btm);
else
Drawing.drawGradientBorder(this.pos_x, this.pos_y, this.size_x, this.size_y, Util.mulColor(this.gm.style.fill_btm, 0.5f), Util.mulColor(this.gm.style.fill_top, 0.5f), 0xff000000, Util.mulColor(this.gm.style.brdr_top, 0.5f), Util.mulColor(this.gm.style.brdr_btm, 0.5f));
}
else
super.drawBackground();
}
}