Advertisement
blendender

OpenComputers pattern API

Mar 23rd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. -- import stuff
  2.  
  3. comp = require "component"
  4.  
  5. gpu = comp.gpu
  6.  
  7.  
  8. -- pattern {{"##00##", "#0#0#0"}, {{"#", 0xffffff}, {"0", 0x00ff00}}}
  9. pattern = {}
  10.  
  11.  
  12. --[[due to the fact, opencomputers doesnt support drawing individual pixel, this program uses a workaround to draw space characters instead of pixels
  13.     on a certain position. The problem is that the space character's shape doesnt match a square pixel. to avoid this you can set this value to 2. 1 will work
  14.     aswell, but it will look a little bit squeezed]]
  15. pattern.pixel_width = 2
  16.  
  17. -- reads the given pattern table and replaces the chars with the given colors
  18. function pattern.draw(x, y, pat)
  19.  
  20.   local old_color = gpu.getBackground()
  21.   for index,line in ipairs(pat[1]) do
  22.     for i = 1, #line do
  23.       local char = string.sub(line, i, i)
  24.       pattern.setColor(char, pat)
  25.      
  26.       local filling_string = " "
  27.       filling_string = string.rep(filling_string, pattern.pixel_width)
  28.       gpu.set(x+((i-1)*pattern.pixel_width), y+index-1, filling_string)
  29.     end
  30.   end
  31.  
  32.   gpu.setBackground(old_color)
  33.  
  34. end
  35.  
  36. -- set the background color depending on the char's color definition in the table
  37. function pattern.setColor(char, pat)
  38.   success = false
  39.   for _,v in pairs(pat[2]) do
  40.     if v[1] == char then
  41.       if v[2] == nil then v[2] = 0xffffff end
  42.       gpu.setBackground(v[2])
  43.       success = true
  44.     end
  45.   end
  46.   if not success then gpu.setBackground(0xffffff) end
  47. end
  48.  
  49. return pattern
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement