HangMan23

Paint v1.4

Aug 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.61 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. local serialization = require("serialization")
  7.  
  8. local close = false
  9.  
  10. local field = {}
  11.  
  12. local debugMode = false
  13.  
  14. local w, h = gpu.getResolution()
  15.  
  16. local width = 32
  17. local height = 16
  18.  
  19. local x, y = math.floor(w / 2 - width / 2), math.floor(h / 2 - height / 2)
  20.  
  21. local defaultBackground = 7767453
  22. local defaultForeground = 0xffffff
  23.  
  24. local backgroundSymbol1 = unicode.char(10267)
  25. local backgroundSymbol2 = unicode.char(10468)
  26.  
  27. local background = 0x000000
  28. local foreground = 0xffffff
  29. local symbol = " "
  30.  
  31. local hotKeys = {
  32. [9] = function() close = true end
  33. }
  34.  
  35. local r, g, b = 100, 100, 100
  36.  
  37. local ss = {
  38. unicode.char(9582),
  39. unicode.char(9687)
  40. }
  41.  
  42. ------------------------------------------------
  43.  
  44. local earse = false
  45.  
  46. local function setScale()
  47.  
  48.   repeat
  49.  
  50.     width = tonumber(EU.ReadyUtilits.WFUIV2(70, 23, 15, "Enter width(1 - " .. w .. "):", "number"))
  51.  
  52.   until width and width > 0 and width <= w
  53.  
  54.   repeat
  55.  
  56.     height = tonumber(EU.ReadyUtilits.WFUIV2(70, 23, 15, "Enter height(1 - " .. h .. "):", "number"))
  57.  
  58.   until height and height > 0 and height <= h
  59.  
  60.   x, y = math.floor(160 / 2 - width / 2), math.floor(h / 2 - height / 2)
  61.  
  62. end
  63.  
  64. local function clear()
  65.  
  66.   for i = 1, width do
  67.  
  68.     field[i] = {}
  69.  
  70.     for j = 1, height do
  71.  
  72.       field[i][j] = {}
  73.  
  74.     end
  75.  
  76.   end
  77.  
  78. end
  79.  
  80. local function draw()
  81.  
  82.   for i = 1, width do
  83.  
  84.     for j = 1, height do
  85.  
  86.       if field[i][j] and field[i][j].symbol then
  87.  
  88.         if field[i][j].background ~= gpu.getBackground() then gpu.setBackground(field[i][j].background) end
  89.         if field[i][j].foreground ~= gpu.getForeground() then gpu.setForeground(field[i][j].foreground) end
  90.  
  91.         gpu.set(x + i, y + j, field[i][j].symbol)
  92.  
  93.       else
  94.  
  95.         if gpu.getBackground() ~= defaultBackground then gpu.setBackground(defaultBackground) end
  96.         if gpu.getForeground() ~= defaultForeground then gpu.setForeground(defaultForeground) end
  97.  
  98.         if math.floor(i / 2) * 2 == i then
  99.  
  100.           gpu.set(x + i, y + j, backgroundSymbol1)
  101.  
  102.         else
  103.  
  104.           gpu.set(x + i, y + j, backgroundSymbol2)
  105.  
  106.         end
  107.  
  108.       end
  109.  
  110.     end
  111.  
  112.   end
  113.  
  114. end
  115.  
  116. local function pull()
  117.  
  118.   while not close do
  119.  
  120.     local signal = {computer.pullSignal()}
  121.  
  122.     if signal[1] == "touch" or signal[1] == "drag" then
  123.  
  124.       local sx, sy = signal[3], signal[4]
  125.  
  126.       if sx <= x + width and sx > x and sy <= y + height and sy > y then
  127.  
  128.         if not earse then
  129.  
  130.           field[math.floor(sx - x)][math.floor(sy - y)].background = background
  131.           field[math.floor(sx - x)][math.floor(sy - y)].foreground = foreground
  132.           field[math.floor(sx - x)][math.floor(sy - y)].symbol = symbol
  133.  
  134.           if gpu.getBackground() ~= background then gpu.setBackground(background) end
  135.           if gpu.getForeground() ~= foreground then gpu.setForeground(foreground) end
  136.           gpu.set(sx, sy, symbol)
  137.  
  138.         else
  139.  
  140.           field[sx - x ][sy - y].symbol = nil
  141.           if gpu.getBackground() ~= defaultBackground then gpu.setBackground(defaultBackground) end
  142.           if gpu.getForeground() ~= defaultForeground then gpu.setForeground(defaultForeground) end
  143.  
  144.           if math.floor((sx - x) / 2) * 2 == sx - x then
  145.  
  146.             gpu.set(sx, sy, backgroundSymbol1)
  147.  
  148.           else
  149.  
  150.             gpu.set(sx, sy, backgroundSymbol2)
  151.  
  152.           end
  153.  
  154.         end
  155.  
  156.         if debugMode then
  157.  
  158.           gpu.setBackground(0x161616)
  159.           gpu.fill(1, 1, 10, 6, " ")
  160.           gpu.setForeground(0x00ffff)
  161.           gpu.set(1, 1, "sx: " .. sx)
  162.           gpu.set(1, 2, "sy: " .. sy)
  163.           gpu.set(1, 3, "x: " .. x)
  164.           gpu.set(1, 4, "y: " .. y)
  165.           gpu.set(1, 5, "width: " .. width)
  166.           gpu.set(1, 6, "height: " .. height)
  167.    
  168.         end
  169.  
  170.       end
  171.  
  172.     elseif signal[1] == "key_down" then
  173.  
  174.       local key, key2 = signal[3], signal[4]
  175.       local char = unicode.char(key)
  176.  
  177.       for hotKey, func in pairs(hotKeys) do
  178.  
  179.         if hotKey == key or hotKey == char or type(hotKeys[hotKey]) == "table" and hotKeys[hotKey].key2 == key2 then
  180.  
  181.           if type(hotKeys[hotKey]) == "table" then
  182.  
  183.             hotKeys[hotKey].func()
  184.  
  185.           else
  186.  
  187.             func()
  188.  
  189.           end
  190.  
  191.         end
  192.  
  193.       end
  194.  
  195.     end
  196.  
  197.   end
  198.  
  199. end
  200.  
  201. local function menu()
  202.  
  203.   local buttons = {}
  204.  
  205.   local function changeBackground()
  206.  
  207.     background = EU.ReadyUtilits.RGBPalette(65, 23, "", background)
  208.     buttons.close = true
  209.  
  210.     gpu.setBackground(0x323232)
  211.     gpu.setForeground(background)
  212.     gpu.set(7, 1, ss[2])
  213.  
  214.   end
  215.  
  216.   local function changeForeground()
  217.  
  218.     foreground = EU.ReadyUtilits.RGBPalette(65, 23, "", foreground)
  219.     buttons.close = true
  220.  
  221.     gpu.setBackground(0x323232)
  222.     gpu.setForeground(foreground)
  223.     gpu.set(7, 2, ss[2])
  224.  
  225.   end
  226.  
  227.   local function save()
  228.  
  229.     local path = EU.ReadyUtilits.WFUIV2(70, 23, 15, "Enter path to saving:")
  230.     local func, reason = loadfile("/paint_modules/save.lua")
  231.  
  232.     if func then
  233.  
  234.       func(field, width, height, path)
  235.  
  236.     else
  237.  
  238.       error("Error with running save module: " .. reason)
  239.  
  240.     end
  241.  
  242.     buttons.close = true
  243.  
  244.   end
  245.  
  246.   local function load()
  247.  
  248.     local path = EU.ReadyUtilits.WFUIV2(70, 23, 15, "Enter image path:")
  249.  
  250.     local c
  251.  
  252.     local file = io.open(path, "r")
  253.  
  254.     if file:read() == "--CAMERA_PICTURE--" then c = true end
  255.  
  256.     file:close()
  257.  
  258.     if c then
  259.  
  260.       local func, reason = loadfile("/camera_render.lua")
  261.  
  262.       if not func then error("Failed with opening camera picture render file: " .. reason) end
  263.  
  264.       local data = func(path)
  265.  
  266.       field = data.field
  267.       width = data.width
  268.       height = data.height
  269.  
  270.     else
  271.  
  272.       local func, reason = loadfile("/paint_modules/load.lua")
  273.  
  274.       if func then
  275.  
  276.         local data = func(path)
  277.  
  278.         field = data.field
  279.         width = data.width
  280.         height = data.height
  281.  
  282.         print(width .. " " .. height .. " " .. #field .. " " .. #field[1])
  283.  
  284.       else
  285.  
  286.         error("Error with runnint load file: " .. reason)
  287.  
  288.       end
  289.  
  290.     end
  291.  
  292.     x, y = w / 2 - width / 2, h  / 2 - height / 2
  293.  
  294.     buttons.close = true
  295.  
  296.   end
  297.  
  298.   local function fill()
  299.  
  300.     for i = 1, width do
  301.  
  302.       for j = 1, height do
  303.  
  304.         field[i][j] = {}
  305.  
  306.         field[i][j].background = background
  307.         field[i][j].foreground = foreground
  308.         field[i][j].symbol = symbol
  309.  
  310.       end
  311.  
  312.     end
  313.  
  314.   end
  315.  
  316.   local function filter()
  317.  
  318.     local buttons = {}
  319.  
  320.     local function invert()
  321.  
  322.       for i = 1, width do
  323.  
  324.         for j = 1, height do
  325.  
  326.           if field[i][j].symbol then
  327.  
  328.             field[i][j].background = EU.Color.Invert(field[i][j].background)
  329.             field[i][j].foreground = EU.Color.Invert(field[i][j].background)
  330.  
  331.           end
  332.  
  333.         end
  334.  
  335.       end
  336.  
  337.       buttons.close = true
  338.  
  339.     end
  340.  
  341.     local function rgb()
  342.  
  343.       local lbuttons = {}
  344.  
  345.       lbuttons = {
  346.       {w / 2 - 3, h / 2 - 2, 6, 1, 0xffffff, 0xff0000, "R:" .. r .. "%", function() r = EU.ReadyUtilits.WFUIV2(w / 2 - 15/2, h / 2 -1, 15, "Enter r value:", "number") lbuttons[1][7] = "R:" .. r .. "%" EU.draw(buttons) end},
  347.       {w / 2 - 3, h / 2 - 1, 6, 1, 0xffffff, 0x00ff00, "G:" .. g .. "%", function() g = EU.ReadyUtilits.WFUIV2(w / 2 - 15/2, h / 2 -1, 15, "Enter g value:", "number") lbuttons[2][7] = "G:" .. g .. "%" EU.draw(buttons) end},
  348.       {w / 2 - 3, h / 2 - 0, 6, 1, 0xffffff, 0x0000ff, "B:" .. b .. "%", function() b = EU.ReadyUtilits.WFUIV2(w / 2 - 15/2, h / 2 -1, 15, "Enter b value:", "number") lbuttons[3][7] = "B:" .. b .. "%" EU.draw(buttons) end}
  349.       }
  350.  
  351.       lbuttons.eventFunc = function(ev) if ev[1] == "key_down" and ev[3] == 13 then
  352.  
  353.         for i = 1, width do
  354.  
  355.           for j = 1, height do
  356.  
  357.             field[i][j].background = EU.Color.Restrict(field[i][j].background, r, g, b)
  358.             field[i][j].foreground = EU.Color.Restrict(field[i][j].foreground, r, g, b)
  359.  
  360.          end
  361.  
  362.         end
  363.  
  364.         lbuttons.close = true end
  365.  
  366.       end
  367.  
  368.       EU.screenBackup(function()
  369.       EU.draw(lbuttons)
  370.       EU.buttonPress(lbuttons)
  371.       end)
  372.  
  373.     end
  374.  
  375.     buttons = {
  376.     {7, 7, 6, 1, 0xffffff, 0x646464, "Invert", invert},
  377.     {7, 8, 6, 1, 0xffffff, 0x646464, "RGB", rgb}
  378.     }
  379.  
  380.     buttons.eventFunc = function(ev)
  381.  
  382.       if ev[1] == "key_down" and ev[3] == 9 then
  383.  
  384.         buttons.close = true
  385.  
  386.       end
  387.  
  388.     end
  389.  
  390.     EU.screenBackup(function()
  391.  
  392.       gpu.setForeground(0xffffff)
  393.       gpu.setBackground(0x323232)
  394.       gpu.set(7, 6, ss[1])
  395.       os.sleep(0.1)
  396.       EU.draw(buttons)
  397.       EU.buttonPress(buttons)
  398.  
  399.     end)
  400.  
  401.   end
  402.  
  403.   local function earser()
  404.  
  405.     if earse == true then
  406.  
  407.       earse = false
  408.  
  409.     else
  410.  
  411.       earse = true
  412.  
  413.     end
  414.  
  415.     buttons.close = true
  416.  
  417.   end
  418.  
  419.   local buttons = {
  420.   {1, 1, 6, 1, 0xffffff, 0x646464, "back", changeBackground},
  421.   {1, 2, 6, 1, 0xffffff, 0x646464, "fore", changeForeground},
  422.   {1, 3, 6, 1, 0xffffff, 0x646464, "Save", save},
  423.   {1, 4, 6, 1, 0xffffff, 0x646464, "Load", load},
  424.   {1, 5, 6, 1, 0xffffff, 0x646464, "Fill", fill},
  425.   {1, 6, 6, 1, 0xffffff, 0x646464, "Filter", filter},
  426.   {1, 7, 6, 1, 0xffffff, 0x646464, "Earser", earser}
  427.   }
  428.  
  429.   buttons.eventFunc = function(ev)
  430.  
  431.     if ev[1] == "key_down" and ev[3] == 9 then buttons.close = true end
  432.  
  433.   end
  434.  
  435.   gpu.setBackground(0x323232)
  436.   gpu.setForeground(background)
  437.   gpu.set(7, 1, ss[2])
  438.   gpu.setForeground(foreground)
  439.   gpu.set(7, 2, ss[2])
  440.   EU.draw(buttons)
  441.   EU.buttonPress(buttons)
  442.   gpu.setBackground(0x323232)
  443.   term.clear()
  444.   draw()
  445.  
  446. end
  447.  
  448. hotKeys["c"] = function() clear() draw() end
  449. hotKeys["d"] = function() draw() end
  450. hotKeys["e"] = function() if debugMode then loadfile("/bin/edit.lua")("/paint.lua") loadfile("/paint.lua")() end end
  451. hotKeys["m"] = menu
  452. 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
  453. hotKeys["s"] = function() setScale() clear() draw() end
  454.  
  455. hotKeys["ss"] = {key2 = 56}
  456.  
  457. hotKeys["ss"].func = function()
  458.  
  459.   local signal = {computer.pullSignal()}
  460.  
  461.   repeat
  462.  
  463.     if signal[1] == "key_up" and signal[4] == 56 then
  464.  
  465.       return
  466.  
  467.     elseif signal[1] == "touch" then
  468.  
  469.       local _, fore, back = gpu.get(signal[3], signal[4])
  470.  
  471.       background = back
  472.       foreground = fore
  473.  
  474.       return
  475.  
  476.     end
  477.  
  478.   until false
  479.  
  480. end
  481.  
  482. hotKeys["t"] = function()
  483.  
  484.   repeat
  485.  
  486.     local signal = {computer.pullSignal()}
  487.  
  488.     if signal[1] == "key_up" and unicode.char(signal[3]) == "t" then return end
  489.  
  490.     if signal[1] == "touch" then
  491.  
  492.       local tx, ty = signal[3], signal[4]
  493.  
  494.       local text = EU.ReadyUtilits.WFUIV2(70, 23, 10)
  495.  
  496.       for i = 1, string.len(text) do    
  497.  
  498.         gpu.setBackground(background)
  499.         gpu.setForeground(foreground)
  500.         field[tx-x+i][ty-y].symbol = string.sub(text, i, i)
  501.         field[tx-x+i][ty-y].background = background
  502.         field[tx-x+i][ty-y].foreground = foreground
  503.         gpu.set(x + tx + i, ty, string.sub(text, i, i))
  504.  
  505.       end      
  506.  
  507.       return
  508.  
  509.     end
  510.  
  511.   until false
  512.  
  513. end
  514.  
  515. gpu.setBackground(0x323232)
  516. term.clear()
  517. clear()
  518.  
  519. if ... and fs.exists(...) then
  520.  
  521.   local image = drawing.load(...)
  522.  
  523.   field, width, height = image.field, image.height, image.width
  524.  
  525. end
  526.  
  527. draw()
  528. pull()
  529. gpu.setBackground(0x000000)
  530. gpu.setForeground(0xffffff)
  531. term.clear()
Add Comment
Please, Sign In to add comment