Advertisement
Asioron

Calculator for OC 0.3

Nov 24th, 2018
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.89 KB | None | 0 0
  1. -------------------------------------------------------
  2. --               Программа КАЛЬКУЛЯТОР               --
  3. --        для компьютеров из мода OpenComputers      --
  4. --          проект http://computercraft.ru           --
  5. --                   Version 0.3                     --
  6. --                  2018, © Asior                    --
  7. -------------------------------------------------------
  8.  
  9. local gpu = require('component').gpu
  10. local computer = require('computer')
  11. local un = require('unicode')
  12. local pull_e = require('event').pull
  13. gpu.setResolution(22, 20)
  14. local W, H = gpu.getResolution()
  15. local b_color, f_color = gpu.getBackground(), gpu.getForeground()
  16. num_vr, num1, mat = '','',''
  17. local setCol = {skrLen = 20, fun1 = 0xff5000, fun2 = 0x808080, fun3 = 0xd3d3d3, key1 = 0xc0c0c0, key2 = 0xa9a9a9, tex1 = 0x000000, tex2 = 0xffff00}
  18.  
  19.  
  20. local tButtons = {
  21.   {
  22.     visible = false,
  23.     X = setCol.skrLen,
  24.     Y = 1,
  25.     W = 3,
  26.     H = 1,
  27.     color = 0xff0000,
  28.     textColor = 0xffffff,
  29.     text = 'X',
  30.     action = function()
  31.       gpu.setBackground(b_color)
  32.       gpu.setForeground(f_color)
  33.       gpu.fill(1, 1, W, H, ' ')
  34.       gpu.setResolution(80, 25)
  35.       os.exit()
  36.   end
  37.   },
  38.   {
  39.     visible = false,
  40.     X = 2,
  41.     Y = 5,
  42.     W = 5,
  43.     H = 3,
  44.     color = setCol.fun1,
  45.     textColor = setCol.tex1,
  46.     text = 'CE',
  47.     action = function()
  48.       gpu.setForeground(0xffffff)
  49.       gpu.setBackground(0x000000)
  50.       num_vr = ''
  51.       num1 = ''
  52.       gpu.set(2, 2, (' '):rep(setCol.skrLen))
  53.       gpu.set(2, 3, (' '):rep(setCol.skrLen))
  54.     end
  55.   },
  56.   {
  57.     visible = false,
  58.     X = 7,
  59.     Y = 5,
  60.     W = 5,
  61.     H = 3,
  62.     color = setCol.fun2,
  63.     textColor = setCol.tex1,
  64.     text = '/',
  65.     action = function()
  66.       gpu.setForeground(0xffffff)
  67.       gpu.setBackground(0x000000)
  68.       mat = '/'
  69.       if num1~='' then
  70.         num1 = arf(num1, tonumber(num_vr), mat)
  71.         num_vr = ''
  72.       else
  73.         num1 = tonumber(num_vr)
  74.         num_vr = ''
  75.       end
  76.       if num1 then
  77.         printText()
  78.       end
  79.     end
  80.   },
  81.   {
  82.     visible = false,
  83.     X = 12,
  84.     Y = 5,
  85.     W = 5,
  86.     H = 3,
  87.     color = setCol.fun3,
  88.     textColor = setCol.tex1,
  89.     text = '*',
  90.     action = function()
  91.       gpu.setForeground(0xffffff)
  92.       gpu.setBackground(0x000000)
  93.       mat = '*'
  94.       if num1~='' then
  95.         num1 = arf(num1, tonumber(num_vr), mat)
  96.         num_vr = ''
  97.       else
  98.         num1 = tonumber(num_vr)
  99.         num_vr = ''
  100.       end
  101.       if num1 then
  102.         printText()
  103.       end
  104.     end
  105.   },
  106.   {
  107.     visible = false,
  108.     X = 17,
  109.     Y = 5,
  110.     W = 5,
  111.     H = 3,
  112.     color = setCol.fun2,
  113.     textColor = setCol.tex1,
  114.     text = '-',
  115.     action = function()
  116.       gpu.setForeground(0xffffff)
  117.       gpu.setBackground(0x000000)
  118.       mat = '-'
  119.       if num1~='' then
  120.         num1 = arf(num1, tonumber(num_vr), mat)
  121.         num_vr = ''
  122.       else
  123.         num1 = tonumber(num_vr)
  124.         num_vr = ''
  125.       end
  126.       if num1 then
  127.         printText()
  128.       end
  129.     end
  130.   },
  131.   {
  132.     visible = false,
  133.     X = 17,
  134.     Y = 8,
  135.     W = 5,
  136.     H = 6,
  137.     color = setCol.fun3,
  138.     textColor = setCol.tex1,
  139.     text = '+',
  140.     action = function()
  141.       gpu.setForeground(0xffffff)
  142.       gpu.setBackground(0x000000)
  143.       mat = '+'
  144.       if num1~='' then
  145.         num1 = arf(num1, tonumber(num_vr), mat)
  146.         num_vr = ''
  147.       else
  148.         num1 = tonumber(num_vr)
  149.         num_vr = ''
  150.       end
  151.       if num1 then
  152.         printText()
  153.       end
  154.     end
  155.   },
  156.   {
  157.     visible = false,
  158.     X = 17,
  159.     Y = 14,
  160.     W = 5,
  161.     H = 6,
  162.     color = setCol.fun2,
  163.     textColor = setCol.tex1,
  164.     text = '=',
  165.     action = function()
  166.       gpu.setForeground(0xffffff)
  167.       gpu.setBackground(0x000000)
  168.       num1 = arf(num1, tonumber(num_vr), mat)
  169.       mat = ''
  170.       num_vr = ''
  171.       printText(1)
  172.       num1 = ''
  173.     end
  174.   },
  175. --################################################################--
  176.   {
  177.     visible = false,
  178.     X = 2,
  179.     Y = 8,
  180.     W = 5,
  181.     H = 3,
  182.     color = setCol.key1,
  183.     textColor = setCol.tex1,
  184.     text = '7',
  185.     action = function()
  186.       gpu.setForeground(0xffffff)
  187.       gpu.setBackground(0x000000)
  188.       num_vr = screenLen(num_vr, '7')
  189.       printText()
  190.     end
  191.   },
  192.   {
  193.     visible = false,
  194.     X = 7,
  195.     Y = 8,
  196.     W = 5,
  197.     H = 3,
  198.     color = setCol.key2,
  199.     textColor = setCol.tex1,
  200.     text = '8',
  201.     action = function()
  202.       gpu.setForeground(0xffffff)
  203.       gpu.setBackground(0x000000)
  204.       num_vr = screenLen(num_vr, '8')
  205.       printText()
  206.     end
  207.   },
  208.   {
  209.     visible = false,
  210.     X = 12,
  211.     Y = 8,
  212.     W = 5,
  213.     H = 3,
  214.     color = setCol.key1,
  215.     textColor = setCol.tex1,
  216.     text = '9',
  217.     action = function()
  218.       gpu.setForeground(0xffffff)
  219.       gpu.setBackground(0x000000)
  220.       num_vr = screenLen(num_vr, '9')
  221.       printText()
  222.     end
  223.   },
  224.   {
  225.     visible = false,
  226.     X = 2,
  227.     Y = 11,
  228.     W = 5,
  229.     H = 3,
  230.     color = setCol.key2,
  231.     textColor = setCol.tex1,
  232.     text = '4',
  233.     action = function()
  234.       gpu.setForeground(0xffffff)
  235.       gpu.setBackground(0x000000)
  236.       num_vr = screenLen(num_vr, '4')
  237.       printText()
  238.     end
  239.   },
  240.   {
  241.     visible = false,
  242.     X = 7,
  243.     Y = 11,
  244.     W = 5,
  245.     H = 3,
  246.     color = setCol.key1,
  247.     textColor = setCol.tex1,
  248.     text = '5',
  249.     action = function()
  250.       gpu.setForeground(0xffffff)
  251.       gpu.setBackground(0x000000)
  252.       num_vr = screenLen(num_vr, '5')
  253.       printText()
  254.     end
  255.   },
  256.   {
  257.     visible = false,
  258.     X = 12,
  259.     Y = 11,
  260.     W = 5,
  261.     H = 3,
  262.     color = setCol.key2,
  263.     textColor = setCol.tex1,
  264.     text = '6',
  265.     action = function()
  266.       gpu.setForeground(0xffffff)
  267.       gpu.setBackground(0x000000)
  268.       num_vr = screenLen(num_vr, '6')
  269.       printText()
  270.     end
  271.   },
  272.   {
  273.     visible = false,
  274.     X = 2,
  275.     Y = 14,
  276.     W = 5,
  277.     H = 3,
  278.     color = setCol.key1,
  279.     textColor = setCol.tex1,
  280.     text = '1',
  281.     action = function()
  282.       gpu.setForeground(0xffffff)
  283.       gpu.setBackground(0x000000)
  284.       num_vr = screenLen(num_vr, '1')
  285.       printText()
  286.     end
  287.   },
  288.   {
  289.     visible = false,
  290.     X = 7,
  291.     Y = 14,
  292.     W = 5,
  293.     H = 3,
  294.     color = setCol.key2,
  295.     textColor = setCol.tex1,
  296.     text = '2',
  297.     action = function()
  298.       gpu.setForeground(0xffffff)
  299.       gpu.setBackground(0x000000)
  300.       num_vr = screenLen(num_vr, '2')
  301.       printText()
  302.     end
  303.   },
  304.   {
  305.     visible = false,
  306.     X = 12,
  307.     Y = 14,
  308.     W = 5,
  309.     H = 3,
  310.     color = setCol.key1,
  311.     textColor = setCol.tex1,
  312.     text = '3',
  313.     action = function()
  314.       gpu.setForeground(0xffffff)
  315.       gpu.setBackground(0x000000)
  316.       num_vr = screenLen(num_vr, '3')
  317.       printText()
  318.     end
  319.   },
  320.   {
  321.     visible = false,
  322.     X = 2,
  323.     Y = 17,
  324.     W = 10,
  325.     H = 3,
  326.     color = setCol.fun2,
  327.     textColor = setCol.tex1,
  328.     text = '0',
  329.     action = function()
  330.       gpu.setForeground(0xffffff)
  331.       gpu.setBackground(0x000000)
  332.       num_vr = screenLen(num_vr, '0')
  333.       printText()
  334.     end
  335.   },
  336.   {
  337.     visible = false,
  338.     X = 12,
  339.     Y = 17,
  340.     W = 5,
  341.     H = 3,
  342.     color = setCol.key2,
  343.     textColor = setCol.tex1,
  344.     text = ',',
  345.     action = function()
  346.       gpu.setForeground(0xffffff)
  347.       gpu.setBackground(0x000000)
  348.       num_vr = screenLen(num_vr, '.')
  349.       printText()
  350.     end
  351.   },
  352. }
  353.  
  354. function arf(n1,n2,m)
  355.   if m ~= '' and n1 and n2 then
  356.     if m == '/' then
  357.       if n1 == 0 then
  358.         return -0
  359.       else
  360.         return n1/n2
  361.       end
  362.     elseif m == '*' then
  363.       return n1*n2
  364.     elseif m == '-' then
  365.       return n1-n2
  366.     elseif m == '+' then
  367.       return n1+n2
  368.     end
  369.   else
  370.     return 0
  371.   end
  372. end
  373.  
  374. function screenLen(sim, n)
  375.   if string.len(sim) < setCol.skrLen then
  376.     return sim..n
  377.   else
  378.     return sim
  379.   end
  380. end
  381.  
  382. function printText(cl)
  383.   if cl == 1 then
  384.     gpu.set(2, 2, (' '):rep(setCol.skrLen))
  385.     gpu.set(2, 3, (' '):rep(setCol.skrLen-string.len(num1))..num1)
  386.   else  
  387.     if num1 then
  388.       gpu.set(2, 2, (' '):rep(setCol.skrLen-2-string.len(num1))..num1..' '..mat)
  389.     end
  390.     gpu.set(2, 3, (' '):rep(setCol.skrLen-string.len(num_vr))..num_vr)
  391.   end
  392. end
  393.  
  394. local function drawButton(n) -- функция рисования кнопки
  395.   gpu.setBackground(tButtons[n].color) -- задаем цвет кнопки
  396.   gpu.setForeground(tButtons[n].textColor) -- задаем цвет текста
  397.   gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- заливаем область
  398.   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) -- пишем текст по центру
  399. end
  400.  
  401. local function toggleVisible(n) -- переключение видимости кнопки
  402.   if tButtons[n].visible then -- если кнопка видима
  403.     tButtons[n].visible = false -- отключаем
  404.     gpu.setBackground(b_color) -- берем цвет фона, полученный при старте программы
  405.     gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- стираем кнопку
  406.   else -- если кнопка не активна
  407.     tButtons[n].visible = true -- активируем
  408.     drawButton(n) -- запускаем отрисовку
  409.   end
  410. end
  411.  
  412. local function blink(n) -- мигание кнопки
  413.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем местами цвета фона и текста
  414.   drawButton(n) -- отрисовываем кнопку
  415.   os.sleep(0.09) -- делаем задержку
  416.   tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем цвета обратно
  417.   drawButton(n) -- перерисовываем кнопку
  418. end
  419.  
  420. gpu.fill(1, 1, W, H, ' ') -- очищаем экран
  421.  
  422. gpu.set(1, 1, '╔'..('═'):rep(setCol.skrLen-2))
  423. for i=2,19 do
  424.   gpu.set(1, i, '║'..(' '):rep(setCol.skrLen)..'║')
  425. end
  426. gpu.set(1, 4, '╠'..('═'):rep(setCol.skrLen)..'╣')
  427. gpu.set(1, 20, '╚'..('═'):rep(setCol.skrLen)..'╝')
  428.  
  429. for i = 1, #tButtons do
  430.   toggleVisible(i) -- активируем каждую кнопку
  431. end
  432.  
  433. while true do
  434.   local tEvent = {pull_e('touch')} -- ждем клика
  435.   for i = 1, #tButtons do -- перебираем все кнопки
  436.     if tButtons[i].visible then -- если кнопка активна
  437.     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 -- если клик произведен в пределах кнопки
  438.      blink(i) -- мигнуть кнопкой
  439.        tButtons[i].action() -- выполнить назначенный код
  440.        break
  441.       end
  442.     end
  443.   end
  444. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement