Clem105

Panel

Apr 6th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. modem = peripheral.wrap("bottom")
  2. modem.open(105)
  3. modem.open(6105)
  4.  
  5. PagePlus = function()
  6.     page = page + 1
  7.     if page>LastPage then
  8.         page = LastPage
  9.     end
  10. end
  11.    
  12. PageMoins = function()
  13.     page = page - 1
  14.     if page<1 then
  15.         page = 1
  16.     end
  17. end
  18.  
  19.  
  20. function Panel()
  21.  
  22.    
  23.  
  24.     os.loadAPI("touch")
  25.     monitor = peripheral.wrap("top")
  26.     xMin = {27,3,15,}
  27.     xMax = {37,13,25,}
  28.     yMin = {2,6,10,14,18}
  29.     yMax = {4,8,12,16,20}
  30.     local h = fs.open("Liste","r")
  31.     temp = h.readAll()
  32.     modem.transmit(105,105,temp)
  33.     tableau = textutils.unserialize(temp)
  34.     h.close()
  35.  
  36.     lastNumber = 0
  37.     page = 1
  38.     screen = {}
  39.  
  40.     for i=1,(math.ceil(#tableau/15)) do
  41.         screen[i] = touch.new("top")
  42.     end
  43.  
  44.     LastPage = #screen
  45.  
  46.     if LastPage ~= 1  then
  47.         for a=1,(LastPage-1) do
  48.             for b=1,15 do
  49.                 xMinCord = xMin[b%3+1]
  50.                 xMaxCord = xMax[b%3+1]
  51.                 yMinCord = yMin[math.ceil(b/3)]
  52.                 yMaxCord = yMax[math.ceil(b/3)]
  53.                 screen[a]:add(tableau[(a-1)*15+b],nil,xMinCord,yMinCord,xMaxCord,yMaxCord,colors.red,colors.lime)
  54.             end
  55.             screen[a]:add("<<Page",function()  PageMoins() end,6,23,17,25,colors.red,colors.lime)
  56.             screen[a]:add("Page>>",function()  PagePlus() end,22,23,33,25,colors.red,colors.lime)
  57.         end
  58.     end
  59.  
  60.     o = #tableau%15
  61.    
  62.     lastNumber = o%3
  63.     screen[LastPage]:add("<<Page",function()  PageMoins() end,6,23,17,25,colors.red,colors.lime)
  64.     screen[LastPage]:add("Page>>",function()  PagePlus() end,22,23,33,25,colors.red,colors.lime)
  65.     if o>2 then
  66.         for i=1,(o-lastNumber) do
  67.             xMinCord = xMin[i%3+1]
  68.             xMaxCord = xMax[i%3+1]
  69.             yMinCord = yMin[math.ceil(i/3)]
  70.             yMaxCord = yMax[math.ceil(i/3)]
  71.             screen[LastPage]:add(tableau[(LastPage-1)*15+i],nil,xMinCord,yMinCord,xMaxCord,yMaxCord,colors.red,colors.lime)
  72.         end
  73.     end
  74.     if lastNumber == 1 then
  75.         screen[LastPage]:add(tableau[#tableau],nil,15,yMin[math.ceil(o/3)],25,yMax[math.ceil(o/3)],colors.red,colors.lime)
  76.     elseif lastNumber == 2 then
  77.         screen[LastPage]:add(tableau[#tableau-1],nil,9,yMin[math.ceil(o/3)],19,yMax[math.ceil(o/3)],colors.red,colors.lime)
  78.         screen[LastPage]:add(tableau[#tableau],nil,21,yMin[math.ceil(o/3)],31,yMax[math.ceil(o/3)],colors.red,colors.lime)
  79.     end
  80.     while true do
  81.         screen[page]:draw()
  82.         monitor.setCursorPos(15,22)
  83.         monitor.setBackgroundColor(colors.black)
  84.         monitor.write("Page "..page.." of "..LastPage)
  85.         local event, p1 = screen[page]:handleEvents(os.pullEvent())
  86.         if event == "button_click" then
  87.             if type(screen[page].buttonList[p1].func) == "function" then
  88.                 screen[page].buttonList[p1].func()
  89.             else
  90.                 screen[page]:flash(p1,0.5)
  91.                 modem.transmit(105,106,"open")
  92.                 modem.transmit(105,104,p1)
  93.             end
  94.         end
  95.     end
  96. end
  97.  
  98.  
  99. function Modem()
  100. local s = true
  101. local work = false
  102.     while true do
  103.         local event,side,channel,channelbis,msg = os.pullEvent("modem_message")
  104.         if channel == 6105 then
  105.             tab = textutils.unserialize(msg)
  106.             local n = fs.open("Liste","r")
  107.             texte = n.readAll()
  108.             t = textutils.unserialize(texte)
  109.             if tab[2] == "rm" then
  110.                 for j = 1,#t do
  111.                     if t[j] == tab[1] then
  112.                         table.remove(t,j)
  113.                         work = true
  114.                         modem.transmit(6105,105,"ok")
  115.                     end
  116.                 end
  117.                 if  work == false then
  118.                     modem.transmit(6105,105,"non")
  119.                 end
  120.             elseif tab[2] == "w" then
  121.                 for j = 1,#t do
  122.                     if t[j] == tab[1] then
  123.                         s = false
  124.                     end
  125.                 end
  126.                 if s then
  127.                     table.insert(t,tab[1])
  128.                 end
  129.             end
  130.             n.close()
  131.             fs.delete("Backup")
  132.             fs.copy("Liste","Backup")
  133.             fs.delete("Liste")
  134.             n = fs.open("Liste","w")
  135.             txt = textutils.serialize(t)
  136.             n.write(txt)
  137.             n.close()
  138.             os.reboot()
  139.         end
  140.     end
  141. end
  142.        
  143.        
  144.        
  145. parallel.waitForAll(Panel,Modem)
Add Comment
Please, Sign In to add comment