Advertisement
danya201272

[OpenComputers]LSC Controller and Auto-Nuke GTCE Libs

Oct 29th, 2022 (edited)
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | Source Code | 0 0
  1. -- Libs from 4GNAME
  2.  
  3. local graphics = {}
  4. local unicode = require("unicode")
  5.  
  6. function graphics.pixel(GPU, x, y, color)
  7.     local screenY = math.ceil(y / 2)
  8.     local baseChar, baseForeground, baseBackground = GPU.get(x, screenY)
  9.     GPU.setForeground(color)
  10.     GPU.setBackground(baseBackground)
  11.     if y % 2 == 1 then -- Upper half of pixel
  12.         GPU.set(x, screenY, "▀")
  13.     else -- Lower half of pixel
  14.         GPU.set(x, screenY, "▄")
  15.     end
  16. end
  17.  
  18. function graphics.rectangle(GPU, x, y, w, h, color)
  19.     local hLeft = h
  20.     if x > 0 and y > 0 then
  21.         if y % 2 == 0 then
  22.             for i = x, x + w - 1 do
  23.                 graphics.pixel(GPU, i, y, color)
  24.             end
  25.             hLeft = hLeft - 1
  26.         end
  27.         GPU.setBackground(color)
  28.         GPU.setForeground(color)
  29.         if hLeft % 2 == 1 then
  30.             GPU.fill(x, math.ceil(y / 2) + (h - hLeft), w, (hLeft - 1) / 2, "█")
  31.             for j = x, x + w - 1 do
  32.                 graphics.pixel(GPU, j, y + h - 1, color)
  33.             end
  34.         else
  35.             GPU.fill(x, math.ceil(y / 2) + (h - hLeft), w, hLeft / 2, "█")
  36.         end
  37.     end
  38. end
  39.  
  40. function graphics.text(GPU, x, y, color, string)
  41.     if y % 2 == 0 then
  42.         error("Text position must be odd on y axis")
  43.     end
  44.     local screenY = math.ceil(y / 2)
  45.     GPU.setForeground(color)
  46.     for i = 0, #string - 1 do
  47.         local baseChar, baseForeground, baseBackground = GPU.get(x + i, screenY)
  48.         GPU.setBackground(baseBackground)
  49.         GPU.fill(x + i, screenY, 1, 1, string:sub(i + 1, i + 1))
  50.     end
  51. end
  52.  
  53. function graphics.centeredText(GPU, x, y, color, string)
  54.     if y % 2 == 0 then
  55.         error("Text position must be odd on y axis")
  56.     end
  57.     local screenY = math.ceil(y / 2)
  58.     local oldForeground = GPU.setForeground(color)
  59.     local oldBackground = GPU.getBackground()
  60.     for i = 0, #string - 1 do
  61.         local baseChar, baseForeground, baseBackground = GPU.get(x + i - math.ceil(#string / 2) + 1, screenY)
  62.         GPU.setBackground(baseBackground)
  63.         GPU.fill(x + i - math.ceil(#string / 2) + 1, screenY, 1, 1, string:sub(i + 1, i + 1))
  64.     end
  65.     GPU.setBackground(oldBackground)
  66.     GPU.setForeground(oldForeground)
  67. end
  68.  
  69. function graphics.border(GPU, w, h, color)
  70.     graphics.rectangle(GPU, 1, 1, w, 1, color)
  71.     graphics.rectangle(GPU, 1, h * 2, w, 1, color)
  72.     graphics.rectangle(GPU, 1, 1, 1, h * 2, color)
  73.     graphics.rectangle(GPU, w, 1, 1, h * 2, color)
  74. end
  75.  
  76. graphics.currentWindows = {}
  77. function graphics.checkCollision(GPU, x, y)
  78.     for window, params in pairs(graphics.currentWindows) do
  79.         if x >= params.x and x <= params.x + params.w - 1 then
  80.             if y >= params.y and y <= params.y + math.ceil(params.h / 2) - 1 then
  81.                 return window
  82.             end
  83.         end
  84.     end
  85.     return nil
  86. end
  87.  
  88. function graphics.createWindow(GPU, width, height, name)
  89.     local pageNumber = GPU.allocateBuffer(width, math.ceil(height / 2))
  90.     graphics.currentWindows[name] = {page = pageNumber, x = 1, y = 1, w = width, h = height, GPU = GPU}
  91.     return pageNumber
  92. end
  93.  
  94. function graphics.copyWindow(GPU, x, y, page, destination)
  95.     destination = 0 or destination
  96.     GPU.bitblt(destination, x, y, 160, 50, page, 1, 1)
  97. end
  98.  
  99. function graphics.refresh(GPU)
  100.     for window, params in pairs(graphics.currentWindows) do
  101.         if params.w > 0 then
  102.             graphics.copyWindow(GPU, params.x, params.y, params.page)
  103.         end
  104.     end
  105.     GPU.setActiveBuffer(0)
  106. end
  107.  
  108. graphics.windows = {}
  109. function graphics.update()
  110.     local function redraw()
  111.         for window, params in pairs(graphics.windows) do
  112.             graphics.copyWindow(params.GPU, params.x, params.y, params.page)
  113.         end
  114.     end
  115.     for name, params in pairs(graphics.windows) do
  116.         params.GPU.setActiveBuffer(params.page)
  117.         params.update(params.GPU, name, params.address)
  118.         params.GPU.setActiveBuffer(0)
  119.     end
  120.     redraw()
  121.     os.sleep()
  122. end
  123.  
  124. function graphics.clear()
  125.     graphics.currentWindows = {}
  126. end
  127.  
  128. return graphics
  129.  
Tags: GTCE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement