From ef5719aa992e149e667fad68a92b9195cf4ccd41 Mon Sep 17 00:00:00 2001 From: worldwidepixel Date: Thu, 9 Jan 2025 22:51:47 -0800 Subject: [PATCH] Feat: Scroll Loupe) --- .../spiritstudios/snapper/SnapperConfig.java | 4 ++ .../snapper/gui/screen/ScreenshotScreen.java | 36 ++++++++++-- .../gui/screen/ScreenshotViewerScreen.java | 58 ++++++++++++++++--- .../gui/widget/ScreenshotListWidget.java | 15 +++-- .../resources/assets/snapper/lang/en_us.json | 3 +- 5 files changed, 99 insertions(+), 17 deletions(-) diff --git a/src/client/java/dev/spiritstudios/snapper/SnapperConfig.java b/src/client/java/dev/spiritstudios/snapper/SnapperConfig.java index abe651f..8d54ebd 100644 --- a/src/client/java/dev/spiritstudios/snapper/SnapperConfig.java +++ b/src/client/java/dev/spiritstudios/snapper/SnapperConfig.java @@ -21,4 +21,8 @@ public final class SnapperConfig extends Config { public final Value showSnapperGameMenu = booleanValue(true) .comment("Whether to show Snapper button in game menu.") .build(); + + public final Value viewMode = booleanValue(false) + .comment("Whether to show screenshot menu with grid or list.") + .build(); } \ No newline at end of file diff --git a/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotScreen.java b/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotScreen.java index d0bcb80..84f24ff 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotScreen.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotScreen.java @@ -27,7 +27,7 @@ public class ScreenshotScreen extends Screen { private static final Identifier PANORAMA_BUTTON_ICON = Identifier.of(MODID, "screenshots/panorama"); private static final Identifier SETTINGS_ICON = Identifier.of(MODID, "screenshots/settings"); - private static final Identifier VIEW_MODE_ICON = Identifier.of(MODID, "screenshots/show_list"); + private static Identifier VIEW_MODE_ICON; private final Screen parent; ScreenshotListWidget screenshotList; private ButtonWidget deleteButton; @@ -35,11 +35,16 @@ public class ScreenshotScreen extends Screen { private ButtonWidget viewButton; private ButtonWidget copyButton; private ButtonWidget openButton; + private TextIconButtonWidget viewModeButton; private ScreenshotListWidget.@Nullable ScreenshotEntry selectedScreenshot = null; + private boolean showGrid = false; public ScreenshotScreen(Screen parent) { super(Text.translatable("menu.snapper.screenshotmenu")); this.parent = parent; + + this.showGrid = SnapperConfig.INSTANCE.viewMode.get(); + VIEW_MODE_ICON = showGrid ? Identifier.of(MODID, "screenshots/show_list") : Identifier.of(MODID, "screenshots/show_grid"); } @Override @@ -145,11 +150,10 @@ protected void init() { settingsButton.setPosition(width / 2 - 178, height - 32); - TextIconButtonWidget viewModeButton = addDrawableChild(TextIconButtonWidget.builder( - Text.translatable("config.snapper.snapper.view_mode"), + this.viewModeButton = addDrawableChild(TextIconButtonWidget.builder( + Text.translatable("config.snapper.snapper.viewMode"), button -> { - screenshotList.toggleGrid(); - screenshotList.refreshScroll(); + this.toggleGrid(); }, true ).width(20).texture(VIEW_MODE_ICON, 15, 15).build()); @@ -178,6 +182,23 @@ public void imageSelected(@Nullable ScreenshotListWidget.ScreenshotEntry screens this.selectedScreenshot = screenshot; } + public void toggleGrid() { + screenshotList.toggleGrid(); + screenshotList.refreshScroll(); + this.showGrid = !this.showGrid; + VIEW_MODE_ICON = showGrid ? Identifier.of(MODID, "screenshots/show_list") : Identifier.of(MODID, "screenshots/show_grid"); + + remove(this.viewModeButton); + this.viewModeButton = addDrawableChild(TextIconButtonWidget.builder( + Text.translatable("config.snapper.snapper.viewMode"), + button -> { + this.toggleGrid(); + }, + true + ).width(20).texture(VIEW_MODE_ICON, 15, 15).build()); + viewModeButton.setPosition(width / 2 - 178, height - 56); + } + @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if (super.keyPressed(keyCode, scanCode, modifiers)) return true; @@ -207,6 +228,11 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) { return false; } + @Override + public void close() { + SnapperConfig.HOLDER.save(); + super.close(); + } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { diff --git a/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotViewerScreen.java b/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotViewerScreen.java index c7d6fd9..39de2a6 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotViewerScreen.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/screen/ScreenshotViewerScreen.java @@ -16,12 +16,15 @@ import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.Util; +import org.jetbrains.annotations.Nullable; +import org.lwjgl.glfw.GLFW; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.util.List; public class ScreenshotViewerScreen extends Screen { private static final Identifier MENU_DECOR_BACKGROUND_TEXTURE = Identifier.ofVanilla("textures/gui/menu_list_background.png"); @@ -33,9 +36,15 @@ public class ScreenshotViewerScreen extends Screen { private final int imageHeight; private final Screen parent; private final File screenshot; + private final @Nullable List screenshots; + private final int screenshotIndex; private Path iconPath; public ScreenshotViewerScreen(ScreenshotImage icon, File screenshot, Screen parent) { + this(icon, screenshot, parent, null); + } + + public ScreenshotViewerScreen(ScreenshotImage icon, File screenshot, Screen parent, @Nullable List screenshots) { super(Text.translatable("menu.snapper.viewermenu")); this.parent = parent; @@ -62,6 +71,13 @@ public ScreenshotViewerScreen(ScreenshotImage icon, File screenshot, Screen pare this.imageHeight = image != null ? image.getHeight() : 0; this.screenshot = screenshot; + this.screenshots = screenshots; + + if (this.screenshots != null) { + this.screenshotIndex = this.screenshots.indexOf(this.screenshot); + } else { + this.screenshotIndex = -1; + } } @Override @@ -72,6 +88,13 @@ public void close() { @Override protected void init() { + // OPEN IMAGE EXTERNALLY + + ButtonWidget openButton = addDrawableChild(ButtonWidget.builder( + Text.translatable("button.snapper.open"), + button -> Util.getOperatingSystem().open(this.iconPath) + ).width(100).build()); + // OPEN FOLDER ButtonWidget folderButton = addDrawableChild(ButtonWidget.builder( @@ -82,13 +105,6 @@ protected void init() { .build() ); - // OPEN IMAGE EXTERNALLY - - ButtonWidget openButton = addDrawableChild(ButtonWidget.builder( - Text.translatable("button.snapper.open"), - button -> Util.getOperatingSystem().open(this.iconPath) - ).width(100).build()); - // EXIT PAGE ButtonWidget doneButton = addDrawableChild(ButtonWidget.builder( @@ -258,4 +274,32 @@ private void drawHeaderAndFooterSeparators(DrawContext context) { RenderSystem.disableBlend(); } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + Snapper.LOGGER.debug(String.format("SCROLL DEBUG 1 %s %s", this.screenshotIndex, this.screenshots == null)); + if (this.screenshotIndex != -1 && this.screenshots != null) { + Snapper.LOGGER.debug(String.format("SCROLL DEBUG 2 %s %s", this.screenshots.size(), this.screenshotIndex)); + if (keyCode == GLFW.GLFW_KEY_LEFT) { + File previousImageFile = screenshots.getLast();; + if (this.screenshotIndex >= 1) { + previousImageFile = screenshots.get(screenshotIndex - 1); + } + ScreenshotImage previousImage = ScreenshotImage.of(previousImageFile, client.getTextureManager()); + Snapper.LOGGER.debug(String.format("SCROLL DEBUG 3a %s", previousImageFile.getName())); + client.setScreen(new ScreenshotViewerScreen(previousImage, previousImageFile, this.parent, this.screenshots)); + } + if (keyCode == GLFW.GLFW_KEY_RIGHT) { + File nextImageFile = screenshots.getFirst(); + if (this.screenshotIndex < this.screenshots.size() - 1) { + nextImageFile = screenshots.get(screenshotIndex + 1); + } + ScreenshotImage nextImage = ScreenshotImage.of(nextImageFile, client.getTextureManager()); + Snapper.LOGGER.debug(String.format("SCROLL DEBUG 3b %s", nextImageFile.getName())); + client.setScreen(new ScreenshotViewerScreen(nextImage, nextImageFile, this.parent, this.screenshots)); + } + } + + return super.keyPressed(keyCode, scanCode, modifiers); + } } diff --git a/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java b/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java index b794fa3..c4c916f 100644 --- a/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java +++ b/src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java @@ -2,6 +2,7 @@ import com.mojang.blaze3d.systems.RenderSystem; import dev.spiritstudios.snapper.Snapper; +import dev.spiritstudios.snapper.SnapperConfig; import dev.spiritstudios.snapper.gui.screen.ScreenshotScreen; import dev.spiritstudios.snapper.gui.screen.ScreenshotViewerScreen; import dev.spiritstudios.snapper.util.ScreenshotActions; @@ -50,7 +51,7 @@ public class ScreenshotListWidget extends AlwaysSelectedEntryListWidget> loadFuture; - private boolean showGrid = true; + private boolean showGrid = false; public ScreenshotListWidget(MinecraftClient client, int width, int height, int y, int itemHeight, @Nullable ScreenshotListWidget previous, Screen parent) { super(client, width, height, y, itemHeight); @@ -69,6 +70,8 @@ public ScreenshotListWidget(MinecraftClient client, int width, int height, int y this.addEntry(new EmptyEntry(client)); } }); + + this.showGrid = SnapperConfig.INSTANCE.viewMode.get(); } @Override @@ -91,7 +94,7 @@ public CompletableFuture> load(MinecraftClient client) { return CompletableFuture.supplyAsync(() -> { List screenshots = ScreenshotActions.getScreenshots(client); return screenshots.parallelStream() - .map(file -> new ScreenshotEntry(file, client, parent)) + .map(file -> new ScreenshotEntry(file, client, parent, screenshots)) .collect(Collectors.toList()); }); } @@ -144,6 +147,8 @@ protected int getMaxPosition() { public void toggleGrid() { this.showGrid = !this.showGrid; for (var entry : this.children()) if (entry instanceof ScreenshotEntry sc) sc.setShowGrid(this.showGrid); + + SnapperConfig.INSTANCE.viewMode.set(this.showGrid); } @Override @@ -278,8 +283,9 @@ public class ScreenshotEntry extends Entry implements AutoCloseable { private long time; public final File screenshot; private boolean showGrid; + private final List screenshots; - public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent) { + public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent, List screenshots) { this.showGrid = ScreenshotListWidget.this.showGrid; this.client = client; this.screenParent = parent; @@ -288,6 +294,7 @@ public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent) { this.iconFileName = screenshot.getName(); this.lastModified = screenshot.lastModified(); this.screenshot = screenshot; + this.screenshots = screenshots; this.loadIcon(); } @@ -410,7 +417,7 @@ public boolean click() { if (this.icon == null) return false; this.client.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); - this.client.setScreen(new ScreenshotViewerScreen(this.icon, this.screenshot, this.screenParent)); + this.client.setScreen(new ScreenshotViewerScreen(this.icon, this.screenshot, this.screenParent, this.screenshots)); return true; } diff --git a/src/client/resources/assets/snapper/lang/en_us.json b/src/client/resources/assets/snapper/lang/en_us.json index b4570b6..28d38cc 100644 --- a/src/client/resources/assets/snapper/lang/en_us.json +++ b/src/client/resources/assets/snapper/lang/en_us.json @@ -40,5 +40,6 @@ "config.snapper.snapper.showSnapperTitleScreen.tooltip": "Show Snapper button on title screen", "config.snapper.snapper.showSnapperGameMenu": "Game Menu Button", "config.snapper.snapper.showSnapperGameMenu.tooltip": "Show Snapper button in game menu", - "config.snapper.snapper.view_mode": "View Mode" + "config.snapper.snapper.viewMode": "View Mode", + "config.snapper.snapper.viewMode.tooltip": "Show screenshot menu with grid or list" } \ No newline at end of file