Guest User

mcPaint.lua

a guest
Jul 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.22 KB | None | 0 0
  1. local function fileGen()
  2.     if not fs.exists('mcPaintFolder/info.txt') then
  3.         term.clear()
  4.         term.write('Enter monitor side: ')
  5.         side = io.read()
  6.         if peripheral.getType(side) ~= 'monitor' then
  7.             term.write('No monitor on this side!')
  8.             fileGen()
  9.         else
  10.         term.write('Data saved!')
  11.         info = fs.open('mcPaintFolder/info.txt', 'w')
  12.         info.write(side)
  13.         info.close()
  14.         os.sleep(2)
  15.         end
  16.     end
  17. end
  18.  
  19. fileGen()
  20. info = fs.open('mcPaintFolder/info.txt', 'r')
  21. side = info.readAll()
  22.  
  23. local mon = peripheral.wrap(side)
  24. mon.setTextScale(0.5)
  25. mon.setBackgroundColor(2^15)
  26. mon.clear()
  27. local x, y = 1, 1
  28. local length, width = mon.getSize()
  29. local colorTable = {}
  30. local currentColor = 2^15
  31. local imageTable = {}
  32. local fileName = ''
  33. local boxMode = false
  34.  
  35. local function createImageFile(color) -- creates a table of the form imageTable[x][y] = black as default
  36.     for x = 1, length-1 do
  37.         imageTable[x] = {}
  38.         for y = 1, width do
  39.             imageTable[x][y] = color
  40.         end
  41.     end
  42. end
  43.    
  44. local function drawPalette() -- Draws up the GUI
  45.     local color = 0
  46.     for i = 1, 16 do
  47.         color = 2^(i-1)
  48.         colorTable[i] = color
  49.         mon.setBackgroundColor(color)
  50.         mon.setCursorPos(length, i)
  51.         mon.write(' ')
  52.     end
  53.     mon.setCursorPos(length, 17)
  54.     mon.write('E')
  55.     mon.setCursorPos(length, 18)
  56.     mon.write('B')
  57.     mon.setCursorPos(length, 19)
  58.     mon.write('S')
  59.     mon.setCursorPos(length, 20)
  60.     mon.write('X')
  61. end
  62.  
  63. local function draw() -- function called to draw and record individual pixels
  64.     if not boxModeToggled then
  65.         if not eraseMode then
  66.             imageTable[x][y] = currentColor
  67.             mon.setCursorPos(x,y)
  68.             mon.write(' ')
  69.         else
  70.             imageTable[x][y] = currentColor
  71.             mon.setCursorPos(x,y)
  72.             mon.write(' ')
  73.         end
  74.     else
  75.         local _, _, x2, y2 = os.pullEvent('monitor_touch')
  76.         local xinc, yinc = 1, 1
  77.         if x < x2 then
  78.             xinc = 1
  79.         else
  80.             xinc = -1
  81.         end
  82.         if y < y2 then
  83.             yinc = 1
  84.         else
  85.             yinc = -1
  86.         end
  87.         for i = x, x2, xinc do
  88.             for k = y, y2, yinc do
  89.                 imageTable[i][k] = currentColor
  90.                 mon.setCursorPos(i,k)
  91.                 mon.write(' ')
  92.             end
  93.         end
  94.     end        
  95. end
  96.  
  97. local function saveImage() -- exports the image to a file
  98.     if not fs.exists('/images/') then
  99.         fs.makeDir('/images')
  100.     end
  101.     term.clear()
  102.     term.setCursorPos(1,1)
  103.     term.write('Enter your file\'s name: \n')
  104.     fileName = io.read()
  105.     if not fs.exists('mcPaintFolder/images/'..fileName) then
  106.     image = fs.open('mcPaintFolder/images/'..fileName, 'w')
  107.     imageString = textutils.serialise(imageTable)
  108.     image.write(imageString)
  109.     image.close()
  110.     else term.write('file already exists! Change file name: \n')
  111.     fileName = io.read()
  112.     saveImage()
  113.     end
  114. end
  115.  
  116. local function boxMode() -- toggles box creation
  117.     if boxModeToggled == true then
  118.         boxModeToggled = false
  119.         mon.setBackgroundColor(2^15)
  120.         mon.setCursorPos(length,18)
  121.         mon.write('B')
  122.     else
  123.         boxModeToggled = true
  124.         mon.setBackgroundColor(2^14)
  125.         mon.setCursorPos(length,18)
  126.         mon.write('B')
  127.     end
  128.     mon.setBackgroundColor(currentColor)
  129. end
  130.  
  131. local function selectColor() -- sets the selected color
  132.     mon.setBackgroundColor(colorTable[y])
  133.     currentColor = colorTable[y]
  134. end
  135.  
  136. drawPalette()
  137. createImageFile(2^15)
  138. while true do
  139.     _, _, x, y = os.pullEvent('monitor_touch')
  140.     if x == length then
  141.         eraseMode = false
  142.         if y <= 16 then
  143.             selectColor()
  144.         elseif y == 17 then
  145.             eraseMode = true
  146.             mon.setBackgroundColor(2^15)
  147.             currentColor = 2^15
  148.         elseif y == 18 then
  149.             boxMode()
  150.         elseif y == 19 then
  151.             saveImage()
  152.         elseif y == 20 then
  153.             mon.setBackgroundColor(2^15)
  154.             mon.clear()
  155.             return
  156.         end
  157.     elseif x < length then
  158.         draw()
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment