Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This function creates a button with the specified label and color and returns it
- function createButton(label, bgColor, textColor)
- local button = {}
- button.label = label
- button.bgColor = bgColor
- button.textColor = textColor
- button.x = 0
- button.y = 0
- button.width = 0
- button.height = 0
- -- This function sets the position and dimensions of the button
- function button:setPos(x, y, width, height)
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- end
- -- This function draws the button on the screen
- function button:draw(monitor)
- monitor.setBackgroundColor(bgColor)
- for i = self.x, self.x + self.width - 1 do
- monitor.setCursorPos(i, self.y)
- monitor.write(" ")
- monitor.setCursorPos(i, self.y + self.height - 1)
- monitor.write(" ")
- end
- for i = self.y, self.y + self.height - 1 do
- monitor.setCursorPos(self.x, i)
- monitor.write(" ")
- monitor.setCursorPos(self.x + self.width - 1, i)
- monitor.write(" ")
- end
- local xPos = math.floor(self.x + self.width / 2 - #self.label / 2)
- local yPos = math.floor(self.y + self.height / 2)
- monitor.setCursorPos(xPos, yPos)
- monitor.setTextColor(button.textColor)
- monitor.write(self.label)
- end
- -- This function checks if the button is being touched at the specified coordinates
- function button:isTouched(x, y)
- return x >= self.x and x < self.x + self.width and y >= self.y and y < self.y + self.height
- end
- return button
- end
- -- This function displays the buttons on the screen and waits for a touch event
- function showButtons(buttons)
- local monitor = peripheral.wrap("top")
- monitor.setTextScale(0.5)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- local width, height = monitor.getSize()
- -- print(width)
- -- Calculate the dimensions of each button
- local buttonHeight = math.floor(height / 2)
- local buttonWidth = math.floor(width / 5)
- -- Set the position and dimensions of each button
- for i, button in ipairs(buttons) do
- local row = math.floor((i - 1) / 5)
- local col = (i - 1) % 5
- -----try to center by shaka
- if width == 79 then loly = 3
- elseif width == 57 then loly = 2
- elseif width < 57 then loly = 1
- end
- button:setPos(col * buttonWidth + loly, row * buttonHeight + 1, buttonWidth, buttonHeight)
- end
- -- Draw the buttons on the screen
- for i, button in ipairs(buttons) do
- button:draw(monitor)
- end
- -- Wait for a touch event
- local event, side, x, y = os.pullEvent("monitor_touch")
- for i, button in ipairs(buttons) do
- if button:isTouched(x, y) then
- -- Button was touched, do something here
- if i == 1 then
- shell.run("topitems")
- elseif i == 2 then
- shell.run("showchanges")
- elseif i == 3 then
- shell.run("cheststuff")
- elseif i == 10 then
- os.reboot()
- else
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setCursorPos(width/2 - 10 , height/2)
- monitor.write("Button not configured.")
- local event, side, x, y = os.pullEvent("monitor_touch")
- end
- break
- end
- end
- end
- -- Create some buttons
- local buttons = {}
- table.insert(buttons, createButton("Top 15", colors.blue, colors.green))
- table.insert(buttons, createButton("Inv Changes", colors.gray, colors.green))
- --for i = 1, 6 do
- --table.insert(buttons, createButton("Button " .. i, colors.green, colors.black))
- --end
- --table.insert(buttons, createButton("test", colors.blue, colors.black))
- --table.insert(buttons, createButton("plz\n kk", colors.red, colors.green))
- -- Show the buttons on the screen
- while true do
- showButtons(buttons)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement