diff options
| author | Michael Lizzio <142752852+Michael-Lizzio@users.noreply.github.com> | 2025-11-11 08:17:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-11 08:17:00 -0500 |
| commit | f438bcc00e442ec18f6a0bc8715398b981b1b189 (patch) | |
| tree | df3e18485e9a4781236f3b6c5dd8fd79ecc564e3 /src/main/java/design/model/holeplay/HolePlayContext.java | |
| parent | 868e0b1e55763f2de686332b0887398839e1fe73 (diff) | |
| parent | b58b98704f6b2d2b4a5938f5a8b87eda268ad88b (diff) | |
| download | designproject-design-6-f438bcc00e442ec18f6a0bc8715398b981b1b189.tar.gz designproject-design-6-f438bcc00e442ec18f6a0bc8715398b981b1b189.tar.bz2 designproject-design-6-f438bcc00e442ec18f6a0bc8715398b981b1b189.zip | |
Merge pull request #15 from RIT-SWEN-262/lizzio-UndoRedoSubsystem
Lizzio undo redo subsystem
Diffstat (limited to 'src/main/java/design/model/holeplay/HolePlayContext.java')
| -rw-r--r-- | src/main/java/design/model/holeplay/HolePlayContext.java | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java index 7a5d8ef..71fba32 100644 --- a/src/main/java/design/model/holeplay/HolePlayContext.java +++ b/src/main/java/design/model/holeplay/HolePlayContext.java @@ -6,7 +6,10 @@ import java.util.ArrayList; import design.model.*; import design.persistence.PersonalDatabase; -public class HolePlayContext { +import design.model.undo.Memento; +import design.model.undo.Originator; + +public class HolePlayContext implements Originator { private final Golfer golfer; private final Round round; private final PersonalDatabase pdb; @@ -109,4 +112,60 @@ public class HolePlayContext { void setStrokes(int s) { this.strokes = s; } + + // Captures current State, current play (hole# + swings), and stroke counter + private static class HolePlayMemento implements Memento { + private final String stateName; + private final Play currentPlayCopy; + private final int strokesCopy; + + HolePlayMemento(HolePlayContext ctx) { + this.stateName = ctx.state.name(); + + // Copy currentPlay if it exists + Play src = ctx.currentPlay; + if (src != null) { + var copySwings = new ArrayList<Swing>(); + for (Swing s : src.getSwings()) { + copySwings.add(new Swing(s.getDistance(), s.getClubUsed())); + } + this.currentPlayCopy = new Play(src.getHoleNumber(), copySwings); + } else { + this.currentPlayCopy = null; + } + + this.strokesCopy = ctx.strokes; + } + } + + @Override + public Memento createMemento() { + return new HolePlayMemento(this); + } + + @Override + public void restore(Memento m) { + HolePlayMemento hm = (HolePlayMemento) m; + + // restore strokes + this.strokes = hm.strokesCopy; + + // restore currentPlay + if (hm.currentPlayCopy == null) { + this.currentPlay = null; + } else { + var copySwings = new ArrayList<Swing>(); + for (Swing s : hm.currentPlayCopy.getSwings()) { + copySwings.add(new Swing(s.getDistance(), s.getClubUsed())); + } + this.currentPlay = new Play(hm.currentPlayCopy.getHoleNumber(), copySwings); + } + + // restore state based on name + switch (hm.stateName) { + case "SetupState" -> this.state = new SetupState(); + case "PlayState" -> this.state = new PlayState(); + case "HoleCompleteState" -> this.state = new HoleCompleteState(); + } + } }
\ No newline at end of file |
