From 5baa457d3fc154f143c84d388cb632a3ee057c36 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 20 Apr 2021 13:39:30 +0200 Subject: [PATCH] Implement Region.undo Implement Region.redo Implement Region undoSession implement --- .../de/steamwar/bausystem/region/Region.java | 70 +++++++++++++++---- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index 7e8dc5a1..0671f240 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -172,13 +172,6 @@ public class Region { } } - private void initSessions() { - if (undoSessions == null) { - undoSessions = new SizedStack<>(20); - redoSessions = new SizedStack<>(20); - } - } - public boolean inRegion(Location location, RegionType regionType, RegionExtensionType regionExtensionType) { if (!hasType(regionType)) { return false; @@ -361,40 +354,44 @@ public class Region { PasteOptions pasteOptions = new PasteOptions((schematic != null && (schematic.getSchemType().fightType() || schematic.getSchemType().check())), ignoreAir, getPlain(Flag.COLOR, ColorMode.class).getColor(), regionExtensionType == RegionExtensionType.EXTENSION, getMinPoint(regionType, regionExtensionType), getMaxPoint(regionType, regionExtensionType), waterLevel); + EditSession editSession = null; switch (regionType) { case BUILD: System.out.println(schematic + " " + prototype.getBuild().getSchematicFile() + " " + regionType + " " + regionExtensionType + " " + minPointBuild); if (schematic != null) { - paste(schematic.load(), minPointBuild, pasteOptions); + editSession = paste(schematic.load(), minPointBuild, pasteOptions); } else { - paste(prototype.getBuild().getSchematicFile(), minPointBuild, pasteOptions); + editSession = paste(prototype.getBuild().getSchematicFile(), minPointBuild, pasteOptions); } break; case TESTBLOCK: System.out.println(schematic + " " + prototype.getBuild().getSchematicFile() + " " + regionType + " " + regionExtensionType + " " + minPointTestblock); if (schematic != null) { - paste(schematic.load(), minPointTestblock, pasteOptions); + editSession = paste(schematic.load(), minPointTestblock, pasteOptions); } else { - paste(prototype.getTestblock().getSchematicFile(), minPointTestblock, pasteOptions); + editSession = paste(prototype.getTestblock().getSchematicFile(), minPointTestblock, pasteOptions); } break; default: case NORMAL: System.out.println(schematic + " " + prototype.getBuild().getSchematicFile() + " " + regionType + " " + regionExtensionType + " " + minPoint); if (schematic != null) { - paste(schematic.load(), minPoint, pasteOptions); + editSession = paste(schematic.load(), minPoint, pasteOptions); } else { - paste(prototype.getSchematicFile(), minPoint, pasteOptions); + editSession = paste(prototype.getSchematicFile(), minPoint, pasteOptions); } break; } + + initSessions(); + undoSessions.push(editSession); } - private static EditSession paste(File file, Point pastePoint, PasteOptions pasteOptions) { + private EditSession paste(File file, Point pastePoint, PasteOptions pasteOptions) { return VersionedCallable.call(new VersionedCallable<>(() -> Region_15.paste(file, pastePoint, pasteOptions), 15)); } - private static EditSession paste(Clipboard clipboard, Point pastePoint, PasteOptions pasteOptions) { + private EditSession paste(Clipboard clipboard, Point pastePoint, PasteOptions pasteOptions) { return VersionedCallable.call(new VersionedCallable<>(() -> Region_15.paste(clipboard, pastePoint, pasteOptions), 15)); } @@ -402,4 +399,47 @@ public class Region { return this == GlobalRegion.getInstance(); } + private void initSessions() { + if (undoSessions == null) { + undoSessions = new SizedStack<>(20); + redoSessions = new SizedStack<>(20); + } + } + + public boolean undo() { + initSessions(); + EditSession session = null; + try { + session = undoSessions.pop(); + if (session == null) { + return false; + } + session.undo(session); + redoSessions.push(session); + return true; + } finally { + if (session != null) { + session.flushSession(); + } + } + } + + public boolean redo() { + initSessions(); + EditSession session = null; + try { + session = redoSessions.pop(); + if (session == null) { + return false; + } + session.redo(session); + undoSessions.push(session); + return true; + } finally { + if (session != null) { + session.flushSession(); + } + } + } + } \ No newline at end of file