Skip to content

Commit

Permalink
#252 Show stage overwrite request
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentFarris committed May 3, 2024
1 parent 794888f commit b6af3d5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 1 addition & 5 deletions src/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
36 changes: 33 additions & 3 deletions src/editor/stages/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/editor/ui/menu/editor_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b6af3d5

Please sign in to comment.