Advertisement
Hikooshi

scrollBars

Sep 10th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.48 KB | None | 0 0
  1. -- pastebin get 10cdmqzP scrollBars
  2. local component = require("component")
  3. local event = require("event")
  4. local gpu = component.gpu
  5. local redstone = component.redstone
  6. local unicode = require("unicode")
  7. local term = require("term")
  8. local keyboard = require("keyboard")
  9.  
  10.  
  11.  
  12. fPix = "█"
  13. dPix = "▄"
  14. uPix = "▀"
  15. uArrow = "▲"
  16. dArrow = "▼"
  17. rArrow = "►"
  18. lArrow = "◄"
  19.  
  20. function funcButton()
  21.  
  22. -- Здесь мог бы быть Ваш шикарнейший код
  23.  
  24. end
  25.  
  26. function funcButton1()
  27.  
  28. gpu.set(buttons.button1.x + buttons.button1.width + 2 + 4 , buttons.button1.y, "Кнопка нажата")
  29.  
  30. end
  31.  
  32. function funcButton2()
  33.  
  34. os.sleep(4)
  35. for k,v in pairs(buttons) do
  36. v.active = false
  37. end
  38. buttons.button3.active = true
  39. --term.clear()
  40. --drawButtons()
  41.  
  42. end
  43.  
  44. function funcDont_Touch()
  45.  
  46. os.sleep(4)
  47. redstone.setOutput(0, 255)
  48.  
  49. end
  50.  
  51. function funcButton4()
  52.  
  53. gpu.set(64, 12, "Кнопка button4 нажата")
  54.  
  55. end
  56.  
  57. function funcButton5()
  58.  
  59. gpu.set(64, 12, "Кнопка button5 нажата")
  60.  
  61. end
  62.  
  63. function funcButton6()
  64.  
  65. gpu.set(64, 12, "Кнопка button6 нажата")
  66.  
  67. end
  68.  
  69. function funcButton7()
  70.  
  71. gpu.set(64, 12, "Кнопка button7 нажата")
  72.  
  73. end
  74.  
  75. function funcButton8()
  76.  
  77. gpu.set(64, 12, "Кнопка button8 нажата")
  78.  
  79. end
  80.  
  81. function funcButton9()
  82.  
  83. gpu.set(64, 12, "Кнопка button9 нажата")
  84.  
  85. end
  86.  
  87. function funcButton10()
  88.  
  89. gpu.set(64, 12, "Кнопка button10 нажата")
  90.  
  91. end
  92.  
  93. function funcButton11()
  94.  
  95. gpu.set(64, 12, "Кнопка button11 нажата")
  96.  
  97. end
  98.  
  99. 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},
  100.            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},
  101.            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},
  102.            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},
  103.            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},
  104.            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},
  105.            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},
  106.            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},
  107.            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},
  108.            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},
  109.            button10 = {x=52, y=8, text="button10", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton10, height=3, cFore = 0x000000, cBack = 0xFFFFFF},
  110.            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}}
  111.  
  112. 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}},
  113.                  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}},
  114.                  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"}]]}}
  115.  
  116. function initListOfButtons()
  117.  
  118. for k,v in pairs(listOfButtons) do
  119. v.width = unicode.wlen(v.name) + 2
  120. end
  121. for k1, v1 in pairs(listOfButtons) do
  122. v1.strPosition = 1
  123.   if v1.maxWidth ~= nil then --------------------------------------------- от сих ----------------------------------------
  124.   if v1.buttons ~= nil then -- программа расчитывает
  125.   for i = 1, #v1.buttons do
  126.   v1.maxWidth = math.max(v1.maxWidth, v1.buttons[i].width) -- максимальную ширину списка здесь
  127.   end
  128.   elseif v1.raws ~= nil then
  129.   for i = 1, #v1.raws do
  130.   v1.maxWidth = math.max(v1.maxWidth, unicode.len(v1.raws[i])) -- и здесь
  131.   end
  132.   end
  133.   end
  134.   if v1.vertical ~= nil and ((v1.buttons ~= nil and #v1.buttons > v1.height) or (v1.raws ~= nil and #v1.raws > v1.height)) then
  135.   v1.vertical[1] = true -- определяет, нужен ли вертикальный скроллбар
  136.   else
  137.   v1.vertical = {}
  138.   v1.vertical[1] = false
  139.   end
  140.   if v1.maxWidth ~= nil and v1.maxWidth > v1.width then
  141.   v1.horizontal[1] = true -- определяет, нужен ли горизонтальный скроллбар
  142.   else
  143.   v1.horizontal = {}
  144.   v1.horizontal[1] = false
  145.   end ---------------------------------------------------------------------- до сих -----------------------------------------
  146.   if v1.buttons ~= nil then           -------------------------------------------
  147.   v1.numberOfRaws = #v1.buttons       -- определяем количество строк в списке, --
  148.   elseif v1.raws ~= nil then          -- в зависимости от того, -----------------
  149.   v1.numberOfRaws = #v1.raws          -- строки находятся в списке или кнопки ---
  150.   end                                 -------------------------------------------
  151.   if v1.vertical[1] == true then
  152.   v1.vertical.q = v1.height / v1.numberOfRaws
  153.         v1.vertical.length = math.ceil(v1.height / v1.numberOfRaws * v1.height * 2) -- длина 'thumb' вертикального скроллбара  
  154.           v1.vertical.difference = v1.height * 2 - 1 - v1.vertical.length -- разница между длинной и 'thumb' вертикального скроллбара
  155.           v1.vertical.shiftScroll = v1.vertical.difference / (v1.numberOfRaws - 1 - v1.height) -- смещение 'thumb' вертикального скроллбара при скролле
  156.           v1.vertical.scrollBarPosition = math.ceil(v1.vertical.shiftScroll * (v1.startPosition - 1) + 1) -- позиция 'thumb' вертикального скроллбара
  157.     if v1.vertical.length > 1 then ----------------------------------------------- от сих ----------------------------------------
  158.     local pixelCount = math.modf(v1.vertical.length / 2) ------------ подготавливаем рисунок 'thumb' вертикального скроллбара ----
  159.       if v1.vertical.length%2 == 1 then ----------------------------- для нечетной длины 'thumb' вертикального скроллбара --------
  160.       v1.vertical.barImages[3] = string.rep(fPix, pixelCount) .. uPix
  161.       v1.vertical.barImages[2] = dPix .. string.rep(fPix, pixelCount)
  162.       elseif v1.vertical.length%2 == 0 then ------------------------- для четной длины 'thumb' вертикального скроллбара ----------
  163.       v1.vertical.barImages[3] = string.rep(fPix, pixelCount)
  164.       v1.vertical.barImages[2] = dPix .. string.rep(fPix, pixelCount - 1) .. uPix
  165.       end
  166.     else ------------------------------------------------------------ для длины = 1 'thumb' вертикального скроллбара --------------
  167.     v1.vertical.barImages[3] = uPix
  168.     v1.vertical.barImages[2] = dPix
  169.     end -------------------------------------------------------------------------- до сих -----------------------------------------
  170.   end
  171. if v1.horizontal[1] == true then ------------------------------------------------- от сих -----------------------------------------
  172. if not v1.horizontal.slimScrollBar then ---------------------------------------------------------------
  173. v1.horizontal.fillerChar = fPix         -- выбирается тип заливки 'thumb' горизонтального скроллбара --
  174. elseif v1.horizontal.slimScrollBar then ---------------------------------------------------------------
  175. v1.horizontal.fillerChar = uPix         ---------------------------------------------------------------
  176. end                                     ---------------------------------------------------------------
  177. v1.horizontal.q = v1.width / v1.maxWidth
  178. v1.horizontal.length = math.floor(v1.horizontal.q * v1.width)                         -- рассчитываются параметры 'thumb' горизонтального скроллбара
  179. v1.horizontal.scrollBarPosition = 1                                                   -- единственное отличие в том,
  180. v1.horizontal.difference = v1.width - 1 - v1.horizontal.length                       -- что рисунок 'thumb' горизонтального скроллбара
  181. v1.horizontal.shiftScroll = v1.horizontal.difference / (v1.maxWidth - 1 - v1.width)  -- заранее не подготавливается
  182. v1.horizontal.scrollBarPosition = math.ceil(v1.horizontal.shiftScroll * (v1.strPosition - 1) + 1)
  183. end ------------------------------------------------------------------------------- до сих ------------------------------------------
  184. end
  185.  
  186. end
  187.  
  188. function initButtons()
  189.  
  190. for k,v in pairs(buttons) do
  191. v.width = unicode.wlen(v.text) + 2
  192. end
  193.  
  194. end
  195.  
  196. initButtons()
  197.  
  198. initListOfButtons()
  199.  
  200. function drawListsOfButtons(tbl)
  201.  
  202. gpu.setForeground(tbl.cFore)
  203. gpu.setBackground(tbl.cBack)
  204. gpu.fill(tbl.x, tbl.y, tbl.width, 1, " ") -- поле для имени списка
  205.         if tbl.droped ~= nil and not tbl.droped then
  206.         gpu.set(tbl.x+tbl.width, tbl.y, dArrow) -- стрелка "вниз" для нераскрытого списка
  207.         elseif tbl.droped ~= nil and tbl.droped then
  208.         gpu.set(tbl.x+tbl.width, tbl.y, uArrow) -- стрлека "вверх" для раскрытого списка
  209.         end
  210. gpu.set(tbl.x+1, tbl.y, tbl.name) -- отображаем имя списка
  211. gpu.setForeground(tbl.cFore1)
  212. gpu.setBackground(tbl.cBack1)
  213. local rawCount
  214.         if tbl.height > tbl.numberOfRaws then      ------------------------------------------------------------------------
  215.         rawCount = tbl.numberOfRaws                ------------------------------------------------------------------------
  216.         elseif tbl.height <= tbl.numberOfRaws then -- вычисляется высота списка в зависимости от количества строк списка --
  217.         rawCount = tbl.height                      ------------------------------------------------------------------------
  218.         end                                        ------------------------------------------------------------------------
  219.           if tbl.droped ~= nil and tbl.droped == false then
  220.           gpu.setBackground(0x000000)
  221.           gpu.fill(tbl.x, tbl.y+1, tbl.width+1, rawCount + 1, " ") -- если список не раскрыт, то "стираем" все его строки
  222.           elseif tbl.droped == nil or tbl.droped == true then
  223.           gpu.fill(tbl.x, tbl.y+1, tbl.width, rawCount, " ") -- если список не может быть раскрыт (всегда раскрыт) или раскрыт, то рисуем его область
  224.             if tbl.vertical[1] or tbl.horizontal[1] then ---------------------------- от сих ----------------------------------------
  225.             gpu.setForeground(tbl.cForeScroll)
  226.             gpu.setBackground(tbl.cBackScroll)
  227.             gpu.fill(tbl.x+tbl.width, tbl.y+1, 1, tbl.height, " ") ------------------ если скроллбар есть
  228.               if tbl.vertical[1] == true then
  229.               gpu.set(tbl.x + tbl.width, tbl.y + math.ceil(tbl.vertical.scrollBarPosition / 2), tbl.vertical.barImages[tbl.vertical.scrollBarPosition%2 + 2], true)
  230.               end
  231.                 if tbl.horizontal[1] == true then
  232.                 gpu.setForeground(tbl.cBackScroll)
  233.                 gpu.setBackground(0x000000)
  234.                 gpu.fill(tbl.x, tbl.y + tbl.height + 1, tbl.width, 1, tbl.horizontal.fillerChar) -- то, как ни внезапно
  235.                 gpu.setForeground(tbl.cForeScroll)
  236.                 gpu.setBackground(0x000000)
  237.                   for i = 1, tbl.horizontal.length do ------------------------------------ рисуем его или их
  238.                   gpu.set(tbl.x + tbl.horizontal.scrollBarPosition + i - 2, tbl.y + tbl.height + 1, tbl.horizontal.fillerChar)
  239.                   end
  240.                 end
  241.             gpu.setForeground(tbl.cFore1)
  242.             gpu.setBackground(tbl.cBack1)
  243.             end ---------------------------------------------------------------------- до сих -------------------------------------------
  244.               if tbl.buttons ~= nil then
  245.                 for i1 = 1, rawCount do
  246.                 gpu.set(tbl.x, tbl.y + i1, unicode.sub(tbl.buttons[i1 + tbl.startPosition - 1].text, tbl.strPosition, tbl.strPosition + tbl.width - 1))
  247.                 end
  248.               elseif tbl.raws ~= nil then
  249.                 for i1 = 1, rawCount do
  250.                 gpu.set(tbl.x, tbl.y + i1, unicode.sub(tbl.raws[i1 + tbl.startPosition - 1], tbl.strPosition, tbl.strPosition + tbl.width - 1))
  251.                 end
  252.               end
  253.           end
  254.         gpu.setForeground(0xFFFFFF)
  255.         gpu.setBackground(0x000000)
  256.  
  257. end
  258.  
  259. function drawListsOfButtons1()
  260.  
  261. for k,v in pairs(listOfButtons) do
  262. drawListsOfButtons(v)
  263. end
  264.  
  265. end
  266.  
  267. function drawButtons()
  268.  
  269. for k,v in pairs(buttons) do
  270. if v.active then
  271. if not v.buttonPressed then -- если кнопка не нажата
  272. gpu.setForeground(v.cFore)
  273. gpu.setBackground(v.cBack)
  274. else                      -- в ином случае
  275. gpu.setForeground(v.cFore1)
  276. gpu.setBackground(v.cBack1)
  277. end
  278. gpu.fill(v.x, v.y, v.width, v.height, " ") -- фон для кнопки
  279. if v.height == 1 then         -- если высота кнопки равна 1
  280. gpu.set(v.x+1, v.y, v.text)
  281. elseif v.height%2 == 0 then   -- если высота кнопки равна четному числу
  282. gpu.set(v.x+1, v.y + (v.height/2 - 1), v.text)
  283. elseif v.height%2 == 1 then   -- если высота кнопки равна нечетному числу
  284. gpu.set(v.x+1, v.y + (math.ceil(v.height/2) - 1), v.text)
  285. end
  286. if v.autoSwitch == true and v.buttonPressed == true then
  287. v.buttonPressed = false
  288. os.sleep(4)
  289. drawButtons()
  290. end
  291. end
  292. end
  293. gpu.setForeground(0xFFFFFF)
  294. gpu.setBackground(0x000000)
  295.  
  296. end
  297.  
  298. function searchButton()
  299.  
  300. while true do
  301. local event,_,x,y,scrl = event.pull()
  302. if event == "touch" then
  303. for k,v in pairs(buttons) do
  304. 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
  305.     if v.switchedButton == true then
  306.       if not v.autoSwitch then
  307.         if v.buttonPressed == false then
  308.         v.buttonPressed = true
  309.         else
  310.         v.buttonPressed = false
  311.         end
  312.       else
  313.       v.buttonPressed = true
  314.       end
  315.     term.clear()
  316.     drawButtons()
  317.     end
  318. v.func()
  319. end
  320. end
  321. for k,v in pairs(listOfButtons) do
  322. 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
  323. if v.buttons ~= nil then
  324. v.buttons[v.startPosition + y-v.y - 1].func() -- если в списке таблица с кнопками, то выполняется функция кнопки
  325. elseif v.raws ~= nil then
  326. v.name = v.raws[v.startPosition + y-v.y - 1]
  327. if unicode.len(v.name) > v.width - 2 then
  328. v.name = unicode.sub(v.name, 1, v.width - 5) .. "..." -- если же в списке таблица со строками, то назначается имя списка из строки этой таблицы
  329. end
  330. drawListsOfButtons(v)
  331. end
  332. elseif x >= v.x and x < v.x + v.width and y == v.y and v.active then
  333. if v.droped ~= nil then
  334. if not v.droped then -----------------------------------------------------------------------------------------
  335. v.droped = true      -- инвертируется состояние списка "раскрыт" или "не раскрыт" при нажатии на имя списка --
  336. else                 -----------------------------------------------------------------------------------------
  337. v.droped = false     -----------------------------------------------------------------------------------------
  338. if not v.solidPosition then --------------------------------------------------------------------- от сих ---------------------------------------------
  339. v.startPosition = 1 -- сбрасываем или не сбрасываем начальные позиции списка
  340. v.strPosition = 1
  341. if v.vertical[1] then
  342. v.vertical.scrollBarPosition = math.ceil(v.vertical.shiftScroll * (v.startPosition - 1) + 1)
  343. end
  344. if v.horizontal[1] then
  345. v.horizontal.scrollBarPosition = math.ceil(v.horizontal.shiftScroll * (v.strPosition - 1) + 1)
  346. end
  347. end ---------------------------------------------------------------------------------------------- до сих ---------------------------------------------
  348. end
  349. end
  350. drawListsOfButtons(v)
  351. end
  352. end
  353. elseif event == "scroll" then
  354.   for k,v in pairs(listOfButtons) do
  355.     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
  356.       if scrl == -1 then -- скроллим список вниз
  357.         if not keyboard.isShiftDown() then -- при нажатом шифте скроллим список вправо
  358.         if not v.infinite then
  359.         if v.startPosition < v.numberOfRaws - v.height + 1 then
  360.         v.startPosition = v.startPosition + 1
  361.         end
  362.         else
  363.         table.insert(v.buttons, v.buttons[1])
  364.         table.remove(v.buttons, 1)
  365.         end
  366.         else
  367.         if v.horizontal[1] == true then
  368.         if v.strPosition < v.maxWidth - v.width + 1 then
  369.         v.strPosition = v.strPosition + 1
  370.         end
  371.         end
  372.         end
  373.       elseif scrl == 1 then -- скроллим список вверх
  374.         if not keyboard.isShiftDown() then -- при нажатом шифте скроллим список влево
  375.         if not v.infinite then
  376.         if v.startPosition > 1 then
  377.         v.startPosition = v.startPosition - 1
  378.         end
  379.         else
  380.         table.insert(v.buttons, 1, v.buttons[#v.buttons])
  381.         table.remove(v.buttons, #v.buttons)
  382.         end
  383.         else
  384.         if v.horizontal[1] == true then
  385.         if v.strPosition > 1 then
  386.         v.strPosition = v.strPosition - 1
  387.         end
  388.         end
  389.         end
  390.       end
  391.         if v.vertical[1] == true then
  392.         v.vertical.scrollBarPosition = math.ceil(v.vertical.shiftScroll * (v.startPosition - 1) + 1) -- вычисляется новая позиция 'thumb' вертикального скроллбара
  393.         end                                                                                          -- при скролле вниз или вверх
  394.         if v.horizontal[1] == true then
  395.         v.horizontal.scrollBarPosition = math.ceil(v.horizontal.shiftScroll * (v.strPosition - 1) + 1) -- вычисляется новая позиция 'thumb' горизонтального скроллбара
  396.         end                                                                                            -- при скролле влево или вправо
  397.         end
  398.   drawListsOfButtons(v)
  399.   end
  400. end
  401. end
  402.  
  403. end
  404.  
  405. drawButtons()
  406. drawListsOfButtons1()
  407. searchButton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement