Advertisement
HangMan23

paint2.lua

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