-
Notifications
You must be signed in to change notification settings - Fork 3
/
autosplits.go
345 lines (328 loc) · 9.11 KB
/
autosplits.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
package main
import (
"bufio"
"bytes"
"errors"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"io"
"path"
"regexp"
"slices"
)
type splitData struct {
description, tooltip, id string
}
var splitsFileBuf []byte
var splitDescriptions []string
var splitsDictIdToDescriptions = make(map[string]string)
var splitsDict = make(map[string]*splitData)
var splitsSearchDict = make(map[string][]string)
func initSplitsSearchDict(content string) {
rs := ([]rune)(content)
for i := 0; i < len(rs); i++ {
for j := i + 1; j <= len(rs); j++ {
s := string(rs[i:j])
v := splitsSearchDict[s]
if !slices.Contains(v, content) {
splitsSearchDict[s] = append(v, content)
}
}
}
}
func initSplitsFile(init bool) error {
var err error
splitsFileBuf, err = assets.ReadFile(path.Join(hkSplitMakerDir, "splits.txt"))
if err != nil && !init {
return err
}
rd := bufio.NewReader(bytes.NewReader(splitsFileBuf))
re, err := regexp.Compile(`\[Description\("(.*?)"\)\s*,\s*ToolTip\("(.*?)"\)]`)
if err != nil {
return err
}
splitDescriptions = nil
splitsDict = make(map[string]*splitData)
var isNameLine bool
line, isPrefix, err := rd.ReadLine()
var result []string
for ; err == nil; line, isPrefix, err = rd.ReadLine() {
if isPrefix {
err = errors.New("尚未支持这种文件,尽情期待更新")
break
}
line = bytes.Trim(bytes.TrimSpace(line), ",")
if len(line) == 0 {
continue
}
if isNameLine {
if len(result) == 3 {
description := translate(result[1])
splitDescriptions = append(splitDescriptions, description)
splitsDict[description] = &splitData{description: description, tooltip: result[2], id: string(line)}
splitsDictIdToDescriptions[string(line)] = description
initSplitsSearchDict(description)
isNameLine = false
} else {
err = errors.New("splits.txt文件格式错误")
break
}
} else {
result = re.FindStringSubmatch(string(line))
if result == nil {
err = errors.New("splits.txt文件格式错误")
break
}
isNameLine = true
}
}
if err == io.EOF {
return nil
}
return err
}
type splitIdModel struct {
walk.ListModelBase
items []string
}
func (s *splitIdModel) Value(index int) interface{} {
return s.items[index]
}
func (s *splitIdModel) ItemCount() int {
return len(s.items)
}
type lineData struct {
line *walk.Composite
name *walk.LineEdit
splitId *walk.ComboBox
splitTime *splitTimeData
}
type finalLineData struct {
lineData
splitId2 *walk.ComboBox
endTrigger *walk.CheckBox
}
var lines []*lineData
var finalLine finalLineData
func addLine(initAll bool) {
line := new(lineData)
c := Composite{
AssignTo: &line.line,
Layout: HBox{},
MaxSize: Size{Width: 0, Height: 25},
Children: []Widget{
LineEdit{AssignTo: &line.name, MinSize: Size{Width: 200}, ToolTipText: "片段名"},
ComboBox{AssignTo: &line.splitId, Editable: true, MinSize: Size{Width: 200},
Model: &splitIdModel{}, Value: splitDescriptions[0],
OnTextChanged: func() {
onSearchSplitId(initAll, line)
},
},
PushButton{Text: "✘", MaxSize: Size{Width: 25}, ToolTipText: "删除", OnClicked: func() {
if len(lines) > 1 {
removeLine(line)
}
}},
PushButton{Text: "↑+", MaxSize: Size{Width: 25}, ToolTipText: "在上方增加一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
addLine(true)
moveLine(idx)
},
},
PushButton{Text: "↓+", MaxSize: Size{Width: 25}, ToolTipText: "在下方增加一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
addLine(true)
moveLine(idx + 1)
},
},
PushButton{Text: "↑", MaxSize: Size{Width: 25}, ToolTipText: "上移一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
swapLine(idx-1, idx)
},
},
PushButton{Text: "↓", MaxSize: Size{Width: 25}, ToolTipText: "下移一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
swapLine(idx, idx+1)
},
},
},
}
err := c.Create(NewBuilder(splitLinesView))
if err != nil {
walk.MsgBox(nil, "错误", err.Error(), walk.MsgBoxIconError)
}
lines = append(lines, line)
}
func removeLine(line *lineData) {
idx := splitLinesView.Children().Index(line.line)
if idx < 0 {
walk.MsgBox(mainWindow, "错误", "无法删除这一行", walk.MsgBoxIconError)
return
}
err := splitLinesView.Children().RemoveAt(idx)
if err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
return
}
line.line.Dispose()
lines = append(lines[:idx], lines[idx+1:]...)
}
func resetLines(count int) {
err := splitLinesViewContainer.Children().RemoveAt(0)
if err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
return
}
splitLinesView.Dispose()
composite := Composite{
AssignTo: &splitLinesView,
Alignment: AlignHCenterVNear,
Layout: VBox{},
}
lines = []*lineData{}
for i := 0; i < count; i++ {
line := new(lineData)
composite.Children = append(composite.Children, Composite{
AssignTo: &line.line,
Layout: HBox{},
MaxSize: Size{Width: 0, Height: 25},
Children: []Widget{
LineEdit{AssignTo: &line.name, MinSize: Size{Width: 200}},
ComboBox{AssignTo: &line.splitId, Editable: true, MinSize: Size{Width: 200},
Model: &splitIdModel{}, Value: splitDescriptions[0],
OnTextChanged: func() {
onSearchSplitId(false, line)
},
},
PushButton{Text: "✘", MaxSize: Size{Width: 25}, ToolTipText: "删除", OnClicked: func() {
if len(lines) > 1 {
removeLine(line)
}
}},
PushButton{Text: "↑+", MaxSize: Size{Width: 25}, ToolTipText: "在上方增加一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
addLine(true)
moveLine(idx)
},
},
PushButton{Text: "↓+", MaxSize: Size{Width: 25}, ToolTipText: "在下方增加一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
addLine(true)
moveLine(idx + 1)
},
},
PushButton{Text: "↑", MaxSize: Size{Width: 25}, ToolTipText: "上移一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
swapLine(idx-1, idx)
},
},
PushButton{Text: "↓", MaxSize: Size{Width: 25}, ToolTipText: "下移一行",
OnClicked: func() {
idx := splitLinesView.Children().Index(line.line)
swapLine(idx, idx+1)
},
},
},
})
lines = append(lines, line)
}
err = composite.Create(NewBuilder(splitLinesViewContainer))
if err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
panic(err)
}
}
func swapLine(index1, index2 int) {
if index1 == index2 || index1 < 0 || index2 < 0 || index1 >= len(lines) || index2 >= len(lines) {
return
}
name1 := lines[index1].name.Text()
name2 := lines[index2].name.Text()
splitId1 := lines[index1].splitId.Text()
splitId2 := lines[index2].splitId.Text()
if err := lines[index1].name.SetText(name2); err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
panic(err)
}
if err := lines[index2].name.SetText(name1); err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
panic(err)
}
if err := lines[index1].splitId.SetText(splitId2); err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
panic(err)
}
if err := lines[index2].splitId.SetText(splitId1); err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
panic(err)
}
}
func moveLine(index int) {
last := lines[len(lines)-1]
if len(last.name.Text()) != 0 {
walk.MsgBox(nil, "错误", "内部错误", walk.MsgBoxIconError)
return
}
for i := len(lines) - 2; i >= index; i-- {
lines[i+1].splitId.Model().(*splitIdModel).items = lines[i].splitId.Model().(*splitIdModel).items
lines[i+1].splitId.Model().(*splitIdModel).PublishItemsReset()
err := lines[i+1].splitId.SetText(lines[i].splitId.Text())
if err != nil {
walk.MsgBox(nil, "错误", err.Error(), walk.MsgBoxIconError)
return
}
err = lines[i+1].name.SetText(lines[i].name.Text())
if err != nil {
walk.MsgBox(nil, "错误", err.Error(), walk.MsgBoxIconError)
return
}
lines[i+1].splitTime = lines[i].splitTime
}
err := lines[index].name.SetText("")
if err != nil {
walk.MsgBox(nil, "错误", err.Error(), walk.MsgBoxIconError)
return
}
lines[index].splitTime = nil
}
func onSearchSplitId(initAll bool, line *lineData) {
s := line.splitId.Text()
model := line.splitId.Model().(*splitIdModel)
if len(model.items) == 0 {
if initAll {
model.items = splitDescriptions
} else {
model.items = []string{s}
}
model.PublishItemsReset()
return
}
if len(s) > 0 {
if _, ok := splitsDict[s]; ok {
if line.name != nil {
err := line.name.SetText(dropBrackets(s))
if err != nil {
walk.MsgBox(mainWindow, "错误", err.Error(), walk.MsgBoxIconError)
}
}
return
}
v, ok := splitsSearchDict[s]
if ok && len(v) > 0 {
if len(model.items) > 0 {
model.PublishItemsRemoved(0, len(model.items)-1)
}
model.items = v
if len(v) > 0 {
model.PublishItemsInserted(0, len(v)-1)
}
}
}
}