Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI('mcPaintFolder/screenAPI.lua')
- local mon = ''
- local event = ''
- mon, isMonitor = screenAPI.wrapMon()
- if not isMonitor then
- mon = term
- eventClick = 'mouse_click'
- eventDrag = 'mouse_drag'
- else
- mon = screenAPI.wrapMon()
- mon.setTextScale(0.5)
- eventClick = 'monitor_touch'
- end
- mon.setBackgroundColor(2^15)
- mon.clear()
- local x, y = 1, 1
- local length, height = mon.getSize()
- local colorTable = {}
- local currentColor = 2^15
- local imageTable = {}
- local fileName = ''
- local boxMode = false
- local lineMode = 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, height 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('L')
- mon.setCursorPos(length, 20)
- mon.write('S')
- mon.setCursorPos(length, 21)
- mon.write('X')
- end
- function infoTrans(table1, table2) --Transfers the information from the various screenAPI functions to the table we want, in this case imageTable
- for i, v in pairs(table1) do
- for k, u in pairs(table1[i]) do
- table2[i][k] = table1[i][k]
- end
- end
- end
- local function draw() -- function called to draw and record individual pixels
- if boxModeToggled then
- local _, _, x2, y2 = os.pullEvent(eventClick)
- if x2 ~= length then
- boxTable = screenAPI.drawBox(x, y, x2, y2, currentColor)
- infoTrans(boxTable, imageTable)
- end
- elseif lineModeToggled then
- local _, _, x2, y2 = os.pullEvent(eventClick)
- if x2 ~= length then
- lineTable = screenAPI.drawLine(x, y, x2, y2, currentColor)
- infoTrans(lineTable, imageTable)
- end
- else
- imageTable[x][y] = currentColor
- mon.setCursorPos(x,y)
- mon.write(' ')
- end
- end
- local function saveImage() -- exports the image to a file
- mon.setBackgroundColor(2^15)
- mon.clear()
- mon.setCursorPos(1,1)
- write('Enter your file\'s name: \n\n')
- for i= 1, #fs.list('mcPaintFolder/images/') do
- write(fs.list('mcPaintFolder/images')[i]..'\n')
- end
- mon.setCursorPos(1,2)
- 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()
- mon.clear()
- shell.run('mcImage.lua', fileName)
- drawPalette()
- else term.write('file already exists!')
- 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 lineMode() -- toggles line creation
- if lineModeToggled == true then
- lineModeToggled = false
- mon.setBackgroundColor(2^15)
- mon.setCursorPos(length,19)
- mon.write('L')
- else
- lineModeToggled = true
- mon.setBackgroundColor(2^14)
- mon.setCursorPos(length,19)
- mon.write('L')
- end
- mon.setBackgroundColor(currentColor)
- end
- local function selectColor() -- sets the selected color
- mon.setBackgroundColor(colorTable[y])
- currentColor = colorTable[y]
- end
- -- This is the MAIN part of code.
- drawPalette()
- createImageFile(2^15)
- while true do
- eventType, _, x, y = os.pullEvent()
- if (eventType == eventClick) or (eventType == eventDrag) then
- if eventType == eventClick and 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
- lineMode()
- elseif y == 20 then
- saveImage()
- elseif y == 21 then
- mon.setBackgroundColor(2^15)
- mon.clear()
- mon.setCursorPos(1,1)
- return
- end
- elseif x < length and (eventType == eventClick or eventType == eventDrag) then
- draw()
- end
- end
- end
Add Comment
Please, Sign In to add comment