Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.13 KB | None | 0 0
  1. --Rotarycraft-Opencomputer integration system, ROIS. Made by Roxox1.
  2. --Thanks to SealabJaster for GUI inspiration. See: https://oc.cil.li/index.php?/topic/815-a-file-explorer/
  3.  
  4. local component = require("component")
  5. local term = require("term")
  6. local gpu = component.gpu
  7. local color = require("colors")  
  8. local event = require("event")
  9. local keyboard = require("keyboard")
  10.  
  11. -- Naming
  12. local version = 0.1
  13. local name = "ROIS"
  14. local formattedName = name .." v" .. version
  15.  
  16. --Screen resolution
  17. local scrWidth, scrHeight = gpu.getResolution()
  18.  
  19. --Drawing values
  20. local startY = 2 -- Minimum y value to start drawing at
  21. local endY = scrHeight -- Maximum y value to draw at.
  22.  
  23. --Status
  24. local status_normal = "[CTRL+C]=Close   [CTRL+R]=Refresh    [W]=Up  [S]=Down   [R]=Rename   [O]=Options"
  25. local status_rename = "[RENAME]: "
  26. local status_options_industrialcoil = "[T]=Set_Torque    [S]=Set_Speed    [B]=Back"
  27. local status_set_torque = "[TORQUE]: "
  28. local status_set_speed = "[SPEED]: "
  29. local status = status_normal
  30.  
  31. --Table to store all machines in
  32. local machines = {}
  33.  
  34. --Variables to store currently selected machine information.
  35. local selected = 1 -- Stores the list int value of the selected machine. Think list[1], list[2]...etc
  36. local selectedName = "" -- Stores the currently selected machines name
  37. local selectedInformation = {} -- Stores the currently selectes machines information.
  38.  
  39. --General variables
  40. local outputWrite = 0
  41. local running = true
  42.  
  43. --Colors to use for drawing to the screen
  44. local colors =
  45. {
  46.     bars = 0,
  47.     text_normal = 0,
  48.     text_highlighted = 0,
  49.     item_cwd = 0,
  50.     item_highlighted = 0,
  51.     item_saved = 0,
  52.     usePallet = false
  53. }
  54.  
  55. -- Setup all of the colours used
  56. if (gpu.getDepth() == 1) then
  57.   colors.bars             = 1
  58.   colors.text_normal      = 1
  59.   colors.text_highlighted   = 0
  60.   colors.item_cwd         = 1
  61.   colors.item_highlighted   = 1
  62.   colors.item_saved       = 1
  63. elseif (gpu.getDepth() == 8) then
  64.   colors.bars             = 0x00264D
  65.   colors.text_normal      = 0xFFFFFF
  66.   colors.text_highlighted   = 0xFFFFFF
  67.   colors.item_cwd         = 0x660066
  68.   colors.item_highlighted   = 0x803300
  69.   colors.item_saved       = 0x004400
  70. else
  71.   local col = require("colors")
  72.   colors.usePallet = true
  73.  
  74.   colors.bars             = col.blue
  75.   colors.text_normal      = col.white
  76.   colors.text_highlighted  = col.white
  77.   colors.item_cwd         = col.magenta
  78.   colors.item_highlighted   = col.red
  79.   colors.item_saved       = col.green
  80. end
  81.  
  82. --Returns a table of information about the specified machine. Args. component address.
  83. local function getMachineInformation(address)
  84.     local info = {}
  85.     theComponent = component.proxy(address)
  86.     info["componentObject"] = theComponent
  87.     info["xCoordinate"], info["yCoordinate"], info["zCoordinate"] = theComponent.getCoords()
  88.     local name = theComponent.getName()
  89.     info["componentName"] = name
  90.     info["componentAddress"] = address
  91.     if(theComponent.getTanks() == nil)  then
  92.         info["canStoreLiquid"] = false
  93.     else
  94.         info["canStoreLiquid"] = true
  95.         info["storedLiquidType"], info["storedLiquidAmount"], info["storedLiquidMax"] = theComponent.getTanks()
  96.     end
  97.     info["powerTotal"], info["powerTorque"], info["powerSpeed"] = theComponent.getPower()
  98.     info["placedBy"] = theComponent.getPlacer()
  99.     if(name == "DC Electric Engine" or name == "AC Electric Engine" or name == "Industrial Coil") then
  100.         info["isEngine"] = true
  101.         if(info["powerTotal"] > 0) then
  102.             info["isOn"] = true
  103.         else
  104.             info["isOn"] = false
  105.         end
  106.     else
  107.         info["isEngine"] = false
  108.     end
  109.     return info
  110. end
  111.  
  112. --Updates the current list of machines. Does not change display names.
  113. local function updateMachines()
  114.     local newMachines = {}
  115.     for machineName, machineInformation in pairs(machines) do
  116.         newMachines[machineName] = getMachineInformation(machineInformation["componentAddress"])
  117.     end
  118.     machines = newMachines
  119. end
  120.  
  121. --Finds all machines, will reset display names. Use only for program initialization.
  122. local function findMachines()
  123.     local allMachines = {}
  124.     for address, componentType in component.list()  do
  125.         if(componentType == "Extractor" or componentType == "AdvancedGears" or componentType == "Engine") then
  126.             allMachines[componentType.." (" .. address .. ")"] = getMachineInformation(address)
  127.         end
  128.     end
  129.     machines = allMachines
  130. end
  131.  
  132. --Returns the machines information associated with the specified address. Will return nil if there is no machine associated with that address.
  133. local function getMachineInformation(address)
  134.     for key,value in pairs(machines) do
  135.         if(value["componentAddress"] == address) then
  136.             return value
  137.         end
  138.     end
  139. end
  140.  
  141. --Returns the amount of machines added.
  142. local function getMachineListSize()
  143.     local i = 0
  144.     for key,value in pairs(machines) do i = i + 1 end
  145.     return i
  146. end
  147.  
  148. --Draws the list of machines to the screen
  149. local function drawMachineList()
  150.     --First sort the list
  151.     local sortedTable = {}
  152.     for displayName, machineInfo in pairs(machines) do
  153.         table.insert(sortedTable, displayName)
  154.     end
  155.     table.sort(sortedTable)
  156.     for i = 1, #sortedTable do
  157.         local k,v = sortedTable[i], machines[sortedTable[i]]
  158.         if(i == selected) then
  159.             gpu.setBackground(colors.item_highlighted, colors.usePallet)
  160.             gpu.setForeground(colors.text_highlighted, colors.usePallet)
  161.             selectedName = k
  162.             selectedInformation = v
  163.         end
  164.        
  165.         term.setCursor(1, startY + i - 1) -- xValue, yValue
  166.         print(k)
  167.         gpu.setBackground(0x000000)
  168.         gpu.setForeground(colors.text_normal, colors.usePallet)
  169.     end
  170.    
  171. end
  172.  
  173. local function printOut(output)
  174.     term.setCursor(scrWidth / 2, outputWrite)
  175.     print(output)
  176.     outputWrite = outputWrite + 1
  177. end
  178.  
  179. local function drawMachineInfo()
  180.     outputWrite = startY + 1
  181.     printOut(selectedName)
  182.     outputWrite = outputWrite + 2
  183.     if(selectedInformation["isEngine"]) then
  184.         printOut("Engine Status:")
  185.         if(selectedInformation["isOn"]) then
  186.             printOut("  Output Total: " .. selectedInformation["powerTotal"])
  187.             printOut("  Output Torque: " .. selectedInformation["powerTorque"])
  188.             printOut("  Output Speed: " .. selectedInformation["powerSpeed"])
  189.         else
  190.             printOut("  This engine is not outputting power")
  191.             if(selectedInformation["componentName"] == "DC Electric Engine") then
  192.                 printOut("  This engine requires a redstone signal!")
  193.             end
  194.         end
  195.     else
  196.         printOut("Power: ")
  197.         if(selectedInformation["powerTotal"] > 0) then
  198.             printOut("  Receiving Total: " .. selectedInformation["powerTotal"])
  199.             printOut("  Receiving Torque: " .. selectedInformation["powerTorque"])
  200.             printOut("  Receiving Speed: " .. selectedInformation["powerSpeed"])
  201.         else
  202.             printOut("  Machine is not receiving power")
  203.         end
  204.     end
  205.     if(selectedInformation["canStoreLiquid"] == true) then
  206.         printOut("Fluid: ")
  207.         printOut("  Type: " .. selectedInformation["storedLiquidType"])
  208.         printOut("  Amount: " .. selectedInformation["storedLiquidAmount"])
  209.         printOut("  Maximum: " .. selectedInformation["storedLiquidMax"])
  210.     end
  211.     outputWrite = outputWrite + 1
  212.     printOut("Coordinates: X:" .. selectedInformation["xCoordinate"] .. " Y:".. selectedInformation["yCoordinate"] .. " Z:" .. selectedInformation["zCoordinate"])
  213.     outputWrite = outputWrite + 1
  214.     printOut("Address: " .. selectedInformation["componentAddress"])
  215.     outputWrite = outputWrite + 1
  216.     printOut("MachineName: " .. selectedInformation["componentName"])
  217.     outputWriter = outputWrite + 1
  218.     printOut("Created by: " .. selectedInformation["placedBy"])
  219. end
  220.  
  221. --Prompt the user for input. Note that this function will not refresh the screen after it's done.
  222. local function getInput()
  223.     term.setCursor(#status, scrHeight)
  224.     term.setCursorBlink(true)
  225.     local input = require("text").trim(term.read())
  226.     term.setCursorBlink(false)
  227.     return input
  228. end
  229.  
  230. local function drawHUD()
  231.     local a = gpu.getBackground()
  232.     gpu.setBackground(colors.bars, colors.usePallet)
  233.     gpu.fill(1,1,scrWidth,1," ") --Top bar
  234.     gpu.fill(1,endY, scrWidth, 1, " ") -- Bottom bar
  235.     gpu.setForeground(colors.text_highlighted, colors.usePallet)
  236.     gpu.set(1,1, "Machines (" .. getMachineListSize() .. "):")
  237.     gpu.set(scrWidth-#formattedName, 1, formattedName)
  238.     gpu.set(1, endY, status)
  239.     gpu.setBackground(a)
  240. end
  241.  
  242. local function refresh()
  243.     updateMachines()
  244.     term.clear()
  245.     drawMachineList()
  246.     drawMachineInfo()
  247.     drawHUD()
  248. end
  249.  
  250. local function handleInput()
  251.     --Handle CTRL keys.
  252.     if(status == status_normal) then
  253.         if(keyboard.isControlDown()) then
  254.             --Exit
  255.             if(keyboard.isKeyDown(keyboard.keys.c)) then
  256.                 term.clear()
  257.                 running = false
  258.             --Refresh
  259.             elseif(keyboard.isKeyDown(keyboard.keys.r)) then
  260.                 refresh()
  261.             end
  262.         else
  263.             --Go up
  264.             if(keyboard.isKeyDown(keyboard.keys.w)) then
  265.                 if not (selected == 1) then
  266.                     selected = selected - 1
  267.                 end
  268.                 refresh()
  269.             --Go down
  270.             elseif (keyboard.isKeyDown(keyboard.keys.s)) then
  271.                 if not (selected == getMachineListSize()) then
  272.                     selected = selected + 1
  273.                 end
  274.                 refresh()
  275.                 --Rename
  276.             elseif (keyboard.isKeyDown(keyboard.keys.r)) then
  277.                 status = status_rename
  278.                 refresh()
  279.                 newName = getInput()
  280.                 refresh()
  281.                 machines[newName] = machines[selectedName]
  282.                 if(selected == 1) then
  283.                     selected = 2
  284.                 else
  285.                     selected = selected - 1
  286.                 end
  287.                 machines[selectedName] = nil
  288.                 status = status_normal
  289.                 refresh()
  290.             elseif(keyboard.isKeyDown(keyboard.keys.o)) then
  291.                 if(selectedInformation["componentName"] == "Industrial Coil") then
  292.                     status = status_options_industrialcoil
  293.                     refresh()
  294.                 end
  295.             end
  296.         end
  297.     elseif(status == status_options_industrialcoil) then
  298.         if(keyboard.isKeyDown(keyboard.keys.b)) then
  299.             status = status_normal
  300.             refresh()
  301.         elseif(keyboard.isKeyDown(keyboard.keys.t)) then
  302.             status = status_set_torque
  303.             refresh()
  304.             local myInput = getInput()
  305.             status = status_options_industrialcoil
  306.             selectedInformation["componentObject"].setTorque(tonumber(myInput))
  307.             refresh()
  308.             refresh()
  309.         elseif(keyboard.isKeyDown(keyboard.keys.s)) then
  310.             status = status_set_speed
  311.             refresh()
  312.             local myInput = getInput()
  313.             status = status_options_industrialcoil
  314.             selectedInformation["componentObject"].setSpeed(tonumber(myinput))
  315.             refresh()
  316.             refresh()
  317.         end
  318.     end
  319. end
  320.  
  321. findMachines()
  322. refresh()
  323.  
  324. while running do
  325.     local _ = event.pull(0.25)
  326.     handleInput()
  327. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement