Barawik_

кнопка1

Jul 29th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local gpu = require('component').gpu
  2. local computer = require('computer')
  3. local pull_e = require('event').pull
  4. local W, H = gpu.getResolution()
  5. local un = require("unicode")
  6. local b_color, f_color = gpu.getBackground(), gpu.getForeground()
  7.  
  8. local tButtons = {
  9.   {
  10.     visible = false,
  11.     X = 1, --местоположение кнопки по Х
  12.     Y = 5, --местоположение кнопки по У
  13.     W = 13, --длина кнопки (длина названия кнопки + 3)
  14.     H = 1,   --ширина кнопки
  15.     color = 0x008800, --цвет фона кнопки
  16.     textColor = 0xffff00,  --цвет текста кнопки
  17.     text = 'Йа кнопофка',  --текст кнопки
  18.     action = function()  --функция с командами которые выполняются по нажатию, в этом примере простой вывод сообщения
  19.       gpu.setForeground(0xffffff)
  20.       gpu.set(1, 1, 'А я её действие')
  21.     end
  22.   }
  23. }
  24.  
  25. local function drawButton(n) -- функция рисования кнопки
  26.   gpu.setBackground(tButtons[n].color) -- задаем цвет кнопки
  27.   gpu.setForeground(tButtons[n].textColor) -- задаем цвет текста
  28.   gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- заливаем область
  29.   gpu.set(tButtons[n].X+(tButtons[n].W/2)-(un.len(tButtons[n].text)/2), tButtons[n].Y+(tButtons[n].H/2), tButtons[n].text) -- пишем текст по центру
  30. end
  31.  
  32. local function toggleVisible(n) -- переключение видимости кнопки
  33.   if tButtons[n].visible then -- если кнопка видима
  34.     tButtons[n].visible = false -- отключаем
  35.     gpu.setBackground(b_color) -- берем цвет фона, полученный при старте программы
  36.     gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- стираем кнопку
  37.   else -- если кнопка не активна
  38.     tButtons[n].visible = true -- активируем
  39.     drawButton(n) -- запускаем отрисовку
  40.   end
  41. end
  42.  
  43. local function blink(n) -- мигание кнопки
  44.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем местами цвета фона и текста
  45.   drawButton(n) -- отрисовываем кнопку
  46.   os.sleep(0.09) -- делаем задержку
  47.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем цвета обратно
  48.   drawButton(n) -- перерисовываем кнопку
  49. end
  50.  
  51. gpu.fill(1, 1, W, H, ' ') -- очищаем экран
  52.  
  53. for i = 1, #tButtons do
  54.   toggleVisible(i) -- активируем каждую кнопку
  55. end
  56.  
  57. while true do
  58.   local tEvent = {pull_e('touch')} -- ждем клика
  59.   for i = 1, #tButtons do -- перебираем все кнопки
  60.     if tButtons[i].visible then -- если кнопка активна
  61.       if tEvent[3] >= tButtons[i].X and tEvent[3] <= tButtons[i].X+tButtons[i].W and tEvent[4] >= tButtons[i].Y and tEvent[4] <= tButtons[i].Y+tButtons[i].H then -- если клик произведен в пределах кнопки
  62.        blink(i) -- мигнуть кнопкой
  63.        tButtons[i].action() -- выполнить назначенный код
  64.        break
  65.       end
  66.     end
  67.   end
  68. end
Add Comment
Please, Sign In to add comment