CycleButton .java

net.minecraft.client.gui.components.CycleButton (class): members: createDefaultNarrationMessage, DEFAULT_ALT_LIST_SELECTOR, defaultValueSupplier, onPress.

net.minecraft.client.gui.componentsclass340 linesclientextends AbstractButton12.0 KB

Implements

API Surface (26)

void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void onPress(final InputWithModifiers input)
boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY)
void setValue(final T newValue)
void resetValue()
T getValue()
MutableComponent createNarrationMessage()
void updateWidgetNarration(final NarrationElementOutput output)
MutableComponent createDefaultNarrationMessage()
<T> CycleButton.Builder<T> builder(final Function<T, Component> valueStringifier, final Supplier<T> defaultValueSupplier)
<T> CycleButton.Builder<T> builder(final Function<T, Component> valueStringifier, final T defaultValue)
CycleButton.Builder<Boolean> booleanBuilder(final Component trueText, final Component falseText, final boolean defaultValue)
CycleButton.Builder<Boolean> onOffBuilder(final boolean initialValue)
CycleButton.Builder<T> withValues(final Collection<T> values)
CycleButton.Builder<T> withValues(final T... values)
CycleButton.Builder<T> withValues(final List<T> values, final List<T> altValues)
CycleButton.Builder<T> withValues(final BooleanSupplier altCondition, final List<T> values, final List<T> altValues)
CycleButton.Builder<T> withValues(final CycleButton.ValueListSupplier<T> valueListSupplier)
CycleButton.Builder<T> withTooltip(final OptionInstance.TooltipSupplier<T> tooltipSupplier)
CycleButton.Builder<T> withCustomNarration(final Function<CycleButton<T>, MutableComponent> narrationProvider)
CycleButton.Builder<T> withSprite(final CycleButton.SpriteSupplier<T> spriteSupplier)
CycleButton.Builder<T> displayState(final CycleButton.DisplayState state)
CycleButton.Builder<T> displayOnlyValue()
CycleButton<T> create(final Component name, final CycleButton.OnValueChange<T> valueChangeListener)
CycleButton<T> create(final int x, final int y, final int width, final int height, final Component name)
CycleButton<T> create(final int x, final int y, final int width, final int height, final Component name, final CycleButton.OnValueChange<T> valueChangeListener)

Fields & Constants (18)

DEFAULT_ALT_LIST_SELECTOR
BOOLEAN_OPTIONS
defaultValueSupplier
name
index
value
values
valueStringifier
narrationProvider
onValueChange
displayState
tooltipSupplier
spriteSupplier
x
y
width
height
message

String literals (3)

"narration.cycle_button.usage.focused"
"narration.cycle_button.usage.hovered"
"No values for cycle button"

Full Source

CycleButton.java340 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components;
2
3
import com.google.common.collect.ImmutableList;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.function.BooleanSupplier;
7
import java.util.function.Function;
8
import java.util.function.Supplier;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.client.Minecraft;
12
import net.minecraft.client.OptionInstance;
13
import net.minecraft.client.gui.GuiGraphicsExtractor;
14
import net.minecraft.client.gui.components.CycleButton.ValueListSupplier.1;
15
import net.minecraft.client.gui.components.CycleButton.ValueListSupplier.2;
16
import net.minecraft.client.gui.narration.NarratedElementType;
17
import net.minecraft.client.gui.narration.NarrationElementOutput;
18
import net.minecraft.client.input.InputWithModifiers;
19
import net.minecraft.client.renderer.RenderPipelines;
20
import net.minecraft.network.chat.CommonComponents;
21
import net.minecraft.network.chat.Component;
22
import net.minecraft.network.chat.MutableComponent;
23
import net.minecraft.resources.Identifier;
24
import net.minecraft.util.Mth;
25
import org.jspecify.annotations.Nullable;
26
27
@Environment(EnvType.CLIENT)
28
public class CycleButton<T> extends AbstractButton implements ResettableOptionWidget {
29
	public static final BooleanSupplier DEFAULT_ALT_LIST_SELECTOR = () -> Minecraft.getInstance().hasAltDown();
30
	private static final List<Boolean> BOOLEAN_OPTIONS = ImmutableList.of(Boolean.TRUE, Boolean.FALSE);
31
	private final Supplier<T> defaultValueSupplier;
32
	private final Component name;
33
	private int index;
34
	private T value;
35
	private final CycleButton.ValueListSupplier<T> values;
36
	private final Function<T, Component> valueStringifier;
37
	private final Function<CycleButton<T>, MutableComponent> narrationProvider;
38
	private final CycleButton.OnValueChange<T> onValueChange;
39
	private final CycleButton.DisplayState displayState;
40
	private final OptionInstance.TooltipSupplier<T> tooltipSupplier;
41
	private final CycleButton.SpriteSupplier<T> spriteSupplier;
42
43
	private CycleButton(
44
		final int x,
45
		final int y,
46
		final int width,
47
		final int height,
48
		final Component message,
49
		final Component name,
50
		final int index,
51
		final T value,
52
		final Supplier<T> defaultValueSupplier,
53
		final CycleButton.ValueListSupplier<T> values,
54
		final Function<T, Component> valueStringifier,
55
		final Function<CycleButton<T>, MutableComponent> narrationProvider,
56
		final CycleButton.OnValueChange<T> onValueChange,
57
		final OptionInstance.TooltipSupplier<T> tooltipSupplier,
58
		final CycleButton.DisplayState displayState,
59
		final CycleButton.SpriteSupplier<T> spriteSupplier
60
	) {
61
		super(x, y, width, height, message);
62
		this.name = name;
63
		this.index = index;
64
		this.defaultValueSupplier = defaultValueSupplier;
65
		this.value = value;
66
		this.values = values;
67
		this.valueStringifier = valueStringifier;
68
		this.narrationProvider = narrationProvider;
69
		this.onValueChange = onValueChange;
70
		this.displayState = displayState;
71
		this.tooltipSupplier = tooltipSupplier;
72
		this.spriteSupplier = spriteSupplier;
73
		this.updateTooltip();
74
	}
75
76
	@Override
77
	protected void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
78
		Identifier sprite = this.spriteSupplier.apply(this, this.getValue());
79
		if (sprite != null) {
80
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), this.getWidth(), this.getHeight());
81
		} else {
82
			this.extractDefaultSprite(graphics);
83
		}
84
85
		if (this.displayState != CycleButton.DisplayState.HIDE) {
86
			this.extractDefaultLabel(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE));
87
		}
88
	}
89
90
	private void updateTooltip() {
91
		this.setTooltip(this.tooltipSupplier.apply(this.value));
92
	}
93
94
	@Override
95
	public void onPress(final InputWithModifiers input) {
96
		if (input.hasShiftDown()) {
97
			this.cycleValue(-1);
98
		} else {
99
			this.cycleValue(1);
100
		}
101
	}
102
103
	private void cycleValue(final int delta) {
104
		List<T> list = this.values.getSelectedList();
105
		this.index = Mth.positiveModulo(this.index + delta, list.size());
106
		T newValue = list.get(this.index);
107
		this.updateValue(newValue);
108
		this.onValueChange.onValueChange(this, newValue);
109
	}
110
111
	private T getCycledValue(final int delta) {
112
		List<T> list = this.values.getSelectedList();
113
		return list.get(Mth.positiveModulo(this.index + delta, list.size()));
114
	}
115
116
	@Override
117
	public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
118
		if (scrollY > 0.0) {
119
			this.cycleValue(-1);
120
		} else if (scrollY < 0.0) {
121
			this.cycleValue(1);
122
		}
123
124
		return true;
125
	}
126
127
	public void setValue(final T newValue) {
128
		List<T> list = this.values.getSelectedList();
129
		int newIndex = list.indexOf(newValue);
130
		if (newIndex != -1) {
131
			this.index = newIndex;
132
		}
133
134
		this.updateValue(newValue);
135
	}
136
137
	@Override
138
	public void resetValue() {
139
		this.setValue(this.defaultValueSupplier.get());
140
	}
141
142
	private void updateValue(final T newValue) {
143
		Component newMessage = this.createLabelForValue(newValue);
144
		this.setMessage(newMessage);
145
		this.value = newValue;
146
		this.updateTooltip();
147
	}
148
149
	private Component createLabelForValue(final T newValue) {
150
		return this.displayState == CycleButton.DisplayState.VALUE ? this.valueStringifier.apply(newValue) : this.createFullName(newValue);
151
	}
152
153
	private MutableComponent createFullName(final T newValue) {
154
		return CommonComponents.optionNameValue(this.name, this.valueStringifier.apply(newValue));
155
	}
156
157
	public T getValue() {
158
		return this.value;
159
	}
160
161
	@Override
162
	protected MutableComponent createNarrationMessage() {
163
		return this.narrationProvider.apply(this);
164
	}
165
166
	@Override
167
	public void updateWidgetNarration(final NarrationElementOutput output) {
168
		output.add(NarratedElementType.TITLE, this.createNarrationMessage());
169
		if (this.active) {
170
			T nextValue = this.getCycledValue(1);
171
			Component nextValueText = this.createLabelForValue(nextValue);
172
			if (this.isFocused()) {
173
				output.add(NarratedElementType.USAGE, Component.translatable("narration.cycle_button.usage.focused", nextValueText));
174
			} else {
175
				output.add(NarratedElementType.USAGE, Component.translatable("narration.cycle_button.usage.hovered", nextValueText));
176
			}
177
		}
178
	}
179
180
	public MutableComponent createDefaultNarrationMessage() {
181
		return wrapDefaultNarrationMessage(this.displayState == CycleButton.DisplayState.VALUE ? this.createFullName(this.value) : this.getMessage());
182
	}
183
184
	public static <T> CycleButton.Builder<T> builder(final Function<T, Component> valueStringifier, final Supplier<T> defaultValueSupplier) {
185
		return new CycleButton.Builder<>(valueStringifier, defaultValueSupplier);
186
	}
187
188
	public static <T> CycleButton.Builder<T> builder(final Function<T, Component> valueStringifier, final T defaultValue) {
189
		return new CycleButton.Builder<>(valueStringifier, () -> defaultValue);
190
	}
191
192
	public static CycleButton.Builder<Boolean> booleanBuilder(final Component trueText, final Component falseText, final boolean defaultValue) {
193
		return new CycleButton.Builder<>(b -> b == Boolean.TRUE ? trueText : falseText, () -> defaultValue).withValues(BOOLEAN_OPTIONS);
194
	}
195
196
	public static CycleButton.Builder<Boolean> onOffBuilder(final boolean initialValue) {
197
		return new CycleButton.Builder<>(b -> b == Boolean.TRUE ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF, () -> initialValue)
198
			.withValues(BOOLEAN_OPTIONS);
199
	}
200
201
	@Environment(EnvType.CLIENT)
202
	public static class Builder<T> {
203
		private final Supplier<T> defaultValueSupplier;
204
		private final Function<T, Component> valueStringifier;
205
		private OptionInstance.TooltipSupplier<T> tooltipSupplier = value -> null;
206
		private CycleButton.SpriteSupplier<T> spriteSupplier = (button, value) -> null;
207
		private Function<CycleButton<T>, MutableComponent> narrationProvider = CycleButton::createDefaultNarrationMessage;
208
		private CycleButton.ValueListSupplier<T> values = CycleButton.ValueListSupplier.create(ImmutableList.of());
209
		private CycleButton.DisplayState displayState = CycleButton.DisplayState.NAME_AND_VALUE;
210
211
		public Builder(final Function<T, Component> valueStringifier, final Supplier<T> defaultValueSupplier) {
212
			this.valueStringifier = valueStringifier;
213
			this.defaultValueSupplier = defaultValueSupplier;
214
		}
215
216
		public CycleButton.Builder<T> withValues(final Collection<T> values) {
217
			return this.withValues(CycleButton.ValueListSupplier.create(values));
218
		}
219
220
		@SafeVarargs
221
		public final CycleButton.Builder<T> withValues(final T... values) {
222
			return this.withValues(ImmutableList.copyOf(values));
223
		}
224
225
		public CycleButton.Builder<T> withValues(final List<T> values, final List<T> altValues) {
226
			return this.withValues(CycleButton.ValueListSupplier.create(CycleButton.DEFAULT_ALT_LIST_SELECTOR, values, altValues));
227
		}
228
229
		public CycleButton.Builder<T> withValues(final BooleanSupplier altCondition, final List<T> values, final List<T> altValues) {
230
			return this.withValues(CycleButton.ValueListSupplier.create(altCondition, values, altValues));
231
		}
232
233
		public CycleButton.Builder<T> withValues(final CycleButton.ValueListSupplier<T> valueListSupplier) {
234
			this.values = valueListSupplier;
235
			return this;
236
		}
237
238
		public CycleButton.Builder<T> withTooltip(final OptionInstance.TooltipSupplier<T> tooltipSupplier) {
239
			this.tooltipSupplier = tooltipSupplier;
240
			return this;
241
		}
242
243
		public CycleButton.Builder<T> withCustomNarration(final Function<CycleButton<T>, MutableComponent> narrationProvider) {
244
			this.narrationProvider = narrationProvider;
245
			return this;
246
		}
247
248
		public CycleButton.Builder<T> withSprite(final CycleButton.SpriteSupplier<T> spriteSupplier) {
249
			this.spriteSupplier = spriteSupplier;
250
			return this;
251
		}
252
253
		public CycleButton.Builder<T> displayState(final CycleButton.DisplayState state) {
254
			this.displayState = state;
255
			return this;
256
		}
257
258
		public CycleButton.Builder<T> displayOnlyValue() {
259
			return this.displayState(CycleButton.DisplayState.VALUE);
260
		}
261
262
		public CycleButton<T> create(final Component name, final CycleButton.OnValueChange<T> valueChangeListener) {
263
			return this.create(0, 0, 150, 20, name, valueChangeListener);
264
		}
265
266
		public CycleButton<T> create(final int x, final int y, final int width, final int height, final Component name) {
267
			return this.create(x, y, width, height, name, (button, value) -> {});
268
		}
269
270
		public CycleButton<T> create(
271
			final int x, final int y, final int width, final int height, final Component name, final CycleButton.OnValueChange<T> valueChangeListener
272
		) {
273
			List<T> values = this.values.getDefaultList();
274
			if (values.isEmpty()) {
275
				throw new IllegalStateException("No values for cycle button");
276
			}
277
278
			T initialValue = this.defaultValueSupplier.get();
279
			int initialIndex = values.indexOf(initialValue);
280
			Component valueText = this.valueStringifier.apply(initialValue);
281
			Component initialTitle = this.displayState == CycleButton.DisplayState.VALUE ? valueText : CommonComponents.optionNameValue(name, valueText);
282
			return new CycleButton<>(
283
				x,
284
				y,
285
				width,
286
				height,
287
				initialTitle,
288
				name,
289
				initialIndex,
290
				initialValue,
291
				this.defaultValueSupplier,
292
				this.values,
293
				this.valueStringifier,
294
				this.narrationProvider,
295
				valueChangeListener,
296
				this.tooltipSupplier,
297
				this.displayState,
298
				this.spriteSupplier
299
			);
300
		}
301
	}
302
303
	@Environment(EnvType.CLIENT)
304
	public enum DisplayState {
305
		NAME_AND_VALUE,
306
		VALUE,
307
		HIDE;
308
	}
309
310
	@FunctionalInterface
311
	@Environment(EnvType.CLIENT)
312
	public interface OnValueChange<T> {
313
		void onValueChange(CycleButton<T> button, T value);
314
	}
315
316
	@FunctionalInterface
317
	@Environment(EnvType.CLIENT)
318
	public interface SpriteSupplier<T> {
319
		@Nullable Identifier apply(CycleButton<T> button, T value);
320
	}
321
322
	@Environment(EnvType.CLIENT)
323
	public interface ValueListSupplier<T> {
324
		List<T> getSelectedList();
325
326
		List<T> getDefaultList();
327
328
		static <T> CycleButton.ValueListSupplier<T> create(final Collection<T> values) {
329
			List<T> copy = ImmutableList.copyOf(values);
330
			return new 1(copy);
331
		}
332
333
		static <T> CycleButton.ValueListSupplier<T> create(final BooleanSupplier altSelector, final List<T> defaultList, final List<T> altList) {
334
			List<T> defaultCopy = ImmutableList.copyOf(defaultList);
335
			List<T> altCopy = ImmutableList.copyOf(altList);
336
			return new 2(altSelector, altCopy, defaultCopy);
337
		}
338
	}
339
}
340

Decompiled Minecraft 26.3 source of CycleButton (net/minecraft/client/gui/components/CycleButton.java) with Mojang official mappings, stored verbatim on the site.