Advertisement
MeXaN1cK

guiEditor

Aug 24th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.94 KB | None | 0 0
  1. --==[Libraries]==--
  2. local cmp = require("component")
  3. local m = cmp.modem
  4. local event = require("event")
  5. local serialization = require("serialization")
  6. local unicode=require("unicode")
  7. local filesystem = require("filesystem")
  8. local shell = require("shell")
  9. local term = require("term")
  10. local unicode=require('unicode')
  11. local computer = require("computer")
  12. local gpu = cmp.gpu
  13. local draw = require("draw")
  14. --local thread = require("thread")
  15. local keyboard = require("keyboard")
  16. a,b=gpu.getResolution()
  17. --==[Locals]==--
  18. symb=" "
  19. local back =0
  20. local fore =0xFFFFFF
  21. -------
  22. gpu.setBackground(0xffffff)
  23. term.clear()
  24. --==[Other functions]==--
  25. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  26. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  27.  
  28. function f()
  29. arr={}
  30. for j=1,a-21 do
  31.   for i=1,b do
  32.     symb1,fore,back=gpu.get(j,i)
  33.     if symb1 ~= '░' then
  34.       arr[#arr+1]={j,i,symb1,fore,back}
  35.     end
  36.   end
  37. end
  38. return arr
  39. end
  40.  
  41. function exit(x,y)
  42. gpu.setBackground(0xff0000)
  43. gpu.fill(a,1,1,1," ")
  44. gpu.setForeground(0xFFFFFF)
  45. gpu.set(a,1,"X")
  46. if x==a and y==1 then
  47. gpu.setBackground(0)
  48. arr=f()
  49. saveTbl(arr,picFile)
  50. term.clear()
  51. os.exit()
  52. end
  53. end
  54. --==[Graphics]==--
  55. function drawField()
  56. gpu.setBackground(0)
  57. gpu.setForeground(0)
  58. drawWeb()
  59. gpu.setBackground(0xffffff)
  60. gpu.fill(a-20,1,21,b," ")
  61. gpu.setBackground(back)
  62. gpu.fill(a-15,3,7,3," ")
  63. draw.field(a-16,b-5,12,4,' ',0x505050,0x505050)
  64. draw.clound('┌','┐','└','┘','│','─',a-16,b-5,12,4,0x505050,0xFFFFFF)
  65. draw.text(a-16,b-5,12,'Авторы:',0x505050,0xFFFFFF)
  66. draw.text(a-15,b-4,10,'MeXaN1cK',0x505050,0xFFFFFF)
  67. draw.text(a-15,b-3,10,'PandaDoddo',0x505050,0xFFFFFF)
  68. end
  69.  
  70. function drawWeb()
  71.   gpu.setForeground(0xffffff)
  72.   gpu.setBackground(0xffffff)
  73.   gpu.fill(1,1,a-20,b,'░')
  74.   gpu.setForeground(0xAAAAAA)
  75.   gpu.setBackground(0xAAAAAA)
  76. for j=1,b do
  77.  for i=1,a-20 do
  78.    if math.ceil(j/2) > math.floor(j/2) then
  79.      if math.ceil(i/2) > math.floor(i/2) then
  80.        gpu.set(i,j,'░')
  81.      end
  82.    else
  83.      if math.ceil(i/2) == math.floor(i/2) then
  84.        gpu.set(i,j,'░')
  85.      end
  86.    end
  87.  end
  88. end
  89. end
  90.  
  91. function pr()
  92. for i=1,#arr do
  93. gpu.setBackground(arr[i][5])  
  94. gpu.setForeground(arr[i][4])
  95. gpu.set(arr[i][1],arr[i][2],arr[i][3])
  96. end
  97. end
  98.  
  99. function drawPixel(key,x,y,mB)
  100.   if key=="drag" or key=="drop" or key=="touch" then
  101.     if x>0 and x<a-20 and y>0 and y<=b then
  102.       if mB == 0 then
  103.         gpu.set(x,y,symb)
  104.       elseif mB ==1 then
  105.         gpu.setForeground(0xAAAAAA)
  106.         gpu.setBackground(0xAAAAAA)
  107.         if math.ceil(y/2) > math.floor(y/2) then
  108.           if math.ceil(x/2) > math.floor(x/2) then
  109.             gpu.set(x,y,'░')
  110.           else
  111.            gpu.setForeground(0xffffff)
  112.            gpu.setBackground(0xffffff)
  113.            gpu.set(x,y,"░")
  114.           end
  115.         elseif math.ceil(y/2) == math.floor(y/2) then
  116.           if math.ceil(x/2) == math.floor(x/2) then
  117.             gpu.set(x,y,'░')
  118.        
  119.           else
  120.           gpu.setForeground(0xffffff)
  121.           gpu.setBackground(0xffffff)
  122.           gpu.set(x,y,'░')
  123.         end
  124.        end
  125.       end
  126.     end
  127.   end
  128. end
  129.  
  130. function selCol(x,y)
  131. gpu.setForeground(0xFFFFFF)
  132. gpu.setBackground(0)
  133. gpu.fill(a-15,8,7,1," ")
  134. gpu.set(a-15,8,"Палитра")
  135. if x>=a-15 and x<=a-8 and y>=8 and y<=8 then
  136. arr=f()
  137. back=draw.colorSelector("Палитра для выбора цвета")
  138. drawField()
  139. pr()
  140. end
  141. end
  142.  
  143. function enter(x,y,w,h,color_back,color_fore)
  144.   got=nil
  145.   function drawTextInPass(x,y,w,h,txt,col)
  146.     if unicode.len(txt) > w-3 then
  147.       txt=unicode.sub(txt,unicode.len(txt)-(w-3),unicode.len(txt))
  148.     end
  149.     draw.field(x+1,y+1,w-2,1,' ',color_back,color_fore)
  150.     oldB=gpu.getBackground()
  151.     oldF=gpu.getForeground()
  152.   gpu.setBackground(color_back)
  153.   gpu.setForeground(col)
  154.   gpu.set(x+1,y+1,txt)
  155.   gpu.setBackground(oldB)
  156.   gpu.setForeground(oldF)
  157.   end
  158.   draw.field(x,y,w,h,' ',color_back,color_fore)
  159.   draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,0xAA0000)
  160.   local function getKey(s)
  161.     local key, _, x1 , code, _ = event.pull()
  162.   if key == 'key_up' or key == 'touch' then
  163.     return code , x1 , key
  164.   end
  165.   end
  166.   local function enterPass(x,y,w,h)
  167.   try = ""
  168.   ex=true
  169.   while ex do
  170.     local err, f , x1 , key  = pcall(getKey)
  171.     if not err then
  172.       return err, try
  173.     end
  174.   if f ~= nil and key == 'key_up' then --
  175.     if keyboard.keys[f] == 'grave' then ex=false end
  176.     if f < 60 and f > 0 then
  177.       if keyboard.keys[f] == 'enter' then
  178.             draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  179.             return err, try , 's'
  180.     end
  181.       if keyboard.keys[f] == 'back' then
  182.         if unicode.len(try) > 0 then
  183.           try=unicode.sub(try,0,unicode.len(try)-1)
  184.       drawTextInPass(x,y,w,h,try,color_fore)
  185.     else
  186.       try = nil
  187.       draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  188.       return err , try , 'f'
  189.         end
  190.       end
  191.       if unicode.len(keyboard.keys[f]) == 1 then
  192.         try = try..keyboard.keys[f]
  193.       drawTextInPass(x,y,w,h,try,color_fore)
  194.     elseif keyboard.keys[f] == 'space' then
  195.       try = try..'_'
  196. drawTextInPass(x,y,w,h,try,color_fore)
  197.     elseif keyboard.keys[f] == 'minus' then
  198.       try = try..'-'
  199. drawTextInPass(x,y,w,h,try,color_fore)
  200.     elseif keyboard.keys[f] == 'period' then
  201.     try = try..'.'
  202. drawTextInPass(x,y,w,h,try,color_fore)
  203.   elseif keyboard.keys[f] == 'comma' then
  204.     try = try..','
  205. drawTextInPass(x,y,w,h,try,color_fore)
  206.   elseif keyboard.keys[f] == 'slash' then
  207.     try = try..'/'
  208. drawTextInPass(x,y,w,h,try,color_fore)
  209.       end
  210.   end
  211.   elseif key == 'touch' and f ~= nil then
  212.       if x1 > x-1 and x1 < x+w and f > y-1 and f < y+h then
  213.       --
  214.       else
  215.           draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  216.           return err, try , 's'
  217.       end
  218.     end
  219.     end
  220.   end --
  221.   while true do
  222.     err, try , got = enterPass(x,y,w,h)
  223.     if got == 's' then -- success
  224.       break
  225.   elseif got == 'f' then
  226.     try = nil
  227.     break
  228.     end
  229.   end
  230.   return try
  231. end
  232.  
  233. local function drawButtonAddMenu(arr,i)
  234.     draw.field(arr[i][1],arr[i][2],arr[i][3],1,'▄',arr[i][4],arr[i][5])
  235.     draw.field(arr[i][1],arr[i][2]+1,arr[i][3],1,' ',arr[i][5],arr[i][5])
  236.     draw.field(arr[i][1],arr[i][2]+2,arr[i][3],1,'▄',arr[i][5],arr[i][4])
  237. end
  238.  
  239. function drawFrame(txt)
  240.   arrDrawFrame={
  241.   {25,12,12,0xAAAAAA,0xBBBB00,0,'Отмена'},
  242.   {43,12,12,0xAAAAAA,0xBBBB00,0,'Готово'}
  243.   }
  244.   draw.field(20,8,40,8,' ',0xAAAAAA,0xFFFFFF) -- frame
  245.   draw.clound('┌','┐','└','┘','│','─',20,8,40,8,0xAAAAAA,0)
  246.   draw.text(20,8,38,txt,0xAAAAAA,0) -- /frame
  247.   draw.field(25,9,30,3,' ',0xAAAAAA,0xFFFFFF) -- nameFrame
  248.   draw.clound('┌','┐','└','┘','│','─',25,9,30,3,0xAAAAAA,0) -- nameFrame
  249.   for i=1,#arrDrawFrame do
  250.       drawButtonAddMenu(arrDrawFrame,i)
  251.     draw.text(arrDrawFrame[i][1],arrDrawFrame[i][2]+1,arrDrawFrame[i][3],arrDrawFrame[i][7],arrDrawFrame[i][5],arrDrawFrame[i][6])
  252.   end
  253.   while true do
  254.     local key,_,x,y,_,_ = event.pull('touch')
  255.     if x > 20 and x < 40 and y >8 and y < 12 then
  256.     name=enter(25,9,30,3,0xAAAAAA,0)
  257.   elseif x > 24 and x < 36 and y > 12 and y < 16 then
  258.     name = nil
  259.     os.exit()
  260.   elseif x > 42 and x < 55 and y > 12 and y < 16 then
  261.     if name ~= nil and unicode.len(name) > 0 and name ~= '' then
  262.       break
  263.       end
  264.   end
  265.   end
  266.   return name
  267. end
  268.  
  269. function selColText(x,y)
  270. gpu.setBackground(0)
  271. gpu.setForeground(0xFFFFFF)
  272. gpu.fill(a-18,12,18,1," ")
  273. gpu.set(a-18,12,"Палитра для текста")
  274. if x>=a-18 and x<=a-2 and y>=12 and y<=12 then
  275. arr=f()
  276. fore=draw.colorSelector("Палитра для выбора цвета текста")
  277. drawField()
  278. pr()
  279. end
  280. end
  281.  
  282. function selSymb()
  283. gpu.setForeground(0xFFFFFF)
  284. gpu.setBackground(0)
  285. gpu.fill(a-17,10,15,1," ")
  286. gpu.set(a-16,10,"Выбор символа")
  287. if x>=a-17 and x<=a-3 and y>=10 and y<=10 then
  288. arr=f()
  289. symb=drawFrame('[Выбор символа]')
  290. if symb == nil then symb = ' ' end
  291. drawField()
  292. pr()
  293. end
  294. end
  295. --==[Events]==--
  296. function getEvent()
  297. key,_,x,y,mB= event.pull()
  298. return key,x,y,mB
  299. end
  300. --==[Main Cycle]==--
  301. picFile=drawFrame('[Редактирование изображения]')
  302.  
  303. if unicode.sub(picFile,unicode.len(picFile)-3,unicode.len(picFile)) ~= '.pic' then
  304. picFile=picFile..'.pic'
  305. elseif picFile == nil then
  306. os.exit()
  307. end
  308.  
  309. if filesystem.exists(picFile) == true then
  310. arr=loadTbl(picFile)
  311. else
  312. arr={}
  313. end
  314. getEvent()
  315. drawField()
  316. selSymb()
  317. selCol(x,y)
  318. selColText(x,y)
  319. exit(x,y)
  320. pr()
  321. while true do
  322. gpu.setBackground(back)
  323. gpu.setForeground(fore)
  324. drawPixel(key,x,y,mB)
  325. getEvent()
  326. selSymb()
  327. selCol(x,y)
  328. selColText(x,y)
  329. exit(x,y)
  330. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement