caucow

ctrlui

Dec 28th, 2016 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.17 KB | None | 0 0
  1. multishell.setTitle(multishell.getCurrent(), "gui")
  2. local m = peripheral.find("modem")
  3. local senderID = os.getComputerLabel() == nil and "ID:" .. os.getComputerID() or os.getComputerLabel()
  4. local channels = {
  5.       ["stdout"] = 1000,
  6.       ["stdin"] = 1001,
  7.       ["witherout"] = 1010,
  8.       ["witherin"] = 1011
  9.     }
  10. local args = {...}
  11. local btns = {}
  12. local labels = {}
  13. local receiver = "^.*$"
  14.  
  15. local function sendCmd(program, cmd, ...)
  16.   m.transmit(channels.stdin, channels.stdout,
  17.       {
  18.           ["program"] = program,
  19.           ["type"] = "cmd",
  20.           ["cmd"] = cmd,
  21.           ["text"] = {...},
  22.           ["sender"] = senderID,
  23.           ["receiver"] = receiver
  24.       })
  25. end
  26.  
  27. local function log(...)
  28.   os.queueEvent("modem_message", "local", 1000, 1001, {
  29.         ["program"] = "control",
  30.         ["type"] = "info",
  31.         ["text"] = {...},
  32.         ["sender"] = senderID
  33.       }, 0)
  34. end
  35.  
  36. local function makeButton(text, x, y, w, h, action)
  37.   local btn = window.create(term.current(), x, y, w, h)
  38.   assert(type(text) == "string", "text is not a string")
  39.   assert(type(x) == "number", "x is not a number")
  40.   assert(type(y) == "number", "y is not a number")
  41.   assert(type(w) == "number", "w is not a number")
  42.   assert(type(h) == "number", "h is not a number")
  43.   btn.x = x
  44.   btn.y = y
  45.   btn.w = w
  46.   btn.h = h
  47.   btn.text = string.sub(text, 1, w-2)
  48.   if type(action) == "function" then
  49.     btn.action = action
  50.   end
  51.   if h == 1 then
  52.     btn.setCursorPos(1 + math.ceil(w/2-string.len(btn.text)/2), 1)
  53.     btn.write(btn.text)
  54.     btn.setCursorPos(1, 1)
  55.     btn.write("[")
  56.     btn.setCursorPos(w, 1)
  57.     btn.write("]")
  58.   elseif h == 2 then
  59.     btn.setCursorPos(1, 1)
  60.     btn.write("/")
  61.     btn.setCursorPos(2, 1)
  62.     btn.write("\\")
  63.     btn.setCursorPos(1, w)
  64.     btn.write("\\")
  65.     btn.setCursorPos(2, w)
  66.     btn.write("/")
  67.   else
  68.     btn.setCursorPos(1 + math.ceil(w/2-string.len(btn.text)/2), math.ceil(h/2))
  69.     btn.write(btn.text)
  70.     btn.setCursorPos(1, 1)
  71.     btn.write("+")
  72.     btn.setCursorPos(w, 1)
  73.     btn.write("+")
  74.     btn.setCursorPos(1, h)
  75.     btn.write("+")
  76.     btn.setCursorPos(w, h)
  77.     btn.write("+")
  78.     for i = 2, h - 1 do
  79.       btn.setCursorPos(1, i)
  80.       btn.write("|")
  81.       btn.setCursorPos(w, i)
  82.       btn.write("|")
  83.     end
  84.     for i = 2, w - 1 do
  85.       btn.setCursorPos(i, 1)
  86.       btn.write("-")
  87.       btn.setCursorPos(i, h)
  88.       btn.write("-")
  89.     end
  90.   end
  91.   return btn
  92. end
  93.  
  94. local function makeLabel(text, x, y)
  95.   local label = window.create(term.current(), x, y, string.len(text), 1)
  96.   label.text = text
  97.   label.x = x
  98.   label.y = y
  99.   label.write(text)
  100.   return label
  101. end
  102.  
  103. local function handleClick(x, y)
  104.   for name, btn in pairs(btns) do
  105.     if x >= btn.x
  106.         and x <= btn.x + btn.w - 1
  107.         and y >= btn.y
  108.         and y <= btn.y + btn.h - 1
  109.         and type(btn.action) == "function" then
  110.       btn.action()
  111.     end
  112.   end
  113. end
  114.  
  115. local function redraw()
  116.   term.clear()
  117.   for name, btn in pairs(btns) do
  118.     btn.redraw()
  119.   end
  120.   for name, label in pairs(labels) do
  121.     label.redraw()
  122.   end
  123. end
  124.  
  125. btns.infinidig = makeButton("infinidig", 1, 1, 11, 1,
  126.     function()
  127.       log("Sending \"run infinidig\"")
  128.       sendCmd("control", "run", "infinidig")
  129.     end)
  130. btns.checkfuel = makeButton("checkfuel", 13, 1, 11, 1,
  131.     function()
  132.       log("Sending \"get fuel\"")
  133.       sendCmd("control", "get", "fuel")
  134.     end)
  135. btns.smine = makeButton("smine", 1, 2, 7, 1,
  136.     function()
  137.       log("Sending \"run smine\"")
  138.       sendCmd("control", "run", "smine")
  139.     end)
  140. btns.vquarry = makeButton("vquarry", 9, 2, 9, 1,
  141.     function()
  142.      
  143.     end)
  144. btns.wither = makeButton("wither", 19, 2, 8, 1,
  145.     function()
  146.      
  147.     end)
  148. btns.refuel = makeButton("refuel", 1, 3, 8, 1,
  149.     function()
  150.       log("Sending \"run refuel 64\"")
  151.       sendCmd("control", "run", "refuel", "64")
  152.     end)
  153. btns.stop = makeButton("STOP", 1, 5, 8, 3,
  154.     function()
  155.       log("Sending \"sigterm\" for 3 seconds")
  156.       shell.switchTab(shell.openTab("sendstop", receiver))
  157.     end)
  158. btns.setrecieve = makeButton("Set Receiver", 10, 5, 16, 3,
  159.     function()
  160.       term.clear()
  161.       term.setCursorPos(1, 1)
  162.       print("Type new receiver pattern")
  163.       receiver = read()
  164.       if receiver:sub(1, 1) ~= "^" then
  165.         receiver = "^" .. receiver
  166.       end
  167.       if receiver:sub(-1, -1) ~= "$" then
  168.         receiver = receiver .. "$"
  169.       end
  170.       labels.receiver = makeLabel("Rec:" .. receiver, 1, 8)
  171.       redraw()
  172.     end)
  173. btns.digon = makeButton("+", 6, 10, 3, 1,
  174.     function()
  175.       log("Sending \"set dig on\"")
  176.       sendCmd("control", "set", "dig", "on")
  177.     end)
  178. btns.digoff = makeButton("-", 10, 10, 3, 1,
  179.     function()
  180.       log("Sending \"set dig off\"")
  181.       sendCmd("control", "set", "dig", "off")
  182.     end)
  183. btns.digdownon = makeButton("+", 20, 10, 3, 1,
  184.     function()
  185.       log("Sending \"set digdown on\"")
  186.       sendCmd("control", "set", "digdown", "on")
  187.     end)
  188. btns.digdownoff = makeButton("-", 24, 10, 3, 1,
  189.     function()
  190.       log("Sending \"set digdown off\"")
  191.       sendCmd("control", "set", "digdown", "off")
  192.     end)
  193. btns.digupon = makeButton("+", 6, 11, 3, 1,
  194.     function()
  195.       log("Sending \"set digup on\"")
  196.       sendCmd("control", "set", "digup", "on")
  197.     end)
  198. btns.digupoff = makeButton("-", 10, 11, 3, 1,
  199.     function()
  200.       log("Sending \"set digup off\"")
  201.       sendCmd("control", "set", "digup", "off")
  202.     end)
  203. btns.persiston = makeButton("+", 20, 11, 3, 1,
  204.     function()
  205.       log("Sending \"set dig on\"")
  206.       sendCmd("control", "set", "persist", "on")
  207.     end)
  208. btns.persistoff = makeButton("-", 24, 11, 3, 1,
  209.     function()
  210.       log("Sending \"set dig on\"")
  211.       sendCmd("control", "set", "persist", "off")
  212.     end)
  213. btns.upx4 = makeButton("UP", 5, 13, 6, 1,
  214.     function()
  215.       log("Sending \"move up 4\"")
  216.       sendCmd("control", "move", "up", 4)
  217.     end)
  218. btns.upx2 = makeButton("UP", 5, 14, 6, 1,
  219.     function()
  220.       log("Sending \"move up 2\"")
  221.       sendCmd("control", "move", "up", 2)
  222.     end)
  223. btns.upx1 = makeButton("UP", 5, 15, 6, 1,
  224.     function()
  225.       log("Sending \"move up 1\"")
  226.       sendCmd("control", "move", "up", 1)
  227.     end)
  228. btns.downx1 = makeButton("DOWN", 5, 17, 6, 1,
  229.     function()
  230.       log("Sending \"move down 1\"")
  231.       sendCmd("control", "move", "down", 1)
  232.     end)
  233. btns.downx2 = makeButton("DOWN", 5, 18, 6, 1,
  234.     function()
  235.       log("Sending \"move down 2\"")
  236.       sendCmd("control", "move", "down", 2)
  237.     end)
  238. btns.downx4 = makeButton("DOWN", 5, 19, 6, 1,
  239.     function()
  240.       log("Sending \"move down 4\"")
  241.       sendCmd("control", "move", "down", 4)
  242.     end)
  243. btns.frwdx4 = makeButton("FRWD", 13, 13, 6, 1,
  244.     function()
  245.       log("Sending \"move frwd 4\"")
  246.       sendCmd("control", "move", "frwd", 4)
  247.     end)
  248. btns.frwdx2 = makeButton("FRWD", 13, 14, 6, 1,
  249.     function()
  250.       log("Sending \"move frwd 2\"")
  251.       sendCmd("control", "move", "frwd", 2)
  252.     end)
  253. btns.frwdx1 = makeButton("FRWD", 13, 15, 6, 1,
  254.     function()
  255.       log("Sending \"move frwd 1\"")
  256.       sendCmd("control", "move", "frwd", 1)
  257.     end)
  258. btns.backx1 = makeButton("BACK", 13, 17, 6, 1,
  259.     function()
  260.       log("Sending \"move back 1\"")
  261.       sendCmd("control", "move", "back", 1)
  262.     end)
  263. btns.backx2 = makeButton("BACK", 13, 18, 6, 1,
  264.     function()
  265.       log("Sending \"move back 2\"")
  266.       sendCmd("control", "move", "back", 2)
  267.     end)
  268. btns.backx4 = makeButton("BACK", 13, 19, 6, 1,
  269.     function()
  270.       log("Sending \"move back 4\"")
  271.       sendCmd("control", "move", "back", 4)
  272.     end)
  273. btns.left = makeButton("<", 21, 13, 5, 3,
  274.     function()
  275.       log("Sending \"move left\"")
  276.       sendCmd("control", "move", "left")
  277.     end)
  278. btns.right = makeButton(">", 21, 17, 5, 3,
  279.     function()
  280.       log("Sending \"move right\"")
  281.       sendCmd("control", "move", "right")
  282.     end)
  283.  
  284. labels.receiver = makeLabel("Rec:" .. receiver, 1, 8)
  285. labels.dig = makeLabel("Dig:", 1, 10)
  286. labels.digdown = makeLabel("DDwn:", 14, 10)
  287. labels.digup = makeLabel("DUp:", 1, 11)
  288. labels.persist = makeLabel("Prst:", 14, 11)
  289. labels.plusx4 = makeLabel("x4", 1, 13)
  290. labels.plusx2 = makeLabel("x2", 1, 14)
  291. labels.plusx1 = makeLabel("x1", 1, 15)
  292. labels.minusx1 = makeLabel("x1", 1, 17)
  293. labels.minusx2 = makeLabel("x2", 1, 18)
  294. labels.minusx4 = makeLabel("x4", 1, 19)
  295.  
  296. term.setCursorPos(1,1)
  297. term.clear()
  298. redraw()
  299. while true do
  300.   evt, btn, x, y = os.pullEvent("mouse_click")
  301.   handleClick(x, y)
  302. end
  303.  
  304. --print("[infinidig] [checkfuel]   ")
  305. --print("[smine] [vquarry] [wither]")
  306. --print("[refuel]                  ")
  307. --print("                          ")
  308. --print("+------+  +--------------+")
  309. --print("| STOP |  | Set receiver |")
  310. --print("+------+  +--------------+")
  311. --print("Rec:                      ")
  312. --print("                          ")
  313. --print("Dig: [+] [-] DDwn: [+] [-]")
  314. --print("DUp: [+] [-] Prst: [+] [-]")
  315. --print("                          ")
  316. --print("x4  [ UP ]  [FRWD]  +---+ ")
  317. --print("x2  [ UP ]  [FRWD]  | < | ")
  318. --print("x1  [ UP ]  [FRWD]  +---+ ")
  319. --print("                          ")
  320. --print("x1  [DOWN]  [BACK]  +---+ ")
  321. --print("x2  [DOWN]  [BACK]  | > | ")
  322. --print("x4  [DOWN]  [BACK]  +---+ ")
Add Comment
Please, Sign In to add comment