Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jab125 committed Jan 19, 2025
2 parents 1cdc33d + 8d9c71f commit 5c02ca0
Show file tree
Hide file tree
Showing 18 changed files with 657 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package xyz.violaflower.legacy_tweaks.client.gui.element;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.*;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.function.Consumer;
import java.util.function.Function;

/// @see StateSwitchingButton
public class LegacyTabButton extends StateSwitchingButton {
public final Function<LegacyTabButton, Renderable> icon;
public final Consumer<LegacyTabButton> onPress;
protected int selectedTabIndex;
public LegacyWidgetSprites widgets;
public final Component text;

public LegacyTabButton(int x, int y, int width, int height, boolean isStateTriggered, int selectedTabIndex, Component text, LegacyWidgetSprites sprites, Function<LegacyTabButton, Renderable> icon, Consumer<LegacyTabButton> onPress) {
super(x, y, width, height, isStateTriggered);
this.icon = icon;
this.onPress = onPress;
this.selectedTabIndex = selectedTabIndex;
this.text = text;
this.isStateTriggered = isStateTriggered;
this.initTextureValues(sprites);
}

public void initTextureValues(LegacyWidgetSprites widgets) {
this.widgets = widgets;
}

@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (this.widgets != null) {
int i = this.getY();
if (this.isStateTriggered) {
i -= 2;
}
RenderSystem.disableDepthTest();
guiGraphics.blitSprite(this.widgets.get(this.isStateTriggered, this.selectedTabIndex), this.getX(), i, this.width, this.height);
RenderSystem.enableDepthTest();
}
}

public void setStateTriggered(boolean triggered) {
this.isStateTriggered = triggered;
}

public boolean isStateTriggered() {
return this.isStateTriggered;
}

public static Function<LegacyTabButton,Renderable> createIconItem(Item item){
return b-> (guiGraphics, i, j, f) -> b.renderItem(guiGraphics, item.getDefaultInstance());
}

public static Function<LegacyTabButton,Renderable> createIconItem(ItemStack itemStack){
return b-> (guiGraphics, i, j, f) -> b.renderItem(guiGraphics, itemStack);
}

public static Function<LegacyTabButton,Renderable> createIconSprite(ResourceLocation icon){
return b-> (guiGraphics, i, j, f) -> b.renderIcon(guiGraphics, icon);
}

public static Function<LegacyTabButton,Renderable> createTextIcon(int color) {
return b -> (graphics, i, j, f) -> b.renderString(graphics, Minecraft.getInstance().font, color);
}

private void renderItem(GuiGraphics guiGraphics, ItemStack itemStack) {
int i = this.isStateTriggered ? -2 : 0;
guiGraphics.renderFakeItem(itemStack, this.getX() + this.width/2 - 12, this.getY() + this.height/2 - 12 + i);
}

private void renderIcon(GuiGraphics guiGraphics, ResourceLocation icon){
int i = this.isStateTriggered ? -2 : 0;
guiGraphics.blitSprite(icon, getX() + this.width/2 - 12, getY() + this.height/2 - 12 + i, 24, 24);
}

protected void renderString(GuiGraphics guiGraphics, Font font, int color) {
guiGraphics.drawString(font, this.text, getX() + (this.width - font.width(this.text))/2, getY() + (this.height - 7)/2, color, false);
}

@Override
public void onClick(double mouseX, double mouseY) {
isStateTriggered = !isStateTriggered;
onPress.accept(this);
}

public void onPress() {
isStateTriggered = !isStateTriggered;
onPress.accept(this);
}

@Override
protected boolean clicked(double d, double e) {
return isMouseOver(d,e);
}

@Override
public void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {
narrationElementOutput.add(NarratedElementType.TITLE, Component.translatable(this.text.getString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package xyz.violaflower.legacy_tweaks.client.gui.element;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import xyz.violaflower.legacy_tweaks.util.common.assets.Sprites;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

public class LegacyTabListing implements Renderable, GuiEventListener, NarratableEntry {
public final List<LegacyTabButton> tabs;
public int selectedIndex = 0;
public boolean isFocused = false;
public LegacyWidgetSprites genericTabWidgets = new LegacyWidgetSprites(Sprites.CRAFTING_TAB_LEFT_SIDE, Sprites.CRAFTING_TAB_CENTER, Sprites.CRAFTING_TAB_RIGHT_SIDE, Sprites.CRAFTING_TAB_SELECTED_SIDE, Sprites.CRAFTING_TAB_SELECTED, Sprites.CRAFTING_TAB_SELECTED_SIDE);

public LegacyTabListing() {
this.tabs = new ArrayList<>();
}

public void addTab(LegacyTabButton tab) {
this.tabs.add(tab);
}

public void addTab(int x, int y, int width, int height, boolean isStateTriggered, Component text, LegacyWidgetSprites sprites, Function<LegacyTabButton, Renderable> icon, Consumer<LegacyTabButton> onPress) {
addTab(new LegacyTabButton(x, y, width, height, isStateTriggered, selectedIndex, text, sprites, icon, p -> {
if (selectedIndex != this.tabs.indexOf(p)) {
selectedIndex = this.tabs.indexOf(p);
onPress.accept(p);
}
}));
}

public LegacyTabListing createTab(int x, int y, int width, int height, boolean isStateTriggered, Component text, LegacyWidgetSprites sprites, Function<LegacyTabButton, Renderable> icon, Consumer<LegacyTabButton> onPress) {
this.addTab(x, y, width, height, isStateTriggered, text, sprites, icon, onPress);
return this;
}

public LegacyTabListing createTabWithPredeterminedTabWidget(int x, int y, int width, int height, boolean isStateTriggered, Component text, Function<LegacyTabButton, Renderable> icon, Consumer<LegacyTabButton> onPress) {
this.addTab(x, y, width, height, isStateTriggered, text, this.genericTabWidgets, icon, onPress);
return this;
}

public LegacyTabListing createMultipleTabs(int tabAmount, int x, int y, int width, int height, Component text, @Nullable LegacyWidgetSprites sprites, List<Function<LegacyTabButton, Renderable>> icons, Consumer<LegacyTabButton> onPress) {
for (int i = 0; i <= tabAmount; i++) {
LegacyWidgetSprites widgets = sprites != null ? sprites : this.genericTabWidgets;
this.addTab(x, y, width, height, false, text, widgets, icons.get(i), onPress);
}
return this;
}

public void init(int x, int y, int width, boolean isVertical) {
int xPos = 0;

BiConsumer<LegacyTabButton, Integer> finalTabManager = (tabList, i)->{};
BiConsumer<LegacyTabButton, Integer> tabManager = (tabList, i) -> {
tabList.setWidth(width / this.tabs.size());
tabList.setX(x + i);
tabList.setY(y);
finalTabManager.accept(tabList, i);
};

for (LegacyTabButton tabList : this.tabs) {
tabManager.accept(tabList, xPos);
xPos += isVertical ? tabList.getHeight() : tabList.getWidth();
}
}

public void setSelectedTab(int index){
if (!this.tabs.isEmpty()) {
selectedIndex = -1;
this.tabs.get(index).onPress();
}
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
for (int index = 0; index < this.tabs.size(); index++) {
LegacyTabButton tab = this.tabs.get(index);
tab.setStateTriggered(this.selectedIndex == index);
tab.render(guiGraphics, mouseX, mouseY, partialTick);
}
}

@Override
public void setFocused(boolean focused) {
focused = this.isFocused;
}

@Override
public boolean isFocused() {
return isFocused;
}

@Override
public boolean mouseClicked(double a, double b, int c) {
return !this.tabs.stream().filter(tabs-> tabs.mouseClicked(a, b, c)).toList().isEmpty();
}

@Override
public NarrationPriority narrationPriority() {
return null;
}

@Override
public void updateNarration(NarrationElementOutput narrationElementOutput) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xyz.violaflower.legacy_tweaks.client.gui.element;

import net.minecraft.resources.ResourceLocation;

public record LegacyWidgetSprites(ResourceLocation enabledLeft, ResourceLocation enabledCenter, ResourceLocation enabledRight, ResourceLocation focusedLeft, ResourceLocation focusedCenter, ResourceLocation focusedRight) {

public LegacyWidgetSprites(ResourceLocation enabled, ResourceLocation focused) {
this(enabled, enabled, enabled, focused, focused, focused);
}

public LegacyWidgetSprites(ResourceLocation enabledLeft, ResourceLocation enabledCenter, ResourceLocation enabledRight, ResourceLocation focusedLeft, ResourceLocation focusedCenter, ResourceLocation focusedRight) {
this.enabledLeft = enabledLeft;
this.enabledCenter = enabledCenter;
this.enabledRight = enabledRight;
this.focusedLeft = focusedLeft;
this.focusedCenter = focusedCenter;
this.focusedRight = focusedRight;
}

public ResourceLocation get(boolean focused, int type) {
if (type == 0) {
return focused ? this.focusedLeft : this.enabledLeft;
} else if (type == 1) {
return focused ? this.focusedCenter : this.enabledCenter;
} else if (type == 2) {
return focused ? this.focusedRight : this.enabledRight;
}
return this.enabledCenter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package xyz.violaflower.legacy_tweaks.client.gui.element;

import net.minecraft.client.RecipeBookCategories;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import xyz.violaflower.legacy_tweaks.util.common.assets.Sprites;

import java.util.function.Consumer;
import java.util.function.Function;

public class RecipeBookTabListing implements Renderable, GuiEventListener, NarratableEntry {
public RecipeBookTabListing(RecipeBookCategories category) {
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {

}

@Override
public void setFocused(boolean focused) {

}

@Override
public boolean isFocused() {
return false;
}

@Override
public NarrationPriority narrationPriority() {
return null;
}

@Override
public void updateNarration(NarrationElementOutput narrationElementOutput) {

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package xyz.violaflower.legacy_tweaks.client.gui.screen.legacy.screens.inventory;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.recipebook.AbstractFurnaceRecipeBookComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package xyz.violaflower.legacy_tweaks.client.gui.screen.legacy.screens.inventory;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.InventoryMenu;
import xyz.violaflower.legacy_tweaks.util.client.screen.graphics.GraphicsUtil;
import xyz.violaflower.legacy_tweaks.util.common.assets.Sprites;
import xyz.violaflower.legacy_tweaks.util.common.sound.SoundUtil;
import xyz.violaflower.legacy_tweaks.util.common.sound.Sounds;

public abstract class LegacyAutoCraftingScreen <T extends AbstractContainerMenu> extends LegacyAbstractContainerScreen<T> {
public float leftPos;
public float topPos;
public float imageWidth;
public float imageHeight;

public LegacyAutoCraftingScreen(T menu, Inventory playerInventory, Component title) {
super(menu, playerInventory, title);
if (useSmallCrafting()) {
this.imageWidth = 591f / 2f;
} else {
this.imageWidth = 689f / 2f;
}
this.imageHeight = 424f / 2f;
this.leftPos = ((this.width + this.imageWidth) / 2);
this.topPos = ((this.height + this.imageHeight) / 2) - 20f;
}

@Override
protected void init() {
SoundUtil.playFullPitchSound(Sounds.PRESS, SoundSource.MASTER);
this.leftPos = this.leftPos - (useSmallCrafting() ? 0f : 40f);
}

@Override
protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {

}

@Override
public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
this.renderBg(guiGraphics, partialTick, mouseX, mouseY);
}

@Override
protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
this.leftPos = this.leftPos - (useSmallCrafting() ? 0f : 40f);
if (useSmallCrafting()) {
GraphicsUtil.blit(guiGraphics, Sprites.SMALL_CRAFTING_1080, this.leftPos, this.topPos, this.imageWidth, this.imageHeight, 0f, 0f, 591f, 424f, 700f, 440f);
} else {
GraphicsUtil.blit(guiGraphics, Sprites.LARGE_CRAFTING_1080, this.leftPos, this.topPos, this.imageWidth, this.imageHeight, 0f, 0f, 689f, 424f, 700f, 440f);
}
}

public boolean useSmallCrafting() {
return menu instanceof InventoryMenu;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package xyz.violaflower.legacy_tweaks.client.gui.screen.legacy.screens.inventory;

import com.mojang.datafixers.util.Pair;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screens.recipebook.SmeltingRecipeBookComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -14,7 +11,6 @@
import xyz.violaflower.legacy_tweaks.util.common.assets.ModAsset;
import xyz.violaflower.legacy_tweaks.util.common.assets.Sprites;

@Environment(EnvType.CLIENT)
public class LegacyFurnaceScreen extends LegacyAbstractFurnaceScreen<FurnaceMenu> {
private static final ResourceLocation LIT_PROGRESS_SPRITE = ResourceLocation.withDefaultNamespace("container/furnace/lit_progress");
private static final ResourceLocation BURN_PROGRESS_SPRITE = ResourceLocation.withDefaultNamespace("container/furnace/burn_progress");
Expand Down
Loading

0 comments on commit 5c02ca0

Please sign in to comment.