Skip to content

Commit

Permalink
Add _N suffix to additional screenshots taken within 1 second
Browse files Browse the repository at this point in the history
  • Loading branch information
UltimateBoomer committed Feb 14, 2021
1 parent 72ec866 commit fefa4ce
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.ultimateboomer.resolutioncontrol.client.gui.screen.SettingsScreen;
import io.github.ultimateboomer.resolutioncontrol.util.Config;
import io.github.ultimateboomer.resolutioncontrol.util.ConfigHandler;
import io.github.ultimateboomer.resolutioncontrol.util.RCUtil;
import io.github.ultimateboomer.resolutioncontrol.util.ScalingAlgorithm;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
Expand Down Expand Up @@ -90,14 +91,18 @@ public void onInitialize() {
client.player.sendMessage(
new TranslatableText("resolutioncontrol.screenshot.wait"), false);
} else {
ScreenshotUtils.saveScreenshot(client.runDirectory,
SCREENSHOT_PREFIX + ScreenshotUtils.getScreenshotFilename(null),
framebuffer.textureWidth, framebuffer.textureHeight, framebuffer,
text -> client.player.sendMessage(text, false));
saveScreenshot(framebuffer);
}
}
});
}

private void saveScreenshot(Framebuffer fb) {
ScreenshotUtils.saveScreenshot(client.runDirectory,
SCREENSHOT_PREFIX + RCUtil.getScreenshotFilename(null),
fb.textureWidth, fb.textureHeight, fb,
text -> client.player.sendMessage(text, false));
}

public void setShouldScale(boolean shouldScale) {
if (shouldScale == this.shouldScale) return;
Expand Down Expand Up @@ -152,10 +157,7 @@ public void setShouldScale(boolean shouldScale) {
window.getFramebufferWidth(), window.getFramebufferHeight()
);

ScreenshotUtils.saveScreenshot(client.runDirectory,
SCREENSHOT_PREFIX + ScreenshotUtils.getScreenshotFilename(null),
framebuffer.textureWidth, framebuffer.textureHeight, screenshotFrameBuffer,
text -> client.player.sendMessage(text, false));
saveScreenshot(screenshotFrameBuffer);

if (!isScreenshotFramebufferAlwaysAllocated()) {
screenshotFrameBuffer.delete();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.ultimateboomer.resolutioncontrol.client.gui.screen;

import io.github.ultimateboomer.resolutioncontrol.ResolutionControlMod;
import io.github.ultimateboomer.resolutioncontrol.util.RCMathUtil;
import io.github.ultimateboomer.resolutioncontrol.util.RCUtil;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float time) {
centerX - 55, centerY - 24, 0x000000);

drawCenteredString(matrices, "\u00a78" + text("settings.main.estimate",
RCMathUtil.formatMetric(ResolutionControlMod.getInstance().getEstimatedMemory()) + "B")
RCUtil.formatMetric(ResolutionControlMod.getInstance().getEstimatedMemory()) + "B")
.getString() + "\u00a7r",
centerX - 55, centerY - 12, 0x000000);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.ultimateboomer.resolutioncontrol.client.gui.screen;

import io.github.ultimateboomer.resolutioncontrol.util.RCMathUtil;
import io.github.ultimateboomer.resolutioncontrol.util.RCUtil;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
Expand Down Expand Up @@ -122,7 +122,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {

drawLeftAlignedString(matrices,
"\u00a78" + text("settings.main.estimate").getString()
+ " " + RCMathUtil.formatMetric(estimatedSize) + "B",
+ " " + RCUtil.formatMetric(estimatedSize) + "B",
centerX + 25, centerY + 12,
0x000000);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.ultimateboomer.resolutioncontrol.util;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public final class RCUtil {
private static final String[] UNITS = "KMGTPE".split("");
private static final NumberFormat FORMAT = NumberFormat.getNumberInstance();

private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");

/**
* Format number with metric conversion (K, M, G etc)
*/
public static String formatMetric(long n) {
if (n < 1000) {
return n + " ";
}

int log10 = (int) Math.log10(n);
int decimalPlace = (int) Math.pow(10, 2 - log10 % 3);
int displayDigits = (int) (n / Math.pow(10, log10 - 2));

float result = (float) displayDigits / decimalPlace;
return String.format("%s %s", FORMAT.format(result), UNITS[log10 / 3 - 1]);
}

public static File getScreenshotFilename(File directory) {
String string = DATE_FORMAT.format(new Date());
int i = 1;

while (true) {
File file = new File(directory, string + (i == 1 ? "" : "_" + i) + ".png");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
throw new IllegalStateException(e);
}
return file;
}

++i;
}
}
}

0 comments on commit fefa4ce

Please sign in to comment.