Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------
- -- Программа КАЛЬКУЛЯТОР --
- -- для компьютеров из мода OpenComputers --
- -- проект http://computercraft.ru --
- -- Version 0.3 --
- -- 2018, © Asior --
- -------------------------------------------------------
- local gpu = require('component').gpu
- local computer = require('computer')
- local un = require('unicode')
- local pull_e = require('event').pull
- gpu.setResolution(22, 20)
- local W, H = gpu.getResolution()
- local b_color, f_color = gpu.getBackground(), gpu.getForeground()
- num_vr, num1, mat = '','',''
- local setCol = {skrLen = 20, fun1 = 0xff5000, fun2 = 0x808080, fun3 = 0xd3d3d3, key1 = 0xc0c0c0, key2 = 0xa9a9a9, tex1 = 0x000000, tex2 = 0xffff00}
- local tButtons = {
- {
- visible = false,
- X = setCol.skrLen,
- Y = 1,
- W = 3,
- H = 1,
- color = 0xff0000,
- textColor = 0xffffff,
- text = 'X',
- action = function()
- gpu.setBackground(b_color)
- gpu.setForeground(f_color)
- gpu.fill(1, 1, W, H, ' ')
- gpu.setResolution(80, 25)
- os.exit()
- end
- },
- {
- visible = false,
- X = 2,
- Y = 5,
- W = 5,
- H = 3,
- color = setCol.fun1,
- textColor = setCol.tex1,
- text = 'CE',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = ''
- num1 = ''
- gpu.set(2, 2, (' '):rep(setCol.skrLen))
- gpu.set(2, 3, (' '):rep(setCol.skrLen))
- end
- },
- {
- visible = false,
- X = 7,
- Y = 5,
- W = 5,
- H = 3,
- color = setCol.fun2,
- textColor = setCol.tex1,
- text = '/',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- mat = '/'
- if num1~='' then
- num1 = arf(num1, tonumber(num_vr), mat)
- num_vr = ''
- else
- num1 = tonumber(num_vr)
- num_vr = ''
- end
- if num1 then
- printText()
- end
- end
- },
- {
- visible = false,
- X = 12,
- Y = 5,
- W = 5,
- H = 3,
- color = setCol.fun3,
- textColor = setCol.tex1,
- text = '*',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- mat = '*'
- if num1~='' then
- num1 = arf(num1, tonumber(num_vr), mat)
- num_vr = ''
- else
- num1 = tonumber(num_vr)
- num_vr = ''
- end
- if num1 then
- printText()
- end
- end
- },
- {
- visible = false,
- X = 17,
- Y = 5,
- W = 5,
- H = 3,
- color = setCol.fun2,
- textColor = setCol.tex1,
- text = '-',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- mat = '-'
- if num1~='' then
- num1 = arf(num1, tonumber(num_vr), mat)
- num_vr = ''
- else
- num1 = tonumber(num_vr)
- num_vr = ''
- end
- if num1 then
- printText()
- end
- end
- },
- {
- visible = false,
- X = 17,
- Y = 8,
- W = 5,
- H = 6,
- color = setCol.fun3,
- textColor = setCol.tex1,
- text = '+',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- mat = '+'
- if num1~='' then
- num1 = arf(num1, tonumber(num_vr), mat)
- num_vr = ''
- else
- num1 = tonumber(num_vr)
- num_vr = ''
- end
- if num1 then
- printText()
- end
- end
- },
- {
- visible = false,
- X = 17,
- Y = 14,
- W = 5,
- H = 6,
- color = setCol.fun2,
- textColor = setCol.tex1,
- text = '=',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num1 = arf(num1, tonumber(num_vr), mat)
- mat = ''
- num_vr = ''
- printText(1)
- num1 = ''
- end
- },
- --################################################################--
- {
- visible = false,
- X = 2,
- Y = 8,
- W = 5,
- H = 3,
- color = setCol.key1,
- textColor = setCol.tex1,
- text = '7',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '7')
- printText()
- end
- },
- {
- visible = false,
- X = 7,
- Y = 8,
- W = 5,
- H = 3,
- color = setCol.key2,
- textColor = setCol.tex1,
- text = '8',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '8')
- printText()
- end
- },
- {
- visible = false,
- X = 12,
- Y = 8,
- W = 5,
- H = 3,
- color = setCol.key1,
- textColor = setCol.tex1,
- text = '9',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '9')
- printText()
- end
- },
- {
- visible = false,
- X = 2,
- Y = 11,
- W = 5,
- H = 3,
- color = setCol.key2,
- textColor = setCol.tex1,
- text = '4',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '4')
- printText()
- end
- },
- {
- visible = false,
- X = 7,
- Y = 11,
- W = 5,
- H = 3,
- color = setCol.key1,
- textColor = setCol.tex1,
- text = '5',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '5')
- printText()
- end
- },
- {
- visible = false,
- X = 12,
- Y = 11,
- W = 5,
- H = 3,
- color = setCol.key2,
- textColor = setCol.tex1,
- text = '6',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '6')
- printText()
- end
- },
- {
- visible = false,
- X = 2,
- Y = 14,
- W = 5,
- H = 3,
- color = setCol.key1,
- textColor = setCol.tex1,
- text = '1',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '1')
- printText()
- end
- },
- {
- visible = false,
- X = 7,
- Y = 14,
- W = 5,
- H = 3,
- color = setCol.key2,
- textColor = setCol.tex1,
- text = '2',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '2')
- printText()
- end
- },
- {
- visible = false,
- X = 12,
- Y = 14,
- W = 5,
- H = 3,
- color = setCol.key1,
- textColor = setCol.tex1,
- text = '3',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '3')
- printText()
- end
- },
- {
- visible = false,
- X = 2,
- Y = 17,
- W = 10,
- H = 3,
- color = setCol.fun2,
- textColor = setCol.tex1,
- text = '0',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '0')
- printText()
- end
- },
- {
- visible = false,
- X = 12,
- Y = 17,
- W = 5,
- H = 3,
- color = setCol.key2,
- textColor = setCol.tex1,
- text = ',',
- action = function()
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- num_vr = screenLen(num_vr, '.')
- printText()
- end
- },
- }
- function arf(n1,n2,m)
- if m ~= '' and n1 and n2 then
- if m == '/' then
- if n1 == 0 then
- return -0
- else
- return n1/n2
- end
- elseif m == '*' then
- return n1*n2
- elseif m == '-' then
- return n1-n2
- elseif m == '+' then
- return n1+n2
- end
- else
- return 0
- end
- end
- function screenLen(sim, n)
- if string.len(sim) < setCol.skrLen then
- return sim..n
- else
- return sim
- end
- end
- function printText(cl)
- if cl == 1 then
- gpu.set(2, 2, (' '):rep(setCol.skrLen))
- gpu.set(2, 3, (' '):rep(setCol.skrLen-string.len(num1))..num1)
- else
- if num1 then
- gpu.set(2, 2, (' '):rep(setCol.skrLen-2-string.len(num1))..num1..' '..mat)
- end
- gpu.set(2, 3, (' '):rep(setCol.skrLen-string.len(num_vr))..num_vr)
- end
- 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, ' ') -- очищаем экран
- gpu.set(1, 1, '╔'..('═'):rep(setCol.skrLen-2))
- for i=2,19 do
- gpu.set(1, i, '║'..(' '):rep(setCol.skrLen)..'║')
- end
- gpu.set(1, 4, '╠'..('═'):rep(setCol.skrLen)..'╣')
- gpu.set(1, 20, '╚'..('═'):rep(setCol.skrLen)..'╝')
- 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-1 and tEvent[4] >= tButtons[i].Y and tEvent[4] <= tButtons[i].Y+ tButtons[i].H-1 then -- если клик произведен в пределах кнопки
- blink(i) -- мигнуть кнопкой
- tButtons[i].action() -- выполнить назначенный код
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement