Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get LWuVFb0e buttons
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local redstone = component.redstone
- local unicode = require("unicode")
- local term = require("term")
- function funcButton()
- -- Здесь мог бы быть Ваш шикарнейший код
- end
- function funcButton1()
- gpu.set(buttons.button1.x + buttons.button1.width + 2 + 4 , buttons.button1.y, "Кнопка нажата")
- end
- function funcButton2()
- os.sleep(4)
- for k,v in pairs(buttons) do
- v.active = false
- end
- buttons.button3.active = true
- --term.clear()
- --drawButtons()
- end
- function funcDont_Touch()
- os.sleep(4)
- redstone.setOutput(0, 255)
- end
- function funcButton4()
- gpu.set(64, 12, "Кнопка button4 нажата")
- end
- function funcButton5()
- gpu.set(64, 12, "Кнопка button5 нажата")
- end
- function funcButton6()
- gpu.set(64, 12, "Кнопка button6 нажата")
- end
- function funcButton7()
- gpu.set(64, 12, "Кнопка button7 нажата")
- end
- function funcButton8()
- gpu.set(64, 12, "Кнопка button8 нажата")
- end
- function funcButton9()
- gpu.set(64, 12, "Кнопка button9 нажата")
- end
- function funcButton10()
- gpu.set(64, 12, "Кнопка button10 нажата")
- end
- function funcButton11()
- gpu.set(64, 12, "Кнопка button11 нажата")
- end
- --listOfButtons = {list = {x=8, y=16, name="Покрути меня", height = 4, buttons={buttons.button, buttons.button1, buttons.button2, buttons.button3, buttons.button4, buttons.button5, buttons.button6, buttons.button7, buttons.button8, buttons.button9, buttons.button10, buttons.button11}}}
- buttons = {button = {x=2, y=2, text="button", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton, height=3, cFore = 0xFFFFFF, cBack = 0xFF0000, cFore1 = 0x000000, cBack1 = 0x00FF00},
- button1 = {x=16, y=4, text="Тестовая кнопка", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton1, height=2, cFore = 0xFFFFFF, cBack = 0x0000FF},
- button2 = {x=4, y=16, text="button2", active=true, switchedButton = true, autoSwitch=true, buttonPressed = false, func = funcButton2, height=4, cFore = 0xFFFFFF, cBack = 0xFFFF00, cFore1 = 0xFFFFFF, cBack1 = 0x00FFFF},
- button3 = {x=72, y=24, text="Не нажимать!", active=false, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcDont_Touch, height=5, cFore = 0x333333, cBack = 0xFF0000, cFore1 = 0xFFFFFF, cBack1 = 0xFF0000},
- button4 = {x=4, y=8, text="button4", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton4, height=3, cFore = 0xFF0000, cBack = 0x00FF00, cFore1 = 0x0000FF, cBack1 = 0x000000},
- button5 = {x=12, y=8, text="button5", active=true, switchedButton = true, autoSwitch=true, buttonPressed = false, func = funcButton5, height=3, cFore = 0xFFFF00, cBack = 0xFF00FF, cFore1 = 0x00FFFF, cBack1 = 0xFFFFFF},
- button6 = {x=20, y=8, text="button6", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton6, height=3, cFore = 0x0000FF, cBack = 0x00FF00, cFore1 = 0xFF0000, cBack1 = 0x000000},
- button7 = {x=28, y=8, text="button7", active=true, switchedButton = true, autoSwitch=true, buttonPressed = false, func = funcButton7, height=3, cFore = 0x00FFFF, cBack = 0xFF00FF, cFore1 = 0xFFFF00, cBack1 = 0xFFFFFF},
- button8 = {x=36, y=8, text="button8", active=true, switchedButton = true, autoSwitch=false, buttonPressed = false, func = funcButton8, height=3, cFore = 0xFF0000, cBack = 0x00FF00, cFore1 = 0x0000FF, cBack1 = 0x000000},
- button9 = {x=44, y=8, text="button9", active=true, switchedButton = true, autoSwitch=true, buttonPressed = false, func = funcButton9, height=3, cFore = 0xFFFF00, cBack = 0x00FFFF, cFore1 = 0xFF00FF, cBack1 = 0xFFFFFF},
- button10 = {x=52, y=8, text="button10", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton10, height=3, cFore = 0x000000, cBack = 0xFFFFFF},
- button11 = {x=60, y=8, text="button11", active=true, switchedButton = true, autoSwitch=true, buttonPressed = false, func = funcButton11, height=3, cFore = 0x00FFFF, cBack = 0xFFFF00, cFore1 = 0xFF00FF, cBack1 = 0xFFFFFF}}
- listOfButtons = {list = {x=32, y=16, name="Покрути меня", height = 4, startPosition = 1, active = true, infinite = false, cFore = 0x000000, cBack = 0xFFFFFF, cFore1 = 0x444444, cBack1 = 0xFFFFFF, buttons={buttons.button, buttons.button1, buttons.button2, buttons.button3, buttons.button4, buttons.button5, buttons.button6, buttons.button7, buttons.button8, buttons.button9, buttons.button10, buttons.button11}},
- list1 = {x=64, y=16, name="Покрути меня тоже", height = 8, startPosition = 1, active = true, infinite = true, cFore = 0x000000, cBack = 0xFFFFFF, cFore1 = 0x444444, cBack1 = 0xFFFFFF, buttons={buttons.button, buttons.button1, buttons.button2, buttons.button3, buttons.button4, buttons.button5, buttons.button6, buttons.button7, buttons.button8, buttons.button9, buttons.button10, buttons.button11}}}
- function initListOfButtons()
- for k,v in pairs(listOfButtons) do
- v.width = unicode.wlen(v.name) + 2
- end
- end
- initListOfButtons()
- function initButtons()
- for k,v in pairs(buttons) do
- v.width = unicode.wlen(v.text) + 2
- end
- end
- initButtons()
- function drawListsOfButtons(tbl)
- gpu.setForeground(tbl.cFore)
- gpu.setBackground(tbl.cBack)
- gpu.fill(tbl.x, tbl.y, tbl.width, 1, " ")
- gpu.set(tbl.x+1, tbl.y, tbl.name)
- gpu.setForeground(tbl.cFore1)
- gpu.setBackground(tbl.cBack1)
- local rawCount
- if tbl.height > #tbl.buttons then
- rawCount = #tbl.buttons
- elseif tbl.height <= #tbl.buttons then
- rawCount = tbl.height
- end
- gpu.fill(tbl.x, tbl.y+1, tbl.width, rawCount, " ")
- for i = 1, rawCount do
- if tbl.buttons[i + tbl.startPosition - 1].width > tbl.width then
- gpu.set(tbl.x+1, tbl.y+i, unicode.wtrunc(tbl.buttons[i + tbl.startPosition - 1].text, tbl.width - 4) .. "...")
- else
- gpu.set(tbl.x+1, tbl.y+i, tbl.buttons[i + tbl.startPosition - 1].text)
- end
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- end
- function drawListsOfButtons1()
- for k,v in pairs(listOfButtons) do
- drawListsOfButtons(v)
- end
- end
- function drawButtons()
- for k,v in pairs(buttons) do
- if v.active then
- if not v.buttonPressed then -- если кнопка не нажата
- gpu.setForeground(v.cFore)
- gpu.setBackground(v.cBack)
- else -- в ином случае
- gpu.setForeground(v.cFore1)
- gpu.setBackground(v.cBack1)
- end
- gpu.fill(v.x, v.y, v.width, v.height, " ") -- фон для кнопки
- if v.height == 1 then -- если высота кнопки равна 1
- gpu.set(v.x+1, v.y, v.text)
- elseif v.height%2 == 0 then -- если высота кнопки равна четному числу
- gpu.set(v.x+1, v.y + (v.height/2 - 1), v.text)
- elseif v.height%2 == 1 then -- если высота кнопки равна нечетному числу
- gpu.set(v.x+1, v.y + (math.ceil(v.height/2) - 1), v.text)
- end
- if v.autoSwitch == true and v.buttonPressed == true then
- v.buttonPressed = false
- os.sleep(4)
- drawButtons()
- end
- end
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- end
- function searchButton()
- while true do
- local event,_,x,y,scrl = event.pull()
- if event == "touch" then
- for k,v in pairs(buttons) do
- if x >= v.x and x < v.x + v.width+2 and y >= v.y and y < v.y + v.height and v.active then
- if v.switchedButton == true then
- if not v.autoSwitch then
- if v.buttonPressed == false then
- v.buttonPressed = true
- else
- v.buttonPressed = false
- end
- else
- v.buttonPressed = true
- end
- term.clear()
- drawButtons()
- end
- v.func()
- end
- end
- for k,v in pairs(listOfButtons) do
- if x >= v.x and x < v.x + v.width and y >= v.y + 1 and y < v.y + 1 + v.height and v.active then
- v.buttons[v.startPosition + y-v.y - 1].func()
- end
- end
- elseif event == "scroll" then
- for k,v in pairs(listOfButtons) do
- if x >= v.x and x < v.x + v.width and y >= v.y + 1 and y < v.y + v.height + 1 and v.active then
- if scrl == -1 then
- if not v.infinite then
- if v.startPosition < #v.buttons - v.height + 1 then
- v.startPosition = v.startPosition + 1
- end
- else
- table.insert(v.buttons, v.buttons[1])
- table.remove(v.buttons, 1)
- end
- elseif scrl == 1 then
- if not v.infinite then
- if v.startPosition > 1 then
- v.startPosition = v.startPosition - 1
- end
- else
- table.insert(v.buttons, 1, v.buttons[#v.buttons])
- table.remove(v.buttons, #v.buttons)
- end
- end
- end
- drawListsOfButtons(v)
- end
- end
- end
- end
- drawButtons()
- drawListsOfButtons1()
- searchButton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement