NMDanny

Energy Cell Management - Master Computer polished

Apr 15th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | None | 0 0
  1. local chan=39701
  2. local monitor=peripheral.wrap("monitor_6")
  3. local modem=peripheral.wrap("left")
  4. modem.open(chan)
  5. local function Button(
  6.                                 width,
  7.                                 height,
  8.                                 label,
  9.                                 backgroundColorNormal,                                
  10.                                 backgroundColorPressed,
  11.                                 textColorNormal,
  12.                                 textColorPressed,
  13.                                 hasBorder,
  14.                                 borderColorNormal,
  15.                                 borderColorPressed,
  16.                                 startColumn,
  17.                                 startRow,
  18.                                 isPressed,
  19.                                 defaultBackgroundColor,
  20.                                 defaultTextColor
  21.                             )
  22.     local button = {}
  23.     button.height=height or 1
  24.     button.width=width or 1
  25.     button.label=label or ""
  26.     button.backgroundColorNormal=backgroundColorNormal or colors.black
  27.     button.backgroundColorPressed=backgroundColorPressed or colors.white
  28.     button.textColorNormal=textColorNormal or colors.white
  29.     button.textColorPressed=textColorPressed or colors.black
  30.     button.hasBorder = hasBorder or false
  31.     button.borderColorNormal = borderColorNormal or backGroundColorNormal
  32.     button.borderColorPressed = borderColorPressed or backGroundColorPressed
  33.     button.defaultBackgroundColor = defaultBackgroundColor or colors.black
  34.     button.defaultTextColor = defaultTextColor or colors.white
  35.     button.startColumn = startColumn or 1
  36.     button.startRow = startRow or 1
  37.     button.isPressed=isPressed or false
  38.     function button.press()
  39.         button.isPressed = not button.isPressed
  40.     end
  41.    
  42.     function button.draw(display,isPressed,startColumn,startRow)
  43.                            
  44.         button.startColumn = startColumn or button.startColumn
  45.         button.startRow = startRow or button.startRow
  46.         display = display or term
  47.         if isPressed == false or isPressed then
  48.             button.isPressed = isPressed
  49.         else isPressed = button.isPressed
  50.         end
  51.         local width = button.width
  52.         local height = button.height
  53.         startRow = button.startRow
  54.         startColumn = button.startColumn
  55.        
  56.         local label = button.label
  57.         local labelPad = 2
  58.        
  59.         -- set border params and draw border if hasBorder
  60.         if button.hasBorder == true then
  61.             -- button must be at least 3x3, if not, make it so
  62.             if width < 3 then
  63.                 width = 3
  64.             end
  65.             if height < 3 then
  66.                 height = 3
  67.             end
  68.            
  69.             -- set border colors
  70.             if not isPressed then
  71.                 if not display.isColor() then
  72.                     display.setBackgroundColor(colors.white)
  73.                 else
  74.                     display.setBackgroundColor(button.borderColorNormal)
  75.                 end
  76.             else
  77.                 if not display.isColor() then
  78.                     display.setBackgroundColor(colors.white)
  79.                 else
  80.                     display.setBackgroundColor(button.borderColorPressed)
  81.                 end
  82.             end
  83.            
  84.             -- draw button border (inset)
  85.             display.setCursorPos(startColumn,startRow)
  86.             display.write(string.rep(" ",width))
  87.             for row = 2,height-1 do
  88.                 display.setCursorPos(startColumn,button.startRow+row -1)
  89.                 display.write(" ")
  90.                 display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  91.                 display.write(" ")
  92.             end
  93.             display.setCursorPos(startColumn,startRow+height-1)
  94.             display.write(string.rep(" ",width))
  95.            
  96.             -- reset startColumn,startRow,width,column to inset button and label
  97.             startColumn=startColumn+1
  98.             startRow = startRow +1
  99.             width = width - 2
  100.             height = height - 2
  101.         end
  102.        
  103.         --set button background and text colors
  104.         if not isPressed then
  105.             if not display.isColor() then
  106.                 display.setBackgroundColor(colors.black)
  107.                 display.setTextColor(colors.white)
  108.             else
  109.                 display.setBackgroundColor(button.backgroundColorNormal)
  110.                 display.setTextColor(button.textColorNormal)
  111.             end
  112.         else
  113.             if not display.isColor() then
  114.                 display.setBackgroundColor(colors.white)
  115.                 display.setTextColor(colors.black)
  116.             else
  117.                 display.setBackgroundColor(button.backgroundColorPressed)
  118.                 display.setTextColor(button.textColorPressed)
  119.             end
  120.         end
  121.        
  122.         -- draw button background (will be inside border if there is one)
  123.         for row = 1,height do
  124.             --print(tostring(startColumn)..","..tostring(startRow-row))
  125.             display.setCursorPos(startColumn,startRow + row -1)
  126.             display.write(string.rep(" ",width))
  127.         end
  128.        
  129.         -- prepare label, truncate label if necessary
  130.        
  131.         -- prepare label, truncate label if necessary
  132.         if width < 3 then
  133.             labelPad = 0
  134.         end
  135.         if #label > width - labelPad then
  136.             label = label:sub(1,width - labelPad)
  137.         end
  138.        
  139.         -- draw label
  140.         display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  141.         display.write(label)
  142.         display.setBackgroundColor(button.defaultBackgroundColor)
  143.         display.setTextColor(button.defaultTextColor)
  144.     end
  145.     button.toggle = function ()
  146.             button.isPressed = not button.isPressed
  147.             return button.isPressed
  148.     end
  149.         function button.clicked(column,row)
  150.                 return (column >= button.startColumn and column < button.startColumn + button.width and row >= button.startRow and row < button.startRow + button.height)
  151.         end    
  152.     return button
  153. end
  154.  
  155.      -- Quit button
  156.         local quit=Button(12,7,"Quit")
  157.         quit.startRow=3
  158.         quit.startColumn=38
  159.         quit.backgroundColorNormal=colors.red
  160.         quit.backgroundColorPressed=colors.white
  161.         quit.hasBorder=true
  162.         quit.borderColorNormal=colors.white
  163.         quit.borderColorPressed=colors.red
  164.         quit.textColorNormal=colors.white
  165.         quit.textColorPressed=colors.red
  166.         quit.draw(monitor)
  167.        
  168.     -- Setting up variables
  169.        local totalData={0,0,0}
  170.        local writeFormat={[1]={"Maximum Energy Stored: "," RF"},[2]={"Energy Stored: "," RF"},[3]={"Energy Percent: ","%"}} -- each record represents a statistic, each statistic includes a label and a unit
  171.        local reporters={}
  172.        local width,height=monitor.getSize()
  173.        
  174.    
  175.     -- Preparing the monitor for first use 
  176.         monitor.setBackgroundColor(colors.black)
  177.         monitor.setTextColor(colors.white)
  178.         monitor.setTextScale(.5)
  179.         monitor.clear()
  180.    
  181.     -- Typing the monitor's title and colors
  182.        
  183.         monitor.setBackgroundColor(colors.purple)
  184.         monitor.setTextColor(colors.yellow)
  185.         monitor.setCursorPos(1,1)
  186.         monitor.write("Energy Cell Management"..string.rep(" ",width-#"Energy Cell Management"))
  187.        
  188.      -- Resetting the colors
  189.         monitor.setBackgroundColor(colors.black)
  190.         monitor.setTextColor(colors.white)
  191.        
  192.        
  193.        
  194.  
  195. while true do
  196.  
  197.         local totalData={0,0,0}
  198.         local event={os.pullEvent()}
  199.         if event[1]=="monitor_touch" then
  200.                 if quit.clicked(event[3],event[4])==true then
  201.                         monitor.clear()
  202.                         break
  203.                 end
  204.         elseif event[1]=="key" and event[2]==keys.q then
  205.                 monitor.clear()
  206.                 break
  207.         elseif event[1]=="modem_message" and event[3]==chan then
  208.                 local data={}
  209.                 print(event[5]) -- debug purposes
  210.                 for word in string.gmatch(event[5],"[^%s]+") do -- splitting the string into usable data (3 slots)
  211.                         table.insert(data,word)
  212.                 end
  213.                 reporters[data[1]]={[1]=data[2],[2]=data[3],[3]=data[4]} -- data[1] is the reporter's label(and key amongst the reporetrs table), data[2] is energy stored, data[3] is max energy, data[4] is energy percent
  214.         end
  215.        
  216.         local maxLength=#writeFormat[1][1]
  217.         local currentRow=3
  218.         for key,value in pairs(reporters) do -- going over all reporters (at a random order)
  219.                 monitor.setCursorPos(1,currentRow)
  220.                 monitor.write(key..": ")
  221.                 currentRow=currentRow+1
  222.                 for key2,value2 in ipairs(reporters[key]) do -- printing data for each reporter(at a specific order)
  223.                         monitor.setCursorPos(3,currentRow)
  224.                         monitor.write(string.rep(" ",maxLength-#writeFormat[key2][1])..writeFormat[key2][1]..value2..writeFormat[key2][2])
  225.                         totalData[key2]=totalData[key2]+value2
  226.                         currentRow=currentRow+1
  227.                 end
  228.                 currentRow=currentRow+1
  229.         end
  230.        
  231.         -- Calculating and printing total statistics
  232.         totalData[3]=math.floor(100*(totalData[2]/totalData[1])) -- total energy percent
  233.         monitor.setCursorPos(1,currentRow)
  234.         monitor.write("Total Energy Grid Information: ")
  235.         currentRow=currentRow+1
  236.         for i=1,3 do -- going over each statistic
  237.                 monitor.setCursorPos(3,currentRow)
  238.                 monitor.write(string.rep(" ",maxLength-#writeFormat[i][1])..writeFormat[i][1]..totalData[i]..writeFormat[i][2])
  239.                 currentRow=currentRow+1
  240.         end
  241. end
Advertisement
Add Comment
Please, Sign In to add comment