Advertisement
Hikooshi

buttons1

Aug 4th, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.95 KB | None | 0 0
  1. --pastebin get LWuVFb0e buttons
  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.  
  9.  
  10.  
  11. function funcButton()
  12.  
  13. -- Здесь мог бы быть Ваш шикарнейший код
  14.  
  15. end
  16.  
  17. function funcButton1()
  18.  
  19. gpu.set(buttons.button1.x + buttons.button1.width + 2 + 4 , buttons.button1.y, "Кнопка нажата")
  20.  
  21. end
  22.  
  23. function funcButton2()
  24.  
  25. os.sleep(4)
  26. for k,v in pairs(buttons) do
  27. v.active = false
  28. end
  29. buttons.button3.active = true
  30. --term.clear()
  31. --drawButtons()
  32.  
  33. end
  34.  
  35. function funcDont_Touch()
  36.  
  37. os.sleep(4)
  38. redstone.setOutput(0, 255)
  39.  
  40. end
  41.  
  42. function funcButton4()
  43.  
  44. gpu.set(64, 12, "Кнопка button4 нажата")
  45.  
  46. end
  47.  
  48. function funcButton5()
  49.  
  50. gpu.set(64, 12, "Кнопка button5 нажата")
  51.  
  52. end
  53.  
  54. function funcButton6()
  55.  
  56. gpu.set(64, 12, "Кнопка button6 нажата")
  57.  
  58. end
  59.  
  60. function funcButton7()
  61.  
  62. gpu.set(64, 12, "Кнопка button7 нажата")
  63.  
  64. end
  65.  
  66. function funcButton8()
  67.  
  68. gpu.set(64, 12, "Кнопка button8 нажата")
  69.  
  70. end
  71.  
  72. function funcButton9()
  73.  
  74. gpu.set(64, 12, "Кнопка button9 нажата")
  75.  
  76. end
  77.  
  78. function funcButton10()
  79.  
  80. gpu.set(64, 12, "Кнопка button10 нажата")
  81.  
  82. end
  83.  
  84. function funcButton11()
  85.  
  86. gpu.set(64, 12, "Кнопка button11 нажата")
  87.  
  88. end
  89.  
  90. --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}}}
  91.  
  92. 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},
  93.            button1 = {x=16, y=4, text="Тестовая кнопка", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton1, height=2, cFore = 0xFFFFFF, cBack = 0x0000FF},
  94.            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},
  95.            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},
  96.            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},
  97.            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},
  98.            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},
  99.            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},
  100.            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},
  101.            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},
  102.            button10 = {x=52, y=8, text="button10", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton10, height=3, cFore = 0x000000, cBack = 0xFFFFFF},
  103.            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}}
  104.  
  105. 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}},
  106.                  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}}}
  107.  
  108. function initListOfButtons()
  109.  
  110. for k,v in pairs(listOfButtons) do
  111. v.width = unicode.wlen(v.name) + 2
  112. end
  113.  
  114. end
  115.  
  116. initListOfButtons()
  117.  
  118. function initButtons()
  119.  
  120. for k,v in pairs(buttons) do
  121. v.width = unicode.wlen(v.text) + 2
  122. end
  123.  
  124. end
  125.  
  126. initButtons()
  127.  
  128. function drawListsOfButtons(tbl)
  129.  
  130. gpu.setForeground(tbl.cFore)
  131. gpu.setBackground(tbl.cBack)
  132. gpu.fill(tbl.x, tbl.y, tbl.width, 1, " ")
  133. gpu.set(tbl.x+1, tbl.y, tbl.name)
  134. gpu.setForeground(tbl.cFore1)
  135. gpu.setBackground(tbl.cBack1)
  136. local rawCount
  137. if tbl.height > #tbl.buttons then
  138. rawCount = #tbl.buttons
  139. elseif tbl.height <= #tbl.buttons then
  140. rawCount = tbl.height
  141. end
  142. gpu.fill(tbl.x, tbl.y+1, tbl.width, rawCount, " ")
  143. for i = 1, rawCount do
  144. if tbl.buttons[i + tbl.startPosition - 1].width > tbl.width then
  145. gpu.set(tbl.x+1, tbl.y+i, unicode.wtrunc(tbl.buttons[i + tbl.startPosition - 1].text, tbl.width - 4) .. "...")
  146. else
  147. gpu.set(tbl.x+1, tbl.y+i, tbl.buttons[i + tbl.startPosition - 1].text)
  148. end
  149. end
  150. gpu.setForeground(0xFFFFFF)
  151. gpu.setBackground(0x000000)
  152.  
  153. end
  154.  
  155. function drawListsOfButtons1()
  156.  
  157. for k,v in pairs(listOfButtons) do
  158. drawListsOfButtons(v)
  159. end
  160.  
  161. end
  162.  
  163. function drawButtons()
  164.  
  165. for k,v in pairs(buttons) do
  166. if v.active then
  167. if not v.buttonPressed then -- если кнопка не нажата
  168. gpu.setForeground(v.cFore)
  169. gpu.setBackground(v.cBack)
  170. else                      -- в ином случае
  171. gpu.setForeground(v.cFore1)
  172. gpu.setBackground(v.cBack1)
  173. end
  174. gpu.fill(v.x, v.y, v.width, v.height, " ") -- фон для кнопки
  175. if v.height == 1 then         -- если высота кнопки равна 1
  176. gpu.set(v.x+1, v.y, v.text)
  177. elseif v.height%2 == 0 then   -- если высота кнопки равна четному числу
  178. gpu.set(v.x+1, v.y + (v.height/2 - 1), v.text)
  179. elseif v.height%2 == 1 then   -- если высота кнопки равна нечетному числу
  180. gpu.set(v.x+1, v.y + (math.ceil(v.height/2) - 1), v.text)
  181. end
  182. if v.autoSwitch == true and v.buttonPressed == true then
  183. v.buttonPressed = false
  184. os.sleep(4)
  185. drawButtons()
  186. end
  187. end
  188. end
  189. gpu.setForeground(0xFFFFFF)
  190. gpu.setBackground(0x000000)
  191.  
  192. end
  193.  
  194. function searchButton()
  195.  
  196. while true do
  197. local event,_,x,y,scrl = event.pull()
  198. if event == "touch" then
  199. for k,v in pairs(buttons) do
  200. 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
  201.     if v.switchedButton == true then
  202.       if not v.autoSwitch then
  203.         if v.buttonPressed == false then
  204.         v.buttonPressed = true
  205.         else
  206.         v.buttonPressed = false
  207.         end
  208.       else
  209.       v.buttonPressed = true
  210.       end
  211.     term.clear()
  212.     drawButtons()
  213.     end
  214. v.func()
  215. end
  216. end
  217. for k,v in pairs(listOfButtons) do
  218. 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
  219. v.buttons[v.startPosition + y-v.y - 1].func()
  220. end
  221. end
  222. elseif event == "scroll" then
  223.   for k,v in pairs(listOfButtons) do
  224.     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
  225.       if scrl == -1 then
  226.         if not v.infinite then
  227.         if v.startPosition < #v.buttons - v.height + 1 then
  228.         v.startPosition = v.startPosition + 1
  229.         end
  230.         else
  231.         table.insert(v.buttons, v.buttons[1])
  232.         table.remove(v.buttons, 1)
  233.         end
  234.       elseif scrl == 1 then
  235.         if not v.infinite then
  236.         if v.startPosition > 1 then
  237.         v.startPosition = v.startPosition - 1
  238.         end
  239.         else
  240.         table.insert(v.buttons, 1, v.buttons[#v.buttons])
  241.         table.remove(v.buttons, #v.buttons)
  242.         end
  243.       end
  244.     end
  245.   drawListsOfButtons(v)
  246.   end
  247. end
  248. end
  249.  
  250. end
  251.  
  252. drawButtons()
  253. drawListsOfButtons1()
  254. searchButton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement