Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 10cdmqzP scrollBars
- local component = require("component")
- local event = require("event")
- local gpu = component.gpu
- local redstone = component.redstone
- local unicode = require("unicode")
- local term = require("term")
- local keyboard = require("keyboard")
- fPix = "█"
- dPix = "▄"
- uPix = "▀"
- uArrow = "▲"
- dArrow = "▼"
- rArrow = "►"
- lArrow = "◄"
- 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
- 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="Тестовая кнопка qwertyuasdfghjzxcvbnmiop[]kl;'nm,./kdkgkgkrgkrmgkagmgwamg", 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=24, 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}},
- list2 = {x=24, y=24, name="Новый список", height = 4, startPosition = 1, active = true, vertical = {barImages = {}}, horizontal = {slimScrollBar = true}, droped = false, maxWidth = 0, infinite = false, solidPosition = false, cFore = 0xFFFFFF, cBack = 0x0000FF, cFore1 = 0xFFFF00, cBack1 = 0x00FFFF, cForeScroll = 0x00FF00, cBackScroll = 0xFF00FF, 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.button1, buttons.button2, buttons.button3} --[[raws = {"testStr", "VeryLongString", "1", "Новая строка", "2", "Еще строка", "hello world", "4"}]]}}
- function initListOfButtons()
- for k,v in pairs(listOfButtons) do
- v.width = unicode.wlen(v.name) + 2
- end
- for k1, v1 in pairs(listOfButtons) do
- v1.strPosition = 1
- if v1.maxWidth ~= nil then --------------------------------------------- от сих ----------------------------------------
- if v1.buttons ~= nil then -- программа расчитывает
- for i = 1, #v1.buttons do
- v1.maxWidth = math.max(v1.maxWidth, v1.buttons[i].width) -- максимальную ширину списка здесь
- end
- elseif v1.raws ~= nil then
- for i = 1, #v1.raws do
- v1.maxWidth = math.max(v1.maxWidth, unicode.len(v1.raws[i])) -- и здесь
- end
- end
- end
- if v1.vertical ~= nil and ((v1.buttons ~= nil and #v1.buttons > v1.height) or (v1.raws ~= nil and #v1.raws > v1.height)) then
- v1.vertical[1] = true -- определяет, нужен ли вертикальный скроллбар
- else
- v1.vertical = {}
- v1.vertical[1] = false
- end
- if v1.maxWidth ~= nil and v1.maxWidth > v1.width then
- v1.horizontal[1] = true -- определяет, нужен ли горизонтальный скроллбар
- else
- v1.horizontal = {}
- v1.horizontal[1] = false
- end ---------------------------------------------------------------------- до сих -----------------------------------------
- if v1.buttons ~= nil then -------------------------------------------
- v1.numberOfRaws = #v1.buttons -- определяем количество строк в списке, --
- elseif v1.raws ~= nil then -- в зависимости от того, -----------------
- v1.numberOfRaws = #v1.raws -- строки находятся в списке или кнопки ---
- end -------------------------------------------
- if v1.vertical[1] == true then
- v1.vertical.q = v1.height / v1.numberOfRaws
- v1.vertical.length = math.ceil(v1.height / v1.numberOfRaws * v1.height * 2) -- длина 'thumb' вертикального скроллбара
- v1.vertical.difference = v1.height * 2 - 1 - v1.vertical.length -- разница между длинной и 'thumb' вертикального скроллбара
- v1.vertical.shiftScroll = v1.vertical.difference / (v1.numberOfRaws - 1 - v1.height) -- смещение 'thumb' вертикального скроллбара при скролле
- v1.vertical.scrollBarPosition = math.ceil(v1.vertical.shiftScroll * (v1.startPosition - 1) + 1) -- позиция 'thumb' вертикального скроллбара
- if v1.vertical.length > 1 then ----------------------------------------------- от сих ----------------------------------------
- local pixelCount = math.modf(v1.vertical.length / 2) ------------ подготавливаем рисунок 'thumb' вертикального скроллбара ----
- if v1.vertical.length%2 == 1 then ----------------------------- для нечетной длины 'thumb' вертикального скроллбара --------
- v1.vertical.barImages[3] = string.rep(fPix, pixelCount) .. uPix
- v1.vertical.barImages[2] = dPix .. string.rep(fPix, pixelCount)
- elseif v1.vertical.length%2 == 0 then ------------------------- для четной длины 'thumb' вертикального скроллбара ----------
- v1.vertical.barImages[3] = string.rep(fPix, pixelCount)
- v1.vertical.barImages[2] = dPix .. string.rep(fPix, pixelCount - 1) .. uPix
- end
- else ------------------------------------------------------------ для длины = 1 'thumb' вертикального скроллбара --------------
- v1.vertical.barImages[3] = uPix
- v1.vertical.barImages[2] = dPix
- end -------------------------------------------------------------------------- до сих -----------------------------------------
- end
- if v1.horizontal[1] == true then ------------------------------------------------- от сих -----------------------------------------
- if not v1.horizontal.slimScrollBar then ---------------------------------------------------------------
- v1.horizontal.fillerChar = fPix -- выбирается тип заливки 'thumb' горизонтального скроллбара --
- elseif v1.horizontal.slimScrollBar then ---------------------------------------------------------------
- v1.horizontal.fillerChar = uPix ---------------------------------------------------------------
- end ---------------------------------------------------------------
- v1.horizontal.q = v1.width / v1.maxWidth
- v1.horizontal.length = math.floor(v1.horizontal.q * v1.width) -- рассчитываются параметры 'thumb' горизонтального скроллбара
- v1.horizontal.scrollBarPosition = 1 -- единственное отличие в том,
- v1.horizontal.difference = v1.width - 1 - v1.horizontal.length -- что рисунок 'thumb' горизонтального скроллбара
- v1.horizontal.shiftScroll = v1.horizontal.difference / (v1.maxWidth - 1 - v1.width) -- заранее не подготавливается
- v1.horizontal.scrollBarPosition = math.ceil(v1.horizontal.shiftScroll * (v1.strPosition - 1) + 1)
- end ------------------------------------------------------------------------------- до сих ------------------------------------------
- end
- end
- function initButtons()
- for k,v in pairs(buttons) do
- v.width = unicode.wlen(v.text) + 2
- end
- end
- initButtons()
- initListOfButtons()
- function drawListsOfButtons(tbl)
- gpu.setForeground(tbl.cFore)
- gpu.setBackground(tbl.cBack)
- gpu.fill(tbl.x, tbl.y, tbl.width, 1, " ") -- поле для имени списка
- if tbl.droped ~= nil and not tbl.droped then
- gpu.set(tbl.x+tbl.width, tbl.y, dArrow) -- стрелка "вниз" для нераскрытого списка
- elseif tbl.droped ~= nil and tbl.droped then
- gpu.set(tbl.x+tbl.width, tbl.y, uArrow) -- стрлека "вверх" для раскрытого списка
- end
- gpu.set(tbl.x+1, tbl.y, tbl.name) -- отображаем имя списка
- gpu.setForeground(tbl.cFore1)
- gpu.setBackground(tbl.cBack1)
- local rawCount
- if tbl.height > tbl.numberOfRaws then ------------------------------------------------------------------------
- rawCount = tbl.numberOfRaws ------------------------------------------------------------------------
- elseif tbl.height <= tbl.numberOfRaws then -- вычисляется высота списка в зависимости от количества строк списка --
- rawCount = tbl.height ------------------------------------------------------------------------
- end ------------------------------------------------------------------------
- if tbl.droped ~= nil and tbl.droped == false then
- gpu.setBackground(0x000000)
- gpu.fill(tbl.x, tbl.y+1, tbl.width+1, rawCount + 1, " ") -- если список не раскрыт, то "стираем" все его строки
- elseif tbl.droped == nil or tbl.droped == true then
- gpu.fill(tbl.x, tbl.y+1, tbl.width, rawCount, " ") -- если список не может быть раскрыт (всегда раскрыт) или раскрыт, то рисуем его область
- if tbl.vertical[1] or tbl.horizontal[1] then ---------------------------- от сих ----------------------------------------
- gpu.setForeground(tbl.cForeScroll)
- gpu.setBackground(tbl.cBackScroll)
- gpu.fill(tbl.x+tbl.width, tbl.y+1, 1, tbl.height, " ") ------------------ если скроллбар есть
- if tbl.vertical[1] == true then
- gpu.set(tbl.x + tbl.width, tbl.y + math.ceil(tbl.vertical.scrollBarPosition / 2), tbl.vertical.barImages[tbl.vertical.scrollBarPosition%2 + 2], true)
- end
- if tbl.horizontal[1] == true then
- gpu.setForeground(tbl.cBackScroll)
- gpu.setBackground(0x000000)
- gpu.fill(tbl.x, tbl.y + tbl.height + 1, tbl.width, 1, tbl.horizontal.fillerChar) -- то, как ни внезапно
- gpu.setForeground(tbl.cForeScroll)
- gpu.setBackground(0x000000)
- for i = 1, tbl.horizontal.length do ------------------------------------ рисуем его или их
- gpu.set(tbl.x + tbl.horizontal.scrollBarPosition + i - 2, tbl.y + tbl.height + 1, tbl.horizontal.fillerChar)
- end
- end
- gpu.setForeground(tbl.cFore1)
- gpu.setBackground(tbl.cBack1)
- end ---------------------------------------------------------------------- до сих -------------------------------------------
- if tbl.buttons ~= nil then
- for i1 = 1, rawCount do
- gpu.set(tbl.x, tbl.y + i1, unicode.sub(tbl.buttons[i1 + tbl.startPosition - 1].text, tbl.strPosition, tbl.strPosition + tbl.width - 1))
- end
- elseif tbl.raws ~= nil then
- for i1 = 1, rawCount do
- gpu.set(tbl.x, tbl.y + i1, unicode.sub(tbl.raws[i1 + tbl.startPosition - 1], tbl.strPosition, tbl.strPosition + tbl.width - 1))
- end
- 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
- if v.buttons ~= nil then
- v.buttons[v.startPosition + y-v.y - 1].func() -- если в списке таблица с кнопками, то выполняется функция кнопки
- elseif v.raws ~= nil then
- v.name = v.raws[v.startPosition + y-v.y - 1]
- if unicode.len(v.name) > v.width - 2 then
- v.name = unicode.sub(v.name, 1, v.width - 5) .. "..." -- если же в списке таблица со строками, то назначается имя списка из строки этой таблицы
- end
- drawListsOfButtons(v)
- end
- elseif x >= v.x and x < v.x + v.width and y == v.y and v.active then
- if v.droped ~= nil then
- if not v.droped then -----------------------------------------------------------------------------------------
- v.droped = true -- инвертируется состояние списка "раскрыт" или "не раскрыт" при нажатии на имя списка --
- else -----------------------------------------------------------------------------------------
- v.droped = false -----------------------------------------------------------------------------------------
- if not v.solidPosition then --------------------------------------------------------------------- от сих ---------------------------------------------
- v.startPosition = 1 -- сбрасываем или не сбрасываем начальные позиции списка
- v.strPosition = 1
- if v.vertical[1] then
- v.vertical.scrollBarPosition = math.ceil(v.vertical.shiftScroll * (v.startPosition - 1) + 1)
- end
- if v.horizontal[1] then
- v.horizontal.scrollBarPosition = math.ceil(v.horizontal.shiftScroll * (v.strPosition - 1) + 1)
- end
- end ---------------------------------------------------------------------------------------------- до сих ---------------------------------------------
- end
- end
- drawListsOfButtons(v)
- 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 keyboard.isShiftDown() then -- при нажатом шифте скроллим список вправо
- if not v.infinite then
- if v.startPosition < v.numberOfRaws - v.height + 1 then
- v.startPosition = v.startPosition + 1
- end
- else
- table.insert(v.buttons, v.buttons[1])
- table.remove(v.buttons, 1)
- end
- else
- if v.horizontal[1] == true then
- if v.strPosition < v.maxWidth - v.width + 1 then
- v.strPosition = v.strPosition + 1
- end
- end
- end
- elseif scrl == 1 then -- скроллим список вверх
- if not keyboard.isShiftDown() 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
- else
- if v.horizontal[1] == true then
- if v.strPosition > 1 then
- v.strPosition = v.strPosition - 1
- end
- end
- end
- end
- if v.vertical[1] == true then
- v.vertical.scrollBarPosition = math.ceil(v.vertical.shiftScroll * (v.startPosition - 1) + 1) -- вычисляется новая позиция 'thumb' вертикального скроллбара
- end -- при скролле вниз или вверх
- if v.horizontal[1] == true then
- v.horizontal.scrollBarPosition = math.ceil(v.horizontal.shiftScroll * (v.strPosition - 1) + 1) -- вычисляется новая позиция 'thumb' горизонтального скроллбара
- end -- при скролле влево или вправо
- end
- drawListsOfButtons(v)
- end
- end
- end
- end
- drawButtons()
- drawListsOfButtons1()
- searchButton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement