Advertisement
Hikooshi

Buttons

Jul 2nd, 2017
1,704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. --pastebin get 6QDe4kZH 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. 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},
  43.            button1 = {x=16, y=4, text="button1", active=true, switchedButton = false, autoSwitch=false, buttonPressed = false, func = funcButton1, height=2, cFore = 0xFFFFFF, cBack = 0x0000FF},
  44.            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},
  45.            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}}
  46.  
  47. function initButtons()
  48.  
  49. for k,v in pairs(buttons) do
  50. v.width = unicode.wlen(v.text) + 2
  51. end
  52.  
  53. end
  54.  
  55. initButtons()
  56.  
  57. function drawButtons()
  58.  
  59. for k,v in pairs(buttons) do
  60. if v.active then
  61. if not v.buttonPressed then -- если кнопка не нажата
  62. gpu.setForeground(v.cFore)
  63. gpu.setBackground(v.cBack)
  64. else                      -- в ином случае
  65. gpu.setForeground(v.cFore1)
  66. gpu.setBackground(v.cBack1)
  67. end
  68. gpu.fill(v.x, v.y, v.width, v.height, " ") -- фон для кнопки
  69. if v.height == 1 then         -- если высота кнопки равна 1
  70. gpu.set(v.x+1, v.y, v.text)
  71. elseif v.height%2 == 0 then   -- если высота кнопки равна четному числу
  72. gpu.set(v.x+1, v.y + (v.height/2 - 1), v.text)
  73. elseif v.height%2 == 1 then   -- если высота кнопки равна нечетному числу
  74. gpu.set(v.x+1, v.y + (math.ceil(v.height/2) - 1), v.text)
  75. end
  76. if v.autoSwitch == true and v.buttonPressed == true then
  77. v.buttonPressed = false
  78. os.sleep(4)
  79. drawButtons()
  80. end
  81. end
  82. end
  83. gpu.setForeground(0xFFFFFF)
  84. gpu.setBackground(0x000000)
  85.  
  86. end
  87.  
  88. function searchButton()
  89.  
  90. while true do
  91. local _,_,x,y = event.pull("touch")
  92. for k,v in pairs(buttons) do
  93. 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
  94. if v.switchedButton == true then
  95. if not v.autoSwitch then
  96. if v.buttonPressed == false then
  97. v.buttonPressed = true
  98. else
  99. v.buttonPressed = false
  100. end
  101. else
  102. v.buttonPressed = true
  103. end
  104. term.clear()
  105. drawButtons()
  106. end
  107. v.func()
  108. end
  109. end
  110. end
  111.  
  112. end
  113.  
  114. drawButtons()
  115. searchButton()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement