Advertisement
theTANCO

testGraphics.lua

Apr 13th, 2023 (edited)
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.54 KB | None | 0 0
  1. local xPos, yPos = 0, 0
  2. local altCode = ""
  3. local xSize, ySize = term.getSize()
  4. local xScroll, yScroll = 0, 0
  5. local xChar, yChar = math.min(17, xSize-11), math.min(17, ySize-1)
  6. local tCol, bCol = 2^0, 2^15
  7. local isPressed = {}
  8.  
  9. local getColorName = function(color)
  10.     local colorList = {
  11.         ["0"] = "white",
  12.         ["1"] = "orange",
  13.         ["2"] = "magenta",
  14.         ["3"] = "lightBlue",
  15.         ["4"] = "yellow",
  16.         ["5"] = "lime",
  17.         ["6"] = "pink",
  18.         ["7"] = "gray",
  19.         ["8"] = "lightGray",
  20.         ["9"] = "cyan",
  21.         ["a"] = "purple",
  22.         ["b"] = "blue",
  23.         ["c"] = "brown",
  24.         ["d"] = "green",
  25.         ["e"] = "red",
  26.         ["f"] = "black"
  27.     }
  28.     if type(color) == "number" then return colorList[colors.toBlit(color)]
  29.     else error("bad argument #1 to 'name' (expected number, got " .. type(color) .. ")", 2)
  30.     end
  31. end
  32.  
  33. local getColourName = function(colour)
  34.     local name = getColorName(colour)
  35.     if name == "gray" or name == "lightGray" then name = name:gsub("ray", "rey") end
  36.     return name
  37. end
  38.  
  39. local revExp = function(product, base) -- Reverse exponents.
  40.     -- 2^4 -> 16
  41.     -- revExp(16, 2) -> 4
  42.     if not tonumber(product) then
  43.         error("bad argument #1 to 'revExp' (expected number, got " .. type(product) .. ")", 2)
  44.     end
  45.     if not tonumber(base) then
  46.         error("bad argument #2 to 'revExp' (expected number, got " .. type(base) .. ")", 2)
  47.     end
  48.     return math.log(product) / math.log(base)
  49. end
  50.  
  51. local tohex = function(n)
  52.     local code = "0123456789ABCDEF"
  53.     local v = ""
  54.     n = tonumber(n)
  55.     if not tonumber(n) then error("bad argument #1 (expected number, got " .. type(n) .. ")") end
  56.     if n < 0 then return end
  57.     repeat
  58.         v = code:sub(n%16+1, n%16+1) .. v
  59.         n = math.floor(n/16)
  60.     until n == 0
  61.     return v
  62. end
  63.  
  64. local printChars = function()
  65.     for y = 1, yChar do
  66.         local ydec = y-2+yScroll
  67.         local yhex = tohex(ydec)
  68.         for x = 1, xChar do
  69.             local xdec = x-2+xScroll
  70.             local xhex = tohex(xdec)
  71.             term.setCursorPos(x, y+1)
  72.             term.setBackgroundColor(colors.black)
  73.             term.setTextColor(colors.yellow)
  74.             if y == 1 and x > 1 and x < 18 and xdec < 16 then term.write(xhex)
  75.             elseif x == 1 and y > 1 and y < 18 and ydec < 16 then term.write(yhex)
  76.             elseif x > 1 and x < 18 and y > 1 and y < 18 and xdec < 16 and ydec < 16 then
  77.                 term.setBackgroundColor(bCol)
  78.                 term.setTextColor(tCol)
  79.                 term.write(string.char(tonumber(yhex .. xhex, 16)))
  80.             else write(" ") end
  81.         end
  82.     end
  83. end
  84.  
  85. local printCharData = function()
  86.     local char = yPos*16+xPos
  87.     local x = xChar+8
  88.     term.setCursorPos(x, 1)
  89.     term.setCursorPos(x+3, 1)
  90.     term.setTextColor(tCol)
  91.     term.setBackgroundColor(bCol)
  92.     term.write(string.char(char))
  93.     term.setBackgroundColor(colors.black)
  94.     term.setCursorPos(x+2, 2)
  95.     term.setTextColor(colors.yellow)
  96.     term.write(tohex(yPos) .. tohex(xPos))
  97.     term.setCursorPos(x+1, 3)
  98.     term.setTextColor(colors.blue)
  99.     term.write(string.rep(" ", 3-string.len(char)) .. char)
  100. end
  101.  
  102. local printCols = function()
  103.     for c = 0, 15 do
  104.         term.setBackgroundColor(2^c)
  105.         if c < 7 or c == 8 then term.setTextColor(colors.black)
  106.         else term.setTextColor(colors.white) end
  107.         term.setCursorPos(xChar+2+c%4, 1+math.floor(c/4))
  108.         if 2^c == tCol and 2^c == bCol then term.write(string.char(7))
  109.         elseif 2^c == tCol then term.write("T")
  110.         elseif 2^c == bCol then term.write("B")
  111.         else term.write(" ") end
  112.     end
  113. end
  114.  
  115. local printColData = function(col, y, label)
  116.     local x = xChar + 2
  117.     term.setCursorPos(x, y)
  118.     term.setBackgroundColor(colors.black)
  119.     term.setTextColor(colors.white)
  120.     term.write(getColorName(col) .. string.rep(" ", 9-getColorName(col):len()))
  121.     term.setCursorPos(x, y+1)
  122.     term.setTextColor(colors.lightGray)
  123.     term.write(getColourName(col) .. string.rep(" ", 9-getColourName(col):len()))
  124.     term.setCursorPos(x, y+2)
  125.     term.setTextColor(colors.blue)
  126.     term.write(string.rep(" ", 5-string.len(col)) .. col .. " ")
  127.     term.setTextColor(colors.green)
  128.     term.write("2^" .. revExp(col, 2) .. string.rep(" ", 2-string.len(revExp(col, 2))))
  129.     term.setCursorPos(x, y+3)
  130.     term.setTextColor(colors.red)
  131.     term.write('"' .. colors.toBlit(col) .. '" ')
  132.     term.setTextColor(colors.yellow)
  133.     term.write("0x" .. tohex(col) .. string.rep(" ", 4-tohex(col):len()))
  134. end
  135. local tColData = function()
  136.     printColData(tCol, 5, "Text")
  137. end
  138. local bColData = function()
  139.     printColData(bCol, 9, "Back")
  140. end
  141.  
  142. local printAll = function()
  143.     term.setCursorPos(1, 1)
  144.     term.setTextColor(colors.gray)
  145.     term.setBackgroundColor(colors.black)
  146.     term.write("Press H for Help")
  147.     printChars()
  148.     printCols()
  149.     printCharData()
  150.     tColData()
  151.     bColData()
  152. end
  153.  
  154. local howto = function()
  155.     local pages = {
  156.         [1] = {
  157.             title = "Character Chart:",
  158.             text = {
  159.                 [1] = "Click or use Arrow Keys to",
  160.                 [2] = "select a character.",
  161.                 [3] = "Alt + Numpad to select a",
  162.                 [4] = "character with alt codes.",
  163.                 [5] = "Scroll Wheel to scroll " .. string.char(18) .. ".",
  164.                 [6] = "Shift + Scroll Wheel to",
  165.                 [7] = "scroll " .. string.char(27) .. string.char(26) .. ".",
  166.                 [8] = "Shift + Arrow Keys to",
  167.                 [9] = "scroll any direction."
  168.             },
  169.             color = {
  170.                 [1] = "88888000000008888888888000",
  171.                 [2] = "0000000000000000000",
  172.                 [3] = "888888888888000000000000",
  173.                 [4] = "0000000000000000000000000",
  174.                 [5] = "8888888888880000000000000",
  175.                 [6] = "88888888888888888888000",
  176.                 [7] = "0000000000",
  177.                 [8] = "888888888888888888000",
  178.                 [9] = "000000000000000000000"
  179.             }
  180.         },
  181.         [2] = {
  182.             title = "Color Chart:",
  183.             text = {
  184.                 [1] = "L.Click or Ctrl + Arrow",
  185.                 [2] = "Keys to select text color.",
  186.                 [3] = "R.Click or Alt + Arrow",
  187.                 [4] = "Keys to select background",
  188.                 [5] = "color.",
  189.                 [6] = "'T' means text color.",
  190.                 [7] = "'B' means background",
  191.                 [8] = "color.",
  192.                 [9] = "'" .. string.char(7) .. "' means both colors are",
  193.                 [10] = "the same."
  194.             },
  195.             color = {
  196.                 [1] = "88888880000888808088888",
  197.                 [2] = "88880000000000000000000000",
  198.                 [3] = "8888888000088808088888",
  199.                 [4] = "8888000000000000000000000",
  200.                 [5] = "000000",
  201.                 [6] = "888000000000000000000",
  202.                 [7] = "88800000000000000000",
  203.                 [8] = "000000",
  204.                 [9] = "8880000000000000000000000",
  205.                 [10] = "000000000"
  206.             }
  207.         },
  208.         [3] = {
  209.             title = "Other:",
  210.             text = {
  211.                 [1] = "Ctrl + E to exit the",
  212.                 [2] = "program.",
  213.                 [3] = "",
  214.                 [4] = "Character Data:",
  215.                 [5] = "Text with selected colors",
  216.                 [6] = "is the selected character.",
  217.                 [7] = "Yellow text is hex code.",
  218.                 [8] = "Blue text is the character",
  219.                 [9] = "code (used in string.char,",
  220.                 [10] = "etc.)"
  221.             },
  222.             color = {
  223.                 [1] = "88888888000000000000",
  224.                 [2] = "00000000",
  225.                 [3] = "",
  226.                 [4] = "444444444444444",
  227.                 [5] = "0000000000000000000000000",
  228.                 [6] = "00000000000000000000000000",
  229.                 [7] = "444444000000000000000000",
  230.                 [8] = "bbbb0000000000000000000000",
  231.                 [9] = "00000000000000000000000000",
  232.                 [10] = "00000"
  233.             }
  234.         },
  235.         [4] = {
  236.             title = "Color Data:",
  237.             text = {
  238.                 [1] = "First group is the",
  239.                 [2] = "selected text color.",
  240.                 [3] = "Second group is the",
  241.                 [4] = "selected background color.",
  242.                 [5] = "",
  243.                 [6] = "White text is the name",
  244.                 [7] = "used in colors API.",
  245.                 [8] = "Light Grey text is the",
  246.                 [9] = "name used in colours API."
  247.             },
  248.             color = {
  249.                 [1] = "888880000000000000",
  250.                 [2] = "00000000000000000000",
  251.                 [3] = "8888880000000000000",
  252.                 [4] = "00000000000000000000000000",
  253.                 [5] = "",
  254.                 [6] = "0000000000000000000000",
  255.                 [7] = "0000000000000000000",
  256.                 [8] = "8888888888000000000000",
  257.                 [9] = "0000000000000000000000000"
  258.             }
  259.         },
  260.         [5] = {
  261.             title = "Color Data:",
  262.             text = {
  263.                 [1] = "Blue text is the selected",
  264.                 [2] = "color code.",
  265.                 [3] = "Green text is math used to",
  266.                 [4] = "get the color code.",
  267.                 [5] = "Red text is the blit code",
  268.                 [6] = "(used in term.blit, etc.)",
  269.                 [7] = "Yellow text is the byte",
  270.                 [8] = "code."
  271.             },
  272.             color = {
  273.                 [1] = "bbbb000000000000000000000",
  274.                 [2] = "00000000000",
  275.                 [3] = "ddddd000000000000000000000",
  276.                 [4] = "0000000000000000000",
  277.                 [5] = "eee0000000000000000000000",
  278.                 [6] = "0000000000000000000000000",
  279.                 [7] = "44444400000000000000000",
  280.                 [8] = "00000"
  281.             }
  282.         }
  283.     }
  284.     local page = 1
  285.     term.setBackgroundColor(colors.black)
  286.     term.setTextColor(colors.yellow)
  287.     term.clear()
  288.     term.setCursorPos(1, 12)
  289.     write("Enter: Return   " .. string.char(24) .. string.char(25) ..string.char(27) .. string.char(26) .. ": Page")
  290.     local function printPage()
  291.         term.setCursorPos(1, 1)
  292.         term.clearLine()
  293.         print(pages[page].title)
  294.         for a = 1, 10 do
  295.             term.clearLine()
  296.             if a <= #pages[page].text then term.blit(pages[page].text[a], pages[page].color[a], string.rep("f", pages[page].text[a]:len())) end
  297.             print()
  298.         end
  299.     end
  300.     printPage()
  301.     while true do
  302.         local event = {os.pullEvent("key")}
  303.         if event[2] == keys.right or event[2] == keys.down then
  304.             page = page + 1
  305.             if page > #pages then page = 1 end
  306.             printPage()
  307.         elseif event[2] == keys.left or event[2] == keys.up then
  308.             page = page - 1
  309.             if page < 1 then page = #pages end
  310.             printPage()
  311.         elseif event[2] == keys.enter or event[2] == keys.numPadEnter then
  312.             break
  313.         end
  314.     end
  315. end
  316.  
  317. term.clear()
  318. printAll()
  319. while true do
  320.     local xCursor, yCursor = xPos-xScroll+2, yPos-yScroll+3
  321.     if xCursor > 1 and xCursor <= xChar and yCursor > 2 and yCursor <= yChar+1 then
  322.         if bCol < colors.gray or bCol == colors.lightGray then
  323.             if tCol == colors.gray then term.setTextColor(colors.black)
  324.             else term.setTextColor(colors.gray) end
  325.         else
  326.             if tCol == colors.lightGray then term.setTextColor(colors.white)
  327.             else term.setTextColor(colors.lightGray) end
  328.         end
  329.         term.setCursorPos(xCursor, yCursor)
  330.         term.setCursorBlink(true)
  331.     end
  332.     local event = {os.pullEvent()}
  333.     term.setCursorBlink(false)
  334.     if event[1] == "mouse_click" then
  335.         if event[3] > 1 and event[3] <= xChar and event[4] > 2 and event[4] <= yChar+1 then
  336.             xPos, yPos = event[3] + xScroll - 2, event[4] + yScroll - 3
  337.             printCharData()
  338.         elseif event[3] > xChar+1 and event[3] < xChar+6 and event[4] > 0 and event[4] < 5 then
  339.             if event[2] == 1 then tCol = 2 ^ (event[3] - xChar - 2 + (event[4]-1) * 4)
  340.             elseif event[2] == 2 then bCol = 2 ^ (event[3] - xChar - 2 + (event[4]-1) * 4) end
  341.             printAll()
  342.         end
  343.     elseif event[1] == "key" then
  344.         isPressed[event[2] ] = true
  345.         if isPressed[keys.leftShift] or isPressed[keys.rightShift] then
  346.             if event[2] == keys.up and yScroll > 0 then yScroll = yScroll - 1
  347.             elseif event[2] == keys.down and yScroll+yChar-1 < 16 then yScroll = yScroll + 1
  348.             elseif event[2] == keys.left and xScroll > 0 then xScroll = xScroll - 1
  349.             elseif event[2] == keys.right and xScroll+xChar-1 < 16 then xScroll = xScroll + 1
  350.             end
  351.             if event[2] == keys.up or event[2] == keys.down or event[2] == keys.left or event[2] == keys.right then printChars() end
  352.         elseif isPressed[keys.leftCtrl] or isPressed[keys.rightCtrl] then
  353.             if event[2] == keys.up then tCol = 2 ^ (revExp(tCol, 2) - 4)
  354.             elseif event[2] == keys.down then tCol = 2 ^ (revExp(tCol, 2) + 4)
  355.             elseif event[2] == keys.left then tCol = 2 ^ (revExp(tCol, 2) - 1)
  356.             elseif event[2] == keys.right then tCol = 2 ^ (revExp(tCol, 2) + 1)
  357.             elseif event[2] == keys.e then
  358.                 term.setBackgroundColor(colors.black)
  359.                 term.clear()
  360.                 term.setCursorPos(1, 1)
  361.                 error()
  362.             end
  363.             if revExp(tCol, 2) < 0 then tCol = 2 ^ (revExp(tCol, 2) + 16) end
  364.             if revExp(tCol, 2) > 15 then tCol = 2 ^ (revExp(tCol, 2) - 16) end
  365.             if event[2] == keys.up or event[2] == keys.down or event[2] == keys.left or event[2] == keys.right then printAll() end
  366.         elseif isPressed[keys.leftAlt] or isPressed[keys.rightAlt] then
  367.             if event[2] == keys.up then bCol = 2 ^ (revExp(bCol, 2) - 4)
  368.             elseif event[2] == keys.down then bCol = 2 ^ (revExp(bCol, 2) + 4)
  369.             elseif event[2] == keys.left then bCol = 2 ^ (revExp(bCol, 2) - 1)
  370.             elseif event[2] == keys.right then bCol = 2 ^ (revExp(bCol, 2) + 1)
  371.             end
  372.             if revExp(bCol, 2) < 0 then bCol = 2 ^ (revExp(bCol, 2) + 16) end
  373.             if revExp(bCol, 2) > 15 then bCol = 2 ^ (revExp(bCol, 2) - 16) end
  374.             if event[2] == keys.up or event[2] == keys.down or event[2] == keys.left or event[2] == keys.right then printAll() end
  375.         else
  376.             if event[2] == keys.up then yPos = yPos - 1
  377.             elseif event[2] == keys.down then yPos = yPos + 1
  378.             elseif event[2] == keys.left then xPos = xPos - 1
  379.             elseif event[2] == keys.right then xPos = xPos + 1
  380.             elseif event[2] == keys.h then
  381.                 howto()
  382.                 term.clear()
  383.                 printAll()
  384.             end
  385.             if xPos < 0 then xPos, yPos = xPos + 16, yPos - 1 end
  386.             if xPos > 15 then xPos, yPos = xPos - 16, yPos + 1 end
  387.             if yPos < 0 then yPos = yPos + 16 end
  388.             if yPos > 15 then yPos = yPos - 16 end
  389.         end
  390.         if event[2] == keys.up or event[2] == keys.down or event[2] == keys.left or event[2] == keys.right then printCharData() end
  391.         local altKeys = {[keys.numPad0] = "0", [keys.numPad1] = "1", [keys.numPad2] = "2", [keys.numPad3] = "3", [keys.numPad4] = "4",
  392.         [keys.numPad5] = "5", [keys.numPad6] = "6", [keys.numPad7] = "7", [keys.numPad8] = "8", [keys.numPad9] = "9"}
  393.         if (isPressed[keys.leftAlt] or isPressed[keys.rightAlt]) and altKeys[event[2] ] then
  394.             altCode = altCode .. altKeys[event[2] ]
  395.         end
  396.     elseif event[1] == "key_up" then
  397.         isPressed[event[2] ] = false
  398.         if event[2] == keys.leftAlt or event[2] == keys.rightAlt then
  399.             if  tonumber(altCode) then
  400.                 altCode = tonumber(altCode) % 256
  401.                 xPos = altCode%16
  402.                 yPos = math.floor(altCode/16)
  403.                 printCharData()
  404.             end
  405.             altCode = ""
  406.         end
  407.     elseif event[1] == "mouse_scroll" then
  408.         if (isPressed[keys.leftShift] or isPressed[keys.rightShift]) and ((event[2] == -1 and xScroll > 0) or (event[2] == 1 and xScroll+xChar-1 < 16)) then
  409.             xScroll = xScroll + event[2]
  410.         elseif not(isPressed[keys.leftShift] or isPressed[keys.rightShift]) and ((event[2] == -1 and yScroll > 0) or (event[2] == 1 and yScroll+yChar-1 < 16)) then
  411.             yScroll = yScroll + event[2]
  412.         end
  413.         printChars()
  414.     end
  415. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement