-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sprite_Sheet_Animator.bb
525 lines (428 loc) · 13.5 KB
/
Sprite_Sheet_Animator.bb
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
;
; It starts, then select a image file with animation cells. It then shows a couple of windows to do the animation with.
;
;
; Show Animations Tool - Use the command line to load in a bitmap.
;
; By Nebula in 2003
;
;
.anim
Global animwin
Global animcan
.setupwindow
Global setupwin
Global selectwin
Global Selectcan
Dim filedimensions$(999,2)
mainprogram()
Function Mainprogram()
loadprefs()
If CommandLine$()<>"" Then az$ = Replace(CommandLine$(),Chr(34),"")
If CommandLine$() <> "" Then filename$ = az$
If FileType(filename$) <> 1 Then filename$ = ""
.retry
If Not filename$ <> "" Then filename$ = RequestFile("Select a image","*.*",0)
If FileType(filename$) <> 1 Then End
If validextension(getextension(filename$)) = False Then
If Confirm("This file might not be a valid picture file, continue?") = False Then
filename$ = ""
Goto retry
End If
End If
ww = 320
wh = 200
setupwin = CreateWindow("Animation setup - by R.v.Etten in 2003",GadgetWidth(Desktop())/2-ww/2,GadgetHeight(Desktop())/2-wh/2,ww,wh,Desktop(),1+8)
loadbutton = CreateButton("Load" ,0,wh/2-40,60,40,Setupwin)
animbutton = CreateButton("Animate" ,ww/2-80,wh/2-40,120,40,Setupwin)
reloadbutton = CreateButton("Reload" ,ww/2-80+120,wh/2-40,120,40,Setupwin)
labelanim = CreateLabel("Animation speed",ww/2-80+20,wh/2+20,80 ,20,setupwin)
animspeed = CreateTextField( ww/2+20 ,wh/2+20,40 ,20,setupwin)
SetGadgetText animspeed,"100"
a = 64
b = 10
Field1 = CreateTextField(0+a ,0+b ,64,20,setupwin)
Field2 = CreateTextField(64+a*2,0+b ,64,20,setupwin)
Updatebutton = CreateButton("Update",64+a*3,0+b ,40,40,setupwin)
Field3 = CreateTextField(0+a ,20+b,64,20,setupwin)
Field4 = CreateTextField(64+a*2,20+B,64,20,setupwin)
;
label1 = CreateLabel(" Width : ",0 ,0+b ,44,20,setupwin)
label2 = CreateLabel(" Height : ",0+a*2,0+b ,44,20,setupwin)
label3 = CreateLabel(" Top : ",0 ,20+b ,44,20,setupwin)
label4 = CreateLabel(" Left : ",0+a*2,20+b ,44,20,setupwin)
;
SetGadgetText field1,64
SetGadgetText field2,64
SetGadgetText field3,0
SetGadgetText field4,0
animmode1 = CreateButton("Loop" ,25+0,100,100,14,setupwin,3)
animmode2 = CreateButton("Loop back",25+100,100,100,14,setupwin,3)
animmode3 = CreateButton("No loop" ,25+200,100,100,14,setupwin,3)
SetButtonState(animmode1,1)
animimage = LoadImage(filename$,2)
iw = ImageWidth(animimage)
ih = ImageHeight(animimage)
Selectwin = CreateWindow("Select Animation cells - lmb + drag - rmb = back to setup",GadgetWidth(Desktop())/2-iw/2,GadgetHeight(Desktop())/2-ih/2,iw,ih,Desktop(),1)
Selectcan = CreateCanvas(0,0,iw,ih,Selectwin)
HideGadget Selectwin
;Notify GadgetHeight(Selectwin)
;Notify ClientHeight(Selectwin)
;Notify ImageHeight(animimage)
;End
cellwidth = Int(TextFieldText(Field1))
cellheight = Int(TextFieldText(Field1))
; Align the windows
If ww+iw > GadgetWidth(Desktop()) And wh+ih > GadgetHeight(Desktop()) Then
SetGadgetShape selectwin,0,0,iw,ih
Else
SetGadgetShape setupwin,0,GadgetHeight(Desktop())/2-wh/2,ww,wh
SetGadgetShape selectwin,ww,GadgetHeight(Desktop())/2-ih/2,iw,ih
End If
x1 = GadgetWidth(Selectwin) + GadgetWidth(Selectwin) - ClientWidth(Selectwin)
y1 = GadgetHeight(Selectwin) + GadgetHeight(Selectwin) - ClientHeight(Selectwin)
Repeat
If x1<iw Then x1 = x1 + 1 Else bone_a = True
If y1<ih Then y1 = y1 + 1 Else bone_b = True
SetGadgetShape Selectwin,GadgetX(Selectwin),GadgetY(Selectwin),x1,y1
Until bone_a=True And bone_b = True
; Get the width and height of the image in the cells
For i=0 To 999
If Trim(filedimensions$(i,0)) = filename$ Then
cellwidth = filedimensions(i,1)
cellheight = filedimensions(i,2)
SetGadgetText(Field1,cellwidth)
SetGadgetText(Field2,cellheight)
Exit
End If
Next
; Draw the grid
SetGadgetShape Selectcan,0,0,iw,ih
SetBuffer CanvasBuffer(Selectcan)
DrawImage animimage,0,0
drawgrid(iw,ih,cellwidth,cellheight,0,0,10,10)
ShowGadget Selectwin
ActivateWindow setupwin
While we <> $803
we = WaitEvent()
Select we
Case $401 ; Button down
If EventSource() = updatebutton Then
DrawImage animimage,0,0
drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
End If
If EventSource() = animbutton Then
; Save the file info
filerecog = False
For i=0 To 99
If filedimensions(i,0) = filename$
filedimensions(i,1) = cellwidth
filedimensions(i,2) = cellheight
filerecog = True
End If
Next
If filerecog=False Then
For i=0 To 99
If filedimensions(i,0) = "" Then
filedimensions(i,0) = filename$
filedimensions(i,1) = cellwidth
filedimensions(i,2) = cellheight
Exit
End If
Next
End If
saveprefs()
; Do the anim
cellwidth = Int(TextFieldText(Field1))
cellheight = Int(TextFieldText(Field2))
a_speed = Int(TextFieldText(animspeed))
If a_speed>1 And a_speed<5000 Then
If cposx>-1 And cposy>-1 And ceposx*cellwidth<iw And ceposy*cellheight<ih Then
a = -1
b = -1
For y=0 To ih/cellheight
For x=0 To (iw-cellwidth)/(cellwidth)
a = a + 1
b = b + 1
If (x= cposx And y = cposy) Then start_frame = a
If (x= ceposx And y = ceposy) Then End_frame = b
Next
Next
If start_frame > -1 And End_frame > start_frame Then
If ButtonState(animmode1) = 1 Then am = 1
If ButtonState(animmode2) = 1 Then am = 2
If ButtonState(animmode3) = 1 Then am = 3
;showanimation(filename$,cellwidth,cellheight,startframe,endframe,animspeed_ms)
showanimation(filename$,cellwidth,cellheight,start_frame,end_frame,a_speed,am)
SetBuffer CanvasBuffer(Selectcan)
Else : Notify "Error in start/end of frame (Drag the mouse across the bitmap viewer)" + start_frame + " : " + End_frame
End If ; start end frame
Else : Notify "error in map position"
End If ; map position
Else : Notify "error in anim speed"
End If ; speed
End If ; button
; Update image with new dimensions
If EventSource() = Field1 Or EventSource() = Field2 Then
cellwidth = Int(TextFieldText(Field1))
cellheight = Int(TextFieldText(Field2))
updateshit = True
End If
; reload the stuff
If EventSource() = reloadbutton Then
animimage = LoadImage(filename$,2)
iw = ImageWidth(animimage)
ih = ImageHeight(animimage)
SetBuffer CanvasBuffer(selectcan)
DrawImage animimage,0,0
drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
End If
; Load a new image
If EventSource() = loadbutton Then
oldfilename$ = filename$
filename$ = RequestFile("Select a image","*.*",0)
If FileType(filename$) <> 1 Then filename$ = oldfilename$
If validextension(getextension(filename$)) = False Then
If Confirm("This file might not be a valid picture file, continue?") = False Then
filename$ = oldfilename$
End If
End If
animimage = LoadImage(filename$,2)
; Get the width and height of the image in the cells
For i=0 To 999
If Trim(filedimensions$(i,0)) = filename$ Then
cellwidth = filedimensions(i,1)
cellheight = filedimensions(i,2)
SetGadgetText(Field1,cellwidth)
SetGadgetText(Field2,cellheight)
Exit
End If
Next
iw = ImageWidth(animimage)
ih = ImageHeight(animimage)
FreeGadget Selectwin
Selectwin = CreateWindow("Select Animation cells - lmb + drag - rmb = back to setup",GadgetWidth(Desktop())/2-iw/2,GadgetHeight(Desktop())/2-ih/2,iw,ih,Desktop(),1)
Selectcan = CreateCanvas(0,0,iw,ih,Selectwin)
SetBuffer CanvasBuffer(Selectcan)
ClsColor 0,0,0
Cls
DrawImage animimage,0,0
drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
End If
Case $203 ; mouse move
If EventSource() = selectcan And selectionstarted = True Then
ceposx = EventX()/cellwidth : ceposy = EventY()/cellheight
If EventX() > iw Then Selectionstarted = False : ceposx = ceposx -1
If EventY() > ih Then Selectionstarted = False : ceposy = ceposy - 1
If EventX() < 0 Then Selectionstarted = False
If EventY() < 0 Then Selectionstarted = False
SetStatusText setupwin,"Start : " + cposx + ","+cposy + " End : " + ceposx + "," + ceposy
ClsColor 0,0,0
Cls
DrawImage animimage,0,0
Color 220,0,0
drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
End If
Case $202; Mouse up
;
If EventData() = 2 Then ActivateWindow setupwin
;
If (EventSource() = selectcan And selectionstarted = True) And (EventData() <> 2)
Selectionstarted = False
ceposx = EventX()/cellwidth : ceposy = EventY()/cellheight
za$ = "Start : " + cposx + ","+cposy + " End : " + ceposx + "," + ceposy
x = 0 : y = 0 : counter = 0
sf = 0 : ef = 0 : crap = False
While crap=False
If (x/cellwidth = cposx) And (y/cellheight = cposy) Then sf = counter
If (x/cellwidth = ceposx) And (y/cellheight = ceposy) Then ef = counter
x = x + cellwidth
counter = counter + 1
If x > iw Then x = 0 : y=y+cellheight
If y > ih Then crap = True
Wend
za$ = za$ + " ; Frame : " + sf + " to " + ef
SetStatusText setupwin,za$
ClsColor 0,0,0
Cls
DrawImage animimage,0,0
Color 220,0,0
a = TextFieldText(Field1)
b = TextFieldText(Field2)
x = cposx*cellwidth
y = cposy*cellheight
start_frame = -1
End_frame = -1
drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
End If
Case $201 ; mouse down
If (EventSource() = selectcan) And (EventData() <> 2) Then
cellwidth = Int(TextFieldText(Field1))
cellheight = Int(TextFieldText(Field2))
Selectionstarted = True
cposx = EventX()/cellwidth : cposy = EventY()/cellheight
SetStatusText setupwin,"Start : " + cposx + ","+cposy + " End : " + ceposx + "," + ceposy
End If
End Select
Wend
End
End Function
Function showanimation(filename$,cellwidth,cellheight,startframe,endframe,animspeed_ms,animMode)
;animmode 0 = loop , 1 = loopback, 2 = stopafterplay
ww = 320
wh = 200
animwin = CreateWindow("Viewing Anim - lmb + drag = movement - rmb to exit",GadgetWidth(Desktop())/2-ww/2,GadgetHeight(Desktop())/2-wh/2,ww,wh,Desktop(),1)
animcan = CreateCanvas(0,0,ww,wh,animwin)
aba =LoadImage(filename$)
mw = ImageWidth(aba)
mh = ImageHeight(aba)
FreeImage aba
Stepanim = 1
x = mw/cellwidth
y = mh/cellheight
numcells = x*y
SetBuffer CanvasBuffer(animcan)
ClsColor 0,0,0
Cls
anim = LoadAnimImage(filename$,cellwidth,cellheight,0,numcells-1)
timer = CreateTimer(60)
HandleImage anim,cellwidth/2,cellheight
Cls
DrawImage anim,ww/2,wh/2,counter
counter = counter + 1
If counter > Endframe Then counter = startframe
FlipCanvas(animcan)
counter = startframe
Exitthis = False
ResetTimer(timer)
brrx = 0
brry = 0
animcounter = MilliSecs() + animspeed_ms + 300
DrawImage anim,(ww/2),(wh/2),counter
Repeat
we = WaitEvent()
Select we
Case $803
If EventSource() = animwin Then Exit
Case $4001
;If TimerTicks(timer) > animspeed_ms Then
Cls
If domoveanim = False Then
DrawImage anim,(ww/2),(wh/2),counter
Else
DrawImage anim,brrx,brry,counter
End If
If animcounter < MilliSecs() Then
counter = counter + Stepanim
animcounter = MilliSecs() + animspeed_ms
End If
If counter > Endframe Then
If animmode = 1 Then counter = startframe
If animmode = 2 Then Stepanim = -1 : counter = Endframe -1
If animmode = 3 Then Stepanim = 0 : counter = Endframe
End If
If counter < startframe Then
If animmode = 1 Then counter = startframe
If animmode = 2 Then Stepanim = 1 : counter = startframe + 1
If animmode = 3 Then Stepanim = 0 : counter = Endframe
End If
FlipCanvas(animcan)
;End If
Case $201 ; mousedown
If EventData() = 1 Then
domoveanim = True
brrx = EventX()
brry = EventY()
HandleImage anim,0,0
End If
If EventData() = 2 Then Exitthis = True
Case $202 ; mouseup
domoveanim = False
brrx = 0
brry = 0
HandleImage anim,cellwidth/2,cellheight
Case $203 ; mousemove
If domoveanim = True Then
brrx = EventX()
brry = EventY()
End If
Case $4001
End Select
Until Exitthis = True Or KeyDown(1) = True
FreeGadget animwin
FreeImage anim
End Function
Function drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
a = cellwidth
b = cellheight
x = cposx*cellwidth
y = cposy*cellheight
counter = 1
SetBuffer CanvasBuffer(Selectcan)
Repeat
If a>-1 And a<129 And b>-1 And b<129
If x = cposx*cellwidth And y=cposy*cellheight Then Text x+cellwidth/2,y+cellheight/2,"Start",1,1
If x = ceposx*cellwidth And y=ceposy*cellheight Then Text x+cellwidth/2,y+cellheight/2,"End",1,1
Rect x+3,y+3,cellwidth-6,cellheight-6,0
Rect x+4,y+4,cellwdith-8,cellheight-8,0
End If
x = x + cellwidth
If (x => ceposx*cellwidth+1) And (y => ceposy*cellheight) Then Exit
If x>iw Then x = 0 : y=y+cellheight
Until y>ih
Color 0,0,0
x = 0 : y = 0
Repeat
If a>-1 And a<129 And b>-1 And b<129
Rect x,y,cellwidth,cellheight,0
End If
x = x + cellwidth
If x>iw Then x = 0 : y=y+cellheight
Until y>ih
FlipCanvas(Selectcan)
End Function
Function saveprefs(); Save the last used filenames and cell dimensions
;
If FileType(CurrentDir()+"Animviewcfg") = 0 Then CreateDir("Animviewcfg")
;
f = WriteFile(CurrentDir()+"Animviewcfg\fileinfo.txt")
For i=0 To 999
If filedimensions$(i,0) <> "" Then
WriteLine(f,filedimensions$(i,0))
WriteLine(f,filedimensions$(i,1))
WriteLine(f,filedimensions$(i,2))
End If
Next
End Function
Function loadprefs() ; Load the history of used filenames and cell dimensions
If FileType(CurrentDir()+"animviewcfg\fileinfo.txt") = 0 Then Return
;
f = ReadFile(CurrentDir()+"animviewcfg\fileinfo.txt")
counter = 0
While Eof(f)=False
For ii=0 To 2
a$ = ReadLine(f)
If a$ = "" Then Exit
filedimensions(counter,ii) = a$
Next
counter = counter + 1
Wend
CloseFile(f)
End Function
Function getextension$(filename$) ; Returns the extension minus the .
lastdir = 1
For i=1 To Len(filename$)
If Mid$(filename$,i,1) = "." Then Lastdir = i
Next
If Lastdir > 1 Then Lastdir = Lastdir + 1
For i=Lastdir To Len(filename$)
a$ = a$ + Mid(filename$,i,1)
Next
Return a$
End Function
Function validextension(ext$)
If ext$ = "bmp" Then Return True
If ext$ = "png" Then Return True
If ext$ = "jpg" Then Return True
Return False
End Function