Skip to content

Commit

Permalink
Allow deleting savestates (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
kivutar authored Jun 8, 2021
1 parent 756754b commit 77e6257
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
26 changes: 23 additions & 3 deletions menu/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,36 @@ func askQuitConfirmation(cb func()) {
}
}

// Displays a confirmation dialog before deleting a playlist entry
func askDeleteConfirmation(cb func()) {
// Displays a confirmation dialog before deleting a playlist game entry
func askDeleteGameConfirmation(cb func()) {
menu.Push(buildYesNoDialog(
"Confirm before deleting",
"You are about to delete a menu entry.",
"You are about to delete a game entry.",
"Games and game data won't be removed.", func() {
cb()
}))
}

// Displays a confirmation dialog before deleting a playlist
func askDeletePlaylistConfirmation(cb func()) {
menu.Push(buildYesNoDialog(
"Confirm before deleting",
"You are about to delete a playlist.",
"Games and game data won't be removed.", func() {
cb()
}))
}

// Displays a confirmation dialog before deleting a savestate
func askDeleteSavestateConfirmation(cb func()) {
menu.Push(buildYesNoDialog(
"Confirm before deleting",
"You are about to delete a savestate.",
"This action is irreversible.", func() {
cb()
}))
}

func genericDrawHintBar() {
w, h := menu.GetFramebufferSize()
menu.DrawRect(0, float32(h)-70*menu.ratio, float32(w), 70*menu.ratio, 0, lightGrey)
Expand Down
2 changes: 1 addition & 1 deletion menu/scene_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func buildHistory() Scene {
system: game.System,
tags: tags,
callbackOK: func() { loadHistoryEntry(&list, game) },
callbackX: func() { askDeleteConfirmation(func() { deleteHistoryEntry(&list, game) }) },
callbackX: func() { askDeleteGameConfirmation(func() { deleteHistoryEntry(&list, game) }) },
})
}

Expand Down
2 changes: 1 addition & 1 deletion menu/scene_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func buildPlaylist(path string) Scene {
tags: tags,
icon: utils.FileName(path) + "-content",
callbackOK: func() { loadPlaylistEntry(&list, list.label, game) },
callbackX: func() { askDeleteConfirmation(func() { deletePlaylistEntry(&list, path, game) }) },
callbackX: func() { askDeleteGameConfirmation(func() { deletePlaylistEntry(&list, path, game) }) },
})
}

Expand Down
33 changes: 32 additions & 1 deletion menu/scene_savestates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package menu

import (
"os"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -60,6 +61,7 @@ func buildSavestates() Scene {
ntf.DisplayAndLog(ntf.Success, "Menu", "State loaded.")
}
},
callbackX: func() { askDeleteSavestateConfirmation(func() { deleteSavestateEntry(&list, path) }) },
})
}

Expand Down Expand Up @@ -88,6 +90,30 @@ func (s *sceneSavestates) update(dt float32) {
genericInput(&s.entry, dt)
}

func removeSavestateEntry(s []entry, path string) []entry {
l := []entry{}
for _, e := range s {
if e.path != path {
l = append(l, e)
}
}

return l
}

func deleteSavestateEntry(list *sceneSavestates, path string) {
err := os.Remove(path)
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Menu", "Could not delete savestate: %s", err.Error())
return
}
list.children = removeSavestateEntry(list.children, path)
if list.ptr >= len(list.children) {
list.ptr = len(list.children) - 1
}
genericAnimate(&list.entry)
}

// Override rendering
func (s *sceneSavestates) render() {
list := &s.entry
Expand Down Expand Up @@ -140,7 +166,7 @@ func (s *sceneSavestates) drawHintBar() {

ptr := menu.stack[len(menu.stack)-1].Entry().ptr

_, upDown, _, a, b, _, _, _, _, guide := hintIcons()
_, upDown, _, a, b, x, _, _, _, guide := hintIcons()

var stack float32
if state.CoreRunning {
Expand All @@ -153,4 +179,9 @@ func (s *sceneSavestates) drawHintBar() {
} else {
stackHint(&stack, a, "LOAD", h)
}

list := menu.stack[len(menu.stack)-1].Entry()
if list.children[list.ptr].callbackX != nil {
stackHint(&stack, x, "DELETE", h)
}
}
2 changes: 1 addition & 1 deletion menu/scene_tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func getPlaylists() []entry {
callbackOK: func() {
menu.Push(buildPlaylist(path))
},
callbackX: func() { askDeleteConfirmation(func() { deletePlaylist(path) }) },
callbackX: func() { askDeletePlaylistConfirmation(func() { deletePlaylist(path) }) },
})
}
return pls
Expand Down

0 comments on commit 77e6257

Please sign in to comment.