Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CallMeEchoCodes committed Aug 8, 2024
1 parent dc7b0e6 commit ad6557e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 69 deletions.
2 changes: 0 additions & 2 deletions src/client/java/dev/spiritstudios/snapper/Snapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class Snapper implements ClientModInitializer {
public static final String MODID = "snapper";
public static final Logger LOGGER = LoggerFactory.getLogger(MODID);

// TELL COPY-LOGIC THAT THE GAME IS NOT HEADLESS

private static final KeyBinding SCREENSHOT_MENU_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding(
"key.snapper.screenshot_menu",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class PanoramaViewerScreen extends Screen {
protected static final CubeMapRenderer PANORAMA_RENDERER = new CubeMapRenderer(Identifier.ofVanilla("screenshots/panorama/panorama"));
Expand All @@ -35,19 +34,17 @@ public class PanoramaViewerScreen extends Screen {
protected static final RotatingCubeMapRenderer FALLBACK_PANORAMA_RENDERER_CUBE = new RotatingCubeMapRenderer(FALLBACK_PANORAMA_RENDERER);
private static final MinecraftClient client = MinecraftClient.getInstance();

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

protected PanoramaViewerScreen(String title, Screen parent) {
super(Text.translatable("menu.snapper.viewermenu"));
this.title = title;
this.parent = parent;
this.iconPath = new File(client.runDirectory + "screenshots/panorama").toPath();
this.load();
}

Expand All @@ -59,8 +56,7 @@ private void load() {
ScreenshotIcon icon = ScreenshotIcon.forPanoramaFace(client.getTextureManager(), face.getName());
this.loadIcon(icon, face.getName(), Path.of(face.getPath()));
return icon;
}).collect(Collectors.toList())
.forEach(ScreenshotIcon::joinLoad);
}).toList().forEach(ScreenshotIcon::joinLoad);

this.loaded = true;
}
Expand Down Expand Up @@ -135,10 +131,10 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
} else {
progress = MathHelper.clamp(progress, 0.0F, 1.0F);
widgetProgress = MathHelper.clampedMap(progress, 0.0F, 0.5F, 0.0F, 1.0F);
;
backgroundAlpha = MathHelper.clampedMap(progress, 0.0F, 0.5F, 0.0F, 1.0F);
}
this.setWidgetOpacity(widgetProgress); // SORT OF IMPORTANT

this.setWidgetOpacity(widgetProgress);
}

super.render(context, mouseX, mouseY, delta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.File;

public class RenameScreenshotScreen extends Screen {

private final File screenshot;
private final TextFieldWidget renameInput;
private final Text RENAME_INPUT_TEXT = Text.translatable("text.snapper.rename_input");
Expand All @@ -32,9 +31,7 @@ protected void init() {
this.addDrawableChild(new TextWidget(RENAME_INPUT_TEXT, textRenderer)).setPosition(this.width / 2 - textRenderer.getWidth(RENAME_INPUT_TEXT) / 2, this.height / 2 - 20);
this.addDrawableChild(this.renameInput).setPosition(this.width / 2 - 100, this.height / 2);
this.renameInput.setText(this.screenshot.getName());
this.addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.rename"), button -> {
this.renameScreenshot(this.renameInput.getText());
})
this.addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.rename"), button -> this.renameScreenshot(this.renameInput.getText()))
.dimensions(width / 2 - 150 - 4, height - 32, 150, 20)
.build()
);
Expand All @@ -46,18 +43,13 @@ protected void init() {
}

private void renameScreenshot(String newName) {
if (newName == null || !newName.endsWith(".png")) {
return;
}
if (newName == null || !newName.endsWith(".png")) return;

ScreenshotActions.renameScreenshot(screenshot, newName);
client.setScreen(this.parent);
// Should I make this an if-else statement just to bug Echo?
}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 15, 0xFFFFFF);
// Should I make this an if-else statement just to bug Echo?
// no.
}

@Override
Expand Down
65 changes: 38 additions & 27 deletions src/client/java/dev/spiritstudios/snapper/gui/ScreenshotScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.*;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -59,10 +58,9 @@ protected void init() {

this.viewButton = addDrawableChild(
ButtonWidget.builder(Text.translatable("button.snapper.view"), button -> {
if (selectedScreenshot != null) {
this.client.setScreen(new ScreenshotViewerScreen(selectedScreenshot.icon, selectedScreenshot.screenshot, selectedScreenshot.screenParent));
}
})
if (selectedScreenshot != null)
this.client.setScreen(new ScreenshotViewerScreen(selectedScreenshot.icon, selectedScreenshot.screenshot, selectedScreenshot.screenParent));
})
.width(100)
.build()
);
Expand All @@ -72,36 +70,46 @@ protected void init() {
.build()
);

this.deleteButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.delete"), button -> ScreenshotActions.deleteScreenshot(selectedScreenshot.screenshot, this))
.width(74)
.build()
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 -> Util.getOperatingSystem().open(selectedScreenshot.screenshot))
.width(74)
.build()
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 -> {
if (this.selectedScreenshot != null) {
client.setScreen(new RenameScreenshotScreen(this.selectedScreenshot.screenshot, this));
}
})
.width(74)
.build()
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 -> ScreenshotActions.copyScreenshot(selectedScreenshot.screenshot))
.width(74)
.build()
this.copyButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.copy"), button -> {
if (selectedScreenshot != null)
ScreenshotActions.copyScreenshot(selectedScreenshot.screenshot);
})
.width(74)
.build()
);

DirectionalLayoutWidget verticalButtonLayout = DirectionalLayoutWidget.vertical().spacing(4);

AxisGridWidget firstRowWidget = verticalButtonLayout.add(new AxisGridWidget(308, 20, AxisGridWidget.DisplayAxis.HORIZONTAL));
firstRowWidget.add(this.deleteButton);
firstRowWidget.add(this.openButton);
firstRowWidget.add(this.renameButton);
firstRowWidget.add(this.copyButton);

AxisGridWidget secondRowWidget = verticalButtonLayout.add(new AxisGridWidget(308, 20, AxisGridWidget.DisplayAxis.HORIZONTAL));
secondRowWidget.add(folderButton);
secondRowWidget.add(this.viewButton);
Expand All @@ -113,9 +121,7 @@ protected void init() {
TextIconButtonWidget panorama_button = addDrawableChild(
TextIconButtonWidget.builder(
Text.translatable("button.snapper.screenshots"),
button -> {
this.client.setScreen(new PanoramaViewerScreen(I18n.translate("menu.snapper.panorama"), this));
},
button -> this.client.setScreen(new PanoramaViewerScreen(Text.translatable("menu.snapper.panorama").getString(), this)),
true
).width(20).texture(PANORAMA_BUTTON_ICON, 15, 15).build()
);
Expand Down Expand Up @@ -146,17 +152,22 @@ public void imageSelected(@Nullable ScreenshotListWidget.ScreenshotEntry screens
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
long handle = MinecraftClient.getInstance().getWindow().getHandle();
if (super.keyPressed(keyCode, scanCode, modifiers)) {
return true;
} else if (keyCode == GLFW.GLFW_KEY_F5) {
if (super.keyPressed(keyCode, scanCode, modifiers)) return true;

if (keyCode == GLFW.GLFW_KEY_F5) {
if (client == null) return false;

client.setScreen(new ScreenshotScreen(this.parent));
return true;
} else 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 ((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);
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ protected void init() {

// DELETE SCREENSHOT

ButtonWidget deleteButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.delete"), button -> {
ScreenshotActions.deleteScreenshot(this.screenshot, this.parent);
})
ButtonWidget deleteButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.delete"), button -> ScreenshotActions.deleteScreenshot(this.screenshot, this.parent))
.width(100)
.build()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ public CompletableFuture<List<ScreenshotEntry>> load(MinecraftClient client) {

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

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

screenshots.removeIf(file -> {
if (Files.isDirectory(file.toPath())) return true;
Expand Down Expand Up @@ -153,7 +152,10 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
}

public class ScreenshotEntry extends Entry implements AutoCloseable {
public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
public static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT)
.withZone(ZoneId.systemDefault());

public final long lastModified;
private final MinecraftClient client;
public final ScreenshotIcon icon;
Expand Down Expand Up @@ -184,15 +186,14 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
try {
creationTime = Files.readAttributes(iconPath, BasicFileAttributes.class).creationTime().toMillis();
} catch (IOException e) {
//Snapper.LOGGER.error("FILE RENAMED/DELETED, RELOADING SCREEN");
client.setScreen(new ScreenshotScreen(screenParent));
}

if (creationTime != -1L)
creationString = Text.translatable("text.snapper.created").getString() + " " + DATE_FORMAT.format(Instant.ofEpochMilli(creationTime));

if (StringHelper.isEmpty(fileName))
fileName = I18n.translate("text.snapper.generic") + " " + (index + 1);
fileName = Text.translatable("text.snapper.generic") + " " + (index + 1);

context.drawText(
this.client.textRenderer,
Expand Down Expand Up @@ -220,7 +221,6 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
y,
32,
32,
//centered
(icon.getHeight()) / 3.0F + 32,
0,
icon.getHeight(),
Expand All @@ -230,6 +230,7 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
);
RenderSystem.disableBlend();
}

if (this.client.options.getTouchscreen().getValue() || hovered) {
context.fill(x, y, x + 32, y + 32, 0xA0909090);
context.drawGuiTexture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.spiritstudios.snapper.gui.ScreenshotScreen;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.gui.widget.TextIconButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -32,7 +31,12 @@ protected void head(CallbackInfo ci) {
this.addDrawableChild(
TextIconButtonWidget.builder(
Text.translatable("button.snapper.screenshots"),
button -> this.client.setScreen(new ScreenshotScreen(new GameMenuScreen(true))),
button -> {
if (this.client == null)
return;

this.client.setScreen(new ScreenshotScreen(new GameMenuScreen(true)));
},
true
).width(20).texture(SNAPPER_BUTTON_ICON, 15, 15).build()
).setPosition(this.width / 2 - 130, height / 4 + 32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ public boolean isDataFlavorSupported(DataFlavor flavor) {

@NotNull
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (!isDataFlavorSupported(flavor)) throw new UnsupportedFlavorException(flavor);

return image();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void load(NativeImage image) {

public void joinLoad() {
// must be called on render thread
if (this.texture == null) return;
this.texture.setFilter(true, false);
}

Expand Down Expand Up @@ -84,8 +85,6 @@ public void close() {
}

private void assertOpen() {
if (this.closed) {
throw new IllegalStateException("Icon already closed");
}
if (this.closed) throw new IllegalStateException("Icon already closed");
}
}

0 comments on commit ad6557e

Please sign in to comment.