-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
palette_map_editor.go
104 lines (85 loc) · 2.69 KB
/
palette_map_editor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Package hspalettemapeditor contains palette map editor's data
package hspalettemapeditor
import (
"fmt"
g "github.com/ianling/giu"
"github.com/OpenDiablo2/dialog"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2pl2"
"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsproject"
"github.com/OpenDiablo2/HellSpawner/hsconfig"
"github.com/OpenDiablo2/HellSpawner/hswidget/palettemapwidget"
"github.com/OpenDiablo2/HellSpawner/hswindow/hseditor"
)
// static check, to ensure, if palette map editor implemented editoWindow
var _ hscommon.EditorWindow = &PaletteMapEditor{}
// PaletteMapEditor represents a palette map editor
type PaletteMapEditor struct {
*hseditor.Editor
pl2 *d2pl2.PL2
textureLoader hscommon.TextureLoader
state []byte
}
// Create creates a new palette map editor
func Create(_ *hsconfig.Config,
textureLoader hscommon.TextureLoader,
pathEntry *hscommon.PathEntry,
state []byte,
data *[]byte, x, y float32, project *hsproject.Project) (hscommon.EditorWindow, error) {
pl2, err := d2pl2.Load(*data)
if err != nil {
return nil, fmt.Errorf("error loading PL2 file: %w", err)
}
result := &PaletteMapEditor{
Editor: hseditor.New(pathEntry, x, y, project),
pl2: pl2,
textureLoader: textureLoader,
state: state,
}
result.Path = pathEntry
return result, nil
}
// Build builds an editor
func (e *PaletteMapEditor) Build() {
e.IsOpen(&e.Visible).
Flags(g.WindowFlagsAlwaysAutoResize).
Layout(g.Layout{
palettemapwidget.Create(e.textureLoader, e.Path.GetUniqueID(), e.pl2, e.state),
})
}
// UpdateMainMenuLayout updates a main menu layout to it contains editors options
func (e *PaletteMapEditor) UpdateMainMenuLayout(l *g.Layout) {
m := g.Menu("Palette Map Editor").Layout(g.Layout{
g.MenuItem("Save\t\t\t\tCtrl+Shift+S").OnClick(e.Save),
g.Separator(),
g.MenuItem("Add to project").OnClick(func() {}),
g.MenuItem("Remove from project").OnClick(func() {}),
g.Separator(),
g.MenuItem("Import from file...").OnClick(func() {}),
g.MenuItem("Export to file...").OnClick(func() {}),
g.Separator(),
g.MenuItem("Close").OnClick(func() {
e.Cleanup()
}),
})
*l = append(*l, m)
}
// GenerateSaveData creates data to be saved
func (e *PaletteMapEditor) GenerateSaveData() []byte {
data := e.pl2.Marshal()
return data
}
// Save saves an editor
func (e *PaletteMapEditor) Save() {
e.Editor.Save(e)
}
// Cleanup hides an editor
func (e *PaletteMapEditor) Cleanup() {
if e.HasChanges(e) {
if shouldSave := dialog.Message("There are unsaved changes to %s, save before closing this editor?",
e.Path.FullPath).YesNo(); shouldSave {
e.Save()
}
}
e.Editor.Cleanup()
}