Advertisement
jamawie

HarvestGUI

Mar 12th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. local select_screen   = "fd5b599c-0519-4bb2-aeb1-5cec9c7bdfc9"
  2. local robot_modem_adr = ""
  3.                   -- buttonBg, buttonFg, pressedButtonBg, pressedButtonFg
  4. local colors          = {0x00accf, 0xFFC700, 0x008EAB,        0xFFC700       }
  5. local port            = 50
  6.  
  7. local component = require("component")
  8. local event = require("event")
  9. local keyboard  = require("keyboard")
  10. local gpu   = component.gpu
  11. local modem     = component.modem
  12. local w,= gpu.getResolution()
  13.  
  14. local buttons   = {}
  15.  
  16. local event_debug  = false
  17. local screen_debug = false
  18. local selected     = ""
  19.  
  20. --[[
  21. # Debug functions
  22.  
  23. > eventDebug(args...): void
  24. > screenDebug(args...): void
  25. --]]
  26. function eventDebug(eventName, arg1, arg2, arg3, arg4, arg5)
  27.     if eventName ~= nil then
  28.         print(eventName)
  29.     end
  30.     if arg1 ~= nil then
  31.         print(arg1)
  32.     end
  33.     if arg2 ~= nil then
  34.         print(arg2)
  35.     end
  36.     if arg3 ~= nil then
  37.         print(arg3)
  38.     end
  39.     if arg4 ~= nil then
  40.         print(arg4)
  41.     end
  42.     if arg5 ~= nil then
  43.         print(arg5)
  44.     end
  45. end
  46.  
  47. function screenDebug()
  48.     for i=1, 200 do
  49.         gpu.set(1, i, tostring(i))
  50.         gpu.set(i, 1, tostring(i), true)
  51.     end
  52. end
  53.  
  54. --[[
  55. # Button functions
  56. all functions around buttons
  57.  
  58. > createButton(
  59.     xPosition: int, yPosition: int, width: int, height: int,
  60.     displayedText: string, internName: string,
  61.     frontColor: color, backgroundColor: color
  62.   ): boolean
  63.   returns success
  64.  
  65. > getButton(
  66.     internName: string
  67.   ): array
  68.   returns array with button information
  69.  
  70. > removeButton(
  71.     internName: string
  72.   ): boolean
  73.   returns success
  74.  
  75. > checkButton(
  76.     screenAddress: string,
  77.     xPosition: int, yPosition: int,
  78.     button: int,
  79.     playerName: string
  80.   ) : string
  81.   returns internName or nil
  82.  
  83. > drawButtons(): void
  84. --]]
  85. function createButton(x1, y1, width, height, txt, name, fColor, bColor)
  86.     buttons[#buttons+1] = {x1, y1, width, height, txt, name, fColor, bColor}
  87. end
  88.  
  89. function getButton(name)
  90.     for button in buttons do
  91.         if button[6] == name then
  92.             return button
  93.         end
  94.     end
  95. end
  96.  
  97. function removeButton(name)
  98.     local no = 0
  99.     for i=1, #buttons do
  100.         if buttons[i][6] == name then
  101.             no = i
  102.         end
  103.     end
  104.  
  105.     local newButtons = {}
  106.     for i=1, #buttons do
  107.         if i ~= no then
  108.             newButtons[#newButtons+1] = buttons[i]
  109.         end
  110.     end
  111.  
  112.     buttons = newButtons
  113.  
  114.     return (no ~= 0)
  115. end
  116.  
  117. function checkButton(screenAdr, x, y, button, playerName)
  118.     for i=1,#buttons do
  119.         if x >= buttons[i][1] and x <= buttons[i][1] + buttons[i][3]
  120.         and y >= buttons[i][2] and y <= buttons[i][2] + buttons[i][4] then
  121.             print(buttons[i][6])
  122.             return buttons[i][6]
  123.         end
  124.     end
  125.  
  126.     return nil
  127. end
  128.  
  129. function drawButtons(clear)
  130.     switchScreen(select_screen)
  131.    
  132.     local aw, ah = gpu.getResolution()
  133.     if aw ~= 150 or ah ~= 20 then
  134.         gpu.setResolution(150,20)
  135.     end
  136.    
  137.     if clear ~= nil and clear == true then
  138.         gpu.fill(0,0,w,h," ")
  139.     end
  140.    
  141.     for i=1, #buttons do
  142.         gpu.setBackground(buttons[i][8], true)
  143.         gpu.setForeground(buttons[i][7], true)
  144.         gpu.fill(buttons[i][1], buttons[i][2], buttons[i][3], buttons[i][4], " ")
  145.         gpu.set(buttons[i][1] + ((buttons[i][3]-#buttons[i][5])/2), buttons[i][2] + (buttons[i][4]/2), buttons[i][5])
  146.     end
  147.    
  148.     if screen_debug == true then
  149.         screenDebug()
  150.     end
  151.    
  152.     switchScreenBack()
  153. end
  154.  
  155. --[[
  156. # Screen functions
  157. all functions around screens
  158.  
  159. > switchScreen(
  160.     oldScreenAddress: string, newScreenAddress: string
  161.   ): void
  162.  
  163. > scwitchScreenBack(): void
  164. --]]
  165. local oldScreenAdr = ""
  166. function switchScreen(new)
  167.     if gpu.getScreen() ~= new then
  168.         oldScreenAdr = gpu.getScreen()
  169.         gpu.bind(new)
  170.        
  171.         w, h = gpu.getResolution()
  172.     end
  173. end
  174.  
  175. function switchScreenBack()
  176.     switchScreen(oldScreenAdr)
  177. end
  178.  
  179. --[[
  180. # GUI functions
  181. all functions around the gui
  182.  
  183. > createSelectGUI(): void
  184. > removeSelectGUI(): void
  185.  
  186. > createAmountGUI(): void
  187. > removeAmountGUI(): void
  188. --]]
  189. function createSelectGUI()
  190.     createButton(10, 5,  20, 3, "Minicio",       "minicio",      1, 2)
  191.     createButton(10, 10, 20, 3, "Coal",          "coal",         1, 2)
  192.     createButton(10, 15, 20, 3, "Iron",          "iron",         1, 2)
  193.     createButton(40, 5,  20, 3, "Redstone",      "redstone",     1, 2)
  194.     createButton(40, 10, 20, 3, "Gold",          "gold",         1, 2)
  195.     createButton(40, 15, 20, 3, "Fluix",         "fluix",        1, 2)
  196.     createButton(70, 5,  20, 3, "Diamond",       "diamond",      1, 2)
  197.     createButton(70, 10, 20, 3, "Certus Quartz", "certusquartz", 1, 2)
  198.     createButton(70, 15, 20, 3, "Quartz",        "quartz",       1, 2)
  199. end
  200.  
  201. function removeSelectGUI()
  202.     removeButton("minicio")
  203.     removeButton("coal")
  204.     removeButton("iron")
  205.     removeButton("redstone")
  206.     removeButton("gold")
  207.     removeButton("fluix")
  208.     removeButton("diamond")
  209.     removeButton("certusquartz")
  210.     removeButton("quartz")
  211. end
  212.  
  213. function createAmountGUI()
  214.     createButton(116, 5,  9, 3, "1", "num_1", 3, 2)
  215.     createButton(126, 5,  9, 3, "2", "num_2", 3, 2)
  216.     createButton(136, 5,  9, 3, "3", "num_3", 3, 2)
  217.     createButton(116, 9,  9, 3, "4", "num_4", 3, 2)
  218.     createButton(126, 9,  9, 3, "5", "num_5", 3, 2)
  219.     createButton(136, 9,  9, 3, "6", "num_6", 3, 2)
  220.     createButton(116, 13, 9, 3, "7", "num_7", 3, 2)
  221.     createButton(126, 13, 9, 3, "8", "num_8", 3, 2)
  222.     createButton(136, 13, 9, 3, "9", "num_9", 3, 2)
  223.     createButton(116, 17, 9, 3, "C", "num_c", 3, 2)
  224.     createButton(126, 17, 9, 3, "0", "num_0", 3, 2)
  225.     createButton(136, 17, 9, 3, "E", "num_e", 3, 2)
  226. end
  227.  
  228. function removeAmountGUI()
  229.     removeButton("num_1")
  230.     removeButton("num_2")
  231.     removeButton("num_3")
  232.     removeButton("num_4")
  233.     removeButton("num_5")
  234.     removeButton("num_6")
  235.     removeButton("num_7")
  236.     removeButton("num_8")
  237.     removeButton("num_9")
  238.     removeButton("num_c")
  239.     removeButton("num_0")
  240.     removeButton("num_e")
  241. end
  242.  
  243. --[[
  244. # Network functions
  245. all functions around communication
  246.  
  247. > sendStatusRequest(): void
  248. > isRobotAvailable(): boolean
  249.  
  250. > sendingQueue(): void
  251. > messageHandler(receiver, sender, port, distance, msg): void
  252.  
  253. > addInstruction(): void
  254. > sendInstructions(): void
  255. --]]
  256. local robot_availability = false
  257. local task_Description   = ""
  258. local messagesToSend     = {}
  259. function sendStatusRequest()
  260.     local success_1 = modem.send(robot_modem_adr, 50, "getStatus")
  261.     local success_2 = modem.send(robot_modem_adr, 50, "getTask")
  262.  
  263.     if success_1 == false or success_2 == false then
  264.         print("ERROR: UNABLE to COMMUNICATE with the robot")
  265.         os.exit()
  266.     end
  267. end
  268.  
  269. function isRobotAvailable()
  270.     return robot_availability
  271. end
  272.  
  273. function sendingQueue()
  274.     if isRobotAvailable == true and #messagesToSend > 0 then
  275.         modem.send(robot_modem_adr, 50, messagesToSend[1])
  276.  
  277.         local help = {}
  278.         for i=2,#messagesToSend do
  279.             help[#help+1] = messagesToSend[i]
  280.         end
  281.         messagesToSend = help
  282.  
  283.         sendStatusRequest()
  284.     end
  285. end
  286.  
  287. function messageHandler(local receiver, local sender, local port, local distance, local msg)
  288.     if sender ~= robot_modem_adr or port ~= 50 then
  289.         return nil
  290.     end
  291.  
  292.     if msg == "available" then
  293.         robot_availability = true
  294.     elseif msg == "unavailable" then
  295.         robot_availability = false
  296.     end
  297. end
  298.  
  299. --[[
  300. # main script
  301. --]]
  302. for i=1, #colors do
  303.     gpu.setPaletteColor(i,colors[i])
  304. end
  305.  
  306. if modem.open(50) == false then
  307.     print("ERROR: UNABLE to OPEN PORT!")
  308.     os.exit()
  309. end
  310.  
  311. createSelectGUI()
  312. createAmountGUI()
  313. drawButtons(true)
  314. while true do  
  315.     eventName, arg1, arg2, arg3, arg4, arg5 = event.pull();
  316.    
  317.     if event_debug == true then
  318.         eventDebug(eventName, arg1, arg2, arg3, arg4, arg5)
  319.     end
  320.    
  321.     if eventName == "touch" and arg1 == select_screen then
  322.         checkButton(arg1, arg2, arg3, arg4, arg5)
  323.         drawButtons()
  324.     elseif eventName == "key_down" then
  325.         if arg2 == 116 and arg4 == "jamawie" then
  326.             break
  327.         elseif arg2 == 101 and arg4 == "jamawie" then
  328.             if event_debug == true then
  329.                 event_debug = false
  330.             else
  331.                 event_debug = true
  332.             end
  333.         elseif arg2 == 115 and arg4 == "jamawie" then
  334.             if screen_debug == true then
  335.                 screen_debug = false
  336.             else
  337.                 screen_debug = true
  338.             end
  339.             drawButtons()
  340.         end
  341.     end
  342. end
  343.  
  344. event.listen("touch", checkButton)
  345. event.listen("modem_message", messageHandler)
  346.  
  347. event.timer(10, sendStatusRequest, math.huge)
  348. event.timer(10, sendingQueue, math.huge)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement