| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.renderer.RenderPipelines;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.util.ARGB;
|
| 10 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 11 | import net.minecraft.world.inventory.Slot;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class CyclingSlotBackground {
|
| 15 | private static final int ICON_CHANGE_TICK_RATE = 30;
|
| 16 | private static final int ICON_SIZE = 16;
|
| 17 | private static final int ICON_TRANSITION_TICK_DURATION = 4;
|
| 18 | private final int slotIndex;
|
| 19 | private List<Identifier> icons = List.of();
|
| 20 | private int tick;
|
| 21 | private int iconIndex;
|
| 22 |
|
| 23 | public CyclingSlotBackground(final int slotIndex) {
|
| 24 | this.slotIndex = slotIndex;
|
| 25 | }
|
| 26 |
|
| 27 | public void tick(final List<Identifier> newIcons) {
|
| 28 | if (!this.icons.equals(newIcons)) {
|
| 29 | this.icons = newIcons;
|
| 30 | this.iconIndex = 0;
|
| 31 | }
|
| 32 |
|
| 33 | if (!this.icons.isEmpty() && ++this.tick % 30 == 0) {
|
| 34 | this.iconIndex = (this.iconIndex + 1) % this.icons.size();
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | public void extractRenderState(final AbstractContainerMenu menu, final GuiGraphicsExtractor graphics, final float a, final int left, final int top) {
|
| 39 | Slot slot = menu.getSlot(this.slotIndex);
|
| 40 | if (!this.icons.isEmpty() && !slot.hasItem()) {
|
| 41 | boolean shouldTransition = this.icons.size() > 1 && this.tick >= 30;
|
| 42 | float alphaProgress = shouldTransition ? this.getIconTransitionTransparency(a) : 1.0F;
|
| 43 | if (alphaProgress < 1.0F) {
|
| 44 | int previousIconIndex = Math.floorMod(this.iconIndex - 1, this.icons.size());
|
| 45 | this.extractIcon(slot, this.icons.get(previousIconIndex), 1.0F - alphaProgress, graphics, left, top);
|
| 46 | }
|
| 47 |
|
| 48 | this.extractIcon(slot, this.icons.get(this.iconIndex), alphaProgress, graphics, left, top);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | private void extractIcon(
|
| 53 | final Slot slot, final Identifier iconIdentifier, final float alphaProgress, final GuiGraphicsExtractor graphics, final int left, final int top
|
| 54 | ) {
|
| 55 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, iconIdentifier, left + slot.x, top + slot.y, 16, 16, ARGB.white(alphaProgress));
|
| 56 | }
|
| 57 |
|
| 58 | private float getIconTransitionTransparency(final float a) {
|
| 59 | float elapsedTransitionTime = this.tick % 30 + a;
|
| 60 | return Math.min(elapsedTransitionTime, 4.0F) / 4.0F;
|
| 61 | }
|
| 62 | }
|
| 63 |
|