serafim7

символы [OpenComputers]

Nov 8th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.38 KB | None | 0 0
  1. local com = require("component")
  2. local unicode = require("unicode")
  3. local event = require("event")
  4. local gpu = com.gpu
  5. -- ─│┌┐└┘├┤┬┴┼
  6.  
  7. local w, h = gpu.maxResolution()
  8. local hMax = math.floor((w-7)/3) -- максимум символов влезет по горизонтали
  9. local vMax = math.floor((h-2)/3) -- максимум символов влезет по вертикали
  10. w, h = hMax*3+7, vMax*3+1
  11. gpu.setResolution(w, h) -- установка оптимального размера
  12.  
  13. gpu.setBackground(0)
  14. gpu.setForeground(0xffffff)
  15. gpu.fill(1, 1, w, h, " ")
  16.  
  17. local position = 1 -- позиция списка избранного
  18. local startSymbol = 0 -- символ, с которого начинается отображаемый список
  19. local liked = {}
  20. local curSymbol = -1
  21. local mode = false -- текущий список - избранное
  22. local file = "" -- текущий сохраняемый файл
  23. local fileNumber = 0
  24.  
  25. local function fillSymbols(start, count)
  26.   local str = ""
  27.   if start == -1 then -- заполнить средними разделителями
  28.     str = "├"
  29.     for i=0, (count-1) do
  30.       str = str.."──┼"
  31.     end
  32.     str = str:sub(1, str:len()-("┼"):len()).."┤"
  33.   elseif start == -2 then -- заполнить двумя пробелами и разделителем
  34.     str = "│"
  35.     for i=0, (count-1) do
  36.       str = str.."  │"
  37.     end
  38.   else
  39.     str = "│"
  40.     for i=start, (start+count-1) do -- заполнить символом, пробелом и разделителем
  41.       if i > 0xffff then
  42.         str = str.."  │"
  43.       else
  44.         char = unicode.char(i)
  45.         if unicode.charWidth(char) > 1 then
  46.           str = str..char.."│"
  47.         else
  48.           str = str..char.." │"
  49.         end
  50.       end
  51.     end
  52.   end
  53.   return str
  54. end
  55. local function drawAllTable()
  56.   gpu.fill(1, 1, w-6, h, " ")
  57.   str = "┌" -- верхняя часть
  58.   for i=0, (hMax-1) do
  59.     str = str.."──┬"
  60.   end
  61.   gpu.set(1, 1, str)
  62.   for i=0, (vMax-1) do
  63.     gpu.set(1, 2+i*3, fillSymbols(startSymbol+i*hMax, hMax))
  64.     gpu.set(1, 3+i*3, fillSymbols(-2, hMax))
  65.     gpu.set(1, 4+i*3, fillSymbols(-1, hMax))
  66.   end
  67.   str = "└" -- нижняя часть
  68.   for i=0, (hMax-1) do
  69.     str = str.."──┴"
  70.   end
  71.   gpu.set(1, h ,str)
  72. end
  73.  
  74. local function fillTableSymbols(start, count, tab)
  75.   local str = ""
  76.   if start == -1 then -- заполнить средними разделителями
  77.     str = "├"
  78.     for i=0, (count-1) do
  79.       str = str.."──┼"
  80.     end
  81.     str = str:sub(1, str:len()-("┼"):len()).."┤"
  82.   elseif start == -2 then -- заполнить двумя пробелами и разделителем
  83.     str = "│"
  84.     for i=0, (count-1) do
  85.       str = str.."  │"
  86.     end
  87.   else
  88.     str = "│"
  89.     for i=start, (start+count-1) do -- заполнить символом, пробелом и разделителем
  90.       if i > #tab then
  91.         str = str.."  │"
  92.       else
  93.         char = unicode.char(tab[i])
  94.         if unicode.charWidth(char) > 1 then
  95.           str = str..char.."│"
  96.         else
  97.           str = str..char.." │"
  98.         end
  99.       end
  100.     end
  101.   end
  102.   return str
  103. end
  104. local function drawLikedTable()
  105.   gpu.fill(2, 2, w-8, h-2, " ")
  106.   for i=0, (vMax-1) do
  107.     gpu.set(1, 2+i*3, fillTableSymbols(position+i*hMax, hMax, liked))
  108.     gpu.set(1, 3+i*3, fillTableSymbols(-2, hMax))
  109.     gpu.set(1, 4+i*3, fillTableSymbols(-1, hMax))
  110.   end
  111.   str = "└"
  112.   for i=0, (hMax-1) do
  113.     str = str.."──┴"
  114.   end
  115.   gpu.set(1, h ,str)
  116. end
  117.  
  118. local function drawButton(x,y,text)
  119.   gpu.set(x, y, "▁▁▁▁▁")
  120.   gpu.set(x, y+1, text)
  121.   gpu.set(x, y+2, "▔▔▔▔▔")
  122. end
  123.  
  124. local function drawSaved(text)
  125.   if text ~= "" then
  126.     gpu.set(w-5, h-6, "Saved")
  127.     gpu.set(w-5, h-5, "as")
  128.     gpu.set(w-5, h-4, text)
  129.   else
  130.     gpu.fill(w-5, h-6, 5, 3, " ")
  131.   end
  132. end
  133.  
  134. local function drawCharInfo(char, liked)
  135.   gpu.fill(w-5, 8, 5, 5, " ")
  136.   local ch = unicode.char(char)
  137.   gpu.set(w-5, 8, ch..(unicode.charWidth(ch) > 1 and "" or " ").." "..(liked and "◢◣" or "/\\"))
  138.   gpu.set(w-5, 9, "   "..(liked and "◥◤" or "\\/"))
  139.   gpu.set(w-5, 10, "Code:")
  140.   gpu.set(w-5, 11, tostring(char))
  141.   gpu.set(w-5, 12, string.format("x%X", char))
  142. end
  143.  
  144. local function drawMenu()
  145.   gpu.set(w-5, 1, "────><")
  146.   gpu.fill(w, 2, 1, h-2, "│")
  147.  
  148.   gpu.set(w-5, 2, " ◢ ◣ ")
  149.   gpu.set(w-5, 3, " ◥ ◤ ")
  150.   gpu.set(w-5, 4, string.format("%X/", startSymbol))
  151.   gpu.set(w-5, 5, "xFFFF")
  152.  
  153.   drawButton(w-5, h-9, "Liked")
  154.   drawButton(w-5, h-3, "Save")
  155.  
  156.   gpu.set(w-5, h, "─────┘")
  157. end
  158.  
  159. local function getSymbolIndex(x, y) -- считается порядковый номер по координатам клика
  160.   if x < 2 or y < 2 or (x-1)%3==0 or
  161.       (y-1)%3==0 then return -1 end
  162.  
  163.   return math.floor((y-1)/3)*hMax + math.floor((x-1)/3)
  164. end
  165.  
  166. drawAllTable()
  167. drawMenu()
  168. while true do
  169.   local evnt = {event.pull("touch")}
  170.   local x, y = evnt[3], evnt[4]
  171.   if evnt[5] == 0 then
  172.     if x > (w-2) and y == 1 then -- выход
  173.       break
  174.     else
  175.       if x < w-5 then -- выбор символа
  176.         local i = getSymbolIndex(x, y)
  177.         if i > -1 then
  178.           if mode then
  179.             if position+i <= #liked then
  180.               drawCharInfo(liked[position+i], true)
  181.               curSymbol = liked[position+i]
  182.             end
  183.           elseif startSymbol+i <= 0xffff then
  184.             local finded = false -- есть ли символ в избранных
  185.             for j=1, #liked do
  186.               if liked[j] == startSymbol+i then
  187.                 finded = true
  188.                 break
  189.               end
  190.             end
  191.             drawCharInfo(startSymbol+i, finded)
  192.             curSymbol = startSymbol+i
  193.           end
  194.         end
  195.       else
  196.         if x < w then
  197.           local str = ""
  198.           if y > 1 and y < 4 then -- перемотка страниц
  199.             if x > w-3 then
  200.               if mode then
  201.                 if (position+hMax*vMax) <= #liked then
  202.                   position = position+hMax*vMax
  203.                   str = string.format("%X/", position)
  204.                   gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  205.                   drawLikedTable()
  206.                 end
  207.               else
  208.                 if (startSymbol+hMax*vMax) <= 0xffff then
  209.                   startSymbol = startSymbol + hMax*vMax
  210.                   str = string.format("%X/", startSymbol)
  211.                   gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  212.                   drawAllTable()
  213.                 end
  214.               end
  215.             elseif x < w-3 then
  216.               if mode then
  217.                 if position > 1 then
  218.                   position = math.max(position - hMax*vMax, 0)
  219.                   str = string.format("%X/", position)
  220.                   gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  221.                   drawLikedTable()
  222.                 end
  223.               else
  224.                 if startSymbol > 0 then
  225.                   startSymbol = math.max(startSymbol - hMax*vMax, 0)
  226.                   str = string.format("%X/", startSymbol)
  227.                   gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  228.                   drawAllTable()
  229.                 end
  230.               end
  231.             end
  232.           elseif y > 7 and y < 10 then
  233.             if x > w-3 then -- добавить/убрать символ в списке "Избранное"
  234.               local finded = false
  235.               for i=1, #liked do
  236.                 if liked[i] == curSymbol then
  237.                   finded = true
  238.                   table.remove(liked, i)
  239.                   break
  240.                 end
  241.               end
  242.               if not finded then
  243.                 table.insert(liked, curSymbol)
  244.               end
  245.               if mode then
  246.                 drawLikedTable()
  247.               end
  248.               drawSaved("")
  249.               drawCharInfo(curSymbol, not finded)
  250.             end
  251.           elseif y > h-10 and y < h-6 then -- избранное/все
  252.             if mode then
  253.               mode = false
  254.               drawButton(w-5, h-9, "Liked")
  255.               str = string.format("%X/", startSymbol)
  256.               gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  257.               gpu.set(w-5, 5, "xFFFF")
  258.               drawAllTable()
  259.             else
  260.               mode = true
  261.               drawButton(w-5, h-9, " All ")
  262.               str = string.format("%X/", position)
  263.               gpu.set(w-5, 4, str..(" "):rep(5-str:len()))
  264.               str = tostring(#liked)
  265.               gpu.set(w-5, 5, str..(" "):rep(5-str:len()))
  266.               drawLikedTable()
  267.             end
  268.           elseif y > h-4 and y < h then -- сохранить
  269.             if #liked > 0 then
  270.               if file == "" then
  271.                 fileNumber = math.random(99999)
  272.                 file = "/symbols-"..tostring(fileNumber)..".txt"
  273.               end
  274.              
  275.               local fs = io.open(file, "w")
  276.               str = ""
  277.               for i=1, #liked do
  278.                 str = str..unicode.char(liked[i])
  279.               end
  280.               fs:write(str)
  281.               fs:close()
  282.              
  283.               drawSaved(tostring(fileNumber))
  284.             end
  285.           end
  286.         end
  287.       end
  288.     end
  289.   end
  290. end
  291.  
  292. w, h = gpu.maxResolution()
  293. gpu.setResolution(w, h)
  294. gpu.fill(1, 1, w, h, " ")
Add Comment
Please, Sign In to add comment