November 30, 2020, 01:57:08 AM

Author Topic: [bb] Animation tool (b+) by Nebula [ 1+ years ago ]  (Read 553 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Animation tool (b+) by Nebula [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Animation tool (b+)
Author : Nebula
Posted : 1+ years ago

Description : ; Show Animations Tool - Use the command line to load in a bitmap.

Animate spites ; Load bitmaps and set rectangles; rewind animation ; loop animation ; animate once. ; animation speed; sizes;


Code :
Code: BlitzBasic
  1. ;
  2. ; Show Animations Tool - Use the command line to load in a bitmap.
  3. ;
  4. ; By Nebula in 2003
  5. ;
  6. ;
  7.  
  8. .anim
  9. Global animwin
  10. Global animcan
  11.  
  12. .setupwindow
  13. Global setupwin
  14. Global selectwin
  15. Global Selectcan
  16.  
  17. Dim filedimensions$(999,2)
  18.  
  19. mainprogram()
  20.  
  21. Function Mainprogram()
  22.  
  23. loadprefs()
  24.  
  25. If CommandLine$()<>"" Then az$ = Replace(CommandLine$(),Chr(34),"")
  26.  
  27. If CommandLine$() <> "" Then filename$ = az$
  28. If FileType(filename$) <> 1 Then filename$ = ""
  29. .retry
  30. If Not filename$ <> "" Then filename$ = RequestFile("Select a image","*.*",0)
  31. If FileType(filename$) <> 1 Then End
  32.  
  33. If validextension(getextension(filename$)) = False Then
  34.         If Confirm("This file might not be a valid picture file, continue?") = False Then
  35.         filename$ = ""
  36.         Goto retry
  37.         End If
  38. End If
  39.  
  40.  
  41.  
  42.  
  43. ww = 320
  44. wh = 200
  45.  
  46. 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)
  47.  
  48. loadbutton = CreateButton("Load"      ,0,wh/2-40,60,40,Setupwin)
  49. animbutton = CreateButton("Animate"      ,ww/2-80,wh/2-40,120,40,Setupwin)
  50. reloadbutton = CreateButton("Reload"      ,ww/2-80+120,wh/2-40,120,40,Setupwin)
  51. labelanim = CreateLabel("Animation speed",ww/2-80+20,wh/2+20,80 ,20,setupwin)
  52. animspeed = CreateTextField(              ww/2+20   ,wh/2+20,40 ,20,setupwin)
  53. SetGadgetText animspeed,"100"
  54.  
  55.  
  56. a = 64
  57. b = 10
  58. Field1 =  CreateTextField(0+a   ,0+b ,64,20,setupwin)
  59. Field2 = CreateTextField(64+a*2,0+b ,64,20,setupwin)
  60. Updatebutton = CreateButton("Update",64+a*3,0+b ,40,40,setupwin)
  61. Field3 =    CreateTextField(0+a   ,20+b,64,20,setupwin)
  62. Field4 =  CreateTextField(64+a*2,20+B,64,20,setupwin)
  63. ;
  64. label1 = CreateLabel("    Width  : ",0    ,0+b  ,44,20,setupwin)
  65. label2 = CreateLabel("    Height : ",0+a*2,0+b  ,44,20,setupwin)
  66. label3 = CreateLabel("    Top    : ",0    ,20+b ,44,20,setupwin)
  67. label4 = CreateLabel("    Left   : ",0+a*2,20+b ,44,20,setupwin)
  68. ;
  69. SetGadgetText field1,64
  70. SetGadgetText field2,64
  71. SetGadgetText field3,0
  72. SetGadgetText field4,0
  73.  
  74. animmode1 =     CreateButton("Loop"     ,25+0,100,100,14,setupwin,3)
  75. animmode2 = CreateButton("Loop back",25+100,100,100,14,setupwin,3)
  76. animmode3 =     CreateButton("No loop"  ,25+200,100,100,14,setupwin,3)
  77. SetButtonState(animmode1,1)
  78.  
  79. animimage = LoadImage(filename$,2)
  80. iw = ImageWidth(animimage)
  81. ih = ImageHeight(animimage)
  82. 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)
  83. Selectcan = CreateCanvas(0,0,iw,ih,Selectwin)
  84. HideGadget Selectwin
  85.  
  86. ;Notify GadgetHeight(Selectwin)
  87. ;Notify ClientHeight(Selectwin)
  88. ;Notify ImageHeight(animimage)
  89. ;End
  90.  
  91.  
  92. cellwidth = Int(TextFieldText(Field1))
  93. cellheight = Int(TextFieldText(Field1))
  94.  
  95.  
  96.  
  97. ; Align the windows
  98. If ww+iw > GadgetWidth(Desktop()) And wh+ih > GadgetHeight(Desktop()) Then
  99.         SetGadgetShape selectwin,0,0,iw,ih
  100.         Else
  101.         SetGadgetShape setupwin,0,GadgetHeight(Desktop())/2-wh/2,ww,wh
  102.         SetGadgetShape selectwin,ww,GadgetHeight(Desktop())/2-ih/2,iw,ih
  103. End If
  104.  
  105.  
  106. x1 = GadgetWidth(Selectwin) + GadgetWidth(Selectwin) - ClientWidth(Selectwin)
  107. y1 = GadgetHeight(Selectwin) + GadgetHeight(Selectwin) - ClientHeight(Selectwin)
  108.  
  109. Repeat
  110. If x1<iw Then x1 = x1 + 1 Else bone_a = True
  111. If y1<ih Then y1 = y1 + 1 Else bone_b = True
  112. SetGadgetShape Selectwin,GadgetX(Selectwin),GadgetY(Selectwin),x1,y1
  113. Until bone_a=True And bone_b = True
  114.  
  115.  
  116.  
  117. ; Get the width and height of the image in the cells
  118. For i=0 To 999
  119.         If Trim(filedimensions$(i,0)) = filename$ Then
  120.                 cellwidth = filedimensions(i,1)
  121.                 cellheight = filedimensions(i,2)
  122.                 SetGadgetText(Field1,cellwidth)
  123.                 SetGadgetText(Field2,cellheight)
  124.                 Exit
  125.         End If
  126. Next
  127.  
  128. ; Draw the grid
  129. SetGadgetShape Selectcan,0,0,iw,ih
  130. SetBuffer CanvasBuffer(Selectcan)
  131. DrawImage animimage,0,0
  132. drawgrid(iw,ih,cellwidth,cellheight,0,0,10,10)
  133.  
  134. ShowGadget Selectwin
  135. ActivateWindow setupwin
  136.  
  137. While we <> $803
  138. we = WaitEvent()
  139. Select we
  140.  
  141. Case $401 ; Button down
  142.  
  143. If EventSource() = updatebutton Then
  144. DrawImage animimage,0,0
  145. drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  146. End If
  147.  
  148. If EventSource() = animbutton Then
  149.  
  150. ; Save the file info
  151. filerecog = False
  152. For i=0 To 99
  153.         If filedimensions(i,0) = filename$
  154.         filedimensions(i,1) = cellwidth
  155.         filedimensions(i,2) = cellheight
  156.         filerecog = True
  157. End If
  158. Next
  159. If filerecog=False Then
  160.         For i=0 To 99
  161.                 If filedimensions(i,0) = "" Then
  162.                         filedimensions(i,0) = filename$
  163.                         filedimensions(i,1) = cellwidth
  164.                         filedimensions(i,2) = cellheight
  165.                         Exit
  166.                 End If
  167.         Next
  168. End If
  169.  
  170. saveprefs()
  171.  
  172. ; Do the anim
  173. cellwidth = Int(TextFieldText(Field1))
  174. cellheight = Int(TextFieldText(Field2))
  175. a_speed = Int(TextFieldText(animspeed))
  176. If a_speed>1 And a_speed<5000 Then
  177. If cposx>-1 And cposy>-1 And ceposx*cellwidth<iw And ceposy*cellheight<ih Then
  178. a = -1
  179. b = -1
  180. For y=0 To ih/cellheight
  181. For x=0 To (iw-cellwidth)/(cellwidth)
  182. a = a + 1
  183. b = b + 1
  184. If (x= cposx And y = cposy) Then start_frame = a
  185. If (x= ceposx And y = ceposy) Then End_frame = b
  186. Next
  187. Next
  188.  
  189. If start_frame > -1 And End_frame > start_frame Then
  190. If ButtonState(animmode1) = 1 Then am = 1
  191. If ButtonState(animmode2) = 1 Then am = 2
  192. If ButtonState(animmode3) = 1 Then am = 3
  193. ;showanimation(filename$,cellwidth,cellheight,startframe,endframe,animspeed_ms)
  194. showanimation(filename$,cellwidth,cellheight,start_frame,end_frame,a_speed,am)
  195. SetBuffer CanvasBuffer(Selectcan)
  196.  
  197. Else : Notify "Error in start/end of frame (Drag the mouse across the bitmap viewer)" + start_frame + " : " + End_frame
  198. End If ; start end frame
  199. Else : Notify "error in map position"
  200. End If ; map position
  201. Else : Notify "error in anim speed"
  202. End If ; speed
  203. End If ; button
  204.  
  205. ; Update image with new dimensions
  206. If EventSource() = Field1 Or EventSource() = Field2 Then
  207.         cellwidth = Int(TextFieldText(Field1))
  208.         cellheight = Int(TextFieldText(Field2))
  209.         updateshit = True
  210. End If
  211.  
  212. ; reload the stuff
  213. If EventSource() = reloadbutton Then
  214.         animimage = LoadImage(filename$,2)
  215.         iw = ImageWidth(animimage)
  216.         ih = ImageHeight(animimage)
  217.         SetBuffer CanvasBuffer(selectcan)
  218.         DrawImage animimage,0,0
  219.         drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  220. End If
  221.  
  222. ; Load a new image
  223. If EventSource() = loadbutton Then     
  224.         oldfilename$ = filename$
  225.         filename$ = RequestFile("Select a image","*.*",0)
  226.         If FileType(filename$) <> 1 Then filename$ = oldfilename$
  227.         If validextension(getextension(filename$)) = False Then
  228.                 If Confirm("This file might not be a valid picture file, continue?") = False Then
  229.                         filename$ = oldfilename$
  230.                 End If
  231.         End If
  232.         animimage = LoadImage(filename$,2)
  233.         ; Get the width and height of the image in the cells
  234.         For i=0 To 999
  235.                 If Trim(filedimensions$(i,0)) = filename$ Then
  236.                         cellwidth = filedimensions(i,1)
  237.                         cellheight = filedimensions(i,2)
  238.                         SetGadgetText(Field1,cellwidth)
  239.                         SetGadgetText(Field2,cellheight)
  240.                         Exit
  241.                 End If
  242.         Next
  243.         iw = ImageWidth(animimage)
  244.         ih = ImageHeight(animimage)
  245.         FreeGadget Selectwin
  246.         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)
  247.         Selectcan = CreateCanvas(0,0,iw,ih,Selectwin)
  248.         SetBuffer CanvasBuffer(Selectcan)
  249.         ClsColor 0,0,0
  250.         Cls
  251.         DrawImage animimage,0,0
  252.         drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  253. End If
  254.  
  255. Case $203 ; mouse move
  256.  
  257. If EventSource() = selectcan And selectionstarted = True Then
  258.  
  259.        
  260.         ceposx = EventX()/cellwidth : ceposy = EventY()/cellheight
  261.  
  262.         If EventX() > iw Then Selectionstarted = False : ceposx = ceposx -1
  263.         If EventY() > ih Then Selectionstarted = False : ceposy = ceposy - 1
  264.         If EventX() < 0 Then Selectionstarted = False
  265.         If EventY() < 0 Then Selectionstarted = False
  266.  
  267.        
  268.         SetStatusText setupwin,"Start : " + cposx + ","+cposy + "  End : " + ceposx + "," + ceposy
  269.         ClsColor 0,0,0
  270.         Cls
  271.         DrawImage animimage,0,0
  272.         Color 220,0,0  
  273.         drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  274. End If
  275.  
  276. Case $202;  Mouse up
  277. ;
  278. If EventData() = 2 Then ActivateWindow setupwin
  279. ;
  280. If (EventSource() = selectcan And selectionstarted = True) And (EventData() <> 2)
  281.         Selectionstarted = False
  282.         ceposx = EventX()/cellwidth : ceposy = EventY()/cellheight
  283.                  za$ = "Start : " + cposx + ","+cposy + "  End : " + ceposx + "," + ceposy
  284.  
  285.  
  286.                
  287.                 x = 0 : y = 0 : counter = 0
  288.                 sf = 0 : ef = 0 : crap = False
  289.                 While crap=False                               
  290.                 If (x/cellwidth = cposx) And (y/cellheight = cposy) Then sf = counter
  291.                 If (x/cellwidth = ceposx) And (y/cellheight = ceposy) Then ef = counter
  292.                 x = x + cellwidth
  293.                 counter = counter + 1
  294.                 If x > iw Then x = 0 : y=y+cellheight
  295.                 If y > ih Then crap = True             
  296.                 Wend
  297.                  
  298.         za$ = za$ + " ; Frame : " + sf + " to " + ef
  299.         SetStatusText setupwin,za$
  300.         ClsColor 0,0,0
  301.         Cls
  302.         DrawImage animimage,0,0
  303.         Color 220,0,0
  304.         a = TextFieldText(Field1)
  305.         b = TextFieldText(Field2)
  306.         x = cposx*cellwidth
  307.         y = cposy*cellheight
  308.         start_frame = -1
  309.         End_frame = -1
  310.         drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  311. End If
  312.  
  313. Case $201 ; mouse down
  314. If (EventSource() = selectcan) And (EventData() <> 2) Then
  315.         cellwidth = Int(TextFieldText(Field1))
  316.         cellheight = Int(TextFieldText(Field2))
  317.         Selectionstarted = True
  318.         cposx = EventX()/cellwidth : cposy = EventY()/cellheight
  319.         SetStatusText setupwin,"Start : " + cposx + ","+cposy + "  End : " + ceposx + "," + ceposy
  320. End If
  321.  
  322.  
  323. End Select
  324. Wend
  325.  
  326.  
  327. End
  328.  
  329. End Function
  330.  
  331. Function showanimation(filename$,cellwidth,cellheight,startframe,endframe,animspeed_ms,animMode)
  332. ;animmode 0 = loop , 1 = loopback, 2 = stopafterplay
  333. ww = 320
  334. wh = 200
  335.  
  336.  
  337. animwin = CreateWindow("Viewing Anim - lmb + drag = movement - rmb to exit",GadgetWidth(Desktop())/2-ww/2,GadgetHeight(Desktop())/2-wh/2,ww,wh,Desktop(),1)
  338.  
  339. animcan = CreateCanvas(0,0,ww,wh,animwin)
  340.  
  341. aba =LoadImage(filename$)
  342. mw = ImageWidth(aba)
  343. mh = ImageHeight(aba)
  344. FreeImage aba
  345.  
  346. Stepanim = 1
  347.  
  348. x = mw/cellwidth
  349. y = mh/cellheight
  350. numcells = x*y
  351.  
  352. SetBuffer CanvasBuffer(animcan)
  353. ClsColor 0,0,0
  354. Cls
  355.  
  356. anim = LoadAnimImage(filename$,cellwidth,cellheight,0,numcells-1)
  357.  
  358. timer = CreateTimer(60)
  359.  
  360. HandleImage anim,cellwidth/2,cellheight
  361.  
  362. Cls
  363. DrawImage anim,ww/2,wh/2,counter
  364. counter = counter + 1
  365. If counter > Endframe Then counter = startframe
  366. FlipCanvas(animcan)
  367.  
  368. counter = startframe
  369.  
  370. Exitthis = False
  371. ResetTimer(timer)
  372.  
  373. brrx = 0
  374. brry = 0
  375. animcounter = MilliSecs() + animspeed_ms + 300
  376. DrawImage anim,(ww/2),(wh/2),counter
  377. Repeat
  378.         we = WaitEvent()
  379.         Select we
  380.                 Case $803
  381.                 If EventSource() = animwin Then Exit
  382.                 Case $4001
  383.                 ;If TimerTicks(timer) > animspeed_ms Then
  384.                 Cls
  385.                 If domoveanim =  False Then            
  386.                         DrawImage anim,(ww/2),(wh/2),counter
  387.                         Else                   
  388.                         DrawImage anim,brrx,brry,counter
  389.                 End If
  390.  
  391.                 If animcounter < MilliSecs() Then              
  392.                 counter = counter + Stepanim
  393.                 animcounter = MilliSecs() + animspeed_ms
  394.                 End If
  395.                
  396.                 If counter > Endframe Then
  397.                         If animmode = 1 Then counter = startframe
  398.                         If animmode = 2 Then Stepanim = -1 : counter = Endframe -1
  399.                         If animmode = 3 Then Stepanim = 0 : counter = Endframe
  400.                 End If
  401.                 If counter < startframe Then
  402.                         If animmode = 1 Then counter = startframe
  403.                         If animmode = 2 Then Stepanim = 1 : counter = startframe + 1
  404.                         If animmode = 3 Then Stepanim = 0 : counter = Endframe
  405.                 End If
  406.                 FlipCanvas(animcan)            
  407.                 ;End If
  408.                 Case $201 ; mousedown
  409.                 If EventData() = 1 Then
  410.                         domoveanim = True
  411.                         brrx = EventX()
  412.                         brry = EventY()
  413.                         HandleImage anim,0,0
  414.                 End If
  415.                 If EventData() = 2 Then Exitthis = True
  416.                 Case $202 ; mouseup
  417.                 domoveanim = False
  418.                 brrx = 0
  419.                 brry = 0
  420.                 HandleImage anim,cellwidth/2,cellheight
  421.                 Case $203 ; mousemove
  422.                 If domoveanim = True Then
  423.                 brrx = EventX()
  424.                 brry = EventY()        
  425.                 End If
  426.                
  427.                 Case $4001
  428.         End Select
  429.        
  430. Until Exitthis = True Or KeyDown(1) = True
  431.  
  432. FreeGadget animwin
  433. FreeImage anim
  434.  
  435. End Function
  436.  
  437. Function drawgrid(iw,ih,cellwidth,cellheight,cposx,cposy,ceposx,ceposy)
  438.         a = cellwidth
  439.         b = cellheight
  440.         x = cposx*cellwidth
  441.         y = cposy*cellheight
  442.         counter = 1
  443.         SetBuffer CanvasBuffer(Selectcan)
  444.         Repeat
  445.                 If a>-1 And a<129 And b>-1 And b<129
  446.                         If x = cposx*cellwidth And y=cposy*cellheight Then Text x+cellwidth/2,y+cellheight/2,"Start",1,1
  447.                         If x = ceposx*cellwidth And y=ceposy*cellheight Then Text x+cellwidth/2,y+cellheight/2,"End",1,1
  448.                         Rect x+3,y+3,cellwidth-6,cellheight-6,0
  449.                         Rect x+4,y+4,cellwdith-8,cellheight-8,0
  450.                 End If
  451.                 x = x + cellwidth
  452.                 If (x => ceposx*cellwidth+1) And (y => ceposy*cellheight) Then Exit
  453.                 If x>iw Then x = 0 : y=y+cellheight
  454.         Until y>ih
  455.         Color 0,0,0
  456.         x = 0 : y = 0
  457.         Repeat
  458.                 If a>-1 And a<129 And b>-1 And b<129
  459.                         Rect x,y,cellwidth,cellheight,0
  460.                 End If
  461.                 x = x + cellwidth
  462.                 If x>iw Then x = 0 : y=y+cellheight
  463.         Until y>ih
  464.         FlipCanvas(Selectcan)
  465. End Function
  466.  
  467. Function saveprefs(); Save the last used filenames and cell dimensions
  468. ;
  469. If FileType(CurrentDir()+"Animviewcfg") = 0 Then CreateDir("Animviewcfg")
  470. ;
  471. f = WriteFile(CurrentDir()+"Animviewcfgfileinfo.txt")
  472. For i=0 To 999
  473.         If filedimensions$(i,0) <> "" Then
  474.                 WriteLine(f,filedimensions$(i,0))
  475.                 WriteLine(f,filedimensions$(i,1))
  476.                 WriteLine(f,filedimensions$(i,2))
  477.         End If
  478. Next
  479. End Function
  480. Function loadprefs() ; Load the history of used filenames and cell dimensions
  481.  
  482. If FileType(CurrentDir()+"animviewcfgfileinfo.txt") = 0 Then Return
  483. ;
  484. f = ReadFile(CurrentDir()+"animviewcfgfileinfo.txt")
  485. counter = 0
  486. While Eof(f)=False
  487.         For ii=0 To 2
  488.                 a$ = ReadLine(f)
  489.                 If a$ = "" Then Exit
  490.                 filedimensions(counter,ii) = a$
  491.         Next
  492.         counter = counter + 1
  493. Wend
  494. CloseFile(f)
  495. End Function
  496.  
  497. Function getextension$(filename$) ; Returns the extension minus the .
  498.         lastdir = 1
  499.         For i=1 To Len(filename$)
  500.                 If Mid$(filename$,i,1) = "." Then Lastdir = i
  501.         Next
  502.         If Lastdir > 1 Then Lastdir = Lastdir + 1
  503.         For i=Lastdir To Len(filename$)
  504.                 a$ = a$ + Mid(filename$,i,1)
  505.         Next
  506.         Return a$
  507. End Function
  508.  
  509. Function validextension(ext$)
  510.         If ext$ = "bmp" Then Return True
  511.         If ext$ = "png" Then Return True
  512.         If ext$ = "jpg" Then Return True
  513.         Return False
  514. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal