Skip to content

Commit

Permalink
More bug fixes and Changes
Browse files Browse the repository at this point in the history
Fixed crash out of bounds years array
Fixed crash when a screenshot is delected when the GUI is open

Added that the cache is check and cleaned up start up
Moved Some Function from GalleryGUU to StaticFuntions class
Deleted some old commented code
Cleaned up a bit
  • Loading branch information
MightyFilipns committed Jun 11, 2023
1 parent 38bf5cc commit 4a199ef
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 133 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id 'org.parchmentmc.librarian.forgegradle' version '1.+'
}

version = '2.0'
version = '2.1.2'
group = 'com.mightyfilipns.screenshotgallery' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'screenshotgallery'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDate;
Expand Down Expand Up @@ -174,11 +176,11 @@ private static void internalbuildcache(boolean onlyaddnew,boolean inital) throws
}
iscaching = false;
}
private static void cacheimg(File file) throws Throwable
private static void cacheimg(File file) throws IOException
{
BufferedImage img = null;
img = ImageIO.read(file);
int xsize =200;
int xsize = 200;
int ysize = 112;

final float ratio = xsize/(float)(ysize);
Expand Down Expand Up @@ -225,6 +227,23 @@ private static void cacheimg(File file) throws Throwable
BasicFileAttributes bf = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
imgd.add(new imgdata(file,img.getWidth(),img.getHeight(),LocalDate.from(ZonedDateTime.ofInstant(bf.lastModifiedTime().toInstant(), ZoneId.systemDefault())),file.length()));
}

/**
* @param i pos in imgd list
*/
private static void deletefromcache(int i)
{
if(imgd.get(i).getFile().exists())
{
try {
imgd.get(i).getFile().delete();
} catch (SecurityException e)
{
e.printStackTrace();
}
imgd.remove(i);
}
}
private static void resort()
{
imgd.sort(new Comparator<imgdata>() {
Expand Down Expand Up @@ -257,7 +276,11 @@ public int compare(imgdata o1, imgdata o2) {
return 0;
}
});
files.sort(new Comparator<File>() {
filesort(files);
}
private static void filesort(List<File> inp)
{
inp.sort(new Comparator<File>() {

@Override
public int compare(File o1, File o2) {
Expand All @@ -281,7 +304,6 @@ public int compare(File o1, File o2) {
return 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
Expand Down Expand Up @@ -354,6 +376,24 @@ public void run() {
}, 2000);

}
public static void checkcache()
{
List<File> fls = Arrays.asList(imgcache.listFiles());
for (File file : fls)
{
//System.out.println(screenshootdir.getAbsolutePath()+"\\"+file.getFile().getName());
if(!Files.exists(Path.of(screenshootdir.getAbsolutePath()+"\\"+file.getName())))
{
try
{
file.delete();
} catch (SecurityException e)
{
e.printStackTrace();
}
}
}
}
public static List<File> getfiles(LocalDate min,LocalDate max)
{
if(imgd.size() == 0)
Expand Down
128 changes: 21 additions & 107 deletions src/main/java/com/mightyfilipns/screenshotgallery/GalleryGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -21,9 +22,7 @@

import com.mightyfilipns.screenshotgallery.Widgets.DatePicker;
import com.mightyfilipns.screenshotgallery.Widgets.Dropbox;

import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.blaze3d.platform.NativeImage.Format;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;

Expand All @@ -42,8 +41,8 @@
public class GalleryGUI extends Screen {
private static final Minecraft INSTANCE = Minecraft.getInstance();
TextureManager tm = INSTANCE.getTextureManager();
public static DynamicTexture whiteimg = new DynamicTexture(whitesq(10, 10,0xFF_FF_FF_FF));
public static DynamicTexture graybar = new DynamicTexture(whitesq(10, 10,0x0F_FF_FF_FF));
public static DynamicTexture whiteimg = new DynamicTexture(StaticFunctions.whitesq(10, 10,0xFF_FF_FF_FF));
public static DynamicTexture graybar = new DynamicTexture(StaticFunctions.whitesq(10, 10,0x0F_FF_FF_FF));
ResourceLocation rlwimng = tm.register("whiteimg", whiteimg);
List<DynamicTexture> dym = new ArrayList<>();
List<Integer> torender = new ArrayList<>();
Expand Down Expand Up @@ -97,11 +96,6 @@ enum sorttype
height,
}

public void stop()
{
int aa = 0;
aa += aa;
}
protected GalleryGUI() {
super(Component.literal("Screenshot gallery"));
ins = this;
Expand Down Expand Up @@ -216,7 +210,14 @@ public int compare(File o1, File o2)
{
return 0;
}
} catch (IOException e) {
} catch (NoSuchFileException e) {
if(!o1.exists())
{

}
}
catch (IOException e)
{
e.printStackTrace();
}
return 0;
Expand Down Expand Up @@ -299,26 +300,23 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton)

if(lasthoverover > -1 && !editmode && files[lasthoverover] != null && !issortingopen() && !issortinghovered())
{
editmode = true;
chosen = loadimg(files[lasthoverover]);
if(chosen == null)
{
return super.mouseClicked(pMouseX, pMouseY, pButton);
}
editmode = true;
rlmainimg = tm.register("mainimg", chosen);
filexp = Button.builder(Component.literal("Open in default image viewer"), (a) -> {
Util.getPlatform().openFile(files[lasthoverover]);
}).bounds(this.width / 2 - 150, this.height-20, 150, 20).build();
this.addRenderableWidget(filexp);
/*filexp = new Button(this.width / 2 - 150, this.height-20, 150, 20, Component.literal("Open in default image viewer"), (a) -> {
Util.getPlatform().openFile(files[lasthoverover]);
});*/

details = Button.builder(Component.literal("More details"), (a) ->
{
detailsopen = true;
}).bounds(this.width / 2, this.height-20, 150, 20).build();
this.addRenderableWidget(details);
/*details = new Button(this.width / 2, this.height-20, 150, 20, new StringTextComponent("More details"), (a) ->
{
detailsopen = true;
});*/

try {
attr = Files.readAttributes(files[lasthoverover].toPath(), BasicFileAttributes.class);
Expand All @@ -338,7 +336,6 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton)
}
togglesortvisibility(false);
}
//StaticFunctions.playDownSound();
return super.mouseClicked(pMouseX, pMouseY, pButton);
}

Expand All @@ -353,7 +350,6 @@ public void render(PoseStack pMatrixStack, int pMouseX, int pMouseY, float pPart
this.renderBackground(pMatrixStack);
int pos1 = 0;
int pos2 = 0;
//System.out.println(CacheManager.iscach.get());

if(files == null || files.length == 0)
{
Expand Down Expand Up @@ -410,9 +406,6 @@ public void render(PoseStack pMatrixStack, int pMouseX, int pMouseY, float pPart
m3 = height/2-imgh/2;
}


//chosen.bind();
//ResourceLocation rlm = tm.register("mainimg", chosen);
RenderSystem.setShaderTexture(0, rlmainimg);
blit(pMatrixStack,m2,m3,imgw,imgh, 0, 0,1,1,1,1);
if(attr != null && detailsopen)
Expand Down Expand Up @@ -506,14 +499,11 @@ public void render(PoseStack pMatrixStack, int pMouseX, int pMouseY, float pPart

int x = (margin * (pos1 + 1) + (pos1 * screenshotxsize))+Math.max(0, screenshotxsize-imgw)/2;
int y = (margin * (pos2 + 1) + (screenshotysize * pos2) - scroll)+20;
//item.bind();
//item.upload();
ResourceLocation rl = tm.register("empty", item);
RenderSystem.setShaderTexture(0, rl);
blit(pMatrixStack,x,y,imgw, imgh, 0, 0, 1, 1, 1, 1);
if(StaticFunctions.iswithin(pMouseY, y, y+imgh) && StaticFunctions.iswithin(pMouseX, x, x+imgw) && !issortingopen() && !issortinghovered())
{
//whiteimg.bind();
RenderSystem.setShaderTexture(0, rlwimng);
blit(pMatrixStack,x,y,hoverborder,imgh,0 , 0, 1, 1, 1, 1);
blit(pMatrixStack,x,y,imgw,hoverborder,0 , 0, 1, 1, 1, 1);
Expand All @@ -537,7 +527,6 @@ public void render(PoseStack pMatrixStack, int pMouseX, int pMouseY, float pPart
}
if(scrollmaxvalue > 0 && !editmode)
{
//this.minecraft.getTextureManager().bindForSetup(sliders);
RenderSystem.setShaderTexture(0, sliders);
int x = width-12;
float scrollp = (float)scroll/((float)scrollmaxvalue);
Expand All @@ -556,12 +545,8 @@ public boolean shouldCloseOnEsc()
}
if(editmode)
{
//buttons.remove(details);
//children.remove(details);
removeWidget(details);
details.active = false;
//buttons.remove(filexp);
//children.remove(filexp);
removeWidget(filexp);
filexp.active = false;
editmode = false;
Expand All @@ -571,10 +556,6 @@ public boolean shouldCloseOnEsc()
return true;
}

@Override
public boolean isPauseScreen() {
return true;
}
@Override
protected void init() {
super.init();
Expand Down Expand Up @@ -651,8 +632,6 @@ private void updateimgs() {
}
torender.add(i + (scrolleff * perrow));
}
//System.out.println(String.format("Visible:%s V1:%s scroleff:%S perrow:%s torender0:%s rendered0:%S",visible,v1,scrolleff,perrow,(torender.size() != 0 ? torender.get(0) : -1),(renderd.size() != 0 ? renderd.get(0) : -1)));
//System.out.println(torender);
if (renderd.size() == 0)
{
int tol = Math.min(files.length, torender.size());
Expand All @@ -664,7 +643,7 @@ private void updateimgs() {
dym.add(null);
continue;
}
dym.add(loadimgresized(files[torender.get(i)]));
dym.add(CacheManager.gethumbnail((files[torender.get(i)]).getName()));
}
renderd = new ArrayList<>(torender);
//System.out.println("initial");
Expand All @@ -688,7 +667,7 @@ private void updateimgs() {
dym.add(null);
continue;
}
dym.add(loadimgresized(files[torender.get(i)]));
dym.add(CacheManager.gethumbnail((files[torender.get(i)].getName())));
}
renderd = new ArrayList<>(torender);
//System.out.println((System.nanoTime()-str)/1000000f);
Expand Down Expand Up @@ -814,32 +793,6 @@ private void updateimgs() {

}

public DynamicTexture loadimgresized(File img) {
return CacheManager.gethumbnail(img.getName());
/*NativeImage nativeimage = null;
//long start = System.currentTimeMillis();
try (InputStream inputstream = new FileInputStream(img.getAbsoluteFile())) {
nativeimage = NativeImage.read(inputstream);
//System.out.println(System.currentTimeMillis()-start+" load");
int x = width/perrow;
int y = x;
if((float)nativeimage.getWidth()/(float)nativeimage.getHeight() != 0)
{
y /= (float)nativeimage.getWidth()/(float)nativeimage.getHeight();
}
nativeimage = resize(x, y, nativeimage);
//System.out.println(System.currentTimeMillis()-start+" full");
return new DynamicTexture(nativeimage);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;*/
}

public DynamicTexture loadimg(File img) {
NativeImage nativeimage = null;
try (InputStream inputstream = new FileInputStream(img.getAbsoluteFile())) {
Expand All @@ -853,47 +806,8 @@ public DynamicTexture loadimg(File img) {
return null;
}

/*private static NativeImage resize(int width, int height, NativeImage org) {
NativeImage img = new NativeImage(width, height, false);
double x_ratio = org.getWidth() / (double) width;
double y_ratio = org.getHeight() / (double) height;
double px, py;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
px = Math.floor(j * x_ratio);
py = Math.floor(i * y_ratio);
img.setPixelRGBA(j, i, org.getPixelRGBA((int) px, (int) py));
}
}
return img;
}*/
static <T> T getlast(List<T> toget)
{
if(toget == null || toget.size() == 0)
{
return null;
}
return toget.get(toget.size()-1);
}
static NativeImage whitesq(int x,int y, int argbcolor)
{
NativeImage ni = new NativeImage(Format.RGBA,x,y,false);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
ni.setPixelRGBA(i, j, argbcolor);
}
}
return ni;
@Override
public boolean isPauseScreen() {
return true;
}
/*
public static void open()
{
KeyBinding kb = new KeyBinding("Open Gallery", 71 , "Gallery Mod");
if(kb.isDown() && INSTANCE.screen == null)
{
INSTANCE.setScreen(new GalleryGUI());
}
}*/
}
Loading

0 comments on commit 4a199ef

Please sign in to comment.