home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Imaging / Create Image Icon < prev    next >
Encoding:
Text File  |  1999-03-04  |  4.3 KB  |  204 lines  |  [TEXT/ToyS]

  1. -- Properties
  2. property kasPrefName : "Image Icons V1.0"
  3.  
  4. property kasImgBox : {0, 0, 128, 128} -- Box of image
  5.  
  6. -- Globals
  7. global gasInfoWind -- Info window
  8. global gasInfoPos -- Position of info window
  9. global gasDrawing -- Draw window
  10. global gasDrawPos
  11.  
  12. global gasFoldersToDo -- The folders left to process
  13. global gasShown -- Number gone!
  14. global gasChecked -- Number checked!
  15.  
  16.  
  17. on open fsObjs
  18.     -- Load prefs, show window
  19.     pfLoad()
  20.     
  21.     -- Set up prefix
  22.     set gasShown to 0
  23.     set gasChecked to 0
  24.     set gasShowWind to 0
  25.     
  26.     set gasInfoWind to display info titled kasPrefName ¬
  27.         located at gasInfoPos ¬
  28.         message "Scanning…"
  29.     
  30.     set gasDrawing to ¬
  31.         display drawing titled ("Icon Creation") ¬
  32.             located at gasDrawPos ¬
  33.             with dimensions {128, 128}
  34.     
  35.     -- Do files
  36.     set gasFoldersToDo to {}
  37.     
  38.     repeat with fsObj in fsObjs
  39.         set myInfo to (basic info for fsObj)
  40.         
  41.         if (system type of myInfo is "fold") then
  42.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  43.         else
  44.             DoOne(fsObj)
  45.         end if
  46.     end repeat
  47.     
  48.     -- Do folders
  49.     repeat while gasFoldersToDo is not {}
  50.         -- Pop one off the end
  51.         set n to the number of items of gasFoldersToDo
  52.         set fsObj to item n of gasFoldersToDo
  53.         
  54.         if (n > 1) then
  55.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  56.         else
  57.             set gasFoldersToDo to {}
  58.         end if
  59.         
  60.         display info gasInfoWind ¬
  61.             message ("Folders to go: " & n) ¬
  62.             at line 6 ¬
  63.             using color (15 * 32)
  64.         
  65.         -- Process it
  66.         GoDeep(fsObj)
  67.     end repeat
  68.     
  69.     display info gasInfoWind message "DONE!"
  70.     
  71.     pause for 2 with seconds timing -- Let screen wait...
  72.     
  73.     beep
  74.     
  75.     set gasInfoPos to screen location of ¬
  76.         (display info gasInfoWind with disposal)
  77.     
  78.     beep
  79.     
  80.     set gasDrawPos to screen location of ¬
  81.         (display info gasDrawing with disposal)
  82.     
  83.     beep
  84.     
  85.     pfSave() -- Save window location
  86. end open
  87.  
  88.  
  89. on DoOne(fsObj)
  90.     set imageType to the image type in fsObj
  91.     set fname to (catalog name of (basic info for fsObj))
  92.     
  93.     display info gasInfoWind ¬
  94.         message fname ¬
  95.         at line 2
  96.     
  97.     if (imageType is not "") then
  98.         display info gasInfoWind ¬
  99.             message imageType ¬
  100.             at line 3
  101.         -- Get picture
  102.         set imagePic to the image from fsObj
  103.         -- Get its bounds
  104.         set pBox to picture bounds of (the picture info for imagePic)
  105.         -- Center it
  106.         set drawInto to PlaceInCenter(pBox, kasImgBox)
  107.         -- Draw image
  108.         draw a picture into gasDrawing ¬
  109.             inside of drawInto ¬
  110.             using data imagePic
  111.         -- Free image
  112.         set imagePic to ¬
  113.             capture picture from gasDrawing ¬
  114.                 using depth 16 with pixel conversion
  115.         -- Redraw - some images don't draw correctly (QT Problem)
  116.         draw a picture into gasDrawing ¬
  117.             inside of drawInto ¬
  118.             using data imagePic
  119.         -- Get Picture / Assign Icon
  120.         set the icon of fsObj to imagePic
  121.     else
  122.         display info gasInfoWind ¬
  123.             message ¬
  124.             "Unknown" at line 3
  125.     end if
  126. end DoOne
  127.  
  128.  
  129. on GoDeep(foldObj)
  130.     display info gasInfoWind ¬
  131.         message "Path: " & (foldObj as string)
  132.     
  133.     set daddy to foldObj as string
  134.     
  135.     -- Do kinds that match
  136.     display info gasInfoWind ¬
  137.         message "Scanning files" at line 5
  138.     
  139.     set myItems to the entries in foldObj ¬
  140.         whose kinds are a file
  141.     
  142.     repeat with myItem in myItems
  143.         DoOne((daddy & myItem) as alias)
  144.     end repeat
  145.     
  146.     -- Do folders
  147.     display info gasInfoWind ¬
  148.         message "Scanning subfolders" at line 5
  149.     
  150.     set myItems to the entries in foldObj ¬
  151.         whose kinds are a folder
  152.     
  153.     repeat with myItem in myItems
  154.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  155.     end repeat
  156.     
  157.     -- Done
  158.     display info gasInfoWind ¬
  159.         message "…" at line 5
  160. end GoDeep
  161.  
  162.  
  163. on PlaceInCenter(src, dst)
  164.     set sW to (item 3 of src) - (item 1 of src)
  165.     set sH to (item 4 of src) - (item 2 of src)
  166.     set dW to (item 3 of dst) - (item 1 of dst)
  167.     set dH to (item 4 of dst) - (item 2 of dst)
  168.     
  169.     set r to dW / sW
  170.     set rH to dH / sH -- ratios
  171.     
  172.     if (rH < r) then set r to rH
  173.     
  174.     -- Round to a .25 ratio if > 1
  175.     if (r > 1) then ¬
  176.         set r to 0.25 * (round (r * 4))
  177.     
  178.     -- Scale the src
  179.     set sW to round (sW * r)
  180.     set sH to round (sH * r)
  181.     set mW to round ((dW - sW) / 2) + (item 1 of dst)
  182.     set mH to round ((dH - sH) / 2) + (item 2 of dst)
  183.     
  184.     -- Center it
  185.     return {mW, mH, mW + sW, mH + sH}
  186. end PlaceInCenter
  187.  
  188.  
  189. on pfLoad()
  190.     try
  191.         set ourPrefs to (load preference named kasPrefName)
  192.     on error
  193.         set ourPrefs to {{8, 48}, {8, 112}}
  194.     end try
  195.     
  196.     set gasInfoPos to item 1 of ourPrefs
  197.     set gasDrawPos to item 2 of ourPrefs
  198. end pfLoad
  199.  
  200.  
  201. on pfSave()
  202.     save preference {gasInfoPos, gasDrawPos} named kasPrefName
  203. end pfSave
  204.