This commit is contained in:
Moulberry committed 2024-09-15 11:35:31 +08:00
1 parent 7f1d199704
commit be99313e81
47 files changed
+1247 -337

No files matched your search

@@ -0,0 +1,112 @@
package com.moulberry.flashback.screen;
import com.google.gson.JsonObject;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.FlashbackGson;
import com.moulberry.flashback.exporting.AsyncFileDialogs;
import com.moulberry.flashback.record.FlashbackMeta;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.StringWidget;
import net.minecraft.client.gui.layouts.FrameLayout;
import net.minecraft.client.gui.layouts.GridLayout;
import net.minecraft.client.gui.screens.AlertScreen;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
public class EditReplayScreen extends Screen {
@Nullable
private final Screen lastScreen;
private FlashbackMeta metadata;
private String changedReplayName;
private Button saveChangesButton;
private Path replayPath;
protected EditReplayScreen(@Nullable Screen lastScreen, ReplaySummary summary) {
super(Component.literal("Edit Replay"));
this.lastScreen = lastScreen;
this.metadata = summary.getReplayMetadata();
this.changedReplayName = summary.getReplayName();
this.replayPath = summary.getPath();
}
@Override
protected void init() {
super.init();
GridLayout gridLayout = new GridLayout();
gridLayout.defaultCellSetting().padding(4, 4, 4, 0);
GridLayout.RowHelper rowHelper = gridLayout.createRowHelper(2);
rowHelper.addChild(new StringWidget(204, 20, Component.literal("Edit Replay"), this.font), 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal("Replay Name"), this.font).alignLeft(), 2);
EditBox replayNameEditBox = new EditBox(this.font, 0, 0, 204, 20, Component.literal(this.changedReplayName));
replayNameEditBox.setValue(this.changedReplayName);
replayNameEditBox.setResponder(this::setReplayName);
rowHelper.addChild(replayNameEditBox, 2);
rowHelper.addChild(new BottomTextWidget(204, 10, Component.literal(""), this.font), 2);
this.saveChangesButton = rowHelper.addChild(Button.builder(Component.literal("Save Changes"), button -> {
this.applyChanges();
}).width(98).build());
this.saveChangesButton.active = false;
rowHelper.addChild(Button.builder(Component.literal("Back"), button -> {
this.minecraft.setScreen(this.lastScreen);
}).width(98).build());
gridLayout.arrangeElements();
FrameLayout.alignInRectangle(gridLayout, 0, 0, this.width, this.height, 0.5f, 0.5f);
gridLayout.visitWidgets(this::addRenderableWidget);
this.updateSaveChangesActive();
}
private void applyChanges() {
this.metadata.name = this.changedReplayName;
try (FileSystem fileSystem = FileSystems.newFileSystem(this.replayPath)) {
Path metadataPath = fileSystem.getPath("/metadata.json");
// Update
String metadataJson = FlashbackGson.PRETTY.toJson(this.metadata.toJson());
Files.writeString(metadataPath, metadataJson);
} catch (IOException e) {
Flashback.LOGGER.error("Unable to edit replay", e);
Minecraft.getInstance().setScreen(new AlertScreen(() -> Minecraft.getInstance().setScreen(this.lastScreen),
Component.literal("Unable to edit replay"), Component.literal(e.toString())));
}
this.updateSaveChangesActive();
this.clearFocus();
}
private void setReplayName(String name) {
this.changedReplayName = name;
this.updateSaveChangesActive();
}
private void updateSaveChangesActive() {
this.saveChangesButton.active = !Objects.equals(this.metadata.name, this.changedReplayName);
}
@Override
public void onClose() {
this.minecraft.setScreen(this.lastScreen);
}
}
@@ -0,0 +1,79 @@
package com.moulberry.flashback.screen;
import com.moulberry.flashback.Flashback;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import java.time.Duration;
import java.util.function.Consumer;
public class RecoverRecordingsScreen extends Screen {
private Screen lastScreen;
private final Component title;
private final Component messageText;
private MultiLineLabel message = MultiLineLabel.EMPTY;
private Consumer<RecoveryOption> handler;
public enum RecoveryOption {
RECOVER,
SKIP,
DELETE
}
public RecoverRecordingsScreen(Screen lastScreen, Component title, Component message, Consumer<RecoveryOption> handler) {
super(title);
this.lastScreen = lastScreen;
this.title = title;
this.messageText = message;
this.handler = handler;
}
@Override
public Component getNarrationMessage() {
return CommonComponents.joinForNarration(super.getNarrationMessage(), this.messageText);
}
@Override
protected void init() {
super.init();
this.message = MultiLineLabel.create(this.font, this.messageText, this.width - 50);
int lines = this.message.getLineCount();
int height = lines * this.font.lineHeight;
int buttonY = Mth.clamp(90 + height + 12, this.height / 6 + 96, this.height - 24);
this.addRenderableWidget(Button.builder(Component.literal("Recover").withStyle(ChatFormatting.GREEN), b -> {
Minecraft.getInstance().setScreen(this.lastScreen);
this.handler.accept(RecoveryOption.RECOVER);
}).bounds((this.width - 304) / 2, buttonY, 96, 20).build());
this.addRenderableWidget(Button.builder(Component.literal("Skip").withStyle(ChatFormatting.YELLOW), b -> {
Minecraft.getInstance().setScreen(this.lastScreen);
this.handler.accept(RecoveryOption.SKIP);
}).bounds((this.width - 304) / 2 + 104, buttonY, 96, 20).build());
this.addRenderableWidget(Button.builder(Component.literal("Delete").withStyle(ChatFormatting.RED), b -> {
Minecraft.getInstance().setScreen(this.lastScreen);
this.handler.accept(RecoveryOption.DELETE);
}).bounds((this.width - 304)/2 + 208, buttonY, 96, 20).build());
}
@Override
public void render(GuiGraphics guiGraphics, int i, int j, float f) {
super.render(guiGraphics, i, j, f);
guiGraphics.drawCenteredString(this.font, this.title, this.width / 2, 70, 0xFFFFFF);
this.message.renderCentered(guiGraphics, this.width / 2, 90);
}
@Override
public boolean shouldCloseOnEsc() {
return false;
}
}
@@ -458,8 +458,9 @@ public class ReplaySelectionList extends ObjectSelectionList<ReplaySelectionList
}
public void editReplay() {
SystemToast.add(this.minecraft.getToasts(), new SystemToast.SystemToastId(3000),
Component.literal("Sorry"), Component.literal("Editing replay data hasn't been implemented yet"));
Minecraft.getInstance().setScreen(new EditReplayScreen(Minecraft.getInstance().screen, this.summary));
// SystemToast.add(this.minecraft.getToasts(), new SystemToast.SystemToastId(3000),
// Component.literal("Sorry"), Component.literal("Editing replay data hasn't been implemented yet"));
// todo: implement editing replay data
// EditWorldScreen editWorldScreen;
// ReplayStorageSource.ReplayStorageAccess levelStorageAccess;
@@ -34,11 +34,11 @@ public class SelectReplayScreen extends Screen {
this.selectButton = this.addRenderableWidget(Button.builder(Component.translatable("flashback.select_replay.open"), this::tryOpenReplay)
.bounds(this.width / 2 - 154, this.height - 52, 308, 20).build());
this.editButton = this.addRenderableWidget(Button.builder(Component.translatable("selectWorld.edit"), this::tryEditReplay)
.bounds(this.width / 2 - 154, this.height - 28, 150, 20).build());
.bounds(this.width / 2 - 154, this.height - 28, 97, 20).build());
this.deleteButton = this.addRenderableWidget(Button.builder(Component.translatable("selectWorld.delete"), this::tryDeleteReplay)
.bounds(this.width / 2 + 4, this.height - 28, 72, 20).build());
.bounds(this.width / 2 - 49, this.height - 28, 98, 20).build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_BACK, button -> this.minecraft.setScreen(this.lastScreen))
.bounds(this.width / 2 + 82, this.height - 28, 72, 20).build());
.bounds(this.width / 2 + 57, this.height - 28, 97, 20).build());
if (this.width/2 > 154 + 128) {
this.addRenderableWidget(Button.builder(Component.literal("Flashback Settings"), btn -> Minecraft.getInstance().setScreen(new ConfigScreen(this)))