aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/core/GuiElementBoolean.java
blob: ab1769330d67b72d68f1511486f80a4676f3402d (plain)
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
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.core;

import io.github.moulberry.notenoughupdates.core.util.lerp.LerpUtils;
import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils;
import io.github.moulberry.notenoughupdates.util.GuiTextures;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class GuiElementBoolean extends GuiElement {
	public int x;
	public int y;
	private Supplier<Boolean> value;
	private final int clickRadius;
	private final Consumer<Boolean> toggleCallback;

	private boolean previewValue;
	private int animation = 0;
	private long lastMillis = 0;

	private static final int xSize = 48;
	private static final int ySize = 14;

	public GuiElementBoolean(int x, int y, Supplier<Boolean> value, Consumer<Boolean> toggleCallback) {
		this(x, y, value, 0, toggleCallback);
	}

	public GuiElementBoolean(int x, int y, Supplier<Boolean> value, int clickRadius, Consumer<Boolean> toggleCallback) {
		this.x = x;
		this.y = y;
		this.value = value;
		this.previewValue = value.get();
		this.clickRadius = clickRadius;
		this.toggleCallback = toggleCallback;
		this.lastMillis = System.currentTimeMillis();

		if (previewValue) animation = 36;
	}

	@Override
	public void render() {
		GlStateManager.color(1, 1, 1, 1);
		Minecraft.getMinecraft().getTextureManager().bindTexture(GuiTextures.BAR);
		RenderUtils.drawTexturedRect(x, y, xSize, ySize);

		ResourceLocation buttonLoc = GuiTextures.ON;
		long currentMillis = System.currentTimeMillis();
		long deltaMillis = currentMillis - lastMillis;
		lastMillis = currentMillis;
		boolean passedLimit = false;
		if (previewValue != value.get()) {
			if ((previewValue && animation > 12) ||
				(!previewValue && animation < 24)) {
				passedLimit = true;
			}
		}
		if (previewValue != passedLimit) {
			animation += deltaMillis / 10;
		} else {
			animation -= deltaMillis / 10;
		}
		lastMillis -= deltaMillis % 10;

		if (previewValue == value.get()) {
			animation = Math.max(0, Math.min(36, animation));
		} else if (!passedLimit) {
			if (previewValue) {
				animation = Math.max(0, Math.min(12, animation));
			} else {
				animation = Math.max(24, Math.min(36, animation));
			}
		} else {
			if (previewValue) {
				animation = Math.max(12, animation);
			} else {
				animation = Math.min(24, animation);
			}
		}

		int animation = (int) (LerpUtils.sigmoidZeroOne(this.animation / 36f) * 36);
		if (animation < 3) {
			buttonLoc = GuiTextures.OFF;
		} else if (animation < 13) {
			buttonLoc = GuiTextures.ONE;
		} else if (animation < 23) {
			buttonLoc = GuiTextures.TWO;
		} else if (animation < 33) {
			buttonLoc = GuiTextures.THREE;
		}

		Minecraft.getMinecraft().getTextureManager().bindTexture(buttonLoc);
		RenderUtils.drawTexturedRect(x + animation, y, 12, 14);
	}

	@Override
	public boolean mouseInput(int mouseX, int mouseY) {
		if (mouseX > x - clickRadius && mouseX < x + xSize + clickRadius &&
			mouseY > y - clickRadius && mouseY < y + ySize + clickRadius) {
			if (Mouse.getEventButton() == 0) {
				if (Mouse.getEventButtonState()) {
					previewValue = !value.get();
				} else if (previewValue == !value.get()) {
					toggleCallback.accept(!value.get());
				}
			}
		} else {
			previewValue = value.get();
		}
		return false;
	}

	@Override
	public boolean keyboardInput() {
		return false;
	}
}