Advertisement
Qivex

QFormat-API

Mar 10th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. -------
  2. --Init:
  3. -------
  4.  
  5. --API-Test
  6. if ( qmain == nil ) then
  7.     error("The QFormat API requires the QMain API! (Reboot to fix half-loaded APIs)", 0)
  8. end
  9.  
  10. --Local Functions
  11. local getChar
  12.  
  13. --Local Vars
  14. local default_image = {{"iiiiiiiiiiiiiiiiiiii","i                  i","i abcdefghijklmnop i","i dddddddddddddddd i","i                  i","iiiiiiiiiiiiiiiiiiii"},{"","","","  abcdefghijklmnop"},{"","","","  All Colors here!"}}
  15.  
  16.  
  17. ----------
  18. --Methods:
  19. ----------
  20.  
  21. writeMarkup = function(...)
  22.     --Input
  23.     local input = {...}
  24.     local default = {"&d&HDemo", {1, 1}, qmain.getScreen()}
  25.     --Parameters
  26.     local script = input[1] or default[1]
  27.     local position = input[2] or default[2]
  28.     local side = input[3] or default[3]
  29.     --Peripheral
  30.     local monitor = qmain.getMonitor(side)
  31.     --Method
  32.     monitor.setCursorPos(unpack(position))
  33.     script = "a&D"..script.."&a&D&"
  34.     for color, text in script:gmatch("(%a)(.-)&") do
  35.         if (color:upper() == color) then
  36.             monitor.setBackgroundColor(qmain.convertColor(color:lower()))
  37.         else
  38.             monitor.setTextColor(qmain.convertColor(color))
  39.         end
  40.         monitor.write(text)
  41.     end
  42. end
  43.  
  44.  
  45. drawFilledBox = function(...)
  46.     --Input
  47.     local input = {...}
  48.     local default = {"a", {1, 1}, {4, 3}, qmain.getScreen()}
  49.     --Parameters
  50.     local color = input[1] or default[1]
  51.     local position = input[2] or default[2]
  52.     local size = input[3] or default[3]
  53.     local side = input[4] or default[4]
  54.     --Peripheral
  55.     local monitor = qmain.getMonitor(side)
  56.     --Method
  57.     monitor.setBackgroundColor(qmain.convertColor(color))
  58.     for box_y = 1, size[2] do
  59.         monitor.setCursorPos(position[1], position[2] + (box_y - 1))
  60.         monitor.write(string.rep(" ", size[1]))
  61.     end
  62.     monitor.setTextColor(colors.white)
  63.     monitor.setBackgroundColor(colors.black)
  64. end
  65.  
  66.  
  67. drawProgressBar = function(...)
  68.     --Input
  69.     local input = {...}
  70.     local default = {0.4, {1, 1}, {10, 1}, {"i", "f"}, qmain.getScreen()}
  71.     --Parameters
  72.     local progress = input[1] or default[1]
  73.     local position = input[2] or default[2]
  74.     local size = input[3] or default[3]
  75.     local colors = input[4] or default[4]
  76.     local side = input[5] or default[5]
  77.     --Peripheral
  78.     local monitor = qmain.getMonitor(side)
  79.     --Method
  80.     local used_pixels = math.floor(progress * size[1])
  81.     drawFilledBox(colors[2], position, size, side)
  82.     drawFilledBox(colors[1], position, {used_pixels, size[2]}, side)
  83. end
  84.  
  85.  
  86. loadImage = function(...)
  87.     --Input
  88.     local input = {...}
  89.     local default = {default_image}
  90.     --Parameters
  91.     local image = input[1] or default[1]
  92.     --Method
  93.     local data = {
  94.         back = image[1] or {},
  95.         col = image[2] or {},
  96.         symbol = image[3] or {}
  97.     }
  98.     local max_x, max_y = 0, 0
  99.     for name, value in pairs(data) do
  100.         if (#value > max_y) then
  101.             max_y = #value
  102.         end
  103.         for row, row_data in pairs(value) do
  104.             if (row_data:len() > max_x) then
  105.                 max_x = row_data:len()
  106.             end
  107.         end
  108.     end
  109.     local new_image = {}
  110.     for x = 1, max_x do
  111.         new_image[x] = {}
  112.         for y = 1, max_y do
  113.             new_image[x][y] = {
  114.                 getChar(data.back, x, y),
  115.                 getChar(data.col, x, y),
  116.                 getChar(data.symbol, x, y)
  117.             }
  118.         end
  119.     end
  120.     return new_image
  121. end
  122.  
  123.  
  124. drawImage = function(...)
  125.     --Input
  126.     local input = {...}
  127.     local default = {loadImage(default_image), {1, 1}, qmain.getScreen()}
  128.     --Parameters
  129.     local image = input[1] or default[1]
  130.     local position = input[2] or default[2]
  131.     local side = input[3] or default[3]
  132.     --Peripheral
  133.     local monitor = qmain.getMonitor(side)
  134.     --Method
  135.     for x = 1, #image do
  136.         for y = 1, #image[1] do
  137.             local backcolor = image[x][y][1]
  138.             local textcolor = image[x][y][2]
  139.             local symbol = image[x][y][3]
  140.             if not(backcolor == " ") then
  141.                 monitor.setCursorPos(position[1] + x - 1, position[2] + y - 1)
  142.                 monitor.setBackgroundColor(qmain.convertColor(backcolor))
  143.                 if (textcolor == " ") then
  144.                     monitor.write(" ")
  145.                 else
  146.                     monitor.setTextColor(qmain.convertColor(textcolor))
  147.                     monitor.write(symbol)
  148.                 end
  149.             end
  150.         end
  151.     end
  152. end
  153.  
  154.  
  155. ----------
  156. --Private:
  157. ----------
  158.  
  159. getChar = function(array, x, y)
  160.     if not(array == nil) then
  161.         if not(array[y] == nil) then
  162.             if not(array[y]:sub(x,x) == "") then
  163.                 return array[y]:sub(x,x)
  164.             end
  165.         end
  166.     end
  167.     return " "
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement