Advertisement
Guest User

QuickPaint.lua

a guest
Oct 22nd, 2017
232
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 1 0
  1. --[[
  2.     QuickPaint v1.0
  3.     Copyright (C) 2017 HaddockDev
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.     ]]
  18.  
  19. --Compatibility snippet
  20. if pocket then
  21.     error("Sorry, but PDAs are not supported.",0)
  22. elseif not term.isColor() then
  23.     error("Sorry, but you need an Advanced (golden/color) Computer to use QuickPaint.",0)
  24. end
  25.  
  26. ----------------------
  27. -- Variables
  28. ----------------------
  29. local w,h = term.getSize()
  30.  
  31. local color = colors.white
  32. local canvas = {}
  33. local running = true
  34. local redrawTop = false
  35.  
  36. ----------------------
  37. -- Canvas Functions
  38. ----------------------
  39. local function paintCanvas(x,y,col)
  40.     canvas[y][x] = col
  41. end
  42.  
  43. local function makeCanvas()
  44.     for y=1,h-1 do
  45.         canvas[y] = {}
  46.         for x=1,w do
  47.             canvas[y][x] = color
  48.         end
  49.     end
  50. end
  51.  
  52. local function drawCanvas()
  53.     for y=1,h-1 do
  54.         for x=1,w do
  55.             paintutils.drawPixel(x,y+1,canvas[y][x])
  56.            
  57.             --Drawing with term (not recommended, slower than paintutils)
  58.             --Although paint uses this method, QuickPaint uses a different method to draw its UI
  59.             --(paintutils) and it becomes a little slower, but still usable.
  60.             --But, if you *really* want to, here's the code:
  61.             --[[
  62.             term.setCursorPos(x,y+1)
  63.             term.setBackgroundColor(canvas[y][x])
  64.             term.setTextColor(canvas[y][x])
  65.             write("+")
  66.             --]]
  67.         end
  68.     end
  69. end
  70.  
  71. ----------------------
  72. -- Colors
  73. ----------------------
  74. local function lookup(color)
  75.     local colorTable = {}
  76.     for i=1,16 do
  77.         colorTable[2^(i-1)] = string.char(string.byte("0123456789abcdef",i))
  78.     end
  79.    
  80.     return colorTable[color]
  81. end
  82.  
  83. local function lookupHex(hex)
  84.     local colorTable = {}
  85.     for i=1,16 do
  86.         colorTable[string.byte("0123456789abcdef",i)] = 2^(i-1)
  87.     end
  88.        
  89.     return colorTable[string.byte(hex)]
  90. end
  91.  
  92. ----------------------
  93. -- GUI
  94. ----------------------
  95. local buttons = {}
  96. local function handleClick(x,y)
  97.     if y == 1 then
  98.         for _,v in pairs(buttons) do
  99.             if x >= v.X and x <= v.X + 1 then
  100.                 v.OnClick()
  101.                 break
  102.             end
  103.         end
  104.     else
  105.         paintCanvas(x,y-1,color)
  106.     end
  107. end
  108.  
  109. local function drawTop()
  110.     paintutils.drawLine(1,1,w,1,colors.gray)
  111.     local x = 1
  112.     for c=0,15 do
  113.         paintutils.drawPixel(x,1,2^c)
  114.         paintutils.drawPixel(x+1,1,2^c)
  115.         table.insert(buttons,{X=x,OnClick=function()color=2^c;paintutils.drawLine(1,1,w,1,2^c);sleep(0.15);redrawTop=true;end})
  116.         x = x + 2
  117.     end
  118.     term.setTextColor(colors.lightGray)
  119.     term.setBackgroundColor(colors.gray)
  120.     local text = "QuickPaint"
  121.     term.setCursorPos(w-#text-1,1)
  122.     write(text)
  123.     term.setCursorPos(w,1)
  124.     term.setTextColor(colors.red)
  125.     write("\7") --circle
  126.     table.insert(buttons,{X=w,OnClick=function()running=false;term.setBackgroundColor(colors.black);term.clear();term.setCursorPos(1,1)end})
  127. end
  128.  
  129. local function redraw()
  130.     buttons={}
  131.     drawTop()
  132.     drawCanvas()
  133. end
  134.  
  135. local function qp_save(file)
  136.     local output = ""
  137.     for y=1,h-1 do
  138.         for x=1,w do
  139.             output = output .. lookup(canvas[y][x])
  140.         end
  141.         output = output .. "\n"
  142.     end
  143.     local h = fs.open(file,"w")
  144.     h.write(output)
  145.     h.close()
  146. end
  147.  
  148. local function qp_load(file)
  149.     if not fs.exists(file) then return; end
  150.     local hh = fs.open(file,"r")
  151.    
  152.     for y=1,h-1 do
  153.         local line = hh.readLine()
  154.         for x=1,w do
  155.             canvas[y][x] = lookupHex(string.char(string.byte(line,x)))
  156.         end
  157.     end
  158.    
  159.     hh.close()
  160. end
  161.  
  162. --main
  163. makeCanvas()
  164. while running do
  165.     redraw()
  166.     local e, key, x, y = os.pullEvent()
  167.    
  168.     --This makes it easier not to change the color on accident
  169.     if e == "mouse_click" or e == "mouse_drag" then
  170.         if y > h then
  171.             y = h
  172.         elseif y == 1 and e == "mouse_drag" then
  173.             y = 2
  174.         elseif y < 1 then
  175.             y = 1
  176.         end        
  177.         if x > w then
  178.             x = w
  179.         elseif x < 1 then
  180.             x = 1
  181.         end
  182.         handleClick(x,y)
  183.     elseif e == "char" then
  184.         if key == "c" or key == "C" then
  185.             term.setBackgroundColor(colors.red)
  186.             term.clear()
  187.             term.setCursorPos(1,1)
  188.             term.setTextColor(colors.white)
  189.             sleep(0.1)
  190.             term.setBackgroundColor(colors.orange)
  191.             term.clear()
  192.             sleep(0.1)
  193.             term.setBackgroundColor(colors.yellow)
  194.             term.clear()
  195.             sleep(0.1)
  196.             makeCanvas()
  197.         elseif key == "s" or key == "S" then
  198.             paintutils.drawLine(1,1,w,1,colors.gray)
  199.             term.setCursorPos(1,1)
  200.             term.setTextColor(colors.lightGray)
  201.             write("Save to: ")
  202.             term.setTextColor(colors.white)
  203.             local savePlace = read()
  204.             qp_save(savePlace)
  205.         elseif key == "l" or key == "L" then
  206.             paintutils.drawLine(1,1,w,1,colors.gray)
  207.             term.setCursorPos(1,1)
  208.             term.setTextColor(colors.lightGray)
  209.             write("Load from: ")
  210.             term.setTextColor(colors.white)
  211.             local loadPlace = read()
  212.             makeCanvas()
  213.             qp_load(loadPlace)
  214.         elseif key == "?" then
  215.             term.setBackgroundColor(colors.black)
  216.             term.setTextColor(colors.white)
  217.             term.clear()
  218.             term.setCursorPos(1,1)
  219.             print("QuickPaint by Haddock")
  220.             print("(c) 2017 HaddockDev")
  221.             print("Under GPLv3")
  222.             print()print("Keybinds:")
  223.             print("s - Save")
  224.             print("l - Load")
  225.             print("c - Clear with selected color")
  226.             print("? - Help (you are here)")
  227.             print()print("Let go of '?' to go back...")
  228.             os.pullEvent("key_up")
  229.             sleep(0.1)
  230.         end
  231.     end
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement