_Jacques

mcPaint.lua

Jul 7th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI('mcPaintFolder/screenAPI.lua')
  2. local mon = ''
  3. local event = ''
  4. mon, isMonitor = screenAPI.wrapMon()
  5.  
  6. if not isMonitor then
  7.     mon = term
  8.     eventClick = 'mouse_click'
  9.     eventDrag = 'mouse_drag'
  10. else
  11.     mon = screenAPI.wrapMon()
  12.     mon.setTextScale(0.5)
  13.     eventClick = 'monitor_touch'
  14. end
  15.  
  16. mon.setBackgroundColor(2^15)
  17. mon.clear()
  18. local x, y = 1, 1
  19. local length, height = mon.getSize()
  20. local colorTable = {}
  21. local currentColor = 2^15
  22. local imageTable = {}
  23. local fileName = ''
  24. local boxMode = false
  25. local lineMode = false
  26.  
  27. local function createImageFile(color) -- creates a table of the form imageTable[x][y] = black as default
  28.     for x = 1, length-1 do
  29.         imageTable[x] = {}
  30.         for y = 1, height do
  31.             imageTable[x][y] = color
  32.         end
  33.     end
  34. end
  35.    
  36. local function drawPalette() -- Draws up the GUI
  37.     local color = 0
  38.     for i = 1, 16 do
  39.         color = 2^(i-1)
  40.         colorTable[i] = color
  41.         mon.setBackgroundColor(color)
  42.         mon.setCursorPos(length, i)
  43.         mon.write(' ')
  44.     end
  45.     mon.setCursorPos(length, 17)
  46.     mon.write('E')
  47.     mon.setCursorPos(length, 18)
  48.     mon.write('B')
  49.     mon.setCursorPos(length, 19)
  50.     mon.write('L')
  51.     mon.setCursorPos(length, 20)
  52.     mon.write('S')
  53.     mon.setCursorPos(length, 21)
  54.     mon.write('X')
  55. end
  56.  
  57. function infoTrans(table1, table2) --Transfers the information from the various screenAPI functions to the table we want, in this case imageTable
  58.     for i, v in pairs(table1) do
  59.         for k, u in pairs(table1[i]) do
  60.             table2[i][k] = table1[i][k]
  61.         end
  62.     end
  63. end
  64.  
  65. local function draw() -- function called to draw and record individual pixels
  66.     if boxModeToggled then
  67.         local _, _, x2, y2 = os.pullEvent(eventClick)
  68.         if x2 ~= length then
  69.             boxTable = screenAPI.drawBox(x, y, x2, y2, currentColor)
  70.             infoTrans(boxTable, imageTable)
  71.         end
  72.     elseif lineModeToggled then
  73.         local _, _, x2, y2 = os.pullEvent(eventClick)
  74.         if x2 ~= length then
  75.             lineTable = screenAPI.drawLine(x, y, x2, y2, currentColor)
  76.             infoTrans(lineTable, imageTable)
  77.         end
  78.     else
  79.         imageTable[x][y] = currentColor
  80.         mon.setCursorPos(x,y)
  81.         mon.write(' ')
  82.     end
  83. end
  84.  
  85. local function saveImage() -- exports the image to a file
  86.     mon.setBackgroundColor(2^15)
  87.     mon.clear()
  88.     mon.setCursorPos(1,1)
  89.     write('Enter your file\'s name: \n\n')
  90.     for i= 1, #fs.list('mcPaintFolder/images/') do
  91.         write(fs.list('mcPaintFolder/images')[i]..'\n')
  92.     end
  93.     mon.setCursorPos(1,2)
  94.     fileName = io.read()
  95.     if not fs.exists('mcPaintFolder/images/'..fileName) then
  96.     image = fs.open('mcPaintFolder/images/'..fileName, 'w')
  97.     imageString = textutils.serialise(imageTable)
  98.     image.write(imageString)
  99.     image.close()
  100.     mon.clear()
  101.     shell.run('mcImage.lua', fileName)
  102.     drawPalette()
  103.     else term.write('file already exists!')
  104.     saveImage()
  105.     end
  106. end
  107.  
  108. local function boxMode() -- toggles box creation
  109.     if boxModeToggled == true then
  110.         boxModeToggled = false
  111.         mon.setBackgroundColor(2^15)
  112.         mon.setCursorPos(length,18)
  113.         mon.write('B')
  114.     else
  115.         boxModeToggled = true
  116.         mon.setBackgroundColor(2^14)
  117.         mon.setCursorPos(length,18)
  118.         mon.write('B')
  119.     end
  120.     mon.setBackgroundColor(currentColor)
  121. end
  122.  
  123. local function lineMode() -- toggles line creation
  124.     if lineModeToggled == true then
  125.         lineModeToggled = false
  126.         mon.setBackgroundColor(2^15)
  127.         mon.setCursorPos(length,19)
  128.         mon.write('L')
  129.     else
  130.         lineModeToggled = true
  131.         mon.setBackgroundColor(2^14)
  132.         mon.setCursorPos(length,19)
  133.         mon.write('L')
  134.     end
  135.     mon.setBackgroundColor(currentColor)
  136. end
  137.  
  138.  
  139. local function selectColor() -- sets the selected color
  140.     mon.setBackgroundColor(colorTable[y])
  141.     currentColor = colorTable[y]
  142. end
  143.  
  144. -- This is the MAIN part of code.
  145.  
  146. drawPalette()
  147. createImageFile(2^15)
  148. while true do
  149.     eventType, _, x, y = os.pullEvent()
  150.     if (eventType == eventClick) or (eventType == eventDrag) then
  151.         if eventType == eventClick and x == length then
  152.             eraseMode = false
  153.             if y <= 16 then
  154.                 selectColor()
  155.             elseif y == 17 then
  156.  
  157.                 eraseMode = true
  158.                 mon.setBackgroundColor(2^15)
  159.                 currentColor = 2^15
  160.            
  161.             elseif y == 18 then
  162.            
  163.                 boxMode()
  164.        
  165.             elseif y == 19 then
  166.            
  167.                 lineMode()
  168.        
  169.             elseif y == 20 then
  170.            
  171.                 saveImage()
  172.        
  173.             elseif y == 21 then
  174.            
  175.                 mon.setBackgroundColor(2^15)
  176.                 mon.clear()
  177.                 mon.setCursorPos(1,1)
  178.                 return
  179.             end
  180.         elseif x < length and (eventType == eventClick or eventType == eventDrag) then
  181.             draw()
  182.         end
  183.     end
  184. end
Add Comment
Please, Sign In to add comment