Skip to content

Commit

Permalink
Move computer button textures to a sprite sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Aug 23, 2023
1 parent 12ee47f commit 194ee95
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import dan200.computercraft.shared.computer.inventory.ViewComputerMenu;
import dan200.computercraft.shared.media.items.DiskItem;
import dan200.computercraft.shared.media.items.TreasureDiskItem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.multiplayer.ClientLevel;
Expand All @@ -30,6 +31,7 @@
import net.minecraft.client.renderer.item.ClampedItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ResourceProvider;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -107,6 +109,10 @@ private static void registerItemProperty(String name, ClampedItemPropertyFunctio
for (var item : items) ItemProperties.register(item.get(), id, getter);
}

public static void registerReloadListeners(Consumer<PreparableReloadListener> register, Minecraft minecraft) {
register.accept(GuiSprites.initialise(minecraft.getTextureManager()));
}

private static final String[] EXTRA_MODELS = new String[]{
"block/turtle_colour",
"block/turtle_elf_overlay",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0

package dan200.computercraft.client.gui;

import dan200.computercraft.api.ComputerCraftAPI;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.TextureAtlasHolder;
import net.minecraft.resources.ResourceLocation;

import javax.annotation.Nullable;

/**
* Sprite sheet for
*/
public class GuiSprites extends TextureAtlasHolder {
public static final ResourceLocation SPRITE = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui");
private static final ResourceLocation ATLAS = SPRITE.withPath(x -> "textures/atlas/" + x + ".png");

public static final ResourceLocation TURNED_OFF = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/turned_off");
public static final ResourceLocation TURNED_OFF_HOVER = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/turned_off_hover");

public static final ResourceLocation TURNED_ON = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/turned_on");
public static final ResourceLocation TURNED_ON_HOVER = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/turned_on_hover");

public static final ResourceLocation TERMINATE = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/terminate");
public static final ResourceLocation TERMINATE_HOVER = new ResourceLocation(ComputerCraftAPI.MOD_ID, "gui/buttons/terminate_hover");

private static @Nullable GuiSprites instance;

private GuiSprites(TextureManager textureManager) {
super(textureManager, ATLAS, SPRITE);
}

public static GuiSprites initialise(TextureManager textureManager) {
if (instance != null) throw new IllegalStateException("GuiSprites has already been initialised");
return instance = new GuiSprites(textureManager);
}

public static TextureAtlasSprite get(ResourceLocation sprite) {
if (instance == null) throw new IllegalStateException("GuiSprites has not been initialised");
return instance.getSprite(sprite);
}

public static TextureAtlasSprite getOrHovered(boolean hovered, ResourceLocation normal, ResourceLocation hover) {
return get(hovered ? hover : normal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
package dan200.computercraft.client.gui.widgets;

import com.mojang.blaze3d.vertex.PoseStack;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.client.gui.GuiSprites;
import dan200.computercraft.client.gui.widgets.DynamicImageButton.HintedMessage;
import dan200.computercraft.client.render.ComputerBorderRenderer;
import dan200.computercraft.shared.computer.core.InputHandler;
import dan200.computercraft.shared.computer.inventory.AbstractComputerMenu;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
Expand All @@ -22,8 +21,6 @@
* Registers buttons to interact with a computer.
*/
public final class ComputerSidebar {
private static final ResourceLocation TEXTURE = new ResourceLocation(ComputerCraftAPI.MOD_ID, "textures/gui/buttons.png");

private static final int TEX_SIZE = 64;

private static final int ICON_WIDTH = 12;
Expand Down Expand Up @@ -51,16 +48,20 @@ public static void addButtons(BooleanSupplier isOn, InputHandler input, Consumer
Component.translatable("gui.computercraft.tooltip.turn_off.key")
);
add.accept(new DynamicImageButton(
x, y, ICON_WIDTH, ICON_HEIGHT, () -> isOn.getAsBoolean() ? 15 : 1, 1, ICON_TEX_Y_DIFF,
TEXTURE, TEX_SIZE, TEX_SIZE, b -> toggleComputer(isOn, input),
x, y, ICON_WIDTH, ICON_HEIGHT,
h -> isOn.getAsBoolean()
? GuiSprites.getOrHovered(h, GuiSprites.TURNED_ON, GuiSprites.TURNED_ON_HOVER)
: GuiSprites.getOrHovered(h, GuiSprites.TURNED_OFF, GuiSprites.TURNED_OFF_HOVER),
b -> toggleComputer(isOn, input),
() -> isOn.getAsBoolean() ? turnOff : turnOn
));

y += ICON_HEIGHT + ICON_MARGIN * 2;

add.accept(new DynamicImageButton(
x, y, ICON_WIDTH, ICON_HEIGHT, 29, 1, ICON_TEX_Y_DIFF,
TEXTURE, TEX_SIZE, TEX_SIZE, b -> input.queueEvent("terminate"),
x, y, ICON_WIDTH, ICON_HEIGHT,
h -> GuiSprites.getOrHovered(h, GuiSprites.TERMINATE, GuiSprites.TERMINATE_HOVER),
b -> input.queueEvent("terminate"),
new HintedMessage(
Component.translatable("gui.computercraft.tooltip.terminate"),
Component.translatable("gui.computercraft.tooltip.terminate.key")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,48 @@

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import it.unimi.dsi.fastutil.booleans.Boolean2ObjectFunction;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import javax.annotation.Nullable;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
* Version of {@link net.minecraft.client.gui.components.ImageButton} which allows changing some properties
* dynamically.
*/
public class DynamicImageButton extends Button {
private final ResourceLocation texture;
private final IntSupplier xTexStart;
private final int yTexStart;
private final int yDiffTex;
private final int textureWidth;
private final int textureHeight;
private final Boolean2ObjectFunction<TextureAtlasSprite> texture;
private final Supplier<HintedMessage> message;

public DynamicImageButton(
int x, int y, int width, int height, int xTexStart, int yTexStart, int yDiffTex,
ResourceLocation texture, int textureWidth, int textureHeight,
OnPress onPress, HintedMessage message
int x, int y, int width, int height, Boolean2ObjectFunction<TextureAtlasSprite> texture, OnPress onPress,
HintedMessage message
) {
this(
x, y, width, height, () -> xTexStart, yTexStart, yDiffTex,
texture, textureWidth, textureHeight,
onPress, () -> message
);
this(x, y, width, height, texture, onPress, () -> message);
}

public DynamicImageButton(
int x, int y, int width, int height, IntSupplier xTexStart, int yTexStart, int yDiffTex,
ResourceLocation texture, int textureWidth, int textureHeight,
int x, int y, int width, int height,
Boolean2ObjectFunction<TextureAtlasSprite> texture,
OnPress onPress, Supplier<HintedMessage> message
) {
super(x, y, width, height, Component.empty(), onPress, DEFAULT_NARRATION);
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
this.xTexStart = xTexStart;
this.yTexStart = yTexStart;
this.yDiffTex = yDiffTex;
this.texture = texture;
this.message = message;
}

@Override
public void renderWidget(PoseStack stack, int mouseX, int mouseY, float partialTicks) {
RenderSystem.setShaderTexture(0, texture);
var texture = this.texture.get(isHoveredOrFocused());
RenderSystem.setShaderTexture(0, texture.atlasLocation());
RenderSystem.disableDepthTest();

var yTex = yTexStart;
if (isHoveredOrFocused()) yTex += yDiffTex;

blit(stack, getX(), getY(), xTexStart.getAsInt(), yTex, width, height, textureWidth, textureHeight);
blit(stack, getX(), getY(), 0, width, height, texture);
RenderSystem.enableDepthTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

package dan200.computercraft.data.client;

import dan200.computercraft.client.gui.GuiSprites;
import dan200.computercraft.data.DataProviders;
import dan200.computercraft.shared.turtle.inventory.UpgradeSlot;
import net.minecraft.client.renderer.texture.atlas.SpriteSource;
import net.minecraft.client.renderer.texture.atlas.SpriteSources;
import net.minecraft.client.renderer.texture.atlas.sources.SingleFile;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
* A version of {@link DataProviders} which relies on client-side classes.
Expand All @@ -29,6 +32,11 @@ public static void add(DataProviders.GeneratorSink generator) {
new SingleFile(UpgradeSlot.LEFT_UPGRADE, Optional.empty()),
new SingleFile(UpgradeSlot.RIGHT_UPGRADE, Optional.empty())
));
out.accept(GuiSprites.SPRITE, Stream.of(
GuiSprites.TURNED_OFF, GuiSprites.TURNED_OFF_HOVER,
GuiSprites.TURNED_ON, GuiSprites.TURNED_ON_HOVER,
GuiSprites.TERMINATE, GuiSprites.TERMINATE_HOVER
).<SpriteSource>map(x -> new SingleFile(x, Optional.empty())).toList());
});
}
}
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
package dan200.computercraft.mixin.client;

import dan200.computercraft.client.ClientHooks;
import dan200.computercraft.client.ClientRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.main.GameConfig;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Minecraft.class)
class MinecraftMixin {
@Shadow
@Final
private ReloadableResourceManager resourceManager;

@Inject(method = "clearLevel(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At("HEAD"))
@SuppressWarnings("UnusedMethod")
private void clearLevel(Screen screen, CallbackInfo ci) {
Expand All @@ -26,4 +35,16 @@ private void clearLevel(Screen screen, CallbackInfo ci) {
private void setLevel(ClientLevel screen, CallbackInfo ci) {
ClientHooks.onWorldUnload();
}

@Inject(
method = "<init>(Lnet/minecraft/client/main/GameConfig;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/ResourceLoadStateTracker;startReload(Lnet/minecraft/client/ResourceLoadStateTracker$ReloadReason;Ljava/util/List;)V",
ordinal = 0
)
)
public void beforeInitialResourceReload(GameConfig gameConfig, CallbackInfo ci) {
ClientRegistry.registerReloadListeners(resourceManager::registerReloadListener, (Minecraft) (Object) this);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.client.model.turtle.TurtleModelLoader;
import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.client.event.RegisterColorHandlersEvent;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand Down Expand Up @@ -44,6 +46,11 @@ public static void onItemColours(RegisterColorHandlersEvent.Item event) {
ClientRegistry.registerItemColours(event::register);
}

@SubscribeEvent
public static void registerReloadListeners(RegisterClientReloadListenersEvent event) {
ClientRegistry.registerReloadListeners(event::registerReloadListener, Minecraft.getInstance());
}

@SubscribeEvent
public static void setupClient(FMLClientSetupEvent event) {
ClientRegistry.register();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 194ee95

Please sign in to comment.