Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- QuickPaint v1.0
- Copyright (C) 2017 HaddockDev
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ]]
- --Compatibility snippet
- if pocket then
- error("Sorry, but PDAs are not supported.",0)
- elseif not term.isColor() then
- error("Sorry, but you need an Advanced (golden/color) Computer to use QuickPaint.",0)
- end
- ----------------------
- -- Variables
- ----------------------
- local w,h = term.getSize()
- local color = colors.white
- local canvas = {}
- local running = true
- local redrawTop = false
- ----------------------
- -- Canvas Functions
- ----------------------
- local function paintCanvas(x,y,col)
- canvas[y][x] = col
- end
- local function makeCanvas()
- for y=1,h-1 do
- canvas[y] = {}
- for x=1,w do
- canvas[y][x] = color
- end
- end
- end
- local function drawCanvas()
- for y=1,h-1 do
- for x=1,w do
- paintutils.drawPixel(x,y+1,canvas[y][x])
- --Drawing with term (not recommended, slower than paintutils)
- --Although paint uses this method, QuickPaint uses a different method to draw its UI
- --(paintutils) and it becomes a little slower, but still usable.
- --But, if you *really* want to, here's the code:
- --[[
- term.setCursorPos(x,y+1)
- term.setBackgroundColor(canvas[y][x])
- term.setTextColor(canvas[y][x])
- write("+")
- --]]
- end
- end
- end
- ----------------------
- -- Colors
- ----------------------
- local function lookup(color)
- local colorTable = {}
- for i=1,16 do
- colorTable[2^(i-1)] = string.char(string.byte("0123456789abcdef",i))
- end
- return colorTable[color]
- end
- local function lookupHex(hex)
- local colorTable = {}
- for i=1,16 do
- colorTable[string.byte("0123456789abcdef",i)] = 2^(i-1)
- end
- return colorTable[string.byte(hex)]
- end
- ----------------------
- -- GUI
- ----------------------
- local buttons = {}
- local function handleClick(x,y)
- if y == 1 then
- for _,v in pairs(buttons) do
- if x >= v.X and x <= v.X + 1 then
- v.OnClick()
- break
- end
- end
- else
- paintCanvas(x,y-1,color)
- end
- end
- local function drawTop()
- paintutils.drawLine(1,1,w,1,colors.gray)
- local x = 1
- for c=0,15 do
- paintutils.drawPixel(x,1,2^c)
- paintutils.drawPixel(x+1,1,2^c)
- table.insert(buttons,{X=x,OnClick=function()color=2^c;paintutils.drawLine(1,1,w,1,2^c);sleep(0.15);redrawTop=true;end})
- x = x + 2
- end
- term.setTextColor(colors.lightGray)
- term.setBackgroundColor(colors.gray)
- local text = "QuickPaint"
- term.setCursorPos(w-#text-1,1)
- write(text)
- term.setCursorPos(w,1)
- term.setTextColor(colors.red)
- write("\7") --circle
- table.insert(buttons,{X=w,OnClick=function()running=false;term.setBackgroundColor(colors.black);term.clear();term.setCursorPos(1,1)end})
- end
- local function redraw()
- buttons={}
- drawTop()
- drawCanvas()
- end
- local function qp_save(file)
- local output = ""
- for y=1,h-1 do
- for x=1,w do
- output = output .. lookup(canvas[y][x])
- end
- output = output .. "\n"
- end
- local h = fs.open(file,"w")
- h.write(output)
- h.close()
- end
- local function qp_load(file)
- if not fs.exists(file) then return; end
- local hh = fs.open(file,"r")
- for y=1,h-1 do
- local line = hh.readLine()
- for x=1,w do
- canvas[y][x] = lookupHex(string.char(string.byte(line,x)))
- end
- end
- hh.close()
- end
- --main
- makeCanvas()
- while running do
- redraw()
- local e, key, x, y = os.pullEvent()
- --This makes it easier not to change the color on accident
- if e == "mouse_click" or e == "mouse_drag" then
- if y > h then
- y = h
- elseif y == 1 and e == "mouse_drag" then
- y = 2
- elseif y < 1 then
- y = 1
- end
- if x > w then
- x = w
- elseif x < 1 then
- x = 1
- end
- handleClick(x,y)
- elseif e == "char" then
- if key == "c" or key == "C" then
- term.setBackgroundColor(colors.red)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- sleep(0.1)
- term.setBackgroundColor(colors.orange)
- term.clear()
- sleep(0.1)
- term.setBackgroundColor(colors.yellow)
- term.clear()
- sleep(0.1)
- makeCanvas()
- elseif key == "s" or key == "S" then
- paintutils.drawLine(1,1,w,1,colors.gray)
- term.setCursorPos(1,1)
- term.setTextColor(colors.lightGray)
- write("Save to: ")
- term.setTextColor(colors.white)
- local savePlace = read()
- qp_save(savePlace)
- elseif key == "l" or key == "L" then
- paintutils.drawLine(1,1,w,1,colors.gray)
- term.setCursorPos(1,1)
- term.setTextColor(colors.lightGray)
- write("Load from: ")
- term.setTextColor(colors.white)
- local loadPlace = read()
- makeCanvas()
- qp_load(loadPlace)
- elseif key == "?" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("QuickPaint by Haddock")
- print("(c) 2017 HaddockDev")
- print("Under GPLv3")
- print()print("Keybinds:")
- print("s - Save")
- print("l - Load")
- print("c - Clear with selected color")
- print("? - Help (you are here)")
- print()print("Let go of '?' to go back...")
- os.pullEvent("key_up")
- sleep(0.1)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement