Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function fileGen()
- if not fs.exists('mcPaintFolder/info.txt') then
- term.clear()
- term.write('Enter monitor side: ')
- side = io.read()
- if peripheral.getType(side) ~= 'monitor' then
- term.write('No monitor on this side!')
- fileGen()
- else
- term.write('Data saved!')
- info = fs.open('mcPaintFolder/info.txt', 'w')
- info.write(side)
- info.close()
- os.sleep(2)
- end
- end
- end
- fileGen()
- info = fs.open('mcPaintFolder/info.txt', 'r')
- side = info.readAll()
- local mon = peripheral.wrap(side)
- mon.setTextScale(0.5)
- mon.setBackgroundColor(2^15)
- mon.clear()
- local x, y = 1, 1
- local length, width = mon.getSize()
- local colorTable = {}
- local currentColor = 2^15
- local imageTable = {}
- local fileName = ''
- local boxMode = false
- local function createImageFile(color) -- creates a table of the form imageTable[x][y] = black as default
- for x = 1, length-1 do
- imageTable[x] = {}
- for y = 1, width do
- imageTable[x][y] = color
- end
- end
- end
- local function drawPalette() -- Draws up the GUI
- local color = 0
- for i = 1, 16 do
- color = 2^(i-1)
- colorTable[i] = color
- mon.setBackgroundColor(color)
- mon.setCursorPos(length, i)
- mon.write(' ')
- end
- mon.setCursorPos(length, 17)
- mon.write('E')
- mon.setCursorPos(length, 18)
- mon.write('B')
- mon.setCursorPos(length, 19)
- mon.write('S')
- mon.setCursorPos(length, 20)
- mon.write('X')
- end
- local function draw() -- function called to draw and record individual pixels
- if not boxModeToggled then
- if not eraseMode then
- imageTable[x][y] = currentColor
- mon.setCursorPos(x,y)
- mon.write(' ')
- else
- imageTable[x][y] = currentColor
- mon.setCursorPos(x,y)
- mon.write(' ')
- end
- else
- local _, _, x2, y2 = os.pullEvent('monitor_touch')
- local xinc, yinc = 1, 1
- if x < x2 then
- xinc = 1
- else
- xinc = -1
- end
- if y < y2 then
- yinc = 1
- else
- yinc = -1
- end
- for i = x, x2, xinc do
- for k = y, y2, yinc do
- imageTable[i][k] = currentColor
- mon.setCursorPos(i,k)
- mon.write(' ')
- end
- end
- end
- end
- local function saveImage() -- exports the image to a file
- if not fs.exists('/images/') then
- fs.makeDir('/images')
- end
- term.clear()
- term.setCursorPos(1,1)
- term.write('Enter your file\'s name: \n')
- fileName = io.read()
- if not fs.exists('mcPaintFolder/images/'..fileName) then
- image = fs.open('mcPaintFolder/images/'..fileName, 'w')
- imageString = textutils.serialise(imageTable)
- image.write(imageString)
- image.close()
- else term.write('file already exists! Change file name: \n')
- fileName = io.read()
- saveImage()
- end
- end
- local function boxMode() -- toggles box creation
- if boxModeToggled == true then
- boxModeToggled = false
- mon.setBackgroundColor(2^15)
- mon.setCursorPos(length,18)
- mon.write('B')
- else
- boxModeToggled = true
- mon.setBackgroundColor(2^14)
- mon.setCursorPos(length,18)
- mon.write('B')
- end
- mon.setBackgroundColor(currentColor)
- end
- local function selectColor() -- sets the selected color
- mon.setBackgroundColor(colorTable[y])
- currentColor = colorTable[y]
- end
- drawPalette()
- createImageFile(2^15)
- while true do
- _, _, x, y = os.pullEvent('monitor_touch')
- if x == length then
- eraseMode = false
- if y <= 16 then
- selectColor()
- elseif y == 17 then
- eraseMode = true
- mon.setBackgroundColor(2^15)
- currentColor = 2^15
- elseif y == 18 then
- boxMode()
- elseif y == 19 then
- saveImage()
- elseif y == 20 then
- mon.setBackgroundColor(2^15)
- mon.clear()
- return
- end
- elseif x < length then
- draw()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment