forked from ThinkingStudios/ObsidianUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpruceSliderWidget.java
More file actions
203 lines (170 loc) · 5.32 KB
/
Copy pathSpruceSliderWidget.java
File metadata and controls
203 lines (170 loc) · 5.32 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
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
/*
* Copyright © 2020~2024 LambdAurora <email@lambdaurora.dev>
* Copyright © 2024 ThinkingStudio
*
* This file is part of ObsidianUI.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package org.thinkingstudio.obsidianui.widget;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.MathHelper;
import org.thinkingstudio.obsidianui.Position;
import org.thinkingstudio.obsidianui.Tooltipable;
import org.thinkingstudio.obsidianui.navigation.NavigationDirection;
import java.util.function.Consumer;
/**
* Represents a slider widget.
*
* @author LambdAurora
* @version 3.3.0
* @since 1.0.0
*/
public class SpruceSliderWidget extends AbstractSpruceButtonWidget implements Tooltipable {
private Text baseMessage;
protected double value;
private final Consumer<SpruceSliderWidget> applyConsumer;
private double multiplier;
private String sign;
private boolean inUse = false;
private static final Identifier SLIDER = Identifier.ofVanilla("widget/slider");
private static final Identifier SLIDER_HANDLE = Identifier.ofVanilla("widget/slider_handle");
private static final Identifier SLIDER_HANDLE_HIGHLIGHTED = Identifier.ofVanilla("widget/slider_handle_highlighted");
public SpruceSliderWidget(Position position, int width, int height, Text message, double value, Consumer<SpruceSliderWidget> applyConsumer, double multiplier, String sign) {
super(position, width, height, message);
this.value = value;
this.baseMessage = message;
this.applyConsumer = applyConsumer;
this.multiplier = multiplier;
this.sign = sign;
this.updateMessage();
}
public SpruceSliderWidget(Position position, int width, int height, Text message, double progress, Consumer<SpruceSliderWidget> applyConsumer) {
this(position, width, height, message, progress, applyConsumer, 100.0, "%");
}
/**
* Gets the value of the slider.
*
* @return the value of the slider
*/
public double getValue() {
return this.value;
}
/**
* Sets the value of the slider.
*
* @param value the value of the slider
*/
private void setValue(double value) {
double oldValue = this.value;
this.value = MathHelper.clamp(value, 0.0D, 1.0D);
if (oldValue != this.value) {
this.applyValue();
}
this.updateMessage();
}
/**
* Returns the value of this slider as an integer.
*
* @return the value as an integer
*/
public int getIntValue() {
return (int) (this.value * this.multiplier);
}
/**
* Sets the value of this slider.
*
* @param value the new value as an integer
*/
public void setIntValue(int value) {
this.setValue(value / this.multiplier);
}
/**
* Gets the base message of the slider.
*
* @return the base message of the slider
*/
public Text getBaseMessage() {
return this.baseMessage;
}
/**
* Sets the base message of the slider.
*
* @param baseMessage the base message of the slider
*/
public void setBaseMessage(Text baseMessage) {
this.baseMessage = baseMessage;
}
protected void updateMessage() {
this.setMessage(this.baseMessage.copy().append(": " + this.getIntValue() + sign));
}
protected void applyValue() {
this.applyConsumer.accept(this);
}
/* Navigation */
@Override
public boolean onNavigation(NavigationDirection direction, boolean tab) {
if (direction.isHorizontal() && !tab) {
if (direction.isLookingForward() && this.value < 1 || this.value > 0) {
this.setValue(this.getValue() + (direction.isLookingForward() ? (1 / this.multiplier) : -(1 / this.multiplier)));
return true;
}
}
return super.onNavigation(direction, tab);
}
/* Input */
@Override
protected void onClick(double mouseX, double mouseY) {
this.setValueFromMouse(mouseX);
this.inUse = true;
}
@Override
protected void onRelease(double mouseX, double mouseY) {
if (this.inUse) {
this.playDownSound();
this.inUse = false;
}
}
@Override
protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) {
this.setValueFromMouse(mouseX);
this.inUse = true;
}
private void setValueFromMouse(double mouseX) {
this.setValue((mouseX - (double) (this.getX() + 4)) / (double) (this.getWidth() - 8));
}
/* Rendering */
@Override
protected Identifier getTexture() {
return SLIDER;
}
@Override
protected void renderButton(DrawContext drawContext, int mouseX, int mouseY, float delta) {
final Identifier texture = this.isFocusedOrHovered() ? SLIDER_HANDLE_HIGHLIGHTED : SLIDER_HANDLE;
drawContext.drawGuiTexture(RenderPipelines.GUI_TEXTURED, texture, this.getX() + (int) (this.value * (double) (this.getWidth() - 8)), this.getY(), 8, 20);
if (!this.isMouseHovered() && this.inUse) {
this.inUse = false;
}
super.renderButton(drawContext, mouseX, mouseY, delta);
}
/* Narration */
@Override
protected Text getNarrationMessage() {
return Text.translatable("gui.narrate.slider", this.getMessage());
}
@Override
protected Text getNarrationFocusedUsageMessage() {
return Text.translatable("narration.slider.usage.focused");
}
@Override
protected Text getNarrationHoveredUsageMessage() {
return Text.translatable("narration.slider.usage.hovered");
}
}