SuperMcBrent

MainProgram

Dec 9th, 2017 (edited)
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- load libraries
  2. os.loadAPI("drawLib")
  3. os.loadAPI("buttonLib")
  4. os.loadAPI("dataLib")
  5. -- peripheral setup
  6.  
  7. local controllername = "monitor_0"
  8. local controller = peripheral.wrap(controllername)
  9. local modem = peripheral.wrap('top')
  10. modem.closeAll()
  11. modem.open(2)
  12. -- local wmod = peripheral.wrap("bottom")
  13. -- wmod.closeAll()
  14.  
  15. -- variables setup
  16. local controllerX, controllerY
  17. local currentApp = "home"
  18. local previousApp = "home"
  19.  
  20. -- Application Variables Setup
  21. local northFarmActive = false
  22. local eastFarmActive = false
  23. local southFarmActive = false
  24. local westFarmActive = false
  25. local totalEssences = {}
  26. local essencesPage = 1
  27. local essencesPerPage = 13
  28. local currentSort = "None"
  29.  
  30. function resizeMon()
  31.     controllerX, controllerY = controller.getSize()
  32. end
  33.  
  34. function transmitToSlaves(message)
  35.     -- print('Transmitting: ' .. message)
  36.     modem.transmit(1,2,message)
  37. end
  38.  
  39. for i = 2, 5 do
  40.     -- wmod.open(i)
  41. end
  42.  
  43. function clearAll()
  44.     drawLib.empty(controller)
  45. end
  46.  
  47. -- Startup Functions
  48. function bootSequence()
  49.     -- drawTitle(displayX/2-5, displayY/2, "Booting...", colors.white, colors.black,display)
  50.     -- Application Triggers
  51.     disableAllFarms()
  52.    
  53.     loadingScreen(controllerX,controllerY,controller)
  54. end
  55. function loadingScreen(monX,monY,monitor)
  56.     drawLib.drawTitle(monX/2-7, monY/2, "All The Mods 7 OS", colors.white, colors.black,monitor)
  57.     drawLib.drawTitle(monX/2-9, monY-3, "Loading... Please wait", colors.white, colors.black,monitor)
  58.     for i=1, 30 do
  59.         --drawContent(2, monY-1, monY-2, 2, i, 100, colors.red, colors.gray,monitor)
  60.         drawLib.drawProg(2,monY-1, monX-2, 1, i, 30, colors.white, colors.gray,monitor)
  61.         sleep(0.05)
  62.     end
  63.     drawLib.empty(monitor)
  64. end
  65.  
  66. -- Utility Functions
  67. function formatNumbers(number,length)
  68.     return tostring(math.floor(number))..string.rep(" ",length-#tostring(math.floor(number)))
  69. end
  70. function table_count(T)
  71.   local count = 0
  72.   for _ in pairs(T) do count = count + 1 end
  73.   return count
  74. end
  75. function maxpages()
  76.     return math.ceil((table_count(totalEssences) / essencesPerPage))
  77. end
  78. function atoz(a,b)
  79.   return a[1] < b[1]
  80. end
  81. function getSortedEssencesTable()
  82.     if currentSort == "None" then
  83.         return totalEssences
  84.     elseif currentSort == " A to Z " then
  85.         return table.sort(totalEssences, compare)
  86.     elseif currentSort == " Z to Z " then
  87.  
  88.     elseif currentSort == "Hi to Lo" then
  89.  
  90.     elseif currentSort == "Lo to Hi" then
  91.  
  92.     end
  93. end
  94.  
  95. -- Application Functions
  96. function disableAllFarms()
  97.     northFarmActive = false
  98.     transmitToSlaves("northFarmOff")
  99.     eastFarmActive = false
  100.     transmitToSlaves("eastFarmOff")
  101.     southFarmActive = false
  102.     transmitToSlaves("southFarmOff")
  103.     westFarmActive = false
  104.     transmitToSlaves("westFarmOff")
  105. end
  106. function updateEssenceCounts()
  107.     transmitToSlaves("provideEssenceCount")
  108.     local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  109.     totalEssences = message
  110. end
  111.  
  112. -- Add all buttons
  113. function addButtons()
  114.     -- Homebar Page Change
  115.     buttonLib.addButton(1,1,3,1,"pageLeft",colors.cyan,colors.red,true,"<- ","",1,1,"")
  116.     buttonLib.addButton(controllerX-2,1,3,1,"pageRight",colors.cyan,colors.red,true," ->","",controllerX-2,1,"")
  117.     -- Homebar Home
  118.     buttonLib.addButton(1,1,5,1,"homeButton1",colors.lime,colors.lightGray,true,"/^\\","",2,1,"")
  119.     buttonLib.addButton(1,2,5,1,"homeButton2",colors.lime,colors.lightGray,true,"|#|","",2,2,"")
  120.     -- Homebar Back
  121.     buttonLib.addButton(controllerX-4,1,5,1,"backButton1",colors.red,colors.lightGray,true,"/__","",controllerX-3,1,"")
  122.     buttonLib.addButton(controllerX-4,2,5,1,"backButton2",colors.red,colors.lightGray,true,"\\  ","",controllerX-3,2,"")
  123.     -- Applications
  124.     buttonLib.addButton(3,4,11,3,"farms",colors.cyan,colors.gray,true,"Farms","",6,5,"")
  125.     buttonLib.addButton(17,4,11,3,"setting",colors.cyan,colors.gray,true,"Setting","",19,5,"")
  126.     buttonLib.addButton(3,8,11,3,"storage",colors.cyan,colors.gray,true,"Storage","",5,9,"")
  127.     buttonLib.addButton(17,8,11,3,"graphs",colors.cyan,colors.gray,true,"Diagram","",19,9,"")
  128.     --addButton(3,12,11,3,"timelog",colors.cyan,colors.gray,true,"TimeLog","",5,13,"")
  129.     --addButton(17,12,11,3,"nothing",colors.cyan,colors.gray,false,"","Nothing",19,13,"")
  130.     -- Farm Application Buttons
  131.     --addButton(7,4,5,3,"northFarm",colors.green,colors.red,true,"N","N",9,5,"")
  132.     --addButton(2,7,5,3,"westFarm",colors.green,colors.red,true,"W","W",4,8,"")
  133.     --addButton(7,10,5,3,"southFarm",colors.green,colors.red,true,"S","S",9,11,"")
  134.     --addButton(12,7,5,3,"eastFarm",colors.green,colors.red,true,"E","E",14,8,"")
  135.     buttonLib.addButton(2,4,5,3,"northFarm",colors.green,colors.red,true,"N","N",4,5,"")
  136.     buttonLib.addButton(2,8,5,3,"eastFarm",colors.green,colors.red,true,"E","E",4,9,"")
  137.     buttonLib.addButton(2,12,5,3,"southFarm",colors.green,colors.red,true,"S","S",4,13,"")
  138.     buttonLib.addButton(2,16,5,3,"westFarm",colors.green,colors.red,true,"W","W",4,17,"")
  139.     buttonLib.addButton(31,6,3,1,"essencePageLeft",colors.blue,colors.gray,true,"<","<",32,6,"")
  140.     buttonLib.addButton(47,6,3,1,"essencePageRight",colors.blue,colors.gray,true,">",">",48,6,"")
  141.     -- Settings Application Buttons
  142.     buttonLib.addButton(3,4,11,3,"restart",colors.red,colors.gray,true,"Restart","",5,5,"")
  143. end
  144.  
  145. -- Generate The Gui Based On Current State
  146. function generateGUI(monitor)
  147.     -- Draw Home Bar
  148.     drawLib.drawLine(1,1,controllerX,2,colors.gray,monitor)
  149.     buttonLib.drawButton("homeButton1",monitor)
  150.     buttonLib.drawButton("homeButton2",monitor)
  151.     buttonLib.drawButton("backButton1",monitor)
  152.     buttonLib.drawButton("backButton2",monitor)
  153.     drawLib.clearBox(controllerX/2-1,1,1,5,monitor,colors.gray)
  154.     drawLib.putTime(controllerX/2-1,1,monitor,true,colors.gray)
  155.     -- Draw Application Specific Items
  156.     if currentApp == "home" then
  157.         buttonLib.drawButton("farms",monitor)
  158.         buttonLib.drawButton("storage",monitor)
  159.         buttonLib.drawButton("graphs",monitor)
  160.         buttonLib.drawButton("setting",monitor)
  161.     elseif currentApp == "farms" then
  162.         drawLib.drawTitle(controllerX/2-1, 2, "Farms", colors.white, colors.gray, monitor)
  163.    
  164.         buttonLib.setButtonState("northFarm",northFarmActive)
  165.         buttonLib.drawButton("northFarm",monitor)
  166.         buttonLib.setButtonState("westFarm",westFarmActive)
  167.         buttonLib.drawButton("westFarm",monitor)
  168.         buttonLib.setButtonState("southFarm",southFarmActive)
  169.         buttonLib.drawButton("southFarm",monitor)
  170.         buttonLib.setButtonState("eastFarm",eastFarmActive)
  171.         buttonLib.drawButton("eastFarm",monitor)
  172.  
  173.         drawLib.drawTitle(9, 4, "Essences Collected :", colors.white, colors.black, monitor)
  174.  
  175.         local index = 0
  176.         for key, value in pairs(getSortedEssencesTable()) do
  177.             if index < essencesPage * essencesPerPage and index >= (essencesPage - 1) * essencesPerPage then
  178.                 local text = key .. string.rep(" ", 15 - #key) .. string.rep(" ", 5 - #(""..value)) .. value
  179.                 drawLib.drawTitle(9, 6 + (index%essencesPerPage), text, colors.white, colors.black, monitor)
  180.             end
  181.             index = index + 1
  182.         end
  183.  
  184.         buttonLib.drawButton("essencePageLeft",monitor)
  185.         drawLib.drawTitle(31, 4, "       Page        ", colors.white, colors.gray, monitor)
  186.         drawLib.drawTitle(37, 6, essencesPage.." of "..maxpages(), colors.white, colors.black, monitor)
  187.         buttonLib.drawButton("essencePageRight",monitor)
  188.  
  189.         --buttonLib.drawButton("essencePageLeft",monitor)
  190.         drawLib.drawTitle(31, 8, "       Sort        ", colors.white, colors.gray, monitor)
  191.         drawLib.drawTitle(38, 10, "None", colors.white, colors.black, monitor)
  192.         --buttonLib.drawButton("essencePageRight",monitor)
  193.  
  194.         --buttonLib.drawButton("essencePageLeft",monitor)
  195.         drawLib.drawTitle(31, 12, "       Data        ", colors.white, colors.gray, monitor)
  196.         drawLib.drawTitle(37, 14, "Totals", colors.white, colors.black, monitor)
  197.         --buttonLib.drawButton("essencePageRight",monitor)
  198.    
  199.     elseif currentApp == "storage" then
  200.         drawLib.drawTitle(controllerX/2-2, 2, "Storage", colors.white, colors.gray, monitor)
  201.        
  202.     elseif currentApp == "graphs" then
  203.         drawLib.drawTitle(controllerX/2-2, 2, "Diagram", colors.white, colors.gray, monitor)
  204.        
  205.     elseif currentApp == "setting" then
  206.         drawLib.drawTitle(controllerX/2-2, 2, "Setting", colors.white, colors.gray, monitor)
  207.         buttonLib.drawButton("restart",monitor)
  208.            
  209.     else error("App not found") end
  210.  
  211.     -- drawButton("pageLeft",monitor)
  212.     -- drawButton("pageRight",monitor)
  213. end
  214.  
  215. -- React To Touch Events
  216. function touchAreas(x,y,side)
  217.     -- Is On the main controller screen
  218.     if side == controllername then
  219.         if buttonLib.isWithinBoundingBox(x,y,"homeButton1") then
  220.             currentApp = "home"
  221.             drawLib.empty(controller)
  222.         elseif buttonLib.isWithinBoundingBox(x,y,"homeButton2") then
  223.             currentApp = "home"
  224.             drawLib.empty(controller)
  225.         elseif buttonLib.isWithinBoundingBox(x,y,"backButton1") then
  226.            
  227.         elseif buttonLib.isWithinBoundingBox(x,y,"backButton2") then
  228.      
  229.         elseif currentApp == "home" then
  230.             if buttonLib.isWithinBoundingBox(x,y,"farms") then
  231.                 currentApp = "farms"
  232.                 previousApp = "home"
  233.                 drawLib.empty(controller)
  234.             elseif buttonLib.isWithinBoundingBox(x,y,"storage") then
  235.                 currentApp = "storage"
  236.                 previousApp = "home"
  237.                 drawLib.empty(controller)
  238.             elseif buttonLib.isWithinBoundingBox(x,y,"graphs") then
  239.                 currentApp = "graphs"
  240.                 previousApp = "home"
  241.                 drawLib.empty(controller)
  242.             elseif buttonLib.isWithinBoundingBox(x,y,"setting") then
  243.                 currentApp = "setting"
  244.                 previousApp = "home"
  245.                 drawLib.empty(controller)
  246.             end
  247.         elseif currentApp == "farms" then
  248.             if buttonLib.isWithinBoundingBox(x,y,"northFarm") then
  249.                 if northFarmActive then transmitToSlaves("northFarmOff") else transmitToSlaves("northFarmOn") end
  250.                 if northFarmActive then northFarmActive = false else northFarmActive = true end
  251.                 -- transmit over network that northfarm must be toggled
  252.                 drawLib.empty(controller)
  253.             elseif buttonLib.isWithinBoundingBox(x,y,"eastFarm") then
  254.                 if northFarmActive then transmitToSlaves("eastFarmOff") else transmitToSlaves("eastFarmOn") end
  255.                 if eastFarmActive then eastFarmActive = false else eastFarmActive = true end
  256.  
  257.                 drawLib.empty(controller)
  258.             elseif buttonLib.isWithinBoundingBox(x,y,"southFarm") then
  259.                 if northFarmActive then transmitToSlaves("southFarmOff") else transmitToSlaves("southFarmOn") end
  260.                 if southFarmActive then southFarmActive = false else southFarmActive = true end
  261.  
  262.                 drawLib.empty(controller)
  263.             elseif buttonLib.isWithinBoundingBox(x,y,"westFarm") then
  264.                 if northFarmActive then transmitToSlaves("westFarmOff") else transmitToSlaves("westFarmOn") end
  265.                 if westFarmActive then westFarmActive = false else westFarmActive = true end
  266.  
  267.                 drawLib.empty(controller)
  268.             elseif buttonLib.isWithinBoundingBox(x,y,"essencePageLeft") then
  269.                 if essencesPage > 1 then essencesPage = essencesPage - 1 end
  270.                 drawLib.empty(controller)
  271.             elseif buttonLib.isWithinBoundingBox(x,y,"essencePageRight") then
  272.                 if essencesPage < maxpages() then essencesPage = essencesPage + 1 end
  273.                 drawLib.empty(controller)
  274.             end
  275.         elseif currentApp == "setting" then
  276.             if buttonLib.isWithinBoundingBox(x,y,"restart") then
  277.                 os.reboot()
  278.             else
  279.             end
  280.         else
  281.         end
  282.     else
  283.     end
  284. end
  285.  
  286. function waitForTouch()
  287.     parallel.waitForAny(applicationWaiter,getTouch)
  288. end
  289.  
  290. function applicationWaiter()
  291.     parallel.waitForAny(wait,applicationLoop)
  292. end
  293.  
  294. function applicationLoop()
  295.     updateEssenceCounts()
  296. end
  297.  
  298. function getTouch()
  299.     event, side, xPos, yPos = os.pullEvent("monitor_touch")
  300.     touchAreas(xPos, yPos, side)
  301. end
  302.  
  303. function wait()
  304.     sleep(0.5)
  305. end
  306.  
  307. function init()
  308.     clearAll()
  309.     resizeMon()
  310.     bootSequence()
  311.     addButtons()
  312.     clearAll()
  313.     while true do
  314.         generateGUI(controller)
  315.         waitForTouch()
  316.     end
  317. end
  318.  
  319. init()
Advertisement
Add Comment
Please, Sign In to add comment