BigETI

ui

Feb 17th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.21 KB | None | 0 0
  1. local device_name = "bigeti.ui"
  2. local device_display_name = "BigETI's UI"
  3. local width, height = term.getSize()
  4. local key_up = 200
  5. local key_down = 208
  6. local key_left = 203
  7. local key_right = 205
  8. local key_enter = 28
  9. local reactor_id = 0
  10. local reactors = {}
  11. local menu =
  12. {
  13.   {
  14.     [ "txt" ] = "On/Off",
  15.     [ "descr" ] = "Turn reactor on/off",
  16.     [ "val" ] = false,
  17.     [ "func" ] = "onOff_Select",
  18.     [ "dfunc" ] = "onOff_D"
  19.   },
  20.   {
  21.     [ "txt" ] = "Eject waste",
  22.     [ "descr" ] = "Eject waste",
  23.     [ "val" ] = nil,
  24.     [ "func" ] = "ejectWaste_Select"
  25.   },
  26.   {
  27.     [ "txt" ] = "Eject fuel",
  28.     [ "descr" ] = "Eject fuel",
  29.     [ "val" ] = nil,
  30.     [ "func" ] = "ejectFuel_Select"
  31.   },
  32.   {
  33.     [ "txt" ] = "Show stats",
  34.     [ "descr" ] = "Show reactor stats",
  35.     [ "val" ] = nil,
  36.     [ "func" ] = "showStats_Select"
  37.   },
  38.   {
  39.     [ "txt" ] = "Settings",
  40.     [ "descr" ] = "Software settings",
  41.     [ "val" ] = nil,
  42.     [ "func" ] = "settings_Select"
  43.   },
  44.   {
  45.     [ "txt" ] = "Back",
  46.     [ "descr" ] = "Back to reactors",
  47.     [ "val" ] = nil,
  48.     [ "func" ] = "back_Select"
  49.   }
  50. }
  51. local settings =
  52. {
  53.   {
  54.     [ "txt" ] = "Background",
  55.     [ "descr" ] = "Background color",
  56.     [ "val" ] = 4,
  57.     [ "is_color" ] = true,
  58.     [ "func" ] = "settingsBackground_Select",
  59.     [ "dfunc" ] = "settingsBackground_D"
  60.   },
  61.   {
  62.     [ "txt" ] = "Text",
  63.     [ "descr" ] = "Text color",
  64.     [ "val" ] = 8,
  65.     [ "is_color" ] = true,
  66.     [ "func" ] = "settingsText_Select",
  67.     [ "dfunc" ] = "settingsText_D"
  68.   },
  69.   {
  70.     [ "txt" ] = "Back",
  71.     [ "descr" ] = "Back to the menu",
  72.     [ "val" ] = nil,
  73.     [ "func" ] = "settingsBack_Select"
  74.   }
  75. }
  76. local stats = {}
  77.  
  78. local color_names =
  79. {
  80.   "White",
  81.   "Orange",
  82.   "Magenta",
  83.   "L-Blue",
  84.   "Yellow",
  85.   "Lime",
  86.   "Pink",
  87.   "Gray",
  88.   "L-Gray",
  89.   "Cyan",
  90.   "Purple",
  91.   "Blue",
  92.   "Brown",
  93.   "Green",
  94.   "Red",
  95.   "Black"
  96. }
  97.  
  98. local selection =
  99. {
  100.   [ "reactors" ] =
  101.   {
  102.     [ "s" ] = 1,
  103.     [ "m" ] = reactors,
  104.     [ "r" ] = false,
  105.     [ "o" ] = 1
  106.   },
  107.   [ "menu" ] =
  108.   {
  109.     [ "s" ] = 1,
  110.     [ "m" ] = menu,
  111.     [ "r" ] = false,
  112.     [ "o" ] = 1
  113.   },
  114.   [ "settings" ] =
  115.   {
  116.     [ "s" ] = 1,
  117.     [ "m" ] = settings,
  118.     [ "r" ] = false,
  119.     [ "o" ] = 1
  120.   },
  121.   [ "stats" ] =
  122.   {
  123.     [ "s" ] = 1,
  124.     [ "m" ] = stats,
  125.     [ "r" ] = false,
  126.     [ "o" ] = 1
  127.   }
  128. }
  129.  
  130. function reduceString(str, count)
  131.   local ret = str
  132.   local l = ret:len()
  133.   if l > count then
  134.     if count <= 3 then
  135.       ret = string.rep(".", count)
  136.     else
  137.       ret = ret:sub(1, count - 3) .. "..."
  138.     end
  139.   end
  140.   return ret
  141. end
  142.  
  143. function alignString(str, count)
  144.   local ret = reduceString(str, count)
  145.   local l = ret:len()
  146.   if l > 0 then
  147.     local n = count - l
  148.     if (n % 2) == 0 then
  149.       ret = string.rep(" ", n / 2) .. ret .. string.rep(" ", n / 2)
  150.     else
  151.       ret = string.rep(" ", (n - 1) / 2) .. ret .. string.rep(" ", (n + 1) / 2)
  152.     end
  153.   end
  154.   return ret
  155. end
  156.  
  157. function getColorByIndex(i)
  158.   return bit.blshift(1, i - 1)
  159. end
  160.  
  161. function refreshReactors()
  162.   reactors =
  163.   {
  164.     {
  165.       [ "txt" ] = "Refresh",
  166.       [ "descr" ] = "Refreshing...",
  167.       [ "val" ] = nil,
  168.       [ "func" ] = "refreshReactors"
  169.     },
  170.     {
  171.       [ "txt" ] = "Settings",
  172.       [ "descr" ] = "Software settings",
  173.       [ "val" ] = nil,
  174.       [ "func" ] = "settings_Select"
  175.     },
  176.     {
  177.       [ "txt" ] = "Exit",
  178.       [ "descr" ] = "Exit this software",
  179.       [ "val" ] = nil,
  180.       [ "func" ] = "exit_Select"
  181.     }
  182.   }
  183.   selection["reactors"]["m"] = reactors
  184.   selection["reactors"]["s"] = 1
  185.   redraw("reactors")
  186.   local r = {rednet.lookup("reactor.req.hello")}
  187.   for i=1, #r do
  188.     rednet.send(r[i], device_name, "reactor.req.name")
  189.     local sender, msg, prot = rednet.receive("reactor.ack.name", 1)
  190.     if tonumber(r[i]) == tonumber(sender) then
  191.       table.insert(reactors,
  192.       {
  193.         [ "txt" ] = msg,
  194.         [ "descr" ] = "Open " .. msg,
  195.         [ "val" ] = sender,
  196.         [ "func" ] = "reactor_Select"
  197.       })
  198.     end
  199.   end
  200.   reactors[1]["descr"] = "Refresh reactors list"
  201. end
  202.  
  203. function header()
  204.   term.setBackgroundColor(getColorByIndex(settings[1]["val"]))
  205.   term.clear()
  206.   term.setTextColor(getColorByIndex(settings[2]["val"]))
  207.   term.setCursorPos(1, 1)
  208.   print("O" .. string.rep("=", width - 2) .. "0")
  209.   print("|" .. alignString(device_display_name, width - 2) .. "|")
  210.   print(">" .. string.rep("-", width - 2) .. "<")
  211.   return 3
  212. end
  213.  
  214. function drawMenu(draw_lines, s)
  215.   local ret = 1
  216.   local m = selection[s]["m"]
  217.   local mn = #m
  218.   if selection[s]["s"] < selection[s]["o"] then
  219.     selection[s]["o"] = selection[s]["s"]
  220.   elseif selection[s]["s"] >= (selection[s]["o"] + draw_lines) then
  221.     selection[s]["o"] = selection[s]["s"] - (draw_lines - 1)
  222.   end
  223.   local j = selection[s]["o"]
  224.   for i=1, draw_lines do
  225.     if (j <= mn) then
  226.       local t = ""
  227.       if selection[s]["s"] == j then
  228.         t = t .. "[ "
  229.       else
  230.         t = t .. "  "
  231.       end
  232.       t = t .. m[j]["txt"]
  233.       if selection[s]["s"] == j then
  234.         t = t .. " ]"
  235.       else
  236.         t = t .. "  "
  237.       end
  238.       local v = ""
  239.       if m[j]["val"] ~= nil then
  240.         if m[j]["is_color"] == nil then
  241.           v = tostring(m[j]["val"])
  242.         else
  243.           v = color_names[m[j]["val"]]
  244.         end
  245.       end
  246.       local c = (width - 8) - (m[j]["txt"]:len() + v:len())
  247.       if c > 0 then
  248.         t = t .. string.rep(" ", c)
  249.       end
  250.       t = t .. v
  251.       print("| " .. reduceString(t, width - 4) .. " |")
  252.       ret = ret + 1
  253.     else
  254.       break
  255.     end
  256.     j = j + 1
  257.   end
  258.   return ret - 1
  259. end
  260.  
  261. function footer(drawn_lines, s)
  262.   local c = height - (3 + drawn_lines)
  263.   for i=1, c do
  264.     print("|" .. string.rep(" ", width - 2) .. "|")
  265.   end
  266.   print(">" .. string.rep("-", width - 2) .. "<")
  267.   print("| " .. alignString(selection[s]["m"][selection[s]["s"]]["descr"], width - 4) .. " |")
  268.   write("0" .. string.rep("=", width - 2) .. "0")
  269. end
  270.  
  271. function redraw(s)
  272.   local hc = header()
  273.   footer(hc + drawMenu(height - (hc + 3), s), s)
  274. end
  275.  
  276. function processMessages(s)
  277.   redraw(s)
  278.   local event, key_button, x, y = os.pullEvent()
  279.   if event == "key" then
  280.     if key_button == key_up then
  281.       selectUp(s)
  282.     elseif key_button == key_down then
  283.       selectDown(s)
  284.     elseif key_button == key_left then
  285.       selectLeft(s)
  286.     elseif key_button == key_right then
  287.       selectRight(s)
  288.     elseif key_button == key_enter then
  289.       accept(s)
  290.     end
  291.   elseif event == "mouse_click" then
  292.     y = y - (3 - (selection[s]["o"] - 1))
  293.     if (y >= 1) and (y <= #(selection[s]["m"])) then
  294.       if selection[s]["s"] == y then
  295.         accept(s)
  296.       else
  297.         selection[s]["s"] = y
  298.       end
  299.     end
  300.   elseif event == "mouse_scroll" then
  301.     if key_button < 0 then
  302.       selectUp(s)
  303.     elseif key_button > 0 then
  304.       selectDown(s)
  305.     end
  306.   end
  307. end
  308.  
  309. function runMenu(m)
  310.   selection[m]["r"] = true
  311.   selection[m]["s"] = 1
  312.   selection[m]["o"] = 1
  313.   while selection[m]["r"] do
  314.     processMessages(m)
  315.   end
  316. end
  317.  
  318. function selectUp(s)
  319.   if selection[s]["s"] <= 1 then
  320.     selection[s]["s"] = #(selection[s]["m"])
  321.   else
  322.     selection[s]["s"] = selection[s]["s"] - 1
  323.   end
  324. end
  325.  
  326. function selectDown(s)
  327.   if selection[s]["s"] >= #(selection[s]["m"]) then
  328.     selection[s]["s"] = 1
  329.   else
  330.     selection[s]["s"] = selection[s]["s"] + 1
  331.   end
  332. end
  333.  
  334. function selectLeft(s)
  335.   local f = selection[s]["m"][selection[s]["s"]]["dfunc"]
  336.   if f ~= nil then
  337.     getfenv()[f](true)
  338.   end
  339. end
  340.  
  341. function selectRight(s)
  342.   local f = selection[s]["m"][selection[s]["s"]]["dfunc"]
  343.   if f ~= nil then
  344.     getfenv()[f](false)
  345.   end
  346. end
  347.  
  348. function onOff_Select()
  349.   rednet.send(reactor_id, not menu[1]["val"], "reactor.ctrl.active")
  350.   local sender, msg, prot = rednet.receive("reactor.ack.active", 2)
  351.   if sender == reactor_id then
  352.     menu[1]["val"] = msg
  353.   end
  354. end
  355.  
  356. function onOff_D(left)
  357.   onOff_Select()
  358. end
  359.  
  360. function settings_Select()
  361.   runMenu("settings")
  362. end
  363.  
  364. function ejectWaste_Select()
  365.   rednet.send(reactor_id, true, "reactor.ctrl.eject_waste")
  366. end
  367.  
  368. function ejectFuel_Select()
  369.   rednet.send(reactor_id, true, "reactor.ctrl.eject_fuel")
  370. end
  371.  
  372. function showStats_Select()
  373.   rednet.send(reactor_id, true, "reactor.req.stats")
  374.   local sender, msg, prot = rednet.receive("reactor.ack.stats", 2)
  375.   if sender == reactor_id then
  376.     stats =
  377.     {
  378.       {
  379.         [ "txt" ] = "Back",
  380.         [ "descr" ] = "Back to menu",
  381.         [ "val" ] = nil,
  382.         [ "func" ] = "backStats_Select"
  383.       }
  384.     }
  385.     selection["stats"]["m"] = stats
  386.     menu[1]["val"] = msg["active"]
  387.     table.insert(stats,
  388.       {
  389.         [ "txt" ] = "Active",
  390.         [ "descr" ] = "Active = " .. tostring(msg["active"]),
  391.         [ "val" ] = msg["active"],
  392.         [ "func" ] = nil
  393.       })
  394.     table.insert(stats,
  395.       {
  396.         [ "txt" ] = "Energy",
  397.         [ "descr" ] = "Energy = " .. tostring(msg["energy"]),
  398.         [ "val" ] = msg["energy"],
  399.         [ "func" ] = nil
  400.       })
  401.     table.insert(stats,
  402.       {
  403.         [ "txt" ] = "Fuel temp.",
  404.         [ "descr" ] = "Fuel temp. = " .. tostring(msg["fuel_temp"]),
  405.         [ "val" ] = msg["fuel_temp"],
  406.         [ "func" ] = nil
  407.       })
  408.     table.insert(stats,
  409.       {
  410.         [ "txt" ] = "Casing temp.",
  411.         [ "descr" ] = "Casing temp. = " .. tostring(msg["casing_temp"]),
  412.         [ "val" ] = msg["casing_temp"],
  413.         [ "func" ] = nil
  414.       })
  415.     table.insert(stats,
  416.       {
  417.         [ "txt" ] = "Fuel",
  418.         [ "descr" ] = "Fuel = " .. tostring(msg["fuel"]),
  419.         [ "val" ] = msg["fuel"],
  420.         [ "func" ] = nil
  421.       })
  422.     table.insert(stats,
  423.       {
  424.         [ "txt" ] = "Waste",
  425.         [ "descr" ] = "Waste = " .. tostring(msg["waste"]),
  426.         [ "val" ] = msg["waste"],
  427.         [ "func" ] = nil
  428.       })
  429.     table.insert(stats,
  430.       {
  431.         [ "txt" ] = "Max. fuel",
  432.         [ "descr" ] = "Max. fuel = " .. tostring(msg["fuel_max"]),
  433.         [ "val" ] = msg["fuel_max"],
  434.         [ "func" ] = nil
  435.       })
  436.     table.insert(stats,
  437.       {
  438.         [ "txt" ] = "Energy prod.",
  439.         [ "descr" ] = "Energy prod. = " .. tostring(msg["energy_prod"]),
  440.         [ "val" ] = msg["energy_prod"],
  441.         [ "func" ] = nil
  442.       })
  443.     table.insert(stats,
  444.       {
  445.         [ "txt" ] = "H-Fluid prod.",
  446.         [ "descr" ] = "H-Fluid prod. = " .. tostring(msg["hot_fluid_prod"]),
  447.         [ "val" ] = msg["hot_fluid_prod"],
  448.         [ "func" ] = nil
  449.       })
  450.     table.insert(stats,
  451.       {
  452.         [ "txt" ] = "Coolant type",
  453.         [ "descr" ] = "Coolant type = " .. tostring(msg["coolant_type"]),
  454.         [ "val" ] = msg["coolant_type"],
  455.         [ "func" ] = nil
  456.       })
  457.     table.insert(stats,
  458.       {
  459.         [ "txt" ] = "Coolant",
  460.         [ "descr" ] = "Coolant = " .. tostring(msg["coolant"]),
  461.         [ "val" ] = msg["coolant"],
  462.         [ "func" ] = nil
  463.       })
  464.     table.insert(stats,
  465.       {
  466.         [ "txt" ] = "Max. coolant",
  467.         [ "descr" ] = "Max. coolant = " .. tostring(msg["coolant_max"]),
  468.         [ "val" ] = msg["coolant_max"],
  469.         [ "func" ] = nil
  470.       })
  471.     table.insert(stats,
  472.       {
  473.         [ "txt" ] = "H-Fluid type",
  474.         [ "descr" ] = "H-Fluid type = " .. tostring(msg["hot_fluid_type"]),
  475.         [ "val" ] = msg["hot_fluid_type"],
  476.         [ "func" ] = nil
  477.       })
  478.     table.insert(stats,
  479.       {
  480.         [ "txt" ] = "H-Fluid",
  481.         [ "descr" ] = "H-Fluid = " .. tostring(msg["hot_fluid"]),
  482.         [ "val" ] = msg["hot_fluid"],
  483.         [ "func" ] = nil
  484.       })
  485.     table.insert(stats,
  486.       {
  487.         [ "txt" ] = "Max. H-Fuel",
  488.         [ "descr" ] = "Max. H-Fuel = " .. tostring(msg["hot_fluid_max"]),
  489.         [ "val" ] = msg["hot_fluid_max"],
  490.         [ "func" ] = nil
  491.       })
  492.     table.insert(stats,
  493.       {
  494.         [ "txt" ] = "F-Reactiv.",
  495.         [ "descr" ] = "F-Reactiv. = " .. tostring(msg["fuel_reactivity"]),
  496.         [ "val" ] = msg["fuel_reactivity"],
  497.         [ "func" ] = nil
  498.       })
  499.     table.insert(stats,
  500.       {
  501.         [ "txt" ] = "F-Consumed",
  502.         [ "descr" ] = "F-Consumed = " .. tostring(msg["fuel_consumed"]),
  503.         [ "val" ] = msg["fuel_consumed"],
  504.         [ "func" ] = nil
  505.       })
  506.     table.insert(stats,
  507.       {
  508.         [ "txt" ] = "A-Cooled",
  509.         [ "descr" ] = "A-Cooled = " .. tostring(msg["actively_cooled"]),
  510.         [ "val" ] = msg["actively_cooled"],
  511.         [ "func" ] = nil
  512.       })
  513.     table.insert(stats,
  514.       {
  515.         [ "txt" ] = "Min. Coord",
  516.         [ "descr" ] = "Min. Coord = " .. tostring(msg["min_coord"]),
  517.         [ "val" ] = msg["min_coord"],
  518.         [ "func" ] = nil
  519.       })
  520.     table.insert(stats,
  521.       {
  522.         [ "txt" ] = "Max. Coord",
  523.         [ "descr" ] = "Max. Coord = " .. tostring(msg["max_coord"]),
  524.         [ "val" ] = msg["max_coord"],
  525.         [ "func" ] = nil
  526.       })
  527.     --for i=0, #(msg["ctrl_rods"]) do
  528.     --  table.insert(stats,
  529.     --    {
  530.     --      [ "txt" ] = "Ctrl-Rod" .. tostring(i) .. " name",
  531.     --      [ "descr" ] = "Ctrl-Rod " .. tostring(i) .. " name = " .. msg["ctrl_rods"][i]["name"],
  532.     --      [ "val" ] = msg["ctrl_rods"][i]["name"],
  533.     --      [ "func" ] = nil
  534.     --    })
  535.     --  table.insert(stats,
  536.     --    {
  537.     --      [ "txt" ] = "Ctrl-Rod" .. tostring(i) .. " lvl",
  538.     --      [ "descr" ] = "Ctrl-Rod " .. tostring(i) .. " lvl = " .. tostring(msg["ctrl_rods"][i]["lvl"]),
  539.     --      [ "val" ] = msg["ctrl_rods"][i]["lvl"],
  540.     --      [ "func" ] = nil
  541.     --    })
  542.     --end
  543.     runMenu("stats")
  544.   end
  545. end
  546.  
  547. function exit_Select()
  548.   selection["reactors"]["r"] = false
  549. end
  550.  
  551. function reactor_Select()
  552.   reactor_id = reactors[selection["reactors"]["s"]]["val"]
  553.   rednet.send(reactor_id, true, "reactor.req.stats")
  554.   local sender, msg, prot = rednet.receive("reactor.ack.stats", 2)
  555.   if sender == reactor_id then
  556.     menu[1]["val"] = msg["active"]
  557.   end
  558.   runMenu("menu")
  559. end
  560.  
  561. function back_Select()
  562.   reactor_id = 0
  563.   selection["menu"]["r"] = false
  564. end
  565.  
  566. function settingsBackground_D(left)
  567.   local v = settings[1]["val"]
  568.   if left then
  569.     if v <= 1 then
  570.       v = 16
  571.     else
  572.       v = v - 1
  573.     end
  574.   else
  575.     if v >= 16 then
  576.       v = 1
  577.     else
  578.       v = v + 1
  579.     end
  580.   end
  581.   settings[1]["val"] = v
  582. end
  583.  
  584. function settingsBackground_Select()
  585.   local v = settings[1]["val"]
  586.   if v >= 16 then
  587.     v = 1
  588.   else
  589.     v = v + 1
  590.   end
  591.   settings[1]["val"] = v
  592. end
  593.  
  594. function settingsText_D(left)
  595.   local v = settings[2]["val"]
  596.   if left then
  597.     if v <= 1 then
  598.       v = 16
  599.     else
  600.       v = v - 1
  601.     end
  602.   else
  603.     if v >= 16 then
  604.       v = 1
  605.     else
  606.       v = v + 1
  607.     end
  608.   end
  609.   settings[2]["val"] = v
  610. end
  611.  
  612. function settingsText_Select()
  613.   local v = settings[2]["val"]
  614.   if v >= 16 then
  615.     v = 1
  616.   else
  617.     v = v + 1
  618.   end
  619.   settings[2]["val"] = v
  620. end
  621.  
  622. function settingsBack_Select()
  623.   selection["settings"]["r"] = false
  624. end
  625.  
  626. function backStats_Select()
  627.   selection["stats"]["r"] = false
  628. end
  629.  
  630. function accept(s)
  631.   local f = selection[s]["m"][selection[s]["s"]]["func"]
  632.   if f ~= nil then
  633.     getfenv()[f]()
  634.   end
  635. end
  636.  
  637. rednet.open("back")
  638. refreshReactors()
  639. runMenu("reactors")
  640. rednet.close("back")
  641. term.setBackgroundColor(colors.black)
  642. term.setTextColor(colors.white)
  643. term.clear()
  644. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment