-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
fonteditor.go
162 lines (131 loc) · 3.99 KB
/
fonteditor.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Package hsfonteditor contains font editor's data
package hsfonteditor
import (
"fmt"
"github.com/OpenDiablo2/dialog"
g "github.com/ianling/giu"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsfiletypes/hsfont"
"github.com/OpenDiablo2/HellSpawner/hscommon/hsproject"
"github.com/OpenDiablo2/HellSpawner/hscommon"
"github.com/OpenDiablo2/HellSpawner/hsconfig"
"github.com/OpenDiablo2/HellSpawner/hswindow/hseditor"
)
const (
mainWindowW, mainWindowH = 400, 300
pathSize = 245
browseW, browseH = 30, 0
)
// static check, to ensure, if font editor implemented editoWindow
var _ hscommon.EditorWindow = &FontEditor{}
// FontEditor represents a font editor
type FontEditor struct {
*hseditor.Editor
*hsfont.Font
}
// Create creates a new font editor
func Create(_ *hsconfig.Config,
_ hscommon.TextureLoader,
pathEntry *hscommon.PathEntry,
_ []byte,
data *[]byte, x, y float32, project *hsproject.Project) (hscommon.EditorWindow, error) {
font, err := hsfont.LoadFromJSON(*data)
if err != nil {
return nil, fmt.Errorf("error loading JSON font: %w", err)
}
result := &FontEditor{
Editor: hseditor.New(pathEntry, x, y, project),
Font: font,
}
if w, h := result.CurrentSize(); w == 0 || h == 0 {
result.Size(mainWindowW, mainWindowH)
}
return result, nil
}
// Build builds an editor
func (e *FontEditor) Build() {
e.IsOpen(&e.Visible).
Layout(g.Layout{
g.Label("DC6 Path"),
g.Row(
g.InputText("##FontEditorDC6Path", &e.SpriteFile).Size(pathSize).Flags(g.InputTextFlags_ReadOnly),
g.Button("...##FontEditorDC6Browse").Size(browseW, browseH).OnClick(e.onBrowseDC6PathClicked),
),
g.Separator(),
g.Label("TBL Path"),
g.Row(
g.InputText("##FontEditorTBLPath", &e.TableFile).Size(pathSize).Flags(g.InputTextFlags_ReadOnly),
g.Button("...##FontEditorTBLBrowse").Size(browseW, browseH).OnClick(e.onBrowseTBLPathClicked),
),
g.Separator(),
g.Label("PL2 Path"),
g.Row(
g.InputText("##FontEditorPL2Path", &e.PaletteFile).Size(pathSize).Flags(g.InputTextFlags_ReadOnly),
g.Button("...##FontEditorPL2Browse").Size(browseW, browseH).OnClick(e.onBrowsePL2PathClicked),
),
})
}
func (e *FontEditor) onBrowseDC6PathClicked() {
path := dialog.File().SetStartDir(e.Project.GetProjectFileContentPath())
path.Filter("DC6 File", "dc6", "DC6")
filePath, err := path.Load()
if err != nil || filePath == "" {
return
}
e.SpriteFile = filePath
}
func (e *FontEditor) onBrowseTBLPathClicked() {
path := dialog.File().SetStartDir(e.Project.GetProjectFileContentPath())
path.Filter("TBL File", "tbl", "TBL")
filePath, err := path.Load()
if err != nil || filePath == "" {
return
}
e.TableFile = filePath
}
func (e *FontEditor) onBrowsePL2PathClicked() {
path := dialog.File().SetStartDir(e.Project.GetProjectFileContentPath())
path.Filter("PL2 File", "pl2", "PL2")
filePath, err := path.Load()
if err != nil || filePath == "" {
return
}
e.PaletteFile = filePath
}
// UpdateMainMenuLayout updates main menu layout to it contains editors options
func (e *FontEditor) UpdateMainMenuLayout(l *g.Layout) {
m := g.Menu("Font Editor").Layout(g.Layout{
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 generates data to be saved
func (e *FontEditor) GenerateSaveData() []byte {
data, err := e.JSON()
if err != nil {
fmt.Println("failed to marshal font to JSON:, ", err)
return nil
}
return data
}
// Save saves an editor
func (e *FontEditor) Save() {
e.Editor.Save(e)
}
// Cleanup hides an editor
func (e *FontEditor) 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()
}