Advertisement
HangMan23

paint.lua

Mar 12th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. local EU = require("ExtraUtilits")
  2. local gpu = require("component").gpu
  3. local unicode = require("unicode")
  4. local computer = require("computer")
  5. local term = require("term")
  6.  
  7. local close = false
  8.  
  9. local field = {}
  10.  
  11. local debugMode = false
  12.  
  13. local w, h = gpu.getResolution()
  14.  
  15. local width = 32
  16. local height = 16
  17.  
  18. local x, y = math.floor(w / 2 - width / 2), math.floor(h / 2 - height / 2)
  19.  
  20. backgroundSymbol = unicode.char(9618)
  21.  
  22. local background = 0x00ffff
  23. local foreground = 0xffffff
  24. local symbol = " "
  25.  
  26. local hotKeys = {
  27. [9] = function() close = true end
  28. }
  29.  
  30. ------------------------------------------------
  31.  
  32. local function setScale()
  33.  
  34.   repeat
  35.  
  36.     width = tonumber(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter massive width(1 - " .. w .. "):"))
  37.  
  38.   until width and width > 0 and width <= w
  39.  
  40.   repeat
  41.  
  42.     height = tonumber(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter massive height(1 - " .. h .. ":"))
  43.  
  44.   until height and height > 0 and height <= h
  45.  
  46.   x, y = math.floor(160 / 2 - width / 2), math.floor(h / 2 - height / 2)
  47.  
  48. end
  49.  
  50. local function clear()
  51.  
  52.   for i = 1, width do
  53.  
  54.     field[i] = {}
  55.  
  56.     for j = 1, height do
  57.  
  58.       field[i][j] = {}
  59.       field[i][j].symbol = backgroundSymbol
  60.       field[i][j].background = 0x000000
  61.  
  62.       if j / 2 * 2 ~= j then field[i][j].foreground = 0xffffff else field[i][j].foreground = 0x323232 end
  63.  
  64.     end
  65.  
  66.   end
  67.  
  68. end
  69.  
  70. local function draw()
  71.  
  72.   for i = 1, width do
  73.  
  74.     for j = 1, height do
  75.  
  76.       if field[i][j].background ~= gpu.getBackground() then gpu.setBackground(field[i][j].background) end
  77.  
  78.       if field[i][j].foreground ~= gpu.getForeground() then gpu.setForeground(field[i][j].foreground) end
  79.  
  80.       gpu.set(x + i, y + j, field[i][j].symbol)
  81.  
  82.     end
  83.  
  84.   end
  85.  
  86. end
  87.  
  88. local function pull()
  89.  
  90.   while not close do
  91.  
  92.     local signal = {computer.pullSignal()}
  93.  
  94.     if signal[1] == "touch" or signal[1] == "drag" then
  95.  
  96.       local sx, sy = signal[3], signal[4]
  97.  
  98.       if sx <= x + width and sx > x and sy <= y + height and sy > y then
  99.  
  100.         field[math.floor(sx - x)][math.floor(sy - y)].background = background
  101.         field[math.floor(sx - x)][math.floor(sy - y)].foreground = foreground
  102.         field[math.floor(sx - x)][math.floor(sy - y)].symbol = symbol
  103.  
  104.         if gpu.getBackground ~= background then gpu.setBackground(background) end
  105.         if gpu.getForeground ~= foreground then gpu.setForeground(foreground) end
  106.         gpu.set(sx, sy, symbol)
  107.  
  108.         if debugMode then
  109.  
  110.           gpu.setBackground(0x161616)
  111.           gpu.fill(1, 1, 10, 6, " ")
  112.           gpu.setForeground(0x00ffff)
  113.           gpu.set(1, 1, "sx: " .. sx)
  114.           gpu.set(1, 2, "sy: " .. sy)
  115.           gpu.set(1, 3, "x: " .. x)
  116.           gpu.set(1, 4, "y: " .. y)
  117.           gpu.set(1, 5, "width: " .. width)
  118.           gpu.set(1, 6, "height: " .. height)
  119.    
  120.         end
  121.  
  122.       end
  123.  
  124.     elseif signal[1] == "key_down" then
  125.  
  126.       local key = signal[3]
  127.       local char = unicode.char(key)
  128.  
  129.       for hotKey, func in pairs(hotKeys) do
  130.  
  131.         if hotKey == key or hotKey == char then
  132.  
  133.           func()
  134.  
  135.         end
  136.  
  137.       end
  138.  
  139.     end
  140.  
  141.   end
  142.  
  143. end
  144.  
  145. local function menu()
  146.  
  147.   local buttons = {}
  148.  
  149.   local function changeBackground()
  150.  
  151.     background = EU.ReadyUtilits.RGBPalette(65, 23, "", background)
  152.     buttons.close = true
  153.  
  154.   end
  155.  
  156.   local function changeForeground()
  157.  
  158.     foreground = EU.ReadyUtilits.RGBPalette(65, 23, "", foreground)
  159.     buttons.close = true
  160.  
  161.   end
  162.  
  163.   local function save()
  164.  
  165.     EU.save({field = field, width = width, height = height}, EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter path to saving:"))
  166.     buttons.close = true
  167.  
  168.   end
  169.  
  170.   local function load()
  171.  
  172.     local data = EU.load(EU.ReadyUtilits.WriteFieldUI(60, 23, "Enter image path:"))
  173.    
  174.     field = data.field
  175.     width = data.width
  176.     height = data.height
  177.  
  178.     buttons.close = true
  179.  
  180.   end
  181.  
  182.   local function fill()
  183.  
  184.     for i = 1, width do
  185.  
  186.       for j = 1, height do
  187.  
  188.         field[i][j].background = background
  189.         field[i][j].foreground = foreground
  190.         field[i][j].symbol = symbol
  191.  
  192.       end
  193.  
  194.     end
  195.  
  196.   end
  197.  
  198.   buttons = {
  199.   {1, 1, 6, 1, background, EU.Color.Invert(background), "back", changeBackground},
  200.   {1, 2, 6, 1, foreground, EU.Color.Invert(foreground), "fore", changeForeground},
  201.   {1, 3, 6, 1, 0x161616, 0x00ffff, "Save", save},
  202.   {1, 4, 6, 1, 0x161616, 0x00ffff, "Load", load},
  203.   {1, 5, 6, 1, 0x161616, 0x00ffff, "Fill", function() fill() draw() buttons.close = true end}
  204.   }
  205.  
  206.   buttons.eventFunc = function(ev)
  207.  
  208.     if ev[1] == "key_down" and ev[3] == 9 then buttons.close = true end
  209.  
  210.   end
  211.  
  212.   EU.draw(buttons)
  213.   EU.buttonPress(buttons)
  214.   gpu.setBackground(0x323232)
  215.   term.clear()
  216.   draw()
  217.  
  218. end
  219.  
  220. hotKeys["c"] = function() clear() draw() end
  221. hotKeys["d"] = function() draw() end
  222. hotKeys["e"] = function() if debugMode then loadfile("/bin/edit.lua")("/paint.lua") loadfile("/paint.lua")() end end
  223. hotKeys["m"] = menu
  224. hotKeys["D"] = function() local text = "[Debug mode]" if debugMode then debugMode = false gpu.setBackground(0x000000) gpu.fill(1, h, w, h, " ") gpu.fill(1, 1, 10, 6, " ") else debugMode = true gpu.setBackground(0x000000) gpu.setForeground(0x00ffff) gpu.set(w/2-string.len(text)/2, h, text)end draw() end
  225. hotKeys["s"] = function() setScale() clear() draw() end
  226.  
  227. EU.screenBackup(function()
  228.  
  229.   gpu.setBackground(0x323232)
  230.   term.clear()
  231.   clear()
  232.   draw()
  233.   pull()
  234.  
  235. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement