From 7d4461a362aad70e44e1ea2a252ea7444f31efa5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 13:52:48 +0000 Subject: [PATCH 1/8] Add fill method to the SkinFilterCallback --- .../com/minelittlepony/common/event/SkinFilterCallback.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java index 8478e73a..c7bed535 100644 --- a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java +++ b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java @@ -33,6 +33,11 @@ default boolean shouldAllowTransparency(NativeImage image, boolean legacy) { return true; // default is true since in most cases this is the desired effect } + static void fill(NativeImage image, int xFrom, int yFrom, int xTo, int yTo, int color) { + int scale = image.getWidth() / 64; + image.fillRect(0, 32 * scale, 64 * scale, 32 * scale, 0); + } + /** * Copies a scaled section from one region to another. * From 620f390daf2d209cb2f1bf91ee6b8af5eb824e87 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 14:13:55 +0000 Subject: [PATCH 2/8] Backport features from 1.21.3 --- .../common/client/gui/ScrollContainer.java | 73 ++++++++++--------- .../common/client/gui/element/EnumSlider.java | 7 ++ .../common/client/gui/element/Slider.java | 6 ++ .../common/client/gui/element/Toggle.java | 7 ++ .../common/event/SkinFilterCallback.java | 65 ++++++++++++++++- .../common/mixin/MixinPlayerSkinTexture.java | 43 ++++++----- .../common/util/settings/Setting.java | 3 +- 7 files changed, 147 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/minelittlepony/common/client/gui/ScrollContainer.java b/src/main/java/com/minelittlepony/common/client/gui/ScrollContainer.java index 7f408d1e..359deed3 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/ScrollContainer.java +++ b/src/main/java/com/minelittlepony/common/client/gui/ScrollContainer.java @@ -7,9 +7,9 @@ import com.minelittlepony.common.client.gui.dimension.Padding; import com.minelittlepony.common.client.gui.element.Scrollbar; import com.minelittlepony.common.client.gui.scrollable.ScrollOrientation; -import com.minelittlepony.common.util.render.ClippingSpace; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.util.Window; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.screen.ScreenTexts; @@ -85,42 +85,44 @@ public void init(Runnable contentInitializer) { @Override public final void render(DrawContext context, int mouseX, int mouseY, float tickDelta) { - ClippingSpace.renderClipped(margin.left, margin.top, getBounds().width, getBounds().height, () -> { - MatrixStack matrices = context.getMatrices(); - matrices.push(); - getBounds().translate(matrices); + context.enableScissor(margin.left, margin.top, margin.left + getBounds().width, margin.top + getBounds().height); - drawBackground(context, mouseX, mouseY, tickDelta); + MatrixStack matrices = context.getMatrices(); + matrices.push(); + getBounds().translate(matrices); - Padding padding = getContentPadding(); + drawBackground(context, mouseX, mouseY, tickDelta); - matrices.push(); - matrices.translate( - getScrollX() + padding.left, - getScrollY() + padding.top, 0); + Padding padding = getContentPadding(); - renderContents(context, - mouseX < margin.left || mouseX > margin.left + getBounds().width ? -1000 : mouseX + getMouseXOffset(), - mouseY < margin.top || mouseY > margin.top + getBounds().height ? -1000 : mouseY + getMouseYOffset(), - tickDelta); + matrices.push(); + matrices.translate( + getScrollX() + padding.left, + getScrollY() + padding.top, 0); - matrices.pop(); + renderContents(context, + mouseX < margin.left || mouseX > margin.left + getBounds().width ? -1000 : mouseX + getMouseXOffset(), + mouseY < margin.top || mouseY > margin.top + getBounds().height ? -1000 : mouseY + getMouseYOffset(), + tickDelta); - verticalScrollbar.render(context, - mouseX - margin.left, - mouseY - margin.top, - tickDelta - ); - horizontalScrollbar.render(context, - mouseX - margin.left, - mouseY - margin.top, - tickDelta - ); + matrices.pop(); - drawDecorations(context, mouseX, mouseY, tickDelta); + verticalScrollbar.render(context, + mouseX - margin.left, + mouseY - margin.top, + tickDelta + ); + horizontalScrollbar.render(context, + mouseX - margin.left, + mouseY - margin.top, + tickDelta + ); - matrices.pop(); - }); + drawDecorations(context, mouseX, mouseY, tickDelta); + + matrices.pop(); + + context.disableScissor(); drawOverlays(context, mouseX, mouseY, tickDelta); } @@ -144,9 +146,14 @@ protected void drawDecorations(DrawContext context, int mouseX, int mouseY, floa protected void drawOverlays(DrawContext context, int mouseX, int mouseY, float tickDelta) { Runnable task; + Window window = MinecraftClient.getInstance().getWindow(); + context.enableScissor(0, 0, window.getScaledWidth(), window.getScaledHeight()); + while ((task = delayedCalls.poll()) != null) { task.run(); } + + context.disableScissor(); } @Override @@ -199,11 +206,9 @@ public boolean mouseScrolled(double mouseX, double mouseY, double xScroll, doubl protected void renderOutside(DrawContext context, int mouseX, int mouseY, BiConsumer renderCall) { delayedCalls.add(() -> { - ClippingSpace.renderUnclipped(() -> { - context.getMatrices().push(); - renderCall.accept(mouseX - getMouseXOffset(), mouseY - getMouseYOffset()); - context.getMatrices().pop(); - }); + context.getMatrices().push(); + renderCall.accept(mouseX - getMouseXOffset(), mouseY - getMouseYOffset()); + context.getMatrices().pop(); }); } diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java b/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java index 177904cb..8f56b54a 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java @@ -1,5 +1,8 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + import net.minecraft.text.Text; /** @@ -11,6 +14,10 @@ public class EnumSlider> extends AbstractSlider { private final T[] values; + public EnumSlider(int x, int y, Supplier value) { + this(x, y, Objects.requireNonNull(value.get(), "value was null")); + } + @SuppressWarnings("unchecked") public EnumSlider(int x, int y, T value) { super(x, y, 0, value.getClass().getEnumConstants().length - 1, value); diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java b/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java index c7c55f83..b5eb1986 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java @@ -1,11 +1,17 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + /** * A slider for sliding. * * @author Sollace */ public class Slider extends AbstractSlider { + public Slider(int x, int y, float min, float max, Supplier value) { + this(x, y, min, max, Objects.requireNonNull(value.get(), "value was null").floatValue()); + } public Slider(int x, int y, float min, float max, float value) { super(x, y, min, max, value); diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java b/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java index 8532b322..63594987 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java @@ -1,5 +1,8 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + import org.jetbrains.annotations.NotNull; import com.minelittlepony.common.client.gui.IField; @@ -21,6 +24,10 @@ public class Toggle extends Button implements IField { @NotNull private IChangeCallback action = IChangeCallback::none; + public Toggle(int x, int y, Supplier value) { + this(x, y, Objects.requireNonNull(value.get(), "value was null")); + } + public Toggle(int x, int y, boolean value) { super(x, y, 30, 15); diff --git a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java index c7bed535..ed8a8666 100644 --- a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java +++ b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java @@ -5,8 +5,32 @@ import net.minecraft.client.texture.NativeImage; public interface SkinFilterCallback { + int VANILLA_SKIN_WIDTH = 64; + int VANILLA_SKIN_HEIGHT = 64; + int OLD_VANILLA_SKIN_HEIGHT = 32; + Event EVENT = EventFactory.createArrayBacked(SkinFilterCallback.class, listeners -> { return new SkinFilterCallback() { + @Override + public NativeImage processImage(NativeImage image, int initialWidth, int initialHeight) { + for (SkinFilterCallback event : listeners) { + image = event.processImage(image, initialWidth, initialHeight); + } + return image; + } + + @Override + public boolean shouldAllowTransparency(NativeImage image, int initialWidth, int initialHeight) { + for (SkinFilterCallback event : listeners) { + if (event.shouldAllowTransparency(image, initialWidth, initialHeight)) { + return true; + } + } + + return false; + } + + @Deprecated @Override public void processImage(NativeImage image, boolean legacy) { for (SkinFilterCallback event : listeners) { @@ -14,6 +38,7 @@ public void processImage(NativeImage image, boolean legacy) { } } + @Deprecated @Override public boolean shouldAllowTransparency(NativeImage image, boolean legacy) { for (SkinFilterCallback event : listeners) { @@ -27,14 +52,48 @@ public boolean shouldAllowTransparency(NativeImage image, boolean legacy) { }; }); - void processImage(NativeImage image, boolean legacy); + default NativeImage processImage(NativeImage image, int initialWidth, int initialHeight) { + processImage(image, isLegacyAspectRatio(initialWidth, initialHeight)); + return image; + } + + @Deprecated + default void processImage(NativeImage image, boolean legacy) {} + + default boolean shouldAllowTransparency(NativeImage image, int initialWidth, int initialHeight) { + return shouldAllowTransparency(image, isLegacyAspectRatio(initialWidth, initialHeight)); + } + @Deprecated(forRemoval = true) default boolean shouldAllowTransparency(NativeImage image, boolean legacy) { return true; // default is true since in most cases this is the desired effect } + /** + * Checks whether the incoming image size corresponds to the old 1:2 ratio player skins. + */ + static boolean isLegacyAspectRatio(int width, int height) { + return width == height * 2; + } + + /** + * Gets the scale of the image as a multiple of its vanilla image width. + */ + static int getResolutionScale(int width, int height) { + return width / VANILLA_SKIN_WIDTH; + } + + /** + * Fills a scaled section of an image with a solid color. + * @param image + * @param xFrom + * @param yFrom + * @param xTo + * @param yTo + * @param color + */ static void fill(NativeImage image, int xFrom, int yFrom, int xTo, int yTo, int color) { - int scale = image.getWidth() / 64; + int scale = getResolutionScale(image.getWidth(), image.getHeight()); image.fillRect(0, 32 * scale, 64 * scale, 32 * scale, 0); } @@ -55,7 +114,7 @@ static void copy(NativeImage image, int xOffset, int yOffset, int width, int height, boolean mirrorX, boolean mirrorY) { - int scale = image.getWidth() / 64; + int scale = getResolutionScale(image.getWidth(), image.getHeight()); image.copyRect( xFrom * scale, yFrom * scale, xOffset * scale, yOffset * scale, diff --git a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java index be440b2c..9e73043a 100644 --- a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java +++ b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java @@ -1,5 +1,7 @@ package com.minelittlepony.common.mixin; +import com.llamalad7.mixinextras.sugar.Share; +import com.llamalad7.mixinextras.sugar.ref.LocalIntRef; import com.minelittlepony.common.event.SkinFilterCallback; import net.minecraft.client.texture.NativeImage; import net.minecraft.client.texture.PlayerSkinTexture; @@ -17,33 +19,36 @@ public abstract class MixinPlayerSkinTexture extends ResourceTexture { private static final String FILTER_IMAGE = "remapTexture(Lnet/minecraft/client/texture/NativeImage;)Lnet/minecraft/client/texture/NativeImage;"; - private static final String STRIP_COLOR = "stripColor(Lnet/minecraft/client/texture/NativeImage;IIII)V"; - private static final String STRIP_ALPHA = "stripAlpha(Lnet/minecraft/client/texture/NativeImage;IIII)V"; - - private static boolean isLegacy; + private static final String STRIP_COLOR = "net/minecraft/client/texture/PlayerSkinTexture.stripColor(Lnet/minecraft/client/texture/NativeImage;IIII)V"; + private static final String STRIP_ALPHA = "net/minecraft/client/texture/PlayerSkinTexture.stripAlpha(Lnet/minecraft/client/texture/NativeImage;IIII)V"; @Inject(method = FILTER_IMAGE, at = @At("HEAD")) - private void beforeUpdate(NativeImage image, CallbackInfoReturnable info) { - isLegacy = image.getHeight() == 32; + private void beforeUpdate(NativeImage image, + @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, + @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, + CallbackInfoReturnable info) { + initialWidth.set(image.getWidth()); + initialHeight.set(image.getHeight()); } - @Inject(method = FILTER_IMAGE, at = @At("RETURN")) - private void update(NativeImage image, CallbackInfoReturnable ci) { + @Inject(method = FILTER_IMAGE, at = @At("RETURN"), cancellable = true) + private void update(NativeImage image, + @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, + @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, + CallbackInfoReturnable ci) { // convert skins from mojang server - SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), isLegacy); + ci.setReturnValue(SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), initialWidth.get(), initialHeight.get())); } // Sorry, Mahjon. Input validation is good 'n all, but this interferes with our other mods. - @Inject(method = STRIP_ALPHA, at = @At("HEAD"), cancellable = true) - private static void cancelAlphaStrip(NativeImage image, int beginX, int beginY, int endX, int endY, CallbackInfo info) { - if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, isLegacy)) { - info.cancel(); - } - } - - @Inject(method = STRIP_COLOR, at = @At("HEAD"), cancellable = true) - private static void cancelColorStrip(NativeImage image, int beginX, int beginY, int endX, int endY, CallbackInfo info) { - if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, isLegacy)) { + @Inject(method = FILTER_IMAGE, at = { + @At(value = "INVOKE", target = STRIP_ALPHA), + @At(value = "INVOKE", target = STRIP_COLOR) + }, cancellable = true) + private static void cancelAlphaStrip(NativeImage image, + @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, + @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, CallbackInfo info) { + if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth.get(), initialHeight.get())) { info.cancel(); } } diff --git a/src/main/java/com/minelittlepony/common/util/settings/Setting.java b/src/main/java/com/minelittlepony/common/util/settings/Setting.java index 31c335de..6458ccc8 100644 --- a/src/main/java/com/minelittlepony/common/util/settings/Setting.java +++ b/src/main/java/com/minelittlepony/common/util/settings/Setting.java @@ -14,7 +14,7 @@ /** * Any settings. */ -public interface Setting extends IChangeCallback { +public interface Setting extends IChangeCallback, Supplier { String name(); @NotNull @@ -32,6 +32,7 @@ default T getDefault() { /** * Gets the config value associated with this entry. */ + @Override @NotNull T get(); From 149f625f84a4ac4cf0178a86ee0a5b24c9c45b4e Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 14:25:53 +0000 Subject: [PATCH 3/8] Backport item stack sprite fixes --- .../client/gui/OutsideWorldRenderer.java | 4 ++- .../client/gui/sprite/ItemStackSprite.java | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/minelittlepony/common/client/gui/OutsideWorldRenderer.java b/src/main/java/com/minelittlepony/common/client/gui/OutsideWorldRenderer.java index 12c3094c..ee849359 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/OutsideWorldRenderer.java +++ b/src/main/java/com/minelittlepony/common/client/gui/OutsideWorldRenderer.java @@ -52,7 +52,9 @@ public static BlockEntityRenderDispatcher configure(@Nullable World world) { * @param y The top-Y position (in pixels) */ public static void renderStack(DrawContext context, ItemStack stack, int x, int y) { - configure(null); + try { + configure(null); + } catch (Throwable ignored) {} context.drawItem(stack, x, y); } } diff --git a/src/main/java/com/minelittlepony/common/client/gui/sprite/ItemStackSprite.java b/src/main/java/com/minelittlepony/common/client/gui/sprite/ItemStackSprite.java index 142a1118..c57db449 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/sprite/ItemStackSprite.java +++ b/src/main/java/com/minelittlepony/common/client/gui/sprite/ItemStackSprite.java @@ -8,12 +8,16 @@ import net.minecraft.component.type.DyedColorComponent; import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemStack; +import net.minecraft.util.Colors; public class ItemStackSprite implements ISprite { private ItemStack stack = ItemStack.EMPTY; - private int tint = 0xFFFFFFFF; + private int tint = Colors.WHITE; + + private boolean renderFailed; + private boolean needsWorld; public ItemStackSprite setStack(ItemConvertible iitem) { return setStack(new ItemStack(iitem)); @@ -21,18 +25,40 @@ public ItemStackSprite setStack(ItemConvertible iitem) { public ItemStackSprite setStack(ItemStack stack) { this.stack = stack; + renderFailed = false; + needsWorld = false; return setTint(tint); } public ItemStackSprite setTint(int tint) { + this.tint = tint; stack.set(DataComponentTypes.DYED_COLOR, new DyedColorComponent(tint, true)); return this; } @Override public void render(DrawContext context, int x, int y, int mouseX, int mouseY, float partialTicks) { - OutsideWorldRenderer.renderStack(context, stack, x + 2, y + 2); - RenderSystem.disableDepthTest(); + if (renderFailed) { + return; + } + + if (!needsWorld) { + try { + context.drawItem(stack, x + 2, y + 2); + RenderSystem.disableDepthTest(); + return; + } catch (Throwable ignored) { + needsWorld = true; + } + } + + try { + OutsideWorldRenderer.configure(null); + context.drawItem(stack, x + 2, y + 2); + RenderSystem.disableDepthTest(); + } catch (Throwable ignored) { + renderFailed = true; + } } } From fb18467016b441f7cda49cbedcdae69c4c0db2e9 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 14:30:32 +0000 Subject: [PATCH 4/8] Fix build --- .../common/mixin/MixinPlayerSkinTexture.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java index 9e73043a..8fb732ff 100644 --- a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java +++ b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java @@ -24,8 +24,8 @@ public abstract class MixinPlayerSkinTexture extends ResourceTexture { @Inject(method = FILTER_IMAGE, at = @At("HEAD")) private void beforeUpdate(NativeImage image, - @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, - @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, + @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, CallbackInfoReturnable info) { initialWidth.set(image.getWidth()); initialHeight.set(image.getHeight()); @@ -33,8 +33,8 @@ private void beforeUpdate(NativeImage image, @Inject(method = FILTER_IMAGE, at = @At("RETURN"), cancellable = true) private void update(NativeImage image, - @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, - @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, + @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, CallbackInfoReturnable ci) { // convert skins from mojang server ci.setReturnValue(SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), initialWidth.get(), initialHeight.get())); @@ -46,8 +46,8 @@ private void update(NativeImage image, @At(value = "INVOKE", target = STRIP_COLOR) }, cancellable = true) private static void cancelAlphaStrip(NativeImage image, - @Share(value = "initialWidth", namespace = "kirinmlp") LocalIntRef initialWidth, - @Share(value = "initialHeight", namespace = "kirinmlp") LocalIntRef initialHeight, CallbackInfo info) { + @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, CallbackInfo info) { if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth.get(), initialHeight.get())) { info.cancel(); } From 8592527c676b7514daf9ccf5496a3863f56addf5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 14:38:38 +0000 Subject: [PATCH 5/8] Fix fill method oopsie --- .../com/minelittlepony/common/event/SkinFilterCallback.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java index ed8a8666..7c1be98b 100644 --- a/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java +++ b/src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java @@ -94,7 +94,7 @@ static int getResolutionScale(int width, int height) { */ static void fill(NativeImage image, int xFrom, int yFrom, int xTo, int yTo, int color) { int scale = getResolutionScale(image.getWidth(), image.getHeight()); - image.fillRect(0, 32 * scale, 64 * scale, 32 * scale, 0); + image.fillRect(xFrom * scale, yFrom * scale, xTo * scale, yTo * scale, color); } /** From 64c355083d885f5df3a3cb395a19754cbc1ea571 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 15:01:24 +0000 Subject: [PATCH 6/8] Fix mixin --- .../common/mixin/MixinPlayerSkinTexture.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java index 8fb732ff..81b996b2 100644 --- a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java +++ b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java @@ -9,7 +9,6 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(PlayerSkinTexture.class) @@ -24,18 +23,18 @@ public abstract class MixinPlayerSkinTexture extends ResourceTexture { @Inject(method = FILTER_IMAGE, at = @At("HEAD")) private void beforeUpdate(NativeImage image, + CallbackInfoReturnable ci, @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, - @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, - CallbackInfoReturnable info) { + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) { initialWidth.set(image.getWidth()); initialHeight.set(image.getHeight()); } @Inject(method = FILTER_IMAGE, at = @At("RETURN"), cancellable = true) private void update(NativeImage image, + CallbackInfoReturnable ci, @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, - @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, - CallbackInfoReturnable ci) { + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) { // convert skins from mojang server ci.setReturnValue(SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), initialWidth.get(), initialHeight.get())); } @@ -45,11 +44,11 @@ private void update(NativeImage image, @At(value = "INVOKE", target = STRIP_ALPHA), @At(value = "INVOKE", target = STRIP_COLOR) }, cancellable = true) - private static void cancelAlphaStrip(NativeImage image, + private static void cancelAlphaStrip(NativeImage image, CallbackInfoReturnable ci, @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, - @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight, CallbackInfo info) { + @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) { if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth.get(), initialHeight.get())) { - info.cancel(); + ci.cancel(); } } // - From 2e301a5c0599b50824da0e4a5098cf9667937574 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 15:05:40 +0000 Subject: [PATCH 7/8] Fix mixin --- .../com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java index 81b996b2..6476ecb1 100644 --- a/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java +++ b/src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java @@ -44,7 +44,7 @@ private void update(NativeImage image, @At(value = "INVOKE", target = STRIP_ALPHA), @At(value = "INVOKE", target = STRIP_COLOR) }, cancellable = true) - private static void cancelAlphaStrip(NativeImage image, CallbackInfoReturnable ci, + private void cancelAlphaStrip(NativeImage image, CallbackInfoReturnable ci, @Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth, @Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) { if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth.get(), initialHeight.get())) { From ea226c9343303483f13f7bc2e098209d4a6b4d02 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 15:49:10 +0000 Subject: [PATCH 8/8] Add a selector element (Cycler but it holds generic values) --- .../common/client/gui/element/Cycler.java | 2 - .../common/client/gui/element/Selector.java | 77 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/minelittlepony/common/client/gui/element/Selector.java diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java b/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java index 0d1480e2..766428ce 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java @@ -9,8 +9,6 @@ /** * Represents a toggle button that switches between different * styles as you toggle through its different states. - *

- * "Iconic" here refers to how it uses an icon instead of text * * @author Sollace * diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java b/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java new file mode 100644 index 00000000..cef7d55c --- /dev/null +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java @@ -0,0 +1,77 @@ +package com.minelittlepony.common.client.gui.element; + +import java.util.function.Function; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.minelittlepony.common.client.gui.IField; +import com.minelittlepony.common.client.gui.style.IMultiStyled; +import com.minelittlepony.common.client.gui.style.Style; + +/** + * Represents a toggle button that switches between different + * values as you toggle through its different states. + * + * @author Sollace + * + */ +public class Selector extends Button implements IMultiStyled>, IField> { + private final Style defaultStyle; + private final T defaultValue; + private T value; + + @NotNull + private IChangeCallback action = IChangeCallback::none; + + private final Function styleFunction; + + public Selector(int x, int y, int width, int height, T value, Function styleFunction) { + super(x, y, width, height); + this.defaultValue = value; + this.value = value; + this.styleFunction = styleFunction; + this.defaultStyle = styleFunction.apply(value); + setStyle(this.defaultStyle); + } + + @Override + public Selector onChange(IChangeCallback action) { + this.action = action; + + return this; + } + + @Override + public T getValue() { + return value; + } + + @Override + public Selector setValue(@Nullable T value) { + this.value = value == null ? defaultValue : value; + this.setStyle(styleFunction.apply(this.value)); + + return this; + } + + /** + * Sets the styles to use for each state this toggle is able to be in. + * The number of styles here determines the number of possible states + * and the value is the index to the array of styles. + */ + @Override + public Selector setStyles(Style... styles) { + return this; + } + + @Override + public Style[] getStyles() { + return new Style[] {getStyle()}; + } + + @Override + public void onPress() { + setValue(action.perform(getValue())); + super.onPress(); + } +}