Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.nio.file.Paths

plugins {
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'fabric-loom' version '1.15-SNAPSHOT'
id 'maven-publish'
}

Expand Down Expand Up @@ -63,8 +63,6 @@ java {
if (JavaVersion.current() < javaVersion) {
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
}
libsDirName = Paths.get(libsDirName).resolve("${project.compatible_mc_version}").toString()
archivesBaseName = project.archives_base_name
version = version.concat("-${project.mod_loader}-mc${project.compatible_mc_version}")
withSourcesJar()
}
Expand Down
15 changes: 8 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true

# Mod Properties
mod_version=1.3.4
maven_group=io.github.lgatodu47
archives_base_name=screenshot_viewer

# Project properties
compatible_mc_version=1.21.4
compatible_mc_version=1.21.11
mod_loader=fabric

# Fabric Properties
minecraft_version=1.21.4
yarn_mappings=1.21.4+build.8
loader_version=0.16.10
minecraft_version=1.21.11
yarn_mappings=1.21.11+build.4
loader_version=0.18.4

# Dependencies
fabric_version=0.118.0+1.21.4
modmenu_version=13.0.2
catconfigmc_version=1.21.4-0.2.1
fabric_version=0.141.2+1.21.11
modmenu_version=17.0.0-beta.2
catconfigmc_version=1.21.11-0.2.1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onInitializeClient() {
}

private void initKeyBindings() {
openScreenshotsScreenKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(ScreenshotViewerTexts.translation("key", "open_screenshots_screen"), InputUtil.UNKNOWN_KEY.getCode(), KeyBinding.MISC_CATEGORY));
openScreenshotsScreenKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(ScreenshotViewerTexts.translation("key", "open_screenshots_screen"), InputUtil.UNKNOWN_KEY.getCode(), KeyBinding.Category.MISC));
}

private static final Identifier DELAYED_PHASE = Identifier.of(MODID, "delayed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ca.weblite.objc.Client;
import ca.weblite.objc.Proxy;
import com.mojang.logging.LogUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Util;

public class ScreenshotViewerMacOsUtils {
// Code taken from ScreenshotToClipboard: https://github.com/comp500/ScreenshotToClipboard
Expand Down Expand Up @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/
public static void doCopyMacOS(String path) {
if (!MinecraftClient.IS_SYSTEM_MAC) {
if (Util.getOperatingSystem() != Util.OperatingSystem.OSX) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.lgatodu47.screenshot_viewer;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.logging.LogUtils;
import io.github.lgatodu47.screenshot_viewer.screen.ScreenshotViewerTexts;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -15,7 +12,7 @@
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.gui.tooltip.TooltipPositioner;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.render.*;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
Expand All @@ -24,7 +21,6 @@
import net.minecraft.util.math.ColorHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.slf4j.Logger;

import javax.imageio.ImageIO;
Expand All @@ -35,7 +31,6 @@
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -64,26 +59,12 @@ public static List<File> getScreenshotFiles(File screenshotsFolder) {
return Arrays.stream(files).filter(file -> file.isFile() && (file.getName().endsWith(".png") || file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg"))).collect(Collectors.toList());
}

public static void drawTexture(DrawContext context, int x, int y, int width, int height, int u, int v, int regionWidth, int regionHeight, int textureWidth, int textureHeight) {
int x2 = x + width;
int y2 = y + height;
float u1 = u / (float) textureWidth;
float u2 = (u + (float) regionWidth) / (float) textureWidth;
float v1 = v / (float) textureHeight;
float v2 = (v + (float) regionHeight) / (float) textureHeight;

Matrix4f matrix4f = context.getMatrices().peek().getPositionMatrix();
BufferBuilder bufferBuilder = Tessellator.getInstance().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
bufferBuilder.vertex(matrix4f, x, y, 0).texture(u1, v1);
bufferBuilder.vertex(matrix4f, x, y2, 0).texture(u1, v2);
bufferBuilder.vertex(matrix4f, x2, y2, 0).texture(u2, v2);
bufferBuilder.vertex(matrix4f, x2, y, 0).texture(u2, v1);
BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
public static void drawTexture(DrawContext context, Identifier texture, int x, int y, int width, int height, int u, int v, int regionWidth, int regionHeight, int textureWidth, int textureHeight) {
context.drawTexture(RenderPipelines.GUI_TEXTURED, texture, x, y, (float)u, (float)v, width, height, regionWidth, regionHeight, textureWidth, textureHeight);
}

@Nullable
private static Clipboard tryGetAWTClipboard() {
if(MinecraftClient.IS_SYSTEM_MAC) {
if(Util.getOperatingSystem() == Util.OperatingSystem.OSX) {
return null;
}
try {
Expand All @@ -95,7 +76,7 @@ private static Clipboard tryGetAWTClipboard() {
}

public static void copyImageToClipboard(File screenshotFile) {
if(MinecraftClient.IS_SYSTEM_MAC) {
if(Util.getOperatingSystem() == Util.OperatingSystem.OSX) {
ScreenshotViewerMacOsUtils.doCopyMacOS(screenshotFile.getAbsolutePath());
return;
}
Expand Down Expand Up @@ -126,60 +107,12 @@ public static List<TooltipComponent> toColoredComponents(MinecraftClient client,
return Tooltip.wrapLines(client, text).stream().map(ColoredTooltipComponent::new).collect(Collectors.toList());
}

private static Method DRAW_TOOLTIP;
private static boolean errorLogged;

public static void renderTooltip(DrawContext context, TextRenderer textRenderer, List<TooltipComponent> tooltipComponents, int posX, int posY) {
if(DRAW_TOOLTIP == null) {
try {
MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver();
String methodName = mappingResolver.mapMethodName("intermediary", "net.minecraft.class_332", "method_51435", "(Lnet/minecraft/class_327;Ljava/util/List;IILnet/minecraft/class_8000;Lnet/minecraft/class_2960;)V");
DRAW_TOOLTIP = DrawContext.class.getDeclaredMethod(methodName, TextRenderer.class, List.class, int.class, int.class, TooltipPositioner.class, Identifier.class);
} catch (NoSuchMethodException e) {
if(!errorLogged) {
LOGGER.error("Failed to render Screenshot Viewer tooltip", e);
errorLogged = true;
}
return;
}
DRAW_TOOLTIP.setAccessible(true);
}
try {
DRAW_TOOLTIP.invoke(context, textRenderer, tooltipComponents, posX, posY, HoveredTooltipPositioner.INSTANCE, null);
} catch (Exception e) {
if(!errorLogged) {
LOGGER.error("Failed to render Screenshot Viewer tooltip", e);
errorLogged = true;
}
}
public static void renderTooltip(DrawContext context, TextRenderer textRenderer, List<OrderedText> text, int posX, int posY) {
context.drawTooltip(textRenderer, text, HoveredTooltipPositioner.INSTANCE, posX, posY, false);
}

private static Method RENDER_WIDGET;
private static boolean errorLogged1;

public static void renderWidget(ClickableWidget widget, DrawContext context, int mouseX, int mouseY, float delta) {
if(RENDER_WIDGET == null) {
try {
MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver();
String methodName = mappingResolver.mapMethodName("intermediary", "net.minecraft.class_339", "method_48579", "(Lnet/minecraft/class_332;IIF)V");
RENDER_WIDGET = ClickableWidget.class.getDeclaredMethod(methodName, DrawContext.class, int.class, int.class, float.class);
} catch (NoSuchMethodException e) {
if(!errorLogged1) {
LOGGER.error("Failed to render widget", e);
errorLogged1 = true;
}
return;
}
RENDER_WIDGET.setAccessible(true);
}
try {
RENDER_WIDGET.invoke(widget, context, mouseX, mouseY, delta);
} catch (Exception e) {
if(!errorLogged1) {
LOGGER.error("Failed to render widget", e);
errorLogged1 = true;
}
}
widget.render(context, mouseX, mouseY, delta);
}

public static void forEachDrawable(Screen screen, Consumer<Drawable> renderer) {
Expand Down Expand Up @@ -208,15 +141,8 @@ public int getHeight(TextRenderer textRenderer) {
}

@Override
public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) {
float[] colors = RenderSystem.getShaderColor();
if(colors.length != 4) {
colors = new float[]{0, 0, 0, 0};
}
// game tweaks the alpha value for some reason (see TextRenderer#tweakTransparency)
int alpha = Math.max((int) (colors[3] * 255), 5);
int textColor = ColorHelper.getArgb(alpha, (int) (colors[0] * 255), (int) (colors[1] * 255), (int) (colors[2] * 255));
textRenderer.draw(this.text, x, y, textColor, true, matrix, vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, 0xF000F0);
public void drawText(DrawContext context, TextRenderer textRenderer, int x, int y) {
context.drawText(textRenderer, this.text, x, y, -1, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.input.KeyInput;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -79,16 +79,16 @@ public ColorTextField(TextRenderer renderer, int x, int y, int width, int height
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (isActive() && Screen.isPaste(keyCode)) {
public boolean keyPressed(KeyInput input) {
if (isActive() && input.isPaste()) {
String clipboard = MinecraftClient.getInstance().keyboard.getClipboard();
if(clipboard.startsWith("#") && clipboard.length() < 8) {
clipboard = clipboard.substring(1);
}
this.write(clipboard);
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
return super.keyPressed(input);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.Click;
import net.minecraft.client.input.KeyInput;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -56,6 +58,7 @@ public static ClickableWidget createDirectoryWidget(ConfigAccess config, ConfigO
Supplier<File> defaultValue = () -> Objects.requireNonNull(option.defaultValue());
widget.setText(config.get(option).orElseGet(defaultValue).getAbsolutePath());
AtomicBoolean corrected = new AtomicBoolean(false);
widget.addFormatter((text, firstCharacterIndex) -> OrderedText.styledForwardsVisitedString(text, corrected.get() ? Style.EMPTY.withColor(Formatting.RED) : Style.EMPTY));
widget.setAcceptChangesListener(() -> {
File target = new File(widget.getText());
if(target.exists() && target.isAbsolute() && target.isDirectory() && target.canRead()) {
Expand All @@ -66,7 +69,6 @@ public static ClickableWidget createDirectoryWidget(ConfigAccess config, ConfigO
widget.setText(defaultValue.get().getAbsolutePath());
corrected.set(true);
});
widget.setRenderTextProvider((text, i) -> OrderedText.styledForwardsVisitedString(text, corrected.get() ? Style.EMPTY.withColor(Formatting.RED) : Style.EMPTY));
return widget;
}

Expand Down Expand Up @@ -108,24 +110,24 @@ public void renderButton(DrawContext context, int mouseX, int mouseY, float delt
}*/

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
boolean res = super.mouseClicked(mouseX, mouseY, button);
public boolean mouseClicked(Click click, boolean doubled) {
boolean res = super.mouseClicked(click, doubled);
if(res && acceptChangesListener != null) {
acceptChangesListener.run();
}
return res;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
public boolean keyPressed(KeyInput input) {
if(!isActive()) {
return false;
}
if(keyCode == GLFW.GLFW_KEY_ENTER && acceptChangesListener != null) {
if(input.key() == GLFW.GLFW_KEY_ENTER && acceptChangesListener != null) {
acceptChangesListener.run();
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
return super.keyPressed(input);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,40 @@
import io.github.lgatodu47.screenshot_viewer.ScreenshotViewer;
import io.github.lgatodu47.screenshot_viewer.config.ScreenshotViewerOptions;
import io.github.lgatodu47.screenshot_viewer.screen.manage_screenshots.ManageScreenshotsScreen;
import io.github.lgatodu47.screenshot_viewer.screen.ScreenshotClickEvent;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.Style;
import org.jetbrains.annotations.Nullable;
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.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.File;

@Mixin(Screen.class)
public abstract class ScreenMixin {
@Shadow @Nullable protected MinecraftClient client;

@Shadow public abstract boolean handleTextClick(@Nullable Style style);
@Inject(method = "handleClickEvent", at = @At("HEAD"), cancellable = true)
private static void screenshot_viewer$inject_handleClickEvent(ClickEvent clickEvent, MinecraftClient client, @Nullable Screen screenAfterRun, CallbackInfo ci) {
if (!ScreenshotViewer.getInstance().getConfig().getOrFallback(ScreenshotViewerOptions.REDIRECT_SCREENSHOT_CHAT_LINKS, false)) {
return;
}
if (!(clickEvent instanceof ClickEvent.OpenFile openFile)) {
return;
}

@Inject(method = "handleTextClick", at = @At("HEAD"), cancellable = true)
private void screenshot_viewer$inject_handleTextClick(Style style, CallbackInfoReturnable<Boolean> cir) {
if(style == null || Screen.hasShiftDown()) {
File file = openFile.file();
if (!file.isFile()) {
return;
}
if(style.getClickEvent() instanceof ScreenshotClickEvent event) {
if(ScreenshotViewer.getInstance().getConfig().getOrFallback(ScreenshotViewerOptions.REDIRECT_SCREENSHOT_CHAT_LINKS, false)) {
//noinspection DataFlowIssue
this.client.setScreen(new ManageScreenshotsScreen((Screen) (Object) this, event.getScreenshotFile()));
cir.setReturnValue(true);
return;
}
cir.setReturnValue(this.handleTextClick(style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, event.getScreenshotFile().getAbsolutePath()))));

String name = file.getName().toLowerCase();
if (!name.endsWith(".png") && !name.endsWith(".jpg") && !name.endsWith(".jpeg") &&
!name.endsWith(".bmp") && !name.endsWith(".tga") && !name.endsWith(".tiff")) {
return;
}

client.setScreen(new ManageScreenshotsScreen(screenAfterRun, file));
ci.cancel();
}
}
Loading