-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgui.go
350 lines (310 loc) · 8.36 KB
/
gui.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"errors"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/k0kubun/go-ansi"
"github.com/macroblock/imed/pkg/ptool"
browser "github.com/malashin/dialog"
)
func setupUI(app fyne.App) {
win = app.NewWindow("TreeSnap")
pBar = widget.NewProgressBar()
pBar.Hide()
win.SetContent(
container.NewVBox(
widget.NewButton("Select focus file(s)", func() { selectFocusFiles() }),
widget.NewButton("Select HOI4 folder", func() { selectGameFolder() }),
widget.NewButton("Add dependency mod folder(s)", func() { selectModFolder() }),
widget.NewButton("Select localisation language", func() { selectLocLanguage(app) }),
widget.NewButton("Generate image", func() { start() }),
widget.NewCheck("Disable line rendering", func(on bool) { lineRenderingToggle(on) }),
pBar,
widget.NewButton("Quit", func() {
app.Quit()
}),
),
)
win.CenterOnScreen()
win.ShowAndRun()
}
func selectFocusFiles() {
filename, err := browser.File().Title("National Focus File").Filter("Text file", "txt").LoadFiles()
if err != nil {
if err.Error() == "Cancelled" {
return
}
ansi.Println("\x1b[31;1m" + err.Error() + "\x1b[0m")
showError(err)
return
}
focusTreePaths = filename
ansi.Println("Focus files selected:", filename)
}
func selectGameFolder() {
directory, err := browser.Directory().Title("HOI4 Folder").Browse()
if err != nil {
if err.Error() == "Cancelled" {
return
}
ansi.Println("\x1b[31;1m" + err.Error() + "\x1b[0m")
showError(err)
return
}
gamePath = directory
ansi.Println("Game folder selected:", directory)
err = encodeCacheFile(gamePath, filepath.Join(binPath, "hoi4treesnapGamePath.txt"))
if err != nil && err.Error() != "Cancelled" {
ansi.Println("\x1b[31;1m" + err.Error() + "\x1b[0m")
showError(err)
return
}
}
func selectModFolder() {
directory, err := browser.Directory().Title("Mod Folder").Browse()
if err != nil {
if err.Error() == "Cancelled" {
return
}
ansi.Println("\x1b[31;1m" + err.Error() + "\x1b[0m")
showError(err)
return
}
modPaths = append(modPaths, directory)
ansi.Println("Mod folder added:", directory)
}
func selectLocLanguage(app fyne.App) {
w := app.NewWindow("Select localisation language")
w.SetContent(
container.NewVBox(
widget.NewRadioGroup([]string{"English", "Brazilian Portuguese", "German", "French", "Spanish", "Polish", "Russian"}, func(s string) { handleLocLanguageChange(s, w) }),
),
)
w.CenterOnScreen()
w.Show()
}
func handleLocLanguageChange(s string, w fyne.Window) {
switch s {
case "English":
language = "l_english"
case "Brazilian Portuguese":
language = "l_braz_por"
case "German":
language = "l_german"
case "French":
language = "l_french"
case "Spanish":
language = "l_spanish"
case "Polish":
language = "l_polish"
case "Russian":
language = "l_russian"
}
ansi.Println("Language selected:", s)
w.Close()
}
func lineRenderingToggle(on bool) {
if on {
isLineRenderingOff = true
} else {
isLineRenderingOff = false
}
}
func start() {
if running {
return
}
running = true
var err error
locMap[language] = make(map[string]Localisation)
gfxList = append(gfxList, "GFX_focus_can_start")
switch {
case len(focusTreePaths) == 0:
showError(errors.New("Focus file not selected"))
return
case gamePath == "":
p := filepath.Join(binPath, "hoi4treesnapGamePath.txt")
if _, err = os.Stat(p); err == nil {
err = decodeCacheFile(&gamePath, p)
if err != nil {
showError(err)
return
}
} else {
showError(errors.New("Game path not selected"))
return
}
}
// Track start time for benchmarking.
startTime := time.Now()
// Build parsers.
pdx, err = ptool.NewBuilder().FromString(pdxRule).Entries("entry").Build()
if err != nil {
showError(err)
return
}
yml, err = ptool.NewBuilder().FromString(ymlRule).Entries("entry").Build()
if err != nil {
showError(err)
return
}
for _, focusTreePath := range focusTreePaths {
// Show progress bar.
pBar.Show()
focusTreeName := filepath.Base(focusTreePath)
focusTreeName = focusTreeName[0 : len(focusTreeName)-len(filepath.Ext(focusTreeName))]
modPath := filepath.Clean(strings.TrimSuffix(filepath.Dir(focusTreePath), filepath.Join("common", "national_focus")))
// Add gamePath to the front of modsPath slice.
if !containsString(modPaths, gamePath) {
modPaths = append([]string{gamePath}, modPaths...)
}
// If modsPaths slice does not contain the mod path the focus tree is in add it to the end of the slice.
if !containsString(modPaths, modPath) {
modPaths = append(modPaths, modPath)
}
ansi.Println("\x1b[33;1m" + "Parsing files:" + "\x1b[0m")
// Focus tree parsing.
err = parseFocus(focusTreePath)
if err != nil {
showError(err)
return
}
pBar.SetValue(0.05)
// Parse focus tree gui.
// Find the last nationalfocusview.gui in the modPaths slice.
guiPath := gamePath
if len(modPaths) > 1 {
for _, p := range modPaths[1:] {
if _, err = os.Stat(filepath.Join(p, "interface", "nationalfocusview.gui")); err == nil {
guiPath = p
}
}
}
err = parseGUI(guiPath)
if err != nil {
showError(err)
return
}
pBar.SetValue(0.1)
// GFX parsing.
for _, p := range modPaths {
err = parseGFX(p, len(modPaths))
if err != nil {
showError(err)
return
}
}
// Parse localisation files.
for _, p := range modPaths {
err = parseLoc(p, len(modPaths))
if err != nil {
showError(err)
return
}
}
ansi.Println("\x1b[33;1m" + "Generating images:" + "\x1b[0m")
var i float64 = 8
// Replace hoi4 textures if mods has the same ones.
useModsTexturesIfPresent()
pBar.SetValue(pBar.Value + 0.1/i)
// Calculate coordinates of focuses with relative positions.
fillAbsoluteFocusPositions(true)
pBar.SetValue(pBar.Value + 0.1/i)
// Fill in focus structs with children data.
fillFocusChildAndParentData()
pBar.SetValue(pBar.Value + 0.1/i)
// Move coordinates of focuses so that negative values are no longer present.
moveAbsoluteFocusPositionsToPositiveValues()
// Create image.
x, y := maxFocusPos(focusMap)
w := (x+2)*gui.FocusSpacing.X + spacingX + 17
h := (y+1)*gui.FocusSpacing.Y + spacingY
img := image.NewRGBA(image.Rectangle{image.ZP, image.Point{w, h}})
draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{0, 0, 0, 0}}, image.ZP, draw.Src)
pBar.SetValue(pBar.Value + 0.1/i)
// Init fonts.
replaceFontPathsIfNotFound()
font, err = initFont(gui.Name.Font)
if err != nil {
showError(err)
return
}
fontTreeTitle, err = initFont(gui.NationalFocusTitle.Font)
if err != nil {
showError(err)
return
}
pBar.SetValue(pBar.Value + 0.1/i)
if !isLineRenderingOff {
// Draw focus tree lines.
renderLines(img)
pBar.SetValue(pBar.Value + 0.1/i)
// Draw exclusivity lines.
err = renderExclusiveLines(img)
if err != nil {
showError(err)
return
}
}
pBar.SetValue(pBar.Value + 0.1/i)
// Draw focus icons.
var focusErrMap = make(map[string]bool)
for _, f := range focusMap {
err = renderFocus(img, f.X*gui.FocusSpacing.X+spacingX, f.Y*gui.FocusSpacing.Y+spacingY, f.ID)
// Save all focus icons errors into a map.
if err != nil {
focusErrMap[err.Error()] = true
}
}
// Print out all of the errors at once, popup window on the last one.
focusErrMapI := 0
for errString := range focusErrMap {
if focusErrMapI == len(focusErrMap)-1 {
showError(errors.New(errString))
return
}
ansi.Println("\x1b[31;1m" + errString + "\x1b[0m")
focusErrMapI++
}
pBar.SetValue(pBar.Value + 0.1/i)
// Save image as PNG.
out, err := os.Create(filepath.Join(binPath, focusTreeName+".png"))
if err != nil {
showError(err)
return
}
err = png.Encode(out, img)
if err != nil {
showError(err)
return
}
err = out.Close()
if err != nil {
showError(err)
return
}
ansi.Println("Image saved at \"" + filepath.Join(binPath, focusTreeName+".png") + "\"")
pBar.SetValue(1)
// Clear maps.
focusMap = make(map[string]Focus)
gfxMap = make(map[string]SpriteType)
fontMap = make(map[string]BitmapFont)
locMap = make(map[string]map[string]Localisation)
// Hide progress bar.
pBar.Hide()
pBar.SetValue(0)
}
// Print out elapsed time.
elapsedTime := time.Since(startTime)
ansi.Printf("\x1b[30;1m"+"Elapsed time: %s\n\n"+"\x1b[0m", elapsedTime)
running = false
}