Advertisement
AdslHouba

Interrupteur à écran (COMPUTERCRAFT)

Jun 3rd, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. -- Explication https://www.youtube.com/watch?v=xWemE5cG0e4
  2.  
  3. term=peripheral.find('monitor')
  4. term.setBackgroundColor(colors.black)
  5. term.clear()
  6.  
  7. premierX=2 -- Premier X
  8. premierY=1 -- Premier Y
  9. largeur=9  -- Largeur bouton
  10. hauteur=5  -- Hauteur bouton
  11. ligneY=3 -- hauteur du texte
  12. marge=1    -- Marge entre deux boutons
  13. coteRed="right"
  14.  
  15.  
  16. liste={
  17.     {
  18.         nom='Sapin',
  19.         color=colors.black,
  20.         choix=true
  21.     },
  22.     {
  23.         nom='Noir',
  24.         color=colors.pink,
  25.         choix=true
  26.     },
  27.     {
  28.         nom='Acajou',
  29.         color=colors.green,
  30.         choix=true
  31.     },
  32.     {
  33.         nom='Bouleau',
  34.         color=colors.brown,
  35.         choix=true
  36.     }, 
  37.     {
  38.         nom='Accacia',
  39.         color=colors.blue,
  40.         choix=true
  41.     }, 
  42.     {
  43.         nom='Chene',
  44.         color=colors.purple,
  45.         choix=true
  46.     },
  47.     {
  48.         nom='Arcanique',
  49.         color=colors.cyan,
  50.         choix=true
  51.     },
  52. }
  53.  
  54. function actuWin(i)
  55.     if liste[i].choix then
  56.         liste[i].win.setBackgroundColor(colors.green)  
  57.     else
  58.         liste[i].win.setBackgroundColor(colors.red)
  59.     end
  60.     liste[i].win.clear()
  61.     liste[i].win.setCursorPos(1+math.floor((largeur-string.len(liste[i].nom))/2),ligneY)
  62.     liste[i].win.write(liste[i].nom)
  63. end
  64. function actuRed()
  65.     redsTous=0 
  66.     table.foreach(liste,function(i,data)
  67.         if liste[i].choix==false then
  68.             redsTous=redsTous+liste[i].color
  69.         end
  70.     end)
  71.     redstone.setBundledOutput(coteRed,redsTous)
  72. end
  73. table.foreach(liste,function(i,data)
  74.     file=fs.open(tostring(i),"r")
  75.     if file~=nil then
  76.         if file.readLine()=='true' then
  77.             liste[i].choix=true
  78.         else
  79.             liste[i].choix=false
  80.         end
  81.         file.close()
  82.     else
  83.         file=fs.open(tostring(i),"w")
  84.         file.write(liste[i].choix)
  85.         file.close()
  86.     end
  87.     liste[i].win=window.create(term,premierX+((i-1)*(largeur+marge)),premierY,largeur,hauteur,true)
  88.     actuWin(i)
  89. end)
  90. actuRed()
  91.  
  92. while true do
  93.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  94.     if (xPos-premierX)%(largeur+marge)<largeur then
  95.         u=math.floor((xPos-premierX)/(largeur+marge))+1
  96.         if liste[u] then
  97.             if liste[u].choix then
  98.                 liste[u].choix=false
  99.             else
  100.                 liste[u].choix=true
  101.             end
  102.             file=fs.open(tostring(u),"w")
  103.             file.write(liste[u].choix)
  104.             file.close()
  105.             actuWin(u)
  106.             actuRed()
  107.         end
  108.     end
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement