Advertisement
Shaka01

mainPC startup

Jan 31st, 2023 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. -- This function creates a button with the specified label and color and returns it
  2. function createButton(label, bgColor, textColor)
  3.   local button = {}
  4.   button.label = label
  5.   button.bgColor = bgColor
  6.   button.textColor = textColor
  7.   button.x = 0
  8.   button.y = 0
  9.   button.width = 0
  10.   button.height = 0
  11.  
  12.   -- This function sets the position and dimensions of the button
  13.   function button:setPos(x, y, width, height)
  14.     self.x = x
  15.     self.y = y
  16.     self.width = width
  17.     self.height = height
  18.   end
  19.  
  20.   -- This function draws the button on the screen
  21.   function button:draw(monitor)
  22.     monitor.setBackgroundColor(bgColor)
  23.     for i = self.x, self.x + self.width - 1 do
  24.       monitor.setCursorPos(i, self.y)
  25.       monitor.write(" ")
  26.       monitor.setCursorPos(i, self.y + self.height - 1)
  27.       monitor.write(" ")
  28.     end
  29.     for i = self.y, self.y + self.height - 1 do
  30.       monitor.setCursorPos(self.x, i)
  31.       monitor.write(" ")
  32.       monitor.setCursorPos(self.x + self.width - 1, i)
  33.       monitor.write(" ")
  34.     end
  35.     local xPos = math.floor(self.x + self.width / 2 - #self.label / 2)
  36.     local yPos = math.floor(self.y + self.height / 2)
  37.     monitor.setCursorPos(xPos, yPos)
  38.     monitor.setTextColor(button.textColor)
  39.     monitor.write(self.label)
  40.   end
  41.  
  42.   -- This function checks if the button is being touched at the specified coordinates
  43.   function button:isTouched(x, y)
  44.     return x >= self.x and x < self.x + self.width and y >= self.y and y < self.y + self.height
  45.   end
  46.  
  47.   return button
  48. end
  49.  
  50. -- This function displays the buttons on the screen and waits for a touch event
  51. function showButtons(buttons)
  52.   local monitor = peripheral.wrap("top")
  53.   monitor.setTextScale(0.5)
  54.   monitor.setBackgroundColor(colors.black)
  55.   monitor.clear()
  56.   local width, height = monitor.getSize()
  57.   -- print(width)
  58.  
  59.   -- Calculate the dimensions of each button
  60.   local buttonHeight = math.floor(height / 2)
  61.   local buttonWidth = math.floor(width / 5)
  62.  
  63.   -- Set the position and dimensions of each button
  64.   for i, button in ipairs(buttons) do
  65.     local row = math.floor((i - 1) / 5)
  66.     local col = (i - 1) % 5
  67.     -----try to center by shaka
  68.     if width == 79 then loly = 3
  69.     elseif width == 57 then loly = 2
  70.     elseif width < 57 then loly = 1
  71.     end
  72.    
  73.    
  74.    
  75.    
  76.     button:setPos(col * buttonWidth + loly, row * buttonHeight + 1, buttonWidth, buttonHeight)
  77.   end
  78.  
  79.   -- Draw the buttons on the screen
  80.   for i, button in ipairs(buttons) do
  81.     button:draw(monitor)
  82.   end
  83.  
  84.   -- Wait for a touch event
  85.   local event, side, x, y = os.pullEvent("monitor_touch")
  86.   for i, button in ipairs(buttons) do
  87.     if button:isTouched(x, y) then
  88.       -- Button was touched, do something here
  89.      
  90.       if i == 1 then
  91.         shell.run("topitems")
  92.        
  93.     elseif i == 2 then
  94.         shell.run("showchanges")
  95.     elseif i == 3 then
  96.         shell.run("cheststuff")
  97.  
  98.     elseif i == 10 then
  99.             os.reboot()
  100.     else
  101.         monitor.setBackgroundColor(colors.black)
  102.         monitor.clear()
  103.         monitor.setCursorPos(width/2 - 10 , height/2)
  104.         monitor.write("Button not configured.")
  105.         local event, side, x, y = os.pullEvent("monitor_touch")
  106.     end
  107.    
  108.    
  109.    
  110.    
  111.       break
  112.     end
  113.   end
  114. end
  115.  
  116. -- Create some buttons
  117. local buttons = {}
  118. table.insert(buttons, createButton("Top 15", colors.blue, colors.green))
  119. table.insert(buttons, createButton("Inv Changes", colors.gray, colors.green))
  120. --for i = 1, 6 do
  121.   --table.insert(buttons, createButton("Button " .. i, colors.green, colors.black))
  122. --end
  123. --table.insert(buttons, createButton("test", colors.blue, colors.black))
  124. --table.insert(buttons, createButton("plz\n kk", colors.red, colors.green))
  125.  
  126. -- Show the buttons on the screen
  127. while true do
  128. showButtons(buttons)
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement