Advertisement
joebodo

panel.lua

Apr 17th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. vos.loadApi('core.api')
  2. vos.loadApi('ui.api')
  3. vos.loadApi('relay.api')
  4.  
  5. Relay.find()
  6. Relay.register('panelUpdate')
  7.  
  8. Logger.filter('modem_receive', 'event')
  9. --Message.enableWirelessLogging()
  10.  
  11. local textRows = 6
  12. local radioRows = 2
  13. local meterRows = 2
  14. local col = 2
  15. local dirty = { }
  16.  
  17. local window = UI.term
  18.  
  19. if Peripheral.isPresent('openperipheral_glassesbridge') then
  20.   window = UI.Window({ parent =
  21.     UI.Glasses({
  22.       height = 37,
  23.       width = 57,
  24.       textScale = .5,
  25.       backgroundOpacity = .75
  26.     })
  27.   })
  28. elseif Peripheral.isPresent('monitor') then
  29.   window = UI.Device({
  30.     deviceType = 'monitor',
  31. --    backgroundColor = colors.gray,
  32.     textScale = .5,
  33.   })
  34. end
  35.  
  36. UI.CenteredText = class.class(UI.Window)
  37.  
  38. function UI.CenteredText:draw()
  39.   local y = 1
  40.   self:clear()
  41.   for w in self.value:gmatch("%S+") do
  42.     self:centeredWrite(y, w)
  43.     y = y + 1
  44.   end
  45. end
  46.  
  47. window:add({
  48.   messages = UI.ScrollingText({
  49.     backgroundColor = colors.gray,
  50.     x = 2,
  51.     width = window.width - 2,
  52.     y = 12,
  53.     height = 13,
  54.   }),
  55.   timeDisplay = UI.Text({
  56.     y = 10,
  57.     x = window.width - 8,
  58.     width = 8
  59.   })  
  60. })
  61.  
  62. window:draw()
  63. window.messages:clear()
  64.  
  65. Message.addHandler('panelUpdate', function(h, id, msg)
  66.   local s = msg.contents
  67.   local t = window[s.uid]
  68.   if s.type == 'text' then
  69.     if not t then
  70.       local x = 33
  71.       t = UI.Text({
  72.         x = x,
  73.         y = textRows,
  74.         width = 24,
  75.       })
  76.       textRows = textRows + 2
  77.     end
  78.     t.value = s.text
  79.     table.insert(dirty, t)
  80.  
  81.   elseif s.type == 'meter' then
  82.     if not t then
  83.       local x = 33
  84.       local label = UI.Text({
  85.         x = x,
  86.         y = meterRows,
  87.         value = s.text,
  88.         width = #s.text
  89.       })
  90.       t = UI.ProgressBar({
  91.         x = x + 9,
  92.         y = meterRows,
  93.         width = 10,
  94.         backgroundColor = colors.lightGray,
  95.       })
  96.       local text = UI.Text({
  97.         x = x + 20,
  98.         y = meterRows,
  99.         width = 4
  100.       })
  101.       table.insert(dirty, label)
  102.       window:add({
  103.         [ s.uid .. 'label' ] = label,
  104.         [ s.uid .. 'text'  ] = text
  105.       })
  106.       meterRows = meterRows + 2
  107.     end
  108.     t.progress = s.value
  109.     t.progressColor = s.color
  110.     window[s.uid .. 'text'].value = s.value .. '%'
  111.  
  112.     table.insert(dirty, window[s.uid .. 'text'])
  113.     table.insert(dirty, t)
  114.  
  115.   elseif s.type == 'radio' then
  116.     if not t then
  117.       local x = 2
  118.       local y = radioRows
  119.       if radioRows > 10 then
  120.         y = radioRows - 10
  121.         x = 18
  122.       end
  123.       local label = UI.Text({
  124.         x = x + 3,
  125.         y = y,
  126.         value = s.text,
  127.         width = #s.text
  128.       })
  129.       window:add({
  130.         [ s.uid .. 'label' ] = label,
  131.       })
  132.       table.insert(dirty, label)
  133.       t = UI.Window({
  134.         x = x,
  135.         y = y,
  136.         width = 2,
  137.         height = 1,
  138.       })
  139.       radioRows = radioRows + 2
  140.     end
  141.     if s.active then
  142.       t.backgroundColor = colors.yellow
  143.     else
  144.       t.backgroundColor = colors.gray
  145.     end
  146.     table.insert(dirty, t)
  147.   elseif s.type == 'message' then
  148.     s.uid = 'messages'
  149.     local time = textutils.formatTime(os.time(), false)
  150.     if type(s.message) == 'table' then
  151.       for _,message in ipairs(s.message) do
  152.         window.messages:write(' ' .. time .. ' ' .. message)
  153.       end
  154.     else
  155.       window.messages:write(' ' .. time .. ' ' .. s.message)
  156.     end
  157.     table.insert(dirty, window.messages)
  158.   elseif s.type == 'fluids' then
  159.     local tanks = s.tanks
  160.  
  161.     for _,tank in pairs(tanks) do
  162.       if not window[tank.name] then
  163.         window:add({
  164.           [ tank.name ] =
  165.           UI.VerticalMeter({
  166.             x = col + 3,
  167.             y = 28,
  168.             width = 4,
  169.             percent = 50,
  170.             height = 7,
  171.             backgroundColor = colors.gray,
  172.           }),
  173.         [ tank.name .. 'label' ] =
  174.           UI.CenteredText({
  175.             x = col,
  176.             y = 26,
  177.             width = 10,
  178.             height = 2,
  179.             value = tank.name,
  180.           }),
  181.           [ tank.name .. 'percent' ] =
  182.             UI.CenteredText({
  183.               x = col,
  184.               y = 36,
  185.               width = 10,
  186.               height = 2,
  187.               value = '',
  188.             }),
  189.         })
  190.         col = col + 9
  191.         table.insert(dirty, window[tank.name .. 'label'])
  192.       end
  193.       local percent = math.floor(tank.amount / tank.capacity * 100)
  194.       local color = colors.green
  195.       if percent < tank.low then
  196.         color = colors.red
  197.       elseif percent < tank.high then
  198.         color = colors.orange
  199.       end
  200.       window[tank.name].meterColor = color
  201.       window[tank.name].percent = percent
  202.       local value = window[tank.name].percent .. '%'
  203.       if tank.difference and tank.difference ~= 0 then
  204.         value = string.format('%s %+d', value, tank.difference)
  205.       end
  206.       window[tank.name .. 'percent' ].value = value
  207.       table.insert(dirty, window[tank.name])
  208.       table.insert(dirty, window[tank.name .. 'percent'])
  209.     end
  210.   end
  211.  
  212.   if s.uid and not window[s.uid] then
  213.     window:add({ [ s.uid ] = t })
  214.   end
  215. end)
  216.  
  217. Event.addHandler('heartbeat', function()
  218.   window.timeDisplay.value = textutils.formatTime(os.time(), false)
  219.   window.timeDisplay:draw()
  220.   for _,v in pairs(dirty) do
  221.     v:draw()
  222.   end
  223.   dirty = { }
  224. end)
  225.  
  226. Event.heartbeat(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement