-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnimPlayerLib.hws
442 lines (381 loc) · 12.1 KB
/
AnimPlayerLib.hws
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
442
/*
** Enable/disable buttons depending on current list selection
*/
Function p_SetStates(ac, num)
moai.Set("rem", "disabled", (ac = -1))
moai.Set("mvdn", "disabled", (ac = num - 1) Or (ac = -1))
moai.Set("mvup", "disabled", (ac = 0) Or (ac = -1))
moai.Set("play", "disabled", (ac = -1))
If musstate$ = "playing"
moai.Set("play", "disabled", True)
moai.Set("last", "disabled", (ac = 0))
moai.Set("next", "disabled", (ac = num - 1))
EndIf
Local title = moai.DoMethod("lv", "GetEntry", ac)
moai.Set("main", "Title", title)
EndFunction
/*
** Disable buttons that only work while playback is active
*/
Function p_SetStopStates()
moai.Set("play", "disabled", False)
moai.Set("last", "disabled", True)
moai.Set("next", "disabled", True)
moai.Set("stop", "disabled", True)
EndFunction
/*
** Enable/disable buttons depending on current list selection
*/
Function p_RefreshActive()
Local ac = moai.Get("lv", "active")
Local num = moai.Get("lv", "entries")
If GetType(ac) = #STRING
If ac = "Off" Then ac = -1
EndIf
p_SetStates(ac, num)
EndFunction
/*
** Skip to previous track
*/
Function p_Last()
Local ac = moai.Get("lv", "active")
Local num = moai.Get("lv", "entries")
If ac - 1 >= 0
moai.Set("lv", "active", ac - 1)
If #DEBUG Then DebugPrint("p_Last(): Changed active Anim to previous, stopping current Anim with p_Stop()...")
p_Stop()
If #DEBUG Then DebugPrint("p_Last(): Now calling p_LoadSelectedAnim()...")
p_LoadSelectedAnim()
If #DEBUG Then DebugPrint("p_Last(): Now calling p_Play() on the loaded Anim...")
p_Play()
EndIf
EndFunction
/*
** Skip to next track
*/
Function p_Next()
Local ac = moai.Get("lv", "active")
Local num = moai.Get("lv", "entries")
If #DEBUG Then DebugPrint("Active Entry: ", ac, "Total Entries: ", num)
If ac + 1 < num
moai.Set("lv", "active", ac + 1)
If #DEBUG Then DebugPrint("p_Next(): Changed active Anim to next, stopping current Anim with p_Stop()...")
p_Stop()
If #DEBUG Then DebugPrint("p_Last(): Now calling p_LoadSelectedAnim()...")
p_LoadSelectedAnim()
If #DEBUG Then DebugPrint("p_Next(): Now calling p_Play() on the loaded Anim...")
p_Play()
Else
If #DEBUG Then DebugPrint("p_Next(): Last Anim in the playlist, now stopping...")
p_Stop()
EndIf
EndFunction
/*
** Return file name of currently selected video
*/
Function p_GetVideoSelection()
Return(moai.DoMethod("lv", "getentry", "active"))
EndFunction
/*
** Start playback
*/
Function p_Play()
If HaveObject(#ANIM, 1) = False
; If we have no Anim, no need to continue
If #DEBUG Then DebugPrint("p_Play(): Anim object was not found, giving up...")
If Not IsNil(animDrawInterval)
ClearInterval(animDrawInterval)
animDrawInterval = Nil
EndIf
Return
EndIf
; If a previous anim was already playing, we need to stop that first
If playing = True
If #DEBUG Then DebugPrint("p_Play(): Playing variable was True, calling FinishAsyncDraw() and setting playing to False.")
FinishAsyncDraw(playingAnim)
playing = False
EndIf
; Set state at Playing
musstate$ = "playing"
playing = True
p_RefreshActive()
; Enable the Stop button
moai.Set("stop", "disabled", False)
; create an interval function to draw the frames
If #DEBUG Then DebugPrint("p_Play(): Setting a new Interval")
animDrawInterval = SetInterval(Nil, p_DrawNextFrame, 20)
If #DEBUG Then DebugPrint("p_Play(): New interval value is:",animDrawInterval)
EndFunction
/*
** Resize Display window according to the option selected
*/
Function p_ResizeDisplay(scaleOption$, width, height)
Local w = 640
Local h = 480
If HaveObject(#ANIM, 1) = True
; Get the current Anim's actual width and height
w = GetAttribute(#ANIM, 1, #ATTRWIDTH)
h = GetAttribute(#ANIM, 1, #ATTRHEIGHT)
EndIf
If width = 0 Then width = curwidth
If height = 0 Then height = curheight
If scaleOption$ = "original"
ScaleLayer(1, "100%", "100%")
ChangeDisplaySize(w, h)
ElseIf scaleOption$ = "double"
ScaleLayer(1, "200%", "200%")
ChangeDisplaySize(w*2, h*2)
ElseIf width > 0 And height > 0
ScaleLayer(1, width, height)
ChangeDisplaySize(width, height)
EndIf
EndFunction
/*
** Stop playback
*/
Function p_Stop()
If Not IsNil(animDrawInterval)
If #DEBUG Then DebugPrint("p_Stop(): Attempting to ClearInterval():", animDrawInterval)
ClearInterval(animDrawInterval)
animDrawInterval = Nil
EndIf
If playing = True And curvideo$ <> ""
If #DEBUG Then DebugPrint("p_Stop(): Playing variable was True or an Anim was active, will now call FinishAsyncDraw()")
FinishAsyncDraw(playingAnim)
EndIf
playing = False
musstate$ = "stopped"
curvideo$ = ""
If #DEBUG Then DebugPrint("p_Stop(): Now calling p_SetStopStates()")
p_SetStopStates()
EndFunction
/* get additional anim file types supported by plugins */
Function p_GetPluginFiletypes()
Local p = GetPlugins()
freqmask$ = "anim|anim5|anim7|anim8|anim8w|anim8l|anim16|anim32|ham6|ham8"
; iterate over all plugins that we have
For Local k = 0 To ListItems(p) - 1
Local ft = p[k].Filetypes
; iterate over all file types that this plugin makes available
For Local i = 0 To ListItems(ft) - 1
; does this plugin support anims? Add its file type to our mask!
If (ft[i].Type = #FILETYPE_ANIM) And (Not (ft[i].Flags & #FILETYPEFLAGS_SAVE))
freqmask$ = freqmask$ .. "|" .. ft[i].extensions
EndIf
Next
Next
EndFunction
/*
** Adds a list of files in the Playlist listview
*/
Function p_InsertFilesInList(files)
If files[0] = "" Then Return
For Local c = 0 To ListItems(files)-1
If files[c]= "" Then Break
moai.DoMethod("lv", "insert", "bottom", 9, files[c])
Next
moai.Set("lv", "active", "Top")
EndFunction
/*
** Loads the selected Anim and displays the first frame
*/
Function p_LoadSelectedAnim()
Local f$ = p_GetVideoSelection()
Local ac = moai.Get("lv", "active")
Local num = moai.Get("lv", "entries")
; do not fail if unknown error format
ExitOnError(False)
If HaveObject(#ANIM, 1) = True
If #DEBUG Then DebugPrint("p_LoadSelectedAnim(): FreeAnim")
FinishAsyncDraw(playingAnim)
p_SetStopStates()
FreeAnim(1)
EndIf
If #DEBUG Then DebugPrint("p_LoadSelectedAnim(): Attempting to call LoadAnim()...")
LoadAnim(1, f$, {FromDisk = True})
Local err = GetLastError()
ExitOnError(True)
If err <> #ERR_NONE
moai.Request("AnimPlayer", GetErrorName(err), "OK")
Return
EndIf
; enable buttons that can be used now
moai.Set("last", "disabled", (ac = 0))
moai.Set("next", "disabled", (ac = num - 1))
; Remove the previous layer
RemoveLayer(1)
curvideo$ = f$
playingAnim = PlayAnim(1, 0, 0, {Speed = #DEFAULTSPEED, Times = 1, Async = True})
p_ResizeDisplay(animSize)
EndFunction
/*
** Draw next Anim frame
*/
Function p_DrawNextFrame()
If HaveObject(#ANIM, 1) = False
; Try to load the selected Anim if possible
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Anim object was not loaded, attempting to load...")
p_LoadSelectedAnim()
EndIf
If HaveObject(#ANIM, 1) = False
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Anim object was not found, giving up...")
If Not IsNil(animDrawInterval)
ClearInterval(animDrawInterval)
animDrawInterval = Nil
EndIf
Return
EndIf
If Not IsNil(playingAnim) And playing = True
If #DEBUG Then DebugPrint("p_DrawNextFrame(): playingAnim and Playing are valid:", playingAnim, playing)
; Draw the next Anim frame
done = AsyncDrawFrame(playingAnim)
EndIf
; If the animation is finished, go to the next in the playlist
If done = True
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Done was True, Anim finished")
musstate$ = "stopped"
playing = False
If Not IsNil(animDrawInterval)
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Clearing Interval before calling p_Next()")
ClearInterval(animDrawInterval)
animDrawInterval = Nil
EndIf
; If Loop Anims was selected, restart the same Anim instead of going to the Next one
If loopAnim = True
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Loop is enabled...")
p_Stop()
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Now calling p_LoadSelectedAnim()...")
p_LoadSelectedAnim()
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Now calling p_Play() on the loaded Anim...")
p_Play()
Else
If #DEBUG Then DebugPrint("p_DrawNextFrame(): Anim ended, no Loop is enabled, skipping to Next...")
p_Next()
EndIf
EndIf
EndFunction
/*
** Event handler for the GUI
*/
Function p_GUIEvent(id)
Switch id
Case "open":
Local files = FileRequest("Select Anim(s) to play", freqmask$, #REQ_MULTISELECT)
If files[0] <> ""
If p_GetVideoSelection() = curvideo$
If #DEBUG Then DebugPrint("Open: Current video is playing, will now attempt to Stop it. Calling p_Stop()...")
p_Stop()
EndIf
If #DEBUG Then DebugPrint("Open: Will now clear the playlist")
moai.DoMethod("lv", "Clear")
If #DEBUG Then DebugPrint("Open: Attempting to insert files in list")
p_InsertFilesInList(files)
If curvideo$ = "" Then p_LoadSelectedAnim()
EndIf
Case "save":
If curvideo$ <> ""
Local destination = FileRequest("Select destination filename", {Mode = #REQ_SAVEMODE})
If destination <> ""
SaveAnim(1, destination, #ANMFMT_GIF)
EndIf
EndIf
Case "add":
Local files = FileRequest("Select Anim(s) to play", freqmask$, #REQ_MULTISELECT)
If files[0] <> ""
p_InsertFilesInList(files)
If curvideo$ = "" Then p_LoadSelectedAnim()
EndIf
Case "mvup"
Local ac = moai.Get("lv", "active")
moai.DoMethod("lv", "move", "active", "previous")
moai.Set("lv", "active", ac - 1)
Case "mvdn"
Local ac = moai.Get("lv", "active")
moai.DoMethod("lv", "move", "active", "next")
moai.Set("lv", "active", ac + 1)
Case "play":
If curvideo$ = "" Then p_LoadSelectedAnim()
p_Play()
Case "stop":
p_Stop()
Case "last"
p_Last()
Case "next"
p_Next()
Case "rem":
If #DEBUG Then DebugPrint("Remove: Received Remove Event")
If p_GetVideoSelection() = curvideo$
If #DEBUG Then DebugPrint("Remove: Current video is playing, will now attempt to Stop it. Calling p_Stop()...")
p_Stop()
EndIf
If #DEBUG Then DebugPrint("Remove: Attempting to remove the active item from the Playlist...")
moai.DoMethod("lv", "remove", "active")
Case "about":
moai.Request("AnimPlayer", "AnimPlayer\nCopyright (C) 2016-2022 by Dimitris Panokostas\n", "OK")
Case "pl":
moai.Set("playlist", "open", moai.Get("mn_pl", "selected"))
Case "loop":
If #DEBUG Then DebugPrint("Clicked on Loop, current status: ", moai.Get("mn_loop", "selected"))
loopAnim = moai.Get("mn_loop", "selected")
Case "original":
animSize = "original"
p_ResizeDisplay(animSize)
Case "double":
animSize = "double"
p_ResizeDisplay(animSize)
Case "free":
animSize = "free"
p_ResizeDisplay(animSize)
Case "quit":
End
EndSwitch
EndFunction
/*
** Handles all incoming events
*/
Function p_EventFunc(msg)
Switch msg.action
Case "RapaGUI":
Switch msg.attribute
Case "DropFile":
If #DEBUG Then DebugPrint("Received DropFile Event")
If p_GetVideoSelection() = curvideo$
If #DEBUG Then DebugPrint("Open: Current video is playing, will now attempt to Stop it. Calling p_Stop()...")
p_Stop()
EndIf
If #DEBUG Then DebugPrint("Open: Will now clear the playlist")
moai.DoMethod("lv", "Clear")
p_InsertFilesInList(msg.triggervalue)
If curvideo$ = "" Then p_LoadSelectedAnim()
Case "CloseRequest":
If #DEBUG Then DebugPrint("Received CloseRequest Event")
moai.Set(msg.id, "open", False)
moai.Set("mn_pl", "selected", False)
Case "Pressed":
If #DEBUG Then DebugPrint("Received Pressed Event")
p_GUIEvent(msg.id)
Case "Selected":
If #DEBUG Then DebugPrint("Received Selected Event")
p_GUIEvent(UnrightStr(msg.id, 3))
Case "DoubleClick":
If #DEBUG Then DebugPrint("Received DoubleClick Event")
p_Stop()
p_LoadSelectedAnim()
p_Play()
Case "Active":
If #DEBUG Then DebugPrint("Received Active Event, now calling p_RefreshActive()")
p_RefreshActive()
Case "Release":
If #DEBUG Then DebugPrint("Received Release Event, now calling p_Stop()")
p_Stop()
EndSwitch
Case "SizeWindow":
If #DEBUG Then DebugPrint("Received SizeWindow Event")
If HaveObject(#ANIM, 1) = True
curwidth = msg.width
curheight = msg.height
moai.Set("mn_free", "Selected", True)
p_ResizeDisplay("free", curwidth, curheight)
EndIf
EndSwitch
EndFunction