Skip to content

Commit

Permalink
chore: sweep sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Sep 4, 2024
1 parent 0f0a134 commit b4fddc6
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 153 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {

modImplementation("net.fabricmc.fabric-api:fabric-api:${deps.fabricApi}")

implementation ("ca.weblite:java-objc-bridge:1.0.0")
implementation("ca.weblite:java-objc-bridge:1.0.0")
}

tasks.processResources {
Expand Down
39 changes: 12 additions & 27 deletions src/client/java/dev/spiritstudios/snapper/Snapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import dev.spiritstudios.snapper.gui.ScreenshotScreen;
import dev.spiritstudios.snapper.gui.ScreenshotViewerScreen;
import dev.spiritstudios.snapper.util.ScreenshotImage;
import dev.spiritstudios.snapper.util.*;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,7 +21,6 @@
import java.util.*;

public class Snapper implements ClientModInitializer {
private final MinecraftClient client = MinecraftClient.getInstance();
public static final String MODID = "snapper";
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);
public static final boolean IS_IRIS_INSTALLED = FabricLoader.getInstance().isModLoaded("iris");
Expand Down Expand Up @@ -61,37 +61,22 @@ public void onInitializeClient() {
client.player.sendMessage(Text.translatable("text.snapper.panorama_success", SCREENSHOT_MENU_KEY.getBoundKeyLocalizedText()), true);
}
while (RECENT_SCREENSHOT_KEY.wasPressed()) {
File latestScreenshot = ScreenshotActions.getScreenshots(client).getFirst();

client.setScreen(new ScreenshotViewerScreen(
ScreenshotImage.of(getLatestScreenshot()),
getLatestScreenshot(),
ScreenshotImage.of(latestScreenshot, client.getTextureManager()),
latestScreenshot,
null
));
}
});
}

private File getLatestScreenshot() {
File screenshotDir = new File(client.runDirectory, "screenshots");

File[] files = screenshotDir.listFiles();
List<File> screenshots = new ArrayList<>(List.of(files == null ? new File[0] : files));

screenshots.removeIf(file -> {
if (Files.isDirectory(file.toPath())) return true;
String fileType;

try {
fileType = Files.probeContentType(file.toPath());
} catch (IOException e) {
Snapper.LOGGER.error("Couldn't load screenshot list", e);
return true;
}

return !Objects.equals(fileType, "image/png");
});

screenshots.sort(Comparator.comparingLong(File::lastModified).reversed());

return screenshots.getFirst();
public static PlatformHelper getPlatformHelper() {
return switch (Util.getOperatingSystem()) {
case WINDOWS -> new WindowsActions();
case OSX -> new MacActions();
default -> new WindowsActions();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dev.spiritstudios.snapper.Snapper;
import dev.spiritstudios.snapper.util.ScreenshotImage;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.CubeMapRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
Expand Down Expand Up @@ -32,14 +31,13 @@ public class PanoramaViewerScreen extends Screen {
protected static final CubeMapRenderer FALLBACK_PANORAMA_RENDERER = new CubeMapRenderer(Identifier.ofVanilla("textures/gui/title/background/panorama"));
protected static final RotatingCubeMapRenderer PANORAMA_RENDERER_CUBE = new RotatingCubeMapRenderer(PANORAMA_RENDERER);
protected static final RotatingCubeMapRenderer FALLBACK_PANORAMA_RENDERER_CUBE = new RotatingCubeMapRenderer(FALLBACK_PANORAMA_RENDERER);
private static final MinecraftClient client = MinecraftClient.getInstance();

private final String title;
private final Screen parent;
private boolean doBackgroundFade = true;
private long backgroundFadeStart;
private boolean loaded;
private float backgroundAlpha;
private final Screen parent;

protected PanoramaViewerScreen(String title, Screen parent) {
super(Text.translatable("menu.snapper.viewermenu"));
Expand All @@ -50,13 +48,13 @@ protected PanoramaViewerScreen(String title, Screen parent) {

private void load() {
List<File> panorama = this.loadPanorama();
if (panorama == null) return;
if (panorama == null || client == null) return;

panorama.parallelStream().map(face -> {
ScreenshotImage icon = ScreenshotImage.forPanoramaFace(client.getTextureManager(), face.getName());
ScreenshotImage icon = ScreenshotImage.forPanoramaFace(this.client.getTextureManager(), face.getName());
this.loadIcon(icon, face.getName(), Path.of(face.getPath()));
return icon;
}).toList().forEach(ScreenshotImage::joinLoad);
}).forEach(ScreenshotImage::joinLoad);

this.loaded = true;
}
Expand All @@ -76,12 +74,16 @@ private void loadIcon(ScreenshotImage icon, String fileName, Path filePath) {

@Nullable
private List<File> loadPanorama() {
File panoramaDir = new File(client.runDirectory, "screenshots/panorama");
if (client == null) return null;

File panoramaDir = new File(this.client.runDirectory, "screenshots/panorama");
List<File> panoramaFaces;
if (!Files.exists(panoramaDir.toPath())) return null;

File[] faceFiles = panoramaDir.listFiles();
panoramaFaces = new ArrayList<>(List.of(faceFiles == null ? new File[0] : faceFiles));
if (faceFiles == null) return new ArrayList<>();
panoramaFaces = new ArrayList<>(List.of(faceFiles));

panoramaFaces.removeIf(file -> {
if (Files.isDirectory(file.toPath())) return true;
String fileType;
Expand All @@ -101,12 +103,15 @@ private List<File> loadPanorama() {

@Override
public void close() {
if (client == null) return;
client.setScreen(this.parent);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
protected void init() {
if (client == null) return;

File panoramaDirectory = new File(client.runDirectory, "screenshots/panorama");
addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.folder"), button -> {
if (!panoramaDirectory.exists()) new File(String.valueOf(panoramaDirectory)).mkdirs();
Expand Down
48 changes: 22 additions & 26 deletions src/client/java/dev/spiritstudios/snapper/gui/ScreenshotScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.lwjgl.glfw.GLFW;

import java.io.File;
import java.io.IOException;

import static dev.spiritstudios.snapper.Snapper.MODID;

Expand Down Expand Up @@ -59,38 +58,43 @@ protected void init() {
.build()
);

ButtonWidget doneButton = addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, button -> this.close())
.width(100)
.build()
ButtonWidget doneButton = addDrawableChild(
ButtonWidget.builder(ScreenTexts.DONE, button -> this.close())
.width(100)
.build()
);

this.deleteButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.delete"), button -> {
this.deleteButton = addDrawableChild(
ButtonWidget.builder(Text.translatable("button.snapper.delete"), button -> {
if (selectedScreenshot != null)
ScreenshotActions.deleteScreenshot(selectedScreenshot.screenshot, this);
})
.width(74)
.build()
);

this.openButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.open"), button -> {
this.openButton = addDrawableChild(
ButtonWidget.builder(Text.translatable("button.snapper.open"), button -> {
if (selectedScreenshot != null)
Util.getOperatingSystem().open(selectedScreenshot.screenshot);
})
.width(74)
.build()
);

this.renameButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.rename"), button -> {
this.renameButton = addDrawableChild(
ButtonWidget.builder(Text.translatable("button.snapper.rename"), button -> {
if (this.selectedScreenshot != null)
client.setScreen(new RenameScreenshotScreen(this.selectedScreenshot.screenshot, this));
})
.width(74)
.build()
);

this.copyButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.copy"), button -> {
this.copyButton = addDrawableChild(
ButtonWidget.builder(Text.translatable("button.snapper.copy"), button -> {
if (selectedScreenshot != null)
ScreenshotActions.copyScreenshot(selectedScreenshot.screenshot);
Snapper.getPlatformHelper().copyScreenshot(selectedScreenshot.screenshot);
})
.width(74)
.build()
Expand Down Expand Up @@ -126,28 +130,20 @@ protected void init() {
}

public void imageSelected(@Nullable ScreenshotListWidget.ScreenshotEntry screenshot) {
if (screenshot == null) {
this.copyButton.active = false;
this.deleteButton.active = false;
this.openButton.active = false;
this.renameButton.active = false;
this.viewButton.active = false;
this.selectedScreenshot = null;
} else {
this.copyButton.active = true;
this.deleteButton.active = true;
this.openButton.active = true;
this.renameButton.active = true;
this.viewButton.active = true;
this.selectedScreenshot = screenshot;
}
boolean hasScreenshot = screenshot != null;
this.copyButton.active = hasScreenshot;
this.deleteButton.active = hasScreenshot;
this.openButton.active = hasScreenshot;
this.renameButton.active = hasScreenshot;
this.viewButton.active = hasScreenshot;
this.selectedScreenshot = screenshot;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
long handle = MinecraftClient.getInstance().getWindow().getHandle();
if (super.keyPressed(keyCode, scanCode, modifiers)) return true;

long handle = MinecraftClient.getInstance().getWindow().getHandle();
if (keyCode == GLFW.GLFW_KEY_F5) {
if (client == null) return false;

Expand All @@ -157,7 +153,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {

if ((InputUtil.isKeyPressed(handle, GLFW.GLFW_KEY_LEFT_CONTROL) || InputUtil.isKeyPressed(handle, GLFW.GLFW_KEY_RIGHT_CONTROL)) && InputUtil.isKeyPressed(handle, InputUtil.GLFW_KEY_C)) {
if (selectedScreenshot != null) {
ScreenshotActions.copyScreenshot(selectedScreenshot.screenshot);
Snapper.getPlatformHelper().copyScreenshot(selectedScreenshot.screenshot);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected void init() {

// COPY SCREENSHOT

ButtonWidget copyButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.copy"), button -> ScreenshotActions.copyScreenshot(this.screenshot))
ButtonWidget copyButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.copy"), button -> Snapper.getPlatformHelper().copyScreenshot(this.screenshot))
.width(100)
.build()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.spiritstudios.snapper.Snapper;
import dev.spiritstudios.snapper.gui.ScreenshotScreen;
import dev.spiritstudios.snapper.gui.ScreenshotViewerScreen;
import dev.spiritstudios.snapper.util.ScreenshotActions;
import dev.spiritstudios.snapper.util.ScreenshotImage;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -69,37 +70,12 @@ protected void clearEntries() {

public CompletableFuture<List<ScreenshotEntry>> load(MinecraftClient client) {
return CompletableFuture.supplyAsync(() -> {
List<File> screenshots = this.loadScreenshots();
List<File> screenshots = ScreenshotActions.getScreenshots(client);
List<ScreenshotEntry> entries = new ArrayList<>();
screenshots.parallelStream().forEach(file -> entries.add(new ScreenshotEntry(file, client, parent)));
return entries;
});
}

private List<File> loadScreenshots() {
File screenshotDir = new File(client.runDirectory, "screenshots");

File[] files = screenshotDir.listFiles();
List<File> screenshots = new ArrayList<>(List.of(files == null ? new File[0] : files));

screenshots.removeIf(file -> {
if (Files.isDirectory(file.toPath())) return true;
String fileType;

try {
fileType = Files.probeContentType(file.toPath());
} catch (IOException e) {
Snapper.LOGGER.error("Couldn't load screenshot list", e);
return true;
}

return !Objects.equals(fileType, "image/png");
});

screenshots.sort(Comparator.comparingLong(File::lastModified).reversed());
return screenshots;
}

private void setEntrySelected(@Nullable ScreenshotEntry entry) {
super.setSelected(entry);
ScreenshotScreen parentScreen = (ScreenshotScreen) this.parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import ca.weblite.objc.Client;
import ca.weblite.objc.Proxy;
import dev.spiritstudios.snapper.Snapper;
import net.minecraft.client.MinecraftClient;

public class ScreenshotActionsMac {
import java.io.File;

public class MacActions implements PlatformHelper {

/* Screenshot copy logic (ScreenshotActions, ScreenshotActionsMac) heavily inspired by
ScreenshotViewer by LGatodu47. (https://github.com/LGatodu47/ScreenshotViewer).
Expand Down Expand Up @@ -61,13 +62,10 @@ of this software and associated documentation files (the "Software"), to deal
SOFTWARE.
*/

public static void copyScreenshotMac(String path) {
if (!MinecraftClient.IS_SYSTEM_MAC) {
return;
}

@Override
public void copyScreenshot(File screenshot) {
Client client = Client.getInstance();
Proxy url = client.sendProxy("NSURL", "fileURLWithPath:", path);
Proxy url = client.sendProxy("NSURL", "fileURLWithPath:", screenshot.getAbsoluteFile());

Proxy image = client.sendProxy("NSImage", "alloc");
image.send("initWithContentsOfURL:", url);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.spiritstudios.snapper.util;

import java.io.File;

public interface PlatformHelper {
void copyScreenshot(File screenshot);
}
Loading

0 comments on commit b4fddc6

Please sign in to comment.