/
view_add_video.go
441 lines (348 loc) · 13.3 KB
/
view_add_video.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package main
import (
"image"
"path/filepath"
g143 "github.com/bankole7782/graphics143"
"github.com/bankole7782/videos349/v3shared"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
"github.com/go-gl/glfw/v3.3/glfw"
)
const (
VAV_AddBtn = 31
VAV_CloseBtn = 32
VAV_PickVideo = 33
VAV_BeginInput = 34
VAV_EndInput = 35
VAV_PickAudio = 36
)
var vavObjCoords map[int]g143.RectSpecs
var vavInputsStore map[string]string
var beginInputEnteredTxt string = "0:00"
var endInputEnteredTxt string = "0:00"
var selectedInput int
func drawViewAddVideo(window *glfw.Window, currentFrame image.Image) {
vavObjCoords = make(map[int]g143.RectSpecs)
vavInputsStore = make(map[string]string)
wWidth, wHeight := window.GetSize()
// frame buffer
ggCtx := gg.NewContext(wWidth, wHeight)
// background image
img := imaging.AdjustBrightness(currentFrame, -40)
ggCtx.DrawImage(img, 0, 0)
// load font
fontPath := getDefaultFontPath()
err := ggCtx.LoadFontFace(fontPath, 20)
if err != nil {
panic(err)
}
// dialog rectangle
dialogWidth := 600
dialogHeight := 300
dialogOriginX := (wWidth - dialogWidth) / 2
dialogOriginY := (wHeight - dialogHeight) / 2
ggCtx.SetHexColor("#fff")
ggCtx.DrawRectangle(float64(dialogOriginX), float64(dialogOriginY), float64(dialogWidth), float64(dialogHeight))
ggCtx.Fill()
// Add Video Header
ggCtx.SetHexColor("#444")
ggCtx.DrawString("Add Video Configuration", float64(dialogOriginX)+20, float64(dialogOriginY)+20+20)
// Add Btn
ggCtx.SetHexColor("#56845A")
addStrW, _ := ggCtx.MeasureString("Add")
addBtnOriginX := dialogWidth - int(addStrW) - 50 + dialogOriginX
ggCtx.DrawRoundedRectangle(float64(addBtnOriginX), float64(dialogOriginY)+20, addStrW+20, 30, 10)
ggCtx.Fill()
addBtnRS := g143.RectSpecs{OriginX: addBtnOriginX, OriginY: dialogOriginY + 20, Width: int(addStrW) + 20, Height: 30}
vavObjCoords[VAV_AddBtn] = addBtnRS
ggCtx.SetHexColor("#fff")
ggCtx.DrawString("Add", float64(addBtnOriginX)+10, float64(dialogOriginY)+20+20)
// Close Btn
ggCtx.SetHexColor("#B75F5F")
closeStrW, _ := ggCtx.MeasureString("Close")
closeBtnX := dialogOriginX + dialogWidth - 50 - int(addStrW) - 30 - int(closeStrW)
ggCtx.DrawRoundedRectangle(float64(closeBtnX), float64(dialogOriginY)+20, closeStrW+20, 30, 10)
ggCtx.Fill()
closeBtnRS := g143.RectSpecs{OriginX: closeBtnX, OriginY: dialogOriginY + 20, Width: int(closeStrW) + 20, Height: 30}
vavObjCoords[VAV_CloseBtn] = closeBtnRS
ggCtx.SetHexColor("#fff")
ggCtx.DrawString("Close", float64(closeBtnX)+10, float64(dialogOriginY)+20+20)
// end of top bar
// pick video label and btn
ggCtx.SetHexColor("#444")
pvStr := "video file"
pvStrW, _ := ggCtx.MeasureString(pvStr)
pvStrX := dialogOriginX + 40
pvStrY := dialogOriginY + fontSize + 70
ggCtx.DrawString(pvStr, float64(pvStrX), float64(pvStrY))
ggCtx.SetHexColor("#eee")
pvInputX := pvStrW + 30 + float64(dialogOriginX) + 40
pvInputW := dialogWidth - int(pvStrW) - 90
ggCtx.DrawRoundedRectangle(pvInputX, float64(pvStrY)-fontSize, float64(pvInputW), 60, 10)
ggCtx.Fill()
pfRS := g143.RectSpecs{OriginX: int(pvInputX), OriginY: pvStrY - fontSize, Width: pvInputW, Height: 60}
vavObjCoords[VAV_PickVideo] = pfRS
ggCtx.SetHexColor("#444")
ggCtx.DrawString("[click to pick file]", pvInputX+10, float64(pvStrY))
// begin str label and input
beginStr := "begin (mm:ss)"
beginStrW, _ := ggCtx.MeasureString(beginStr)
beginStrX := dialogOriginX + 40
beginStrY := pvStrY + fontSize + 60
ggCtx.DrawString(beginStr, float64(beginStrX), float64(beginStrY))
ggCtx.SetHexColor("#eee")
beginInputX := beginStrW + 30 + float64(dialogOriginX) + 40
ggCtx.DrawRoundedRectangle(beginInputX, float64(beginStrY)-fontSize, 100, 30, 10)
ggCtx.Fill()
biRS := g143.RectSpecs{OriginX: int(beginInputX), OriginY: beginStrY - fontSize, Width: 100, Height: 30}
vavObjCoords[VAV_BeginInput] = biRS
ggCtx.SetHexColor("#444")
ggCtx.DrawString("0:00", beginInputX+10, float64(beginStrY))
// end str label and input
endStr := "end (mm:ss)"
endStrW, _ := ggCtx.MeasureString(endStr)
endStrX := dialogOriginX + 40
endStrY := beginStrY + fontSize + 30
ggCtx.DrawString(endStr, float64(endStrX), float64(endStrY))
ggCtx.SetHexColor("#eee")
endInputX := endStrW + 30 + float64(dialogOriginX) + 40
ggCtx.DrawRoundedRectangle(endInputX, float64(endStrY)-fontSize, 100, 30, 10)
ggCtx.Fill()
eiRS := g143.RectSpecs{OriginX: int(endInputX), OriginY: endStrY - fontSize, Width: 100, Height: 30}
vavObjCoords[VAV_EndInput] = eiRS
ggCtx.SetHexColor("#444")
ggCtx.DrawString("0:00", endInputX+10, float64(endStrY))
// add audio input
sfl := "audio file (optional)"
sflW, _ := ggCtx.MeasureString(sfl)
sflY := eiRS.OriginY + eiRS.Height + 10 + 20
ggCtx.DrawString(sfl, float64(dialogOriginX)+40, float64(sflY))
ggCtx.SetHexColor("#eee")
sflBtnX := sflW + 50 + float64(dialogOriginX) + 40
ggCtx.DrawRoundedRectangle(sflBtnX, float64(sflY)-20, float64(dialogWidth)/2, 30, 10)
ggCtx.Fill()
sflBtnRS := g143.RectSpecs{Width: dialogWidth / 2, Height: 30, OriginX: int(sflBtnX), OriginY: sflY - 20}
vavObjCoords[VAV_PickAudio] = sflBtnRS
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
}
func viewAddVideoMouseCallback(window *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
if action != glfw.Release {
return
}
rootPath, _ := v3shared.GetRootPath()
xPos, yPos := window.GetCursorPos()
xPosInt := int(xPos)
yPosInt := int(yPos)
wWidth, wHeight := window.GetSize()
var widgetRS g143.RectSpecs
var widgetCode int
for code, RS := range vavObjCoords {
if g143.InRectSpecs(RS, xPosInt, yPosInt) {
widgetRS = RS
widgetCode = code
break
}
}
if widgetCode == 0 {
return
}
clearIndicators := func(window *glfw.Window, currentFrame image.Image) {
ggCtx := gg.NewContextForImage(currentWindowFrame)
beginInputRS := vavObjCoords[VAV_BeginInput]
endInputRS := vavObjCoords[VAV_EndInput]
ggCtx.SetHexColor("#fff")
ggCtx.DrawCircle(float64(beginInputRS.OriginX)+float64(beginInputRS.Width)+20, float64(beginInputRS.OriginY)+15, 20)
ggCtx.Fill()
ggCtx.SetHexColor("#fff")
ggCtx.DrawCircle(float64(endInputRS.OriginX)+float64(endInputRS.Width)+20, float64(endInputRS.OriginY)+15, 20)
ggCtx.Fill()
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
}
switch widgetCode {
case VAV_CloseBtn:
allDraws(window)
// register the ViewMain mouse callback
window.SetMouseButtonCallback(mouseBtnCallback)
// unregister the keyCallback
window.SetKeyCallback(nil)
case VAV_PickVideo:
filename := pickFileUbuntu("mp4|mkv|webm")
if filename == "" {
return
}
vavInputsStore["video"] = filepath.Join(rootPath, filename)
// write audio name
ggCtx := gg.NewContextForImage(currentWindowFrame)
// load font
fontPath := getDefaultFontPath()
err := ggCtx.LoadFontFace(fontPath, 20)
if err != nil {
panic(err)
}
ggCtx.SetHexColor("#eee")
ggCtx.DrawRoundedRectangle(float64(widgetRS.OriginX), float64(widgetRS.OriginY),
float64(widgetRS.Width), float64(widgetRS.Height), 10)
ggCtx.Fill()
ggCtx.SetHexColor("#444")
ggCtx.DrawString(filename, float64(widgetRS.OriginX+10), float64(widgetRS.OriginY+20))
// update end str
endInputRS := vavObjCoords[VAV_EndInput]
videoLength := lengthOfVideo(filepath.Join(rootPath, filename))
endInputEnteredTxt = videoLength
ggCtx.SetHexColor("#eee")
ggCtx.DrawRoundedRectangle(float64(endInputRS.OriginX), float64(endInputRS.OriginY),
float64(endInputRS.Width), float64(endInputRS.Height), 10)
ggCtx.Fill()
ggCtx.SetHexColor("#444")
ggCtx.DrawString(videoLength, float64(endInputRS.OriginX)+10, float64(endInputRS.OriginY+fontSize))
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
case VAV_BeginInput:
selectedInput = VAV_BeginInput
clearIndicators(window, currentWindowFrame)
ggCtx := gg.NewContextForImage(currentWindowFrame)
ggCtx.SetHexColor("#444")
ggCtx.DrawCircle(float64(widgetRS.OriginX)+float64(widgetRS.Width)+20, float64(widgetRS.OriginY)+15, 10)
ggCtx.Fill()
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
case VAV_EndInput:
selectedInput = VAV_EndInput
clearIndicators(window, currentWindowFrame)
ggCtx := gg.NewContextForImage(currentWindowFrame)
ggCtx.SetHexColor("#444")
ggCtx.DrawCircle(float64(widgetRS.OriginX)+float64(widgetRS.Width)+20, float64(widgetRS.OriginY)+15, 10)
ggCtx.Fill()
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
case VAV_PickAudio:
filename := pickFileUbuntu("mp3")
if filename == "" {
return
}
vavInputsStore["audio_optional"] = filepath.Join(rootPath, filename)
// write audio name
ggCtx := gg.NewContextForImage(currentWindowFrame)
// load font
fontPath := getDefaultFontPath()
err := ggCtx.LoadFontFace(fontPath, 20)
if err != nil {
panic(err)
}
ggCtx.SetHexColor("#eee")
ggCtx.DrawRoundedRectangle(float64(widgetRS.OriginX), float64(widgetRS.OriginY),
float64(widgetRS.Width), float64(widgetRS.Height), 10)
ggCtx.Fill()
ggCtx.SetHexColor("#444")
ggCtx.DrawString(filename, float64(widgetRS.OriginX)+10, float64(widgetRS.OriginY)+20)
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
case VAV_AddBtn:
if vavInputsStore["video"] == "" {
return
}
instructions = append(instructions, map[string]string{
"kind": "video",
"video": vavInputsStore["video"],
"begin": beginInputEnteredTxt,
"end": endInputEnteredTxt,
"audio_optional": vavInputsStore["audio_optional"],
})
allDraws(window)
// register the ViewMain mouse callback
window.SetMouseButtonCallback(mouseBtnCallback)
// unregister the keyCallback
window.SetKeyCallback(nil)
}
}
func vavkeyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if action != glfw.Release {
return
}
wWidth, wHeight := window.GetSize()
if selectedInput == VAV_BeginInput {
beginInputRS := vavObjCoords[VAV_BeginInput]
// enforce number types, semicolon and backspace
if isKeyNumeric(key) {
beginInputEnteredTxt += glfw.GetKeyName(key, scancode)
} else if key == glfw.KeySemicolon {
beginInputEnteredTxt += ":"
} else if key == glfw.KeyBackspace && len(beginInputEnteredTxt) != 0 {
beginInputEnteredTxt = beginInputEnteredTxt[:len(beginInputEnteredTxt)-1]
}
ggCtx := gg.NewContextForImage(currentWindowFrame)
// load font
fontPath := getDefaultFontPath()
err := ggCtx.LoadFontFace(fontPath, 20)
if err != nil {
panic(err)
}
ggCtx.SetHexColor("#eee")
ggCtx.DrawRoundedRectangle(float64(beginInputRS.OriginX), float64(beginInputRS.OriginY), float64(beginInputRS.Width),
float64(beginInputRS.Height), 10)
ggCtx.Fill()
ggCtx.SetHexColor("#444")
ggCtx.DrawString(beginInputEnteredTxt, float64(beginInputRS.OriginX+10), float64(beginInputRS.OriginY)+fontSize)
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
} else if selectedInput == VAV_EndInput {
endInputRS := vavObjCoords[VAV_EndInput]
// enforce number types, semicolon and backspace
if isKeyNumeric(key) {
endInputEnteredTxt += glfw.GetKeyName(key, scancode)
} else if key == glfw.KeySemicolon {
endInputEnteredTxt += ":"
} else if key == glfw.KeyBackspace && len(endInputEnteredTxt) != 0 {
endInputEnteredTxt = endInputEnteredTxt[:len(endInputEnteredTxt)-1]
}
ggCtx := gg.NewContextForImage(currentWindowFrame)
// load font
fontPath := getDefaultFontPath()
err := ggCtx.LoadFontFace(fontPath, 20)
if err != nil {
panic(err)
}
ggCtx.SetHexColor("#eee")
ggCtx.DrawRoundedRectangle(float64(endInputRS.OriginX), float64(endInputRS.OriginY), float64(endInputRS.Width),
float64(endInputRS.Height), 10)
ggCtx.Fill()
ggCtx.SetHexColor("#444")
ggCtx.DrawString(endInputEnteredTxt, float64(endInputRS.OriginX+10), float64(endInputRS.OriginY)+fontSize)
// send the frame to glfw window
windowRS := g143.RectSpecs{Width: wWidth, Height: wHeight, OriginX: 0, OriginY: 0}
g143.DrawImage(wWidth, wHeight, ggCtx.Image(), windowRS)
window.SwapBuffers()
// save the frame
currentWindowFrame = ggCtx.Image()
}
}