-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathScreenshotScreen.java
More file actions
269 lines (219 loc) · 11 KB
/
ScreenshotScreen.java
File metadata and controls
269 lines (219 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package dev.spiritstudios.snapper.gui.screen;
import dev.spiritstudios.snapper.Snapper;
import dev.spiritstudios.snapper.SnapperConfig;
import dev.spiritstudios.snapper.gui.widget.ScreenshotListWidget;
import dev.spiritstudios.snapper.util.ScreenshotActions;
import dev.spiritstudios.snapper.util.SnapperUtil;
import dev.spiritstudios.snapper.util.uploading.ScreenshotUploading;
import dev.spiritstudios.specter.api.config.client.RootConfigScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.AxisGridWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.DirectionalLayoutWidget;
import net.minecraft.client.gui.widget.SimplePositioningWidget;
import net.minecraft.client.gui.widget.TextIconButtonWidget;
import net.minecraft.client.util.InputUtil;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import java.nio.file.Path;
import static dev.spiritstudios.snapper.Snapper.MODID;
public class ScreenshotScreen extends Screen {
private static final Identifier PANORAMA_BUTTON_ICON = Identifier.of(MODID, "screenshots/panorama");
private static final Identifier PANORAMA_BUTTON_DISABLED_ICON = Identifier.of(MODID, "screenshots/panorama_disabled");
private static final Identifier SETTINGS_ICON = Identifier.of(MODID, "screenshots/settings");
private static Identifier VIEW_MODE_ICON;
private final Screen parent;
ScreenshotListWidget screenshotList;
private ButtonWidget deleteButton;
private ButtonWidget renameButton;
private ButtonWidget viewButton;
private ButtonWidget copyButton;
private ButtonWidget openButton;
private ButtonWidget uploadButton;
private TextIconButtonWidget viewModeButton;
private ScreenshotListWidget.@Nullable ScreenshotEntry selectedScreenshot = null;
private boolean showGrid;
private final boolean isOffline;
public ScreenshotScreen(Screen parent) {
super(Text.translatable("menu.snapper.screenshot_menu"));
this.parent = parent;
this.showGrid = SnapperConfig.INSTANCE.viewMode.get().equals(ScreenshotViewerScreen.ViewMode.GRID);
this.isOffline = SnapperUtil.isOfflineAccount();
VIEW_MODE_ICON = showGrid ? Identifier.of(MODID, "screenshots/show_list") : Identifier.of(MODID, "screenshots/show_grid");
}
@Override
protected void init() {
if (client == null) return;
screenshotList = this.addDrawableChild(new ScreenshotListWidget(
client,
width,
height - 48 - 68,
48,
36,
screenshotList,
this
));
int secondRowButtonWidth = 100;
ButtonWidget folderButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.folder"),
button -> Util.getOperatingSystem().open(SnapperUtil.getConfiguredScreenshotDirectory())
).width(secondRowButtonWidth).build());
this.openButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.open"),
button -> {
if (selectedScreenshot != null)
Util.getOperatingSystem().open(selectedScreenshot.icon.getPath());
}
).width(secondRowButtonWidth).build());
ButtonWidget doneButton = addDrawableChild(ButtonWidget.builder(
ScreenTexts.DONE,
button -> this.close()
).width(secondRowButtonWidth).build());
int firstRowButtonWidth = 58;
this.deleteButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.delete"),
button -> {
if (selectedScreenshot != null)
ScreenshotActions.deleteScreenshot(selectedScreenshot.icon.getPath(), this);
}
).width(firstRowButtonWidth).build());
this.renameButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.rename"),
button -> {
if (this.selectedScreenshot != null)
client.setScreen(new RenameScreenshotScreen(this.selectedScreenshot.icon.getPath(), this));
}
).width(firstRowButtonWidth).build());
this.copyButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.copy"),
button -> {
if (selectedScreenshot != null)
Snapper.getPlatformHelper().copyScreenshot(selectedScreenshot.icon.getPath());
}
).width(firstRowButtonWidth).build());
this.viewButton = addDrawableChild(ButtonWidget.builder(
Text.translatable("button.snapper.view"),
button -> {
if (selectedScreenshot != null)
this.client.setScreen(new ScreenshotViewerScreen(
selectedScreenshot.icon,
selectedScreenshot.icon.getPath(),
selectedScreenshot.screenParent
));
}
).width(firstRowButtonWidth).build());
this.uploadButton = addDrawableChild(ButtonWidget.builder(Text.translatable("button.snapper.upload"), button -> {
if (selectedScreenshot == null) return;
button.active = false;
ScreenshotUploading.upload(selectedScreenshot.icon.getPath())
.thenRun(() -> button.active = true);
}).width(firstRowButtonWidth).build());
if (isOffline) {
this.uploadButton.setTooltip(Tooltip.of(Text.translatable("button.snapper.upload.tooltip")));
}
DirectionalLayoutWidget verticalButtonLayout = DirectionalLayoutWidget.vertical().spacing(4);
AxisGridWidget firstRowWidget = verticalButtonLayout.add(new AxisGridWidget(
308,
20,
AxisGridWidget.DisplayAxis.HORIZONTAL
));
firstRowWidget.add(this.deleteButton);
firstRowWidget.add(this.renameButton);
firstRowWidget.add(this.copyButton);
firstRowWidget.add(this.viewButton);
firstRowWidget.add(this.uploadButton);
AxisGridWidget secondRowWidget = verticalButtonLayout.add(new AxisGridWidget(
308,
20,
AxisGridWidget.DisplayAxis.HORIZONTAL
));
secondRowWidget.add(folderButton);
secondRowWidget.add(this.openButton);
secondRowWidget.add(doneButton);
verticalButtonLayout.refreshPositions();
SimplePositioningWidget.setPos(verticalButtonLayout, 0, this.height - 66, this.width, 64);
TextIconButtonWidget settingsButton = addDrawableChild(TextIconButtonWidget.builder(
Text.translatable("config.snapper.snapper.title"),
button -> this.client.setScreen(
new RootConfigScreen(SnapperConfig.HOLDER, new ScreenshotScreen(this.parent))),
true
).width(20).texture(SETTINGS_ICON, 15, 15).build());
settingsButton.setPosition(width / 2 - 178, height - 32);
this.viewModeButton = addDrawableChild(TextIconButtonWidget.builder(
Text.translatable("config.snapper.snapper.viewMode"),
button -> this.toggleGrid(),
true
).width(20).texture(VIEW_MODE_ICON, 15, 15).build());
viewModeButton.setPosition(width / 2 - 178, height - 56);
Path panoramaDir = SnapperUtil.getConfiguredScreenshotDirectory().resolve("panorama");
boolean hasPanorama = SnapperUtil.panoramaPresent(panoramaDir);
TextIconButtonWidget panoramaButton = addDrawableChild(TextIconButtonWidget.builder(
Text.translatable("button.snapper.screenshots"),
button -> this.client.setScreen(new PanoramaViewerScreen(Text.translatable("menu.snapper.panorama").getString(), this)),
true
).width(20).texture(hasPanorama ? PANORAMA_BUTTON_ICON : PANORAMA_BUTTON_DISABLED_ICON, 15, 15).build());
panoramaButton.active = hasPanorama;
panoramaButton.setPosition(width / 2 + 158, height - 32);
panoramaButton.setTooltip(Tooltip.of(Text.translatable(hasPanorama ?
"button.snapper.panorama.tooltip" :
"text.snapper.panorama_encourage")));
this.imageSelected(selectedScreenshot);
}
public void imageSelected(@Nullable ScreenshotListWidget.ScreenshotEntry 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;
this.uploadButton.active = !isOffline && hasScreenshot;
}
public void toggleGrid() {
screenshotList.toggleGrid();
screenshotList.refreshScroll();
this.showGrid = !this.showGrid;
VIEW_MODE_ICON = showGrid ? Identifier.of(MODID, "screenshots/show_list") : Identifier.of(MODID, "screenshots/show_grid");
remove(this.viewModeButton);
this.viewModeButton = addDrawableChild(TextIconButtonWidget.builder(
Text.translatable("config.snapper.snapper.viewMode"),
button -> this.toggleGrid(),
true
).width(20).texture(VIEW_MODE_ICON, 15, 15).build());
viewModeButton.setPosition(width / 2 - 178, height - 56);
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (super.keyPressed(keyCode, scanCode, modifiers)) return true;
if (client == null) return false;
if (keyCode == GLFW.GLFW_KEY_F5) {
client.setScreen(new ScreenshotScreen(this.parent));
return true;
}
if (selectedScreenshot == null) return false;
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0 && keyCode == InputUtil.GLFW_KEY_C) {
Snapper.getPlatformHelper().copyScreenshot(selectedScreenshot.icon.getPath());
return true;
}
if (keyCode == GLFW.GLFW_KEY_ENTER) {
client.setScreen(new ScreenshotViewerScreen(selectedScreenshot.icon, selectedScreenshot.icon.getPath(), this));
}
return false;
}
@Override
public void close() {
SnapperConfig.HOLDER.save();
super.close();
}
@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, 20, 0xffffff);
}
}