Skip to content

Commit

Permalink
Feat: Scroll Loupe)
Browse files Browse the repository at this point in the history
  • Loading branch information
worldwidepixel committed Jan 10, 2025
1 parent 066a965 commit ef5719a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/client/java/dev/spiritstudios/snapper/SnapperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public final class SnapperConfig extends Config<SnapperConfig> {
public final Value<Boolean> showSnapperGameMenu = booleanValue(true)
.comment("Whether to show Snapper button in game menu.")
.build();

public final Value<Boolean> viewMode = booleanValue(false)
.comment("Whether to show screenshot menu with grid or list.")
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@
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;
private ButtonWidget renameButton;
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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<File> 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<File> screenshots) {
super(Text.translatable("menu.snapper.viewermenu"));
this.parent = parent;

Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class ScreenshotListWidget extends AlwaysSelectedEntryListWidget<Screensh

public final CompletableFuture<List<ScreenshotEntry>> 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);
Expand All @@ -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
Expand All @@ -91,7 +94,7 @@ public CompletableFuture<List<ScreenshotEntry>> load(MinecraftClient client) {
return CompletableFuture.supplyAsync(() -> {
List<File> 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());
});
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -278,8 +283,9 @@ public class ScreenshotEntry extends Entry implements AutoCloseable {
private long time;
public final File screenshot;
private boolean showGrid;
private final List<File> screenshots;

public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent) {
public ScreenshotEntry(File screenshot, MinecraftClient client, Screen parent, List<File> screenshots) {
this.showGrid = ScreenshotListWidget.this.showGrid;
this.client = client;
this.screenParent = parent;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/client/resources/assets/snapper/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

1 comment on commit ef5719a

@blryface
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loupe 🧐

Please sign in to comment.