diff --git a/src/editor/editor.go b/src/editor/editor.go index 6f6f870f..5ec863d5 100644 --- a/src/editor/editor.go +++ b/src/editor/editor.go @@ -322,11 +322,7 @@ func (ed *Editor) update(delta float64) { } else if kb.KeyDown(hid.KeyboardKeyY) { ed.history.Redo() } else if kb.KeyUp(hid.KeyboardKeyS) { - if err := ed.stageManager.Save(); err != nil { - slog.Error("Save stage failed", slog.String("error", err.Error())) - } else { - ed.statusBar.SetMessage("Stage saved") - } + ed.stageManager.Save(ed.statusBar) } else if kb.KeyUp(hid.KeyboardKeyP) { ed.selection.Parent(&ed.history) ed.statusBar.SetMessage("Parented entities") diff --git a/src/editor/stages/manager.go b/src/editor/stages/manager.go index 6efd40bf..4ad4c878 100644 --- a/src/editor/stages/manager.go +++ b/src/editor/stages/manager.go @@ -39,19 +39,26 @@ package stages import ( "bytes" + "errors" "kaiju/assets/asset_importer" "kaiju/assets/asset_info" "kaiju/editor/alert" "kaiju/editor/editor_config" "kaiju/editor/memento" + "kaiju/editor/ui/status_bar" "kaiju/engine" "kaiju/filesystem" "kaiju/klib" "kaiju/systems/stages" + "log/slog" "os" "path/filepath" ) +var ( + ErrorSaveCancelled = errors.New("save was requested then cancelled") +) + type Manager struct { host *engine.Host registry *asset_importer.ImportRegistry @@ -85,14 +92,23 @@ func (m *Manager) New() { m.host.ClearEntities() } -func (m *Manager) Save() error { +func (m *Manager) saveInternal() error { if m.stage == "" { name := <-alert.NewInput("Stage Name", "Name of stage...", "", "Save", "Cancel", m.host) if name == "" { - return nil + return ErrorSaveCancelled } - m.stage = filepath.Join("content/stages/", name+editor_config.FileExtensionStage) + path := filepath.Join("content/stages/", name+editor_config.FileExtensionStage) + if _, err := os.Stat(path); err == nil { + ok := <-alert.New("Overwrite stage?", + "The stage "+path+" already exists. Would you like to overwrite it?", + "Yes", "No", m.host) + if !ok { + return ErrorSaveCancelled + } + } + m.stage = path } stream := bytes.NewBuffer(make([]byte, 0)) all := m.host.Entities() @@ -118,6 +134,20 @@ func (m *Manager) Save() error { return nil } +func (m *Manager) Save(statusBar *status_bar.StatusBar) error { + err := m.saveInternal() + if err == nil { + if statusBar != nil { + statusBar.SetMessage("Stage saved") + } + } else if errors.Is(err, ErrorSaveCancelled) && statusBar != nil { + statusBar.SetMessage("Stage save cancelled") + } else { + slog.Error("Save stage failed", slog.String("error", err.Error())) + } + return err +} + func (m *Manager) Load(adi asset_info.AssetDatabaseInfo, host *engine.Host) error { if !m.confirmCheck() { return nil diff --git a/src/editor/ui/menu/editor_menu.go b/src/editor/ui/menu/editor_menu.go index f83b988b..474acb78 100644 --- a/src/editor/ui/menu/editor_menu.go +++ b/src/editor/ui/menu/editor_menu.go @@ -187,11 +187,7 @@ func (m *Menu) newStage(*document.Element) { } func (m *Menu) saveStage(*document.Element) { - if err := m.editor.StageManager().Save(); err != nil { - slog.Error("Save stage failed", slog.String("error", err.Error())) - } else { - m.editor.StatusBar().SetMessage("Stage saved") - } + m.editor.StageManager().Save(m.editor.StatusBar()) } func (m *Menu) openProject(*document.Element) {