| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.ClientRecipeBook;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.ImageButton;
|
| 10 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 11 | import net.minecraft.client.renderer.RenderPipelines;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.world.item.crafting.ExtendedRecipeBookCategory;
|
| 14 | import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class RecipeBookTabButton extends ImageButton {
|
| 18 | private static final WidgetSprites SPRITES = new WidgetSprites(
|
| 19 | Identifier.withDefaultNamespace("recipe_book/tab"), Identifier.withDefaultNamespace("recipe_book/tab_selected")
|
| 20 | );
|
| 21 | public static final int WIDTH = 35;
|
| 22 | public static final int HEIGHT = 27;
|
| 23 | private final RecipeBookComponent.TabInfo tabInfo;
|
| 24 | private static final float ANIMATION_TIME = 15.0F;
|
| 25 | private float animationTime;
|
| 26 | private boolean selected = false;
|
| 27 |
|
| 28 | public RecipeBookTabButton(final int x, final int y, final RecipeBookComponent.TabInfo tabInfo, final Button.OnPress onPress) {
|
| 29 | super(x, y, 35, 27, SPRITES, onPress);
|
| 30 | this.tabInfo = tabInfo;
|
| 31 | }
|
| 32 |
|
| 33 | public void startAnimation(final ClientRecipeBook recipeBook, final boolean isFiltering) {
|
| 34 | RecipeCollection.CraftableStatus recipesToShow = isFiltering ? RecipeCollection.CraftableStatus.CRAFTABLE : RecipeCollection.CraftableStatus.ANY;
|
| 35 |
|
| 36 | for (RecipeCollection recipeCollection : recipeBook.getCollection(this.tabInfo.category())) {
|
| 37 | for (RecipeDisplayEntry recipe : recipeCollection.getSelectedRecipes(recipesToShow)) {
|
| 38 | if (recipeBook.willHighlight(recipe.id())) {
|
| 39 | this.animationTime = 15.0F;
|
| 40 | return;
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 48 | if (this.animationTime > 0.0F) {
|
| 49 | float squeeze = 1.0F + 0.1F * (float)Math.sin(this.animationTime / 15.0F * (float) Math.PI);
|
| 50 | graphics.pose().pushMatrix();
|
| 51 | graphics.pose().translate(this.getX() + 8, this.getY() + 12);
|
| 52 | graphics.pose().scale(1.0F, squeeze);
|
| 53 | graphics.pose().translate(-(this.getX() + 8), -(this.getY() + 12));
|
| 54 | }
|
| 55 |
|
| 56 | Identifier sprite = this.sprites.get(true, this.selected);
|
| 57 | int xPos = this.getX();
|
| 58 | if (this.selected) {
|
| 59 | xPos -= 2;
|
| 60 | }
|
| 61 |
|
| 62 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, xPos, this.getY(), this.width, this.height);
|
| 63 | this.extractIcon(graphics);
|
| 64 | if (this.animationTime > 0.0F) {
|
| 65 | graphics.pose().popMatrix();
|
| 66 | this.animationTime -= a;
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void handleCursor(final GuiGraphicsExtractor graphics) {
|
| 72 | if (!this.selected) {
|
| 73 | super.handleCursor(graphics);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | private void extractIcon(final GuiGraphicsExtractor graphics) {
|
| 78 | int moveLeft = this.selected ? -2 : 0;
|
| 79 | if (this.tabInfo.secondaryIcon().isPresent()) {
|
| 80 | graphics.fakeItem(this.tabInfo.primaryIcon(), this.getX() + 3 + moveLeft, this.getY() + 5);
|
| 81 | graphics.fakeItem(this.tabInfo.secondaryIcon().get(), this.getX() + 14 + moveLeft, this.getY() + 5);
|
| 82 | } else {
|
| 83 | graphics.fakeItem(this.tabInfo.primaryIcon(), this.getX() + 9 + moveLeft, this.getY() + 5);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public ExtendedRecipeBookCategory getCategory() {
|
| 88 | return this.tabInfo.category();
|
| 89 | }
|
| 90 |
|
| 91 | public boolean updateVisibility(final ClientRecipeBook book) {
|
| 92 | List<RecipeCollection> collections = book.getCollection(this.tabInfo.category());
|
| 93 | this.visible = false;
|
| 94 |
|
| 95 | for (RecipeCollection collection : collections) {
|
| 96 | if (collection.hasAnySelected()) {
|
| 97 | this.visible = true;
|
| 98 | break;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return this.visible;
|
| 103 | }
|
| 104 |
|
| 105 | public void select() {
|
| 106 | this.selected = true;
|
| 107 | }
|
| 108 |
|
| 109 | public void unselect() {
|
| 110 | this.selected = false;
|
| 111 | }
|
| 112 | }
|
| 113 |
|