Advertisement
CaptainSpaceCat

Image File Viewer

Jun 1st, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.44 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs < 1 then
  3.   error("Usage: view <image filename>")
  4. end
  5. if not fs.exists(tArgs[1]) then
  6.   error("Not an image file")
  7. end
  8. image = paintutils.loadImage(tArgs[1])
  9. local w, h = term.getSize()
  10. local imgW = 0
  11. local imgH = 0
  12. local imgWMax = 0
  13. for i, v in pairs(image) do
  14.   imgH = imgH + 1
  15.   imgW = 0
  16.   for k, e in pairs(v) do
  17.     imgW = imgW + 1
  18.     if imgW > imgWMax then
  19.       imgWMax = imgW
  20.     end
  21.   end
  22. end
  23. imgW = imgWMax
  24. drawX = w / 2 - imgW / 2 + 1
  25. drawY = h / 2 - imgH / 2 + 1
  26. local begin = true
  27. while true do
  28.   term.setBackgroundColor(colors.black)
  29.   term.setTextColor(colors.white)
  30.   term.clear()
  31.   paintutils.drawImage(image, drawX, drawY)
  32.   if begin then
  33.     term.setBackgroundColor(colors.black)
  34.     term.setTextColor(colors.white)
  35.     term.setCursorPos(1, h)
  36.     term.write("Arrow keys to move, Ctrl to quit")
  37.   end
  38.   events = {os.pullEvent()}
  39.   if events[1] == "key" then
  40.     if events[2] == 200 then
  41.       drawY = drawY + 1
  42.       begin = false
  43.     elseif events[2] == 205 then
  44.       drawX = drawX - 1
  45.       begin = false
  46.     elseif events[2] == 208 then
  47.       drawY = drawY - 1
  48.       begin = false
  49.     elseif events[2] == 203 then
  50.       drawX = drawX + 1
  51.       begin = false
  52.     elseif events[2] == 29 then
  53.       term.setBackgroundColor(colors.black)
  54.       term.setTextColor(colors.white)
  55.       term.clear()
  56.       term.setCursorPos(1, 1)
  57.       break
  58.     end
  59.     sleep(.1)
  60.   end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement