Advertisement
Shaka01

Messenger Client Main - maybe dependancy

Mar 5th, 2023 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.73 KB | None | 0 0
  1. shaka = require("API")
  2. if fs.exists("viewAll") == false then
  3. shell.run("pastebin", "get", "CTsf2Rbb", "viewAll")
  4. end
  5.  
  6.  
  7.  
  8. if not fs.exists("serverID") then
  9.     rednet.broadcast("give", "serverID")
  10.     local id, message = rednet.receive("idSupply")
  11.     local file = fs.open("serverID", "w")
  12.     file.write(id)
  13.     file.close()
  14.     serverID = tonumber(id)
  15. else
  16.     local file = fs.open("serverID", "r")
  17.     serverID = file.readAll()
  18.     file.close()
  19.     serverID = tonumber(serverID)
  20. end
  21.    
  22. local function receiveInfos()
  23.     local msg = nil
  24.     local file = fs.open(".userData", "r")
  25.     local userName = file.readAll()
  26.     file.close()
  27.     rednet.broadcast(userName, "anyMessages?")
  28.     local sender, msg = rednet.receive("notificationReminder", 1)
  29.     if msg ~= nil then
  30.         if tonumber(msg) ~= 0 then
  31.             term.setCursorPos(1,1)
  32.             shaka.changeColors(colors.green, colors.black)
  33.             term.clearLine()
  34.             if tonumber(msg) > 1 then
  35.                 shaka.centerText("New messages: " ..msg)
  36.             else
  37.                 shaka.centerText("New message!")
  38.             end
  39.         end
  40.     else
  41.         shaka.changeColors(colors.red, colors.black)
  42.         term.clear()
  43.         shaka.centerText("Server not responding!", 7)
  44.         term.setCursorPos(8, 9)
  45.         textutils.slowPrint("Restarting..", 10)
  46.         sleep(2)
  47.         os.reboot()
  48.     end
  49. end
  50.  
  51. function saveUserName()
  52.     shaka.clearScreen()
  53.     -- Prompt the user to enter their name
  54.     term.setBackgroundColor(colors.gray)
  55.     term.setTextColor(colors.black)
  56.     term.clearLine()
  57.     print("Please enter your name:\n")
  58.     shaka.resetColors()
  59.     local name = tostring(read())
  60.     if name ~= nil and name ~= "" then
  61.         --send information to server
  62.         rednet.send(serverID, name, "newUser")
  63.         local sender, message = rednet.receive("userConfirm", 2)
  64.         if message == "worked" then
  65.             -- Open the ".userData" file for writing
  66.             local file = fs.open(".userData", "w")
  67.  
  68.             -- Write the user's name to the file
  69.             file.writeLine(name)
  70.  
  71.             -- Close the file
  72.             file.close()
  73.             shaka.clearScreen()
  74.             term.setTextColor(colors.black)
  75.             term.setBackgroundColor(colors.green)
  76.             term.clearLine()
  77.             textutils.slowPrint("Success!", 5)
  78.             shaka.resetColors()
  79.             print("\nThank you, " ..name.."!")
  80.             print("\nYour name has been saved.")
  81.         elseif message == nil then -- no response
  82.             shaka.clearScreen()
  83.             shaka.changeColors(colors.red, colors.black)
  84.             term.clearLine()
  85.             print("Server not responding!")
  86.             shaka.resetColors()
  87.         elseif message == "fail" then -- username already exists
  88.                 shaka.nextLine()
  89.                 shaka.changeColors(colors.red, colors.black)
  90.                 term.clearLine()
  91.                 print("User " ..name.. " already exists!\n")
  92.                 shaka.changeColors(colors.black, colors.yellow)
  93.                 print("Please choose another name.\n")
  94.                 term.setTextColor(colors.gray)
  95.                 textutils.slowPrint("Press any key to continue..")
  96.                 event = os.pullEvent("key")
  97.                 shell.run("messenger")
  98.                 return
  99.         end
  100.     else
  101.         term.setBackgroundColor(colors.red)
  102.         term.clear()
  103.         shaka.centerText("Not a valid name!\n", 8)
  104.        
  105.     end
  106.     term.setTextColor(colors.gray)
  107.     textutils.slowPrint("\nPress any key to continue..")
  108.     event = os.pullEvent("key")
  109. end
  110.  
  111. if not fs.exists(".userData") then ---if no username exists create one
  112.     saveUserName()
  113. end
  114.  
  115.  
  116. function getUserList()
  117. knownUsers = {}
  118.     rednet.broadcast("give", "requestUserList")
  119.     local sender, message = rednet.receive("userRequestAnswer", 2)
  120.     if message ~= "noUsers" and message ~= "" and message ~= nil then
  121.         message = textutils.unserialize(message)
  122.         for k, v in pairs(message) do
  123.             knownUsers[k] = v.name
  124.         end
  125.         -- table.sort(knownUsers)
  126.     elseif message == "noUsers" or message == "" then
  127.         knownUsers = {}
  128.     else
  129.     term.setBackgroundColor(colors.red)
  130.     term.clear()
  131.     term.setTextColor(colors.black)
  132.     shaka.centerText("Can't connect to server", 8)
  133.     sleep(2)
  134.     os.reboot()
  135.     end
  136. end
  137.  
  138.  
  139. function sendMessage()
  140.   -- Load the user's name from the ".userData" file
  141.   local file = fs.open(".userData", "r")
  142.   local userName = file.readAll()
  143.   file.close()
  144.  
  145.   -- Load the list of known users from the "knownUsers" table
  146.   getUserList()
  147.  
  148.     local function checkUserInDatabase()
  149.         for k, v in ipairs(knownUsers) do
  150.             if v == userName then
  151.                 return true
  152.             end
  153.         end
  154.     return false
  155.     end
  156.    
  157.     if checkUserInDatabase() == false then
  158.         shaka.clearScreen()
  159.         shaka.changeColors(colors.red, colors.black)
  160.         print("User " ..userName.. " doesn't exist on the server.")
  161.         shaka.changeColors(colors.black, colors.yellow)
  162.         textutils.slowPrint("\nPlease register your user!")
  163.         shaka.changeColors(colors.black, colors.gray)
  164.         textutils.slowPrint("\nPress any key to continue..")
  165.         os.pullEvent("key")
  166.         saveUserName() 
  167.         os.reboot()
  168.     end
  169.    
  170.   -- Prompt the user to select a receiver from the list of known users
  171.   shaka.clearScreen()
  172.   shaka.changeColors(colors.lightBlue, colors.black)
  173.   term.clearLine()
  174.   print("Click a recipient:\n")
  175.   local a, b = term.getSize()
  176.   local xa, ya = term.getCursorPos()
  177.   term.setCursorPos(a - 1, 1)
  178.   term.setBackgroundColor(colors.red)
  179.   term.write(" X")
  180.   term.setCursorPos(xa,ya)
  181.  
  182.   for i, user in ipairs(knownUsers) do
  183.     if i % 2 == 0 then -- alternate gray background
  184.       term.setBackgroundColor(colors.gray)
  185.       term.setTextColor(colors.black)
  186.       term.clearLine()
  187.     else
  188.       term.setBackgroundColor(colors.lightGray)
  189.       term.setTextColor(colors.black)
  190.       term.clearLine()
  191.     end
  192.     user = shortenString(user, 26)
  193.     userShort = shortenString(userName, 26)
  194.     if user == userShort then
  195.         print("Note to Self")
  196.     else
  197.         print(user)
  198.     end
  199.   end
  200.  
  201.   -- Wait for the user to click on a receiver
  202.   local receiverIndex
  203.   while not receiverIndex do
  204.     local event, button, x, y = os.pullEvent("mouse_click")
  205.         if y == 1 then
  206.             shell.run("messenger")
  207.             return
  208.         end
  209.     -- Check if the click is within the bounds of the receiver list
  210.     if x >= 1 and x <= term.getSize() and y >= 3 and y <= 2 + #knownUsers then
  211.       receiverIndex = y - 2
  212.     end
  213.   end
  214.  
  215.   -- Get the selected receiver from the list
  216.   local receiver = knownUsers[receiverIndex]
  217.  
  218.   -- Prompt the user to enter a message
  219.   shaka.clearScreen()
  220.   shaka.changeColors(colors.lightBlue, colors.black)
  221.   term.clearLine()
  222.   term.write("Message to: ")
  223.   term.setTextColor(colors.black)
  224.   if user == userShort then
  225.     term.write("Self")
  226. else
  227.   term.write(shaka.shortenString(receiver, 12))
  228.  end
  229.   shaka.nextLine()
  230.   shaka.changeColors(colors.black, colors.gray)
  231.   term.clearLine()
  232.   print("(double enter to send)\n")
  233.   shaka.changeColors(colors.black, colors.white)
  234.    
  235. local messageLines = {}
  236.  
  237. local function readAllText()
  238.   repeat
  239.     local line = read()
  240.     if line ~= "" then
  241.       table.insert(messageLines, line)
  242.     end
  243.   until line == ""
  244.  
  245.     message = table.concat(messageLines, "\n")
  246.    
  247.     if message ~= "" then
  248.         shaka.clearScreen()
  249.         shaka.changeColors(colors.yellow, colors.black)
  250.         term.clearLine()
  251.         print("Send to " ..receiver.. "?")
  252.         shaka.changeColors(colors.black, colors.lightGray)
  253.         term.clearLine()
  254.         print("Enter to send\nAny other key to cancel\n")
  255.         shaka.resetColors()
  256.         print(message)
  257.         event, key = os.pullEvent("key")
  258.         if key ~= keys.enter then
  259.             shaka.nextLine()
  260.             shaka.changeColors(colors.yellow, colors.black)
  261.             term.clearLine()
  262.             textutils.slowPrint("Aborting..")
  263.             sleep(1)
  264.             shell.run("messenger")
  265.             return
  266.         else
  267.             shaka.nextLine()
  268.             shaka.changeColors(colors.yellow, colors.black)
  269.             term.clearLine()
  270.             textutils.slowPrint("Sending..", 10)
  271.           -- Send the message to the selected receiver
  272.             if receiver then
  273.                 rednet.send(serverID, userName .. "+|" .. message .. "+|" .. receiver, "newMessage")
  274.             else
  275.                 print("Invalid receiver selection.")
  276.             end
  277.            
  278.  
  279.            
  280.             local sender, text = rednet.receive("messageConfirm", 2)
  281.             if text == nil then
  282.                 term.setBackgroundColor(colors.red)
  283.                 term.clear()
  284.                 term.setTextColor(colors.black)
  285.                 print("No confirmation! Is server offline?")
  286.                 textutils.slowPrint("Restarting..")
  287.                 sleep(1)
  288.                 shell.run("messenger")
  289.                 return
  290.             elseif text == "failed" then
  291.                 shaka.clearScreen()
  292.                 shaka.changeColors(colors.red, colors.black)
  293.                 print("User " ..userName.. " isn't registered with the server.")
  294.                 shaka.changeColors(colors.black, colors.yellow)
  295.                 textutils.slowPrint("\nPlease register your user!")
  296.                 shaka.changeColors(colors.black, colors.gray)
  297.                 textutils.slowPrint("\nPress any key to continue..")
  298.                 os.pullEvent("key")
  299.                 saveUserName()
  300.                 return
  301.             else
  302.                 local plz, kk = term.getCursorPos()
  303.                 term.setCursorPos(1, 1)
  304.                 shaka.changeColors(colors.green, colors.black)
  305.                 term.clearLine()
  306.                 print("Send to " ..receiver.. "?\n")
  307.                 term.setCursorPos(1, kk)
  308.                 shaka.nextLine()
  309.                 local plz, kk = term.getCursorPos()
  310.                 kkUp = kk - 2
  311.                 term.setCursorPos(1, kkUp)
  312.                 shaka.changeColors(colors.green, colors.black)
  313.                 term.clearLine()
  314.                 print("Sending..")
  315.                 term.setCursorPos(plz, kk)
  316.                 shaka.changeColors(colors.black, colors.green)
  317.                 term.clearLine()
  318.                 textutils.slowPrint("Success!", 10)
  319.             end
  320.         sleep(1)
  321.         end
  322.     else
  323.         shaka.nextLine()
  324.         shaka.changeColors(colors.orange, colors.black)
  325.         term.clearLine()
  326.         print("Message is empty!\n")
  327.         shaka.changeColors(colors.black, colors.yellow)
  328.         textutils.slowPrint("Restarting..", 10)
  329.         sleep(1)
  330.     end
  331.         shell.run("messenger")
  332.         return
  333. end
  334.  
  335. readAllText()
  336.  
  337. end
  338.  
  339. local function getMessages()
  340.     local file = fs.open(".userData", "r")
  341.     local userName = file.readAll()
  342.     file.close()
  343.     rednet.broadcast(userName, "messageUpdate")
  344.     local id, msg = rednet.receive("allMessages", 2)
  345.     if fs.exists(".myMessages") then
  346.         fs.delete(".myMessages")
  347.     end
  348.     local file = fs.open(".myMessages", "w")
  349.     file.write(msg)
  350.     file.close()
  351.         if msg == nil then
  352.         term.setBackgroundColor(colors.red)
  353.         term.clear()
  354.         term.setTextColor(colors.black)
  355.         shaka.centerText("Can't connect to server", 8)
  356.         sleep(2)
  357.         os.reboot()
  358.     else
  359.         msg = textutils.unserialize(msg)
  360.         return msg
  361.     end
  362. end
  363.  
  364. function shortenString(str, length)
  365.   if string.len(str) > length then
  366.     return string.sub(str, 1, length)
  367.   else
  368.     return str
  369.   end
  370. end
  371.  
  372. function inverseTable(t)
  373.   local inversedTable = {}
  374.   for i = #t, 1, -1 do
  375.     table.insert(inversedTable, t[i])
  376.   end
  377.   return inversedTable
  378. end
  379.  
  380. function filterTable(tableName, userName)  
  381.   local filtered = {}
  382.   for _, message in ipairs(tableName) do
  383.     if message.receiver == userName then
  384.       table.insert(filtered, message)
  385.     end
  386.   end
  387.   return filtered
  388. end
  389.  
  390.  
  391.  
  392. local function displayMessages()
  393.   local file = fs.open(".userData", "r")
  394.   local userName = file.readAll()
  395.   file.close()
  396.   rednet.broadcast(userName, "delNotifications")
  397.   local msg = getMessages()
  398.   msg = filterTable(msg, userName)
  399.   shaka.clearScreen()
  400.   shaka.changeColors(colors.green, colors.black)
  401.   term.clearLine()
  402.   print("Recent inbox:")
  403.  
  404.   local a, b = term.getSize()
  405.   local xa, ya = term.getCursorPos()
  406.   term.setCursorPos(a - 1, 1)
  407.   term.setBackgroundColor(colors.red)
  408.   term.write(" X")
  409.   term.setCursorPos(xa,ya)
  410.  
  411.   -- Display the last 6 messages
  412.   local startIndex = math.max(1, #msg - 5)
  413.   for i = #msg, startIndex, -1 do
  414.     local message = msg[i]
  415.     local sender = shortenString(message.sender, 16)
  416.     local receiver = shortenString(message.receiver, 11)
  417.     local date = message.date
  418.     local time = message.time
  419.     local text = message.text
  420.  
  421.         -- Check if the user is the receiver
  422.         local isReceiver = string.find(userName, receiver) ~= nil
  423.  
  424.         -- Check if the sender and receiver are the same
  425.         local isSameUser = sender == receiver
  426.  
  427.         -- Set the text color based on whether the user is the receiver or the sender
  428.         if isSameUser then
  429.           term.setTextColor(colors.yellow)
  430.         elseif isReceiver then
  431.           term.setTextColor(colors.green)
  432.         else
  433.           term.setTextColor(colors.white)
  434.         end
  435.  
  436.  
  437.  
  438.     -- Display message information without text
  439.     term.setBackgroundColor(colors.black)
  440.     term.clearLine()
  441.     term.setBackgroundColor(colors.black)
  442.     print()
  443.     term.setBackgroundColor(colors.gray)
  444.     term.clearLine()
  445.     shaka.centerText(date .. " - " .. time)
  446.     shaka.nextLine()
  447.     term.clearLine()
  448.     if isSameUser then
  449.         shaka.centerText("Note to Self")
  450.     else
  451.     shaka.centerText("Sender: " ..sender)
  452.     end
  453.     shaka.nextLine()
  454.   end
  455.  
  456.  
  457.   -- Wait for mouse click event
  458.   while true do
  459.     local event, _, x, y = os.pullEvent("mouse_click")
  460.     if y == 1 then
  461.         shell.run("messenger")
  462.         return
  463.     end
  464.    
  465.    
  466.     y = y - 2 ------adjustments
  467.     if _ == 1 then
  468.       -- Calculate the index of the clicked message based on the y coordinate
  469.       if #msg < 6 then
  470.         clickedIndex = (startIndex + (#msg - 1)) - math.floor(y / 3)
  471.         else
  472.         clickedIndex = (startIndex + 5) - math.floor(y / 3)
  473.       end
  474.       -- Check if the clicked message is within the valid range
  475.       if clickedIndex >= startIndex and clickedIndex <= #msg then
  476.         local message = msg[clickedIndex]
  477.         local sender = shortenString(message.sender, 16)
  478.         local receiver = shortenString(message.receiver, 13)
  479.         local date = message.date
  480.         local time = message.time
  481.         local text = message.text
  482.  
  483.         -- Define the maximum number of lines that can fit on the screen
  484.         local maxLines = 15
  485.  
  486.         -- Define the current line number and the total number of lines
  487.         local currentLine = 0
  488.         local totalLines = 0
  489.  
  490.         -- Split the message text into lines
  491.         local lines = {}
  492.         for line in string.gmatch(text, "[^\r\n]+") do
  493.           table.insert(lines, line)
  494.           totalLines = totalLines + 1
  495.         end
  496.        
  497.         ---check if scrolling makes sense
  498.         if totalLines > maxLines then
  499.             scrollAble = true
  500.         else
  501.             scrollAble = false
  502.         end
  503.  
  504.         -- Clear the screen and display the message text
  505.         shaka.clearScreen()
  506.         term.setTextColor(colors.gray)
  507.         print(date .. " - " .. time)
  508.         shaka.changeColors(colors.gray, colors.lightBlue)
  509.         term.clearLine()
  510.   if sender == receiver then
  511.     print("Note to Self")
  512.   else
  513.     print(sender .. " -> " .. receiver)
  514.   end
  515.         shaka.resetColors()
  516.         shaka.nextLine()
  517.         if scrollAble then
  518.         local ab, cd = term.getCursorPos()
  519.         local ba, dc = term.getSize()
  520.         term.setCursorPos(1, dc-1)
  521.         term.setTextColor(colors.gray)
  522.         print("Scrolling available..")
  523.         term.setTextColor(colors.white)
  524.         term.setCursorPos(ab, cd)
  525.         end
  526.  
  527.         -- Print the first set of lines that can fit on the screen
  528.         for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  529.             if i ~= 0 then
  530.                 print(lines[i])
  531.             end
  532.         end
  533.        
  534.         -- Listen for mouse scroll to scroll up or down or click to end
  535.         -- sleep(0.5)
  536.         repeat
  537.             local eventData = {os.pullEvent()}
  538.             local event = eventData[1]
  539.           button = tonumber(eventData[2])
  540.           if button == -1 and currentLine > 1 then
  541.             -- Scroll up
  542.             currentLine = currentLine - 1
  543.             shaka.clearScreen()
  544.             term.setTextColor(colors.gray)
  545.             print(date .. " - " .. time)
  546.             shaka.changeColors(colors.black, colors.lightBlue)
  547.             print(sender .. " -> " .. receiver)
  548.             shaka.resetColors()
  549.             print()
  550.             for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  551.               print(lines[i])
  552.             end
  553.  
  554.           elseif button == 1 and currentLine < totalLines - maxLines + 1 then
  555.             -- Scroll down
  556.             currentLine = currentLine + 1
  557.             shaka.clearScreen()
  558.             term.setTextColor(colors.gray)
  559.             print(date .. " - " .. time)
  560.             shaka.changeColors(colors.black, colors.lightBlue)
  561.             print(sender .. " -> " .. receiver)
  562.             shaka.resetColors()
  563.             print()
  564.             for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  565.               print(lines[i])
  566.             end
  567.           end
  568.         until event == "mouse_click"
  569.        
  570.  
  571.         -- Redraw the messages on the screen
  572.         shaka.clearScreen()
  573.          shaka.changeColors(colors.green, colors.black)
  574.         term.clearLine()
  575.         print("Recent Inbox:")
  576.           local a, b = term.getSize()
  577.           local xa, ya = term.getCursorPos()
  578.           term.setCursorPos(a - 1, 1)
  579.           term.setBackgroundColor(colors.red)
  580.           term.write(" X")
  581.           term.setCursorPos(xa,ya)
  582.        
  583.         for i = #msg, startIndex, -1 do
  584.           local message = msg[i]
  585.         local sender = shortenString(message.sender, 10)
  586.         local receiver = shortenString(message.receiver, 10)
  587.         local date = message.date
  588.         local time = message.time
  589.          
  590.          
  591.         -- Check if the user is the receiver
  592.         local isReceiver = string.find(userName, receiver) ~= nil
  593.  
  594.         -- Check if the sender and receiver are the same
  595.         local isSameUser = sender == receiver
  596.  
  597.         -- Set the text color based on whether the user is the receiver or the sender
  598.         if isSameUser then
  599.           term.setTextColor(colors.yellow)
  600.         elseif isReceiver then
  601.           term.setTextColor(colors.green)
  602.         else
  603.           term.setTextColor(colors.white)
  604.         end
  605.  
  606.  
  607.           -- Display message information without text
  608.             term.setBackgroundColor(colors.black)
  609.             term.clearLine()
  610.             term.setBackgroundColor(colors.black)
  611.             print()
  612.             term.setBackgroundColor(colors.gray)
  613.             term.clearLine()
  614.             shaka.centerText(date .. " - " .. time)
  615.             shaka.nextLine()
  616.             term.clearLine()
  617.             if isSameUser then
  618.         shaka.centerText("Note to Self")
  619.     else
  620.     shaka.centerText("Sender: " ..sender)
  621.     end
  622.             shaka.nextLine()
  623.         end
  624.       end
  625.     end
  626.   end
  627. end
  628.  
  629. function reboot()
  630.     os.reboot()
  631. end
  632.  
  633. function viewAll()
  634.     getMessages()
  635.     shell.run("viewAll")
  636. end
  637.  
  638.  
  639. function showButtons()
  640. term.setBackgroundColor(colors.black)
  641. term.clear()
  642.   -- Define the buttons and their associated programs and colors
  643.   local buttons = {
  644.       {label="Send message", program=sendMessage, color = colors.lightBlue},
  645.       {label="Recent inbox", program=displayMessages, color = colors.green},
  646.       {label="View all messages", program=viewAll, color = colors.gray},
  647.       {label="Exit", program=reboot, color = colors.red},
  648.       -- {label="Program 5", program="program3.lua", color = colors.red}
  649.   }
  650.  
  651.   -- Set the background color and padding size
  652.   local bgColor = colors.black
  653.   local padding = 2
  654.  
  655.   -- Calculate the size of the buttons and the total size of the interface
  656.   local buttonWidth = 24
  657.   local buttonHeight = 3
  658.   local interfaceWidth, interfaceHeight = term.getSize()
  659.   interfaceHeight = (buttonHeight + padding) * #buttons + padding
  660.  
  661.   -- Create a new canvas to draw the interface on
  662.   local canvas = {}
  663.   for y = 1, interfaceHeight do
  664.     canvas[y] = {}
  665.     for x = 1, interfaceWidth do
  666.       canvas[y][x] = {bg = bgColor, fg = colors.white, char = " "}
  667.     end
  668.   end
  669.  
  670.   -- Draw the buttons on the canvas
  671.   for i, button in ipairs(buttons) do
  672.       local x = padding
  673.       local y = padding + (i-1) * (buttonHeight + padding)
  674.       for y2 = y, y + buttonHeight - 1 do
  675.         for x2 = x, x + buttonWidth - 1 do
  676.           canvas[y2][x2].bg = button.color
  677.         end
  678.       end
  679.       local label = string.sub(button.label, 1, buttonWidth)
  680.       local labelLength = #label
  681.       local labelX = x + math.floor((buttonWidth - labelLength) / 2)
  682.       local labelY = y + math.floor((buttonHeight - 1) / 2)
  683.       for j = 1, labelLength do
  684.         canvas[labelY][labelX+j-1] = {bg = button.color, fg = colors.black, char = string.sub(label, j, j)}
  685.       end
  686.   end
  687.  
  688.   -- Draw the canvas on the screen
  689.   term.setBackgroundColor(bgColor)
  690.   term.clear()
  691.   for y = 1, interfaceHeight do
  692.     for x = 1, interfaceWidth do
  693.       term.setCursorPos(x, y)
  694.       term.setTextColor(canvas[y][x].fg)
  695.       term.setBackgroundColor(canvas[y][x].bg)
  696.       term.write(canvas[y][x].char)
  697.     end
  698.   end
  699.   receiveInfos()
  700.  
  701. -- Wait for a button to be clicked
  702.   while true do
  703.     local event, _, x, y = os.pullEvent("mouse_click")
  704.     for i, button in ipairs(buttons) do
  705.       local buttonX = padding
  706.       local buttonY = padding + (i-1) * (buttonHeight + padding)
  707.       if x >= buttonX and x < buttonX + buttonWidth and y >= buttonY and y < buttonY + buttonHeight then
  708.       showNotifications = false
  709.         -- print(button.label)
  710.         button.program()
  711.         return
  712.       end
  713.     end
  714.   end
  715. end
  716.  
  717.  
  718. while true do
  719. showButtons()
  720. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement