Skip to content

Commit d7d308a

Browse files
feat: Lay scale groundwork for grid-style menu
1 parent 52598de commit d7d308a

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

src/client/java/dev/spiritstudios/snapper/gui/widget/ScreenshotListWidget.java

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dev.spiritstudios.snapper.SnapperConfig;
66
import dev.spiritstudios.snapper.gui.screen.ScreenshotScreen;
77
import dev.spiritstudios.snapper.gui.screen.ScreenshotViewerScreen;
8+
import dev.spiritstudios.snapper.mixin.EntryListWidgetAccessor;
89
import dev.spiritstudios.snapper.util.ScreenshotActions;
910
import dev.spiritstudios.snapper.util.ScreenshotImage;
1011
import dev.spiritstudios.specter.api.core.exception.UnreachableException;
@@ -51,6 +52,8 @@ public class ScreenshotListWidget extends AlwaysSelectedEntryListWidget<Screensh
5152

5253
public final CompletableFuture<List<ScreenshotEntry>> loadFuture;
5354

55+
private final int gridItemHeight = 48;
56+
private final int listItemHeight = 36;
5457
private boolean showGrid = false;
5558

5659
public ScreenshotListWidget(MinecraftClient client, int width, int height, int y, int itemHeight, @Nullable ScreenshotListWidget previous, Screen parent) {
@@ -72,6 +75,7 @@ public ScreenshotListWidget(MinecraftClient client, int width, int height, int y
7275
});
7376

7477
this.showGrid = SnapperConfig.INSTANCE.viewMode.get();
78+
((EntryListWidgetAccessor) (Object) this).setItemHeight(this.showGrid ? this.gridItemHeight : this.listItemHeight);
7579
}
7680

7781
@Override
@@ -107,7 +111,8 @@ private void setEntrySelected(@Nullable ScreenshotEntry entry) {
107111

108112
@Override
109113
protected void renderList(DrawContext context, int mouseX, int mouseY, float delta) {
110-
if (showGrid) {int rowLeft = this.getRowLeft();
114+
if (showGrid) {
115+
int rowLeft = this.getRowLeft();
111116
int rowWidth = this.getRowWidth();
112117
int entryHeight = this.itemHeight - 4;
113118
int entryWidth = showGrid ? entryHeight : rowWidth;
@@ -146,6 +151,7 @@ protected int getMaxPosition() {
146151

147152
public void toggleGrid() {
148153
this.showGrid = !this.showGrid;
154+
((EntryListWidgetAccessor) (Object) this).setItemHeight(this.showGrid ? this.gridItemHeight : this.listItemHeight);
149155
for (var entry : this.children()) if (entry instanceof ScreenshotEntry sc) sc.setShowGrid(this.showGrid);
150156

151157
SnapperConfig.INSTANCE.viewMode.set(this.showGrid);
@@ -157,12 +163,12 @@ public void toggleGrid() {
157163

158164
int rowWidth = this.getRowWidth();
159165
int relX = MathHelper.floor(x - this.getRowLeft());
160-
int relY = MathHelper.floor(y - (double)this.getY()) - this.headerHeight;
166+
int relY = MathHelper.floor(y - (double) this.getY()) - this.headerHeight;
161167

162168
if (relX < 0 || relX > rowWidth || relY < 0 || relY > getBottom()) return null;
163169

164-
int rowIndex = (relY + (int)this.getScrollAmount()) / this.itemHeight;
165-
int colIndex = MathHelper.floor(((float)relX / (float)rowWidth) * (float)NUM_COLUMNS);
170+
int rowIndex = (relY + (int) this.getScrollAmount()) / this.itemHeight;
171+
int colIndex = MathHelper.floor(((float) relX / (float) rowWidth) * (float) NUM_COLUMNS);
166172
int entryIndex = rowIndex * NUM_COLUMNS + colIndex;
167173

168174
return entryIndex >= 0 && entryIndex < getEntryCount() ? getEntry(entryIndex) : null;
@@ -304,6 +310,80 @@ public void setShowGrid(boolean showGrid) {
304310

305311
@Override
306312
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
313+
if (this.showGrid) {
314+
renderGrid(context, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta);
315+
return;
316+
}
317+
renderList(context, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta);
318+
}
319+
320+
public void renderList(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
321+
String fileName = this.iconFileName;
322+
String creationString = "undefined";
323+
324+
long creationTime = 0;
325+
try {
326+
creationTime = Files.readAttributes(iconPath, BasicFileAttributes.class).creationTime().toMillis();
327+
} catch (IOException e) {
328+
client.setScreen(new ScreenshotScreen(screenParent));
329+
}
330+
331+
if (creationTime != -1L)
332+
creationString = Text.translatable("text.snapper.created").getString() + " " + DATE_FORMAT.format(Instant.ofEpochMilli(creationTime));
333+
334+
if (StringHelper.isEmpty(fileName))
335+
fileName = Text.translatable("text.snapper.generic") + " " + (index + 1);
336+
337+
context.drawText(
338+
this.client.textRenderer,
339+
fileName,
340+
x + 32 + 3,
341+
y + 1,
342+
0xFFFFFF,
343+
false
344+
);
345+
346+
context.drawText(
347+
this.client.textRenderer,
348+
creationString,
349+
x + 35,
350+
y + 12,
351+
Colors.GRAY,
352+
false
353+
);
354+
355+
356+
if (this.icon != null) {
357+
RenderSystem.enableBlend();
358+
context.drawTexture(
359+
this.icon.getTextureId(),
360+
x,
361+
y,
362+
entryHeight,
363+
entryHeight,
364+
(icon.getHeight()) / 3.0F + 32,
365+
0,
366+
icon.getHeight(),
367+
icon.getHeight(),
368+
icon.getWidth(),
369+
icon.getHeight()
370+
);
371+
RenderSystem.disableBlend();
372+
}
373+
374+
if (this.client.options.getTouchscreen().getValue() || hovered) {
375+
context.fill(x, y, x + 32, y + 32, 0xA0909090);
376+
context.drawGuiTexture(
377+
mouseX - x < 32 && this.icon != null ? ScreenshotListWidget.VIEW_HIGHLIGHTED_TEXTURE : ScreenshotListWidget.VIEW_TEXTURE,
378+
x,
379+
y,
380+
32,
381+
32
382+
);
383+
}
384+
}
385+
386+
public void renderGrid(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
307387
if (!this.showGrid) {
308388
String fileName = this.iconFileName;
309389
String creationString = "undefined";
@@ -342,13 +422,12 @@ public void render(DrawContext context, int index, int y, int x, int entryWidth,
342422

343423
if (this.icon != null) {
344424
RenderSystem.enableBlend();
345-
int iconSize = entryHeight;
346425
context.drawTexture(
347426
this.icon.getTextureId(),
348427
x,
349428
y,
350-
iconSize,
351-
iconSize,
429+
entryHeight,
430+
entryHeight,
352431
(icon.getHeight()) / 3.0F + 32,
353432
0,
354433
icon.getHeight(),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.spiritstudios.snapper.mixin;
2+
3+
import net.minecraft.client.gui.widget.EntryListWidget;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
import org.spongepowered.asm.mixin.Mutable;
6+
import org.spongepowered.asm.mixin.gen.Accessor;
7+
8+
@Mixin(EntryListWidget.class)
9+
public interface EntryListWidgetAccessor {
10+
@Mutable
11+
@Accessor
12+
void setItemHeight(int height);
13+
}

src/client/resources/snapper.mixins.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"package": "dev.spiritstudios.snapper.mixin",
44
"compatibilityLevel": "JAVA_21",
55
"client": [
6-
"accessor.CameraAccessor",
76
"CameraMixin",
7+
"EntryListWidgetAccessor",
88
"GameMenuMixin",
99
"KeyboardMixin",
1010
"MinecraftClientMixin",
11+
"ScreenshotRecorderMixin",
1112
"TitleScreenMixin",
12-
"ScreenshotRecorderMixin"
13+
"accessor.CameraAccessor"
1314
],
1415
"injectors": {
1516
"defaultRequire": 1

0 commit comments

Comments
 (0)