| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.InputType;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 10 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 11 | import net.minecraft.client.input.KeyEvent;
|
| 12 | import net.minecraft.client.input.MouseButtonEvent;
|
| 13 | import net.minecraft.client.renderer.RenderPipelines;
|
| 14 | import net.minecraft.client.sounds.SoundManager;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.MutableComponent;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.util.ARGB;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public abstract class AbstractSliderButton extends AbstractWidget.WithInactiveMessage {
|
| 23 | private static final Identifier SLIDER_SPRITE = Identifier.withDefaultNamespace("widget/slider");
|
| 24 | private static final Identifier HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/slider_highlighted");
|
| 25 | private static final Identifier SLIDER_HANDLE_SPRITE = Identifier.withDefaultNamespace("widget/slider_handle");
|
| 26 | private static final Identifier SLIDER_HANDLE_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/slider_handle_highlighted");
|
| 27 | protected static final int TEXT_MARGIN = 2;
|
| 28 | public static final int DEFAULT_HEIGHT = 20;
|
| 29 | protected static final int HANDLE_WIDTH = 8;
|
| 30 | private static final int HANDLE_HALF_WIDTH = 4;
|
| 31 | protected double value;
|
| 32 | protected boolean canChangeValue;
|
| 33 | private boolean dragging;
|
| 34 |
|
| 35 | public AbstractSliderButton(final int x, final int y, final int width, final int height, final Component message, final double initialValue) {
|
| 36 | super(x, y, width, height, message);
|
| 37 | this.value = initialValue;
|
| 38 | }
|
| 39 |
|
| 40 | private Identifier getSprite() {
|
| 41 | return this.isActive() && this.isFocused() && !this.canChangeValue ? HIGHLIGHTED_SPRITE : SLIDER_SPRITE;
|
| 42 | }
|
| 43 |
|
| 44 | private Identifier getHandleSprite() {
|
| 45 | return !this.isActive() || !this.isHovered && !this.canChangeValue ? SLIDER_HANDLE_SPRITE : SLIDER_HANDLE_HIGHLIGHTED_SPRITE;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected MutableComponent createNarrationMessage() {
|
| 50 | return Component.translatable("gui.narrate.slider", this.getMessage());
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 55 | output.add(NarratedElementType.TITLE, this.createNarrationMessage());
|
| 56 | if (this.active) {
|
| 57 | if (this.isFocused()) {
|
| 58 | if (this.canChangeValue) {
|
| 59 | output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.focused"));
|
| 60 | } else {
|
| 61 | output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.focused.keyboard_cannot_change_value"));
|
| 62 | }
|
| 63 | } else {
|
| 64 | output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.hovered"));
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 71 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.getSprite(), this.getX(), this.getY(), this.getWidth(), this.getHeight(), ARGB.white(this.alpha));
|
| 72 | graphics.blitSprite(
|
| 73 | RenderPipelines.GUI_TEXTURED,
|
| 74 | this.getHandleSprite(),
|
| 75 | this.getX() + (int)(this.value * (this.width - 8)),
|
| 76 | this.getY(),
|
| 77 | 8,
|
| 78 | this.getHeight(),
|
| 79 | ARGB.white(this.alpha)
|
| 80 | );
|
| 81 | this.extractScrollingStringOverContents(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE), this.getMessage(), 2);
|
| 82 | this.handleCursor(graphics);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected void handleCursor(final GuiGraphicsExtractor graphics) {
|
| 87 | if (this.isHovered()) {
|
| 88 | graphics.requestCursor(this.isActive() ? (this.dragging ? CursorTypes.RESIZE_EW : CursorTypes.POINTING_HAND) : CursorTypes.NOT_ALLOWED);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 94 | this.dragging = this.active;
|
| 95 | this.setValueFromMouse(event);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void setFocused(final boolean focused) {
|
| 100 | super.setFocused(focused);
|
| 101 | if (!focused) {
|
| 102 | this.canChangeValue = false;
|
| 103 | } else {
|
| 104 | InputType lastInputType = Minecraft.getInstance().getLastInputType();
|
| 105 | if (lastInputType == InputType.MOUSE || lastInputType == InputType.KEYBOARD_TAB) {
|
| 106 | this.canChangeValue = true;
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public boolean keyPressed(final KeyEvent event) {
|
| 113 | if (event.isSelection()) {
|
| 114 | this.canChangeValue = !this.canChangeValue;
|
| 115 | return true;
|
| 116 | }
|
| 117 |
|
| 118 | if (this.canChangeValue) {
|
| 119 | boolean left = event.isLeft();
|
| 120 | boolean right = event.isRight();
|
| 121 | if (left || right) {
|
| 122 | float direction = left ? -1.0F : 1.0F;
|
| 123 | this.setValue(this.value + direction / (this.width - 8));
|
| 124 | return true;
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | return false;
|
| 129 | }
|
| 130 |
|
| 131 | private void setValueFromMouse(final MouseButtonEvent event) {
|
| 132 | this.setValue((event.x() - (this.getX() + 4)) / (this.width - 8));
|
| 133 | }
|
| 134 |
|
| 135 | protected void setValue(final double newValue) {
|
| 136 | double oldValue = this.value;
|
| 137 | this.value = Mth.clamp(newValue, 0.0, 1.0);
|
| 138 | if (oldValue != this.value) {
|
| 139 | this.applyValue();
|
| 140 | }
|
| 141 |
|
| 142 | this.updateMessage();
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected void onDrag(final MouseButtonEvent event, final double dx, final double dy) {
|
| 147 | this.setValueFromMouse(event);
|
| 148 | super.onDrag(event, dx, dy);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public void playDownSound(final SoundManager soundManager) {
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void onRelease(final MouseButtonEvent event) {
|
| 157 | this.dragging = false;
|
| 158 | super.playDownSound(Minecraft.getInstance().getSoundManager());
|
| 159 | }
|
| 160 |
|
| 161 | protected abstract void updateMessage();
|
| 162 |
|
| 163 | protected abstract void applyValue();
|
| 164 | }
|
| 165 |
|