Asioron

reactor_desk

Jan 31st, 2021
806
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require('component')
  2. local event = require("event")
  3. local term = require('term')
  4. local srl = require('serialization')
  5. local fl = require('filesystem')
  6. local un = require('unicode')
  7. local mod = c.modem
  8. local inv = c.invoke
  9. local gpu = c.gpu
  10. local W, H = gpu.getResolution()
  11. local b_color, f_color = gpu.getBackground(), gpu.getForeground()
  12.  
  13. local setting = {}
  14. local temp = {}
  15.  
  16. tButtons = {}
  17.  
  18. local function curPhrase(line) --вырезка фразы с разделителем
  19.   local tbl = {}
  20.   for part in line:gmatch("[^;]+") do
  21.     table.insert(tbl, part)
  22.   end
  23.   return tbl
  24. end
  25.  
  26. local function drawButton(n) -- функция рисования кнопки
  27.   gpu.setBackground(tButtons[n].color)
  28.   gpu.setForeground(tButtons[n].textColor)
  29.   gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ')
  30.   if tButtons[n].vertical then
  31.     gpu.set(tButtons[n].X+(tButtons[n].W/2), tButtons[n].Y+(tButtons[n].H/2)-(un.len(tButtons[n].text)/2), tButtons[n].text, true)
  32.   else
  33.     if tButtons[n].textR then
  34.       local tmp = curPhrase(tButtons[n].text)
  35.       for i = 1, #tmp do
  36.         if tmp[i] ~= '#' then
  37.           gpu.set(tButtons[n].X, tButtons[n].Y+i-1, tmp[i])
  38.         end
  39.       end
  40.     else
  41.       gpu.set(tButtons[n].X+(tButtons[n].W/2)-(un.len(tButtons[n].text)/2), tButtons[n].Y+(tButtons[n].H/2), tButtons[n].text)
  42.     end
  43.   end
  44. end
  45.  
  46. function toggleVisible(n) -- переключение видимости кнопки
  47.   if tButtons[n].visible then
  48.     tButtons[n].visible = false
  49.     gpu.setBackground(b_color)
  50.     gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ')
  51.   else
  52.     tButtons[n].visible = true
  53.     drawButton(n)
  54.   end
  55. end
  56.  
  57. local function blink(n) -- мигание кнопки
  58.   local temp = ''
  59.   for i=tButtons[n].X, tButtons[n].W+tButtons[n].X-1 do
  60.     temp = temp..gpu.get(i,tButtons[n].Y)
  61.   end
  62.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color
  63.   drawButton(n)
  64.   os.sleep(0.09)
  65.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color
  66.   drawButton(n)
  67.   gpu.set(tButtons[n].X,tButtons[n].Y,temp)
  68.   gpu.setBackground(b_color)
  69.   gpu.setForeground(f_color)
  70. end
  71.  
  72. function activBottom() --активатор кнопок
  73.   for i = 1, #tButtons do
  74.     toggleVisible(i)
  75.   end
  76.   gpu.setBackground(b_color)
  77.   gpu.setForeground(f_color)
  78. end
  79.  
  80. local function bottomInstall(tbl)
  81.   tButtons = {}
  82.   local posX, j, k = 0, 1, 0
  83.   local tmp = {}
  84.   local vr = ''
  85.   local resX, _ = math.modf(#tbl/5)+1
  86.   local resY = 0
  87.   if #tbl < 4 then
  88.     resY = 7*#tbl
  89.   else
  90.     resY = 25
  91.   end
  92.   gpu.setResolution(14*resX+1, resY)
  93.   for i = 1, #tbl do
  94.     vr = ''
  95.     tmp[i] = {['text'] = 0, ['color'] = 0}
  96.     if i < 10 then
  97.       vr = vr..'#; Reactor: '..i..';'
  98.     else
  99.       vr = vr..'#; Reactor:'..i..';'
  100.     end
  101.     if tbl[i].Act then
  102.       tmp[i].color = 0x00ff00
  103.     else
  104.       tmp[i].color = 0xff0000
  105.     end
  106.     vr = vr..' Heat: '..tbl[i].Heat..'%;'
  107.     vr = vr..' EU/t:'..tbl[i].Out..';'
  108.     tmp[i].text = vr
  109.   end
  110.   for i = 1, #tbl do
  111.     if k+1 == 5 then
  112.       j = j + 1
  113.       posX = posX + 12
  114.       k = 1
  115.     else
  116.       k = k + 1
  117.     end
  118.     tButtons[#tButtons+1] = { visible = false, X = 2*j+posX, Y = ((k-1)*6)+2, W = 12, H = 5, color = tmp[i].color, textColor = 0xffffff, textR = true, text = tmp[i].text,
  119.       action = function()
  120.         mod.broadcast(setting.port, 'ON_OFF', i)
  121.       end}
  122.   end
  123.   activBottom()
  124. end
  125.  
  126. local function readSettingBase()
  127.   setting = {}
  128.   print('Загрузка базы реакторов')
  129.   local f = io.open('/etc/reactor/setting.cfg', 'r')
  130.   if f then
  131.     setting = srl.unserialize(f:read())
  132.     f:close()
  133.     print('Загрузка завершена')
  134.   else
  135.     fl.makeDirectory('/etc/reactor')
  136.     print('Укажите номер диапазона для дистанционной связи')
  137.     setting.port = tonumber(io.read())
  138.     local f = io.open('/etc/reactor/setting.cfg', 'w')
  139.     f:write(srl.serialize(setting))
  140.     f:close()
  141.   end
  142. end
  143.  
  144. readSettingBase()
  145. mod.open(setting.port)
  146. term.clear()
  147.  
  148. while true do
  149.   tmp = {event.pullMultiple('modem_message', 'touch')}
  150.   if tmp[1] == 'modem_message' then
  151.     if tmp[6] == 'INFO' then
  152.       bottomInstall(srl.unserialize(tmp[7]))
  153.     end
  154.   else
  155.     for i = 1, #tButtons do
  156.       if tButtons[i].visible then
  157.         if tmp[3] >= tButtons[i].X and tmp[3] <= tButtons[i].X+tButtons[i].W-1 and tmp[4] >= tButtons[i].Y and tmp[4] <= tButtons[i].Y+tButtons[i].H-1 then
  158.           blink(i)
  159.           tButtons[i].action()
  160.           break
  161.         end
  162.       end
  163.     end
  164.   end
  165. end
  166.  
RAW Paste Data