Advertisement
MeXaN1cK

panel

Mar 29th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. local event = require('event')
  2. local term = require('term')
  3. local c = require('component')
  4. local gpu = c.gpu
  5. local serialization = require('serialization')
  6. local unicode=require('unicode')
  7. local m = c.tunnel
  8. buttons={}
  9. ------------{settings}-----------
  10. local exitWord ='Выход'
  11. local refreshWord='Обн.Кр.'
  12. color ={back=0x000000 ,button=123123 , ex=0xFF0000, err=0xFF0000,fore=0xFFFFFF,refresh=0xFFA500}
  13. x={1} y={2} w={1} h=3
  14. ----------------------------------
  15. oldFore=gpu.getForeground() oldBack=gpu.getBackground()
  16. A,B=gpu.getResolution() gpu.setBackground(color.back) amountButtons={}
  17. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  18. 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
  19. function check() ba1=loadTbl('bcr.txt') for i=1,#ba1 do buttons[i]=ba1[i] end  bf=loadTbl('but.txt') ab=#buttons if bf[1] == ab then else bf[1]=ab saveTbl(bf,'but.txt') reCalc() end saveTbl(bf,'but.txt') end
  20.  
  21. function buttonDraw()
  22. bf=loadTbl('but.txt')
  23. term.clear()
  24. for i=2,#buttons do
  25. nbt=buttons[i]
  26. nbt1=bf[nbt]
  27. nx=nbt1[1] ny=nbt1[2] nw=nbt1[3]
  28. gpu.setBackground(color.back)
  29. if buttons[i]==exitWord then gpu.setBackground(color.ex) elseif buttons[i]==refreshWord then gpu.setBackground(color.refresh) else gpu.setBackground(color.button) end
  30. gpu.fill(nx,ny,nw,3,' ') gpu.set(nx+1,ny+1,buttons[i])
  31. end
  32. gpu.setBackground(color.back)
  33. end
  34. function motionCatch()
  35. key,_,p1,p2,_,message=event.pull()
  36. if key=='modem_message' then
  37. a1=string.sub(message,1,3)
  38. b1=string.sub(message,4)
  39. if a1=='add' then
  40. buti=loadTbl('bcr.txt')
  41. buti[#buti+1]=b1
  42. saveTbl(buti,'bcr.txt')
  43. elseif a1=='del' then
  44. buti=loadTbl('bcr.txt')
  45. for i=2,#buti do
  46. if buti[i]==b1 then buti[i]=del end
  47. end
  48. saveTbl(buti,'bcr.txt')
  49. end
  50. else
  51. for i=1,#buttons do
  52. nbt=buttons[i]
  53. nbt1=bf[nbt]
  54. nx=nbt1[1] ny=nbt1[2] nw=nbt1[3]
  55. if p1 >nx-1 and p1<nx+nw+1 and p2 > ny-1 and p2<ny+h-1 then
  56. gpu.setBackground(color.back)
  57. term.clear()
  58. b=buttons[i]
  59. if b==exitWord then gpu.setBackground(oldBack) gpu.setForeground(oldFore) term.clear() os.exit() end
  60. if b==refreshWord then check() break end
  61. term.setCursor(10,5)
  62. io.write("Введите количество: ")
  63. a=io.read() a=tonumber(a) if a > 64 then a=64 term.setCursor(10,6) gpu.setForeground(color.err) io.write("Недопустимое число! [Больше 64:измененно на 64]") os.sleep(3)
  64. elseif a==0 then gpu.setForeground(color.err) io.write("Недопустимое число! [Равно нулю]") os.sleep(3)
  65. elseif a<0 then gpu.setForeground(color.err) io.write("Недопустимое число! [Отрицательное число]") os.sleep(3) end
  66. gpu.setForeground(color.fore)
  67. a=tostring(a)
  68. a=string.gsub(a,'\n','')
  69. a=tonumber(a)
  70. if a<10 then mes="0"..a..b else mes=a..b end
  71. if a~=0 and a>0 then m.send(mes) end
  72. end end end end
  73.  
  74.  
  75. function reCalc()
  76. bf=loadTbl('but.txt')
  77. bf={ab}
  78. saveTbl(bf,'but.txt')
  79. if x[2] ~= nil then for i=2,#buttons do x[i]=del y[i]=del w[i]=del end end
  80. for i=1,#buttons do
  81. bl=unicode.len(buttons[i])
  82. if i>1 then w[#w+1]=bl+2 else w[i]=bl+2 end
  83. if i>1 then x[#x+1]=x[i-1]+unicode.len(buttons[i-1])+4 y[i]=y[i-1] end
  84. if i>1 then if x[i]+w[i] > A then x[i]=x[1] y[i]=y[i-1]+4 end end
  85. bf1=buttons[i]
  86. bf[bf1]={x[i],y[i],w[i]}
  87. saveTbl(bf,'but.txt')
  88. end
  89. end
  90. check()
  91. while true do
  92. buttonDraw()
  93. motionCatch()
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement