Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gpu = require('component').gpu
- local computer = require('computer')
- local pull_e = require('event').pull
- local W, H = gpu.getResolution()
- local un = require("unicode")
- local b_color, f_color = gpu.getBackground(), gpu.getForeground()
- local tButtons = {
- {
- visible = false,
- X = 1, --местоположение кнопки по Х
- Y = 5, --местоположение кнопки по У
- W = 13, --длина кнопки (длина названия кнопки + 3)
- H = 1, --ширина кнопки
- color = 0x008800, --цвет фона кнопки
- textColor = 0xffff00, --цвет текста кнопки
- text = 'Йа кнопофка', --текст кнопки
- action = function() --функция с командами которые выполняются по нажатию, в этом примере простой вывод сообщения
- gpu.setForeground(0xffffff)
- gpu.set(1, 1, 'А я её действие')
- end
- }
- }
- local function drawButton(n) -- функция рисования кнопки
- gpu.setBackground(tButtons[n].color) -- задаем цвет кнопки
- gpu.setForeground(tButtons[n].textColor) -- задаем цвет текста
- gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- заливаем область
- 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) -- пишем текст по центру
- end
- local function toggleVisible(n) -- переключение видимости кнопки
- if tButtons[n].visible then -- если кнопка видима
- tButtons[n].visible = false -- отключаем
- gpu.setBackground(b_color) -- берем цвет фона, полученный при старте программы
- gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- стираем кнопку
- else -- если кнопка не активна
- tButtons[n].visible = true -- активируем
- drawButton(n) -- запускаем отрисовку
- end
- end
- local function blink(n) -- мигание кнопки
- tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем местами цвета фона и текста
- drawButton(n) -- отрисовываем кнопку
- os.sleep(0.09) -- делаем задержку
- tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем цвета обратно
- drawButton(n) -- перерисовываем кнопку
- end
- gpu.fill(1, 1, W, H, ' ') -- очищаем экран
- for i = 1, #tButtons do
- toggleVisible(i) -- активируем каждую кнопку
- end
- while true do
- local tEvent = {pull_e('touch')} -- ждем клика
- for i = 1, #tButtons do -- перебираем все кнопки
- if tButtons[i].visible then -- если кнопка активна
- 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 -- если клик произведен в пределах кнопки
- blink(i) -- мигнуть кнопкой
- tButtons[i].action() -- выполнить назначенный код
- break
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment