Advertisement
DabDaddy6223

os_gfx

Mar 23rd, 2023 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local utils = require("utils")
  2.  
  3. HasSomethingChanged = false
  4. RenderBuffer = {}
  5. Width, Height = term.getSize()
  6.  
  7.  
  8.  
  9. -- Helper functions
  10. function getWidth()
  11.     return Width
  12. end
  13.  
  14. function getHeight()
  15.     return Height
  16. end
  17.  
  18. function clear()
  19.     term.clear()
  20. end
  21.  
  22.  
  23.  
  24. -- Text
  25. function addCentralXText(message, posY)
  26.     -- y goes unused here
  27.     local x, y = utils.midpoint(Width, Height)
  28.     local newX = utils.getXPosOfString(message, x)
  29.     return addText(message, newX, posY)
  30. end
  31.  
  32. function addCentralText(message)
  33.     -- x goes unused here
  34.     local x, y = utils.midpoint(Width, Height)
  35.     return addCentralXText(message, y)
  36. end
  37.  
  38. function addText(message, posX, posY)
  39.     data = {}
  40.  
  41.     data["message"] = message
  42.     data["posX"] = posX
  43.     data["posY"] = posY
  44.  
  45.     RenderBuffer[(#RenderBuffer) + 1] = data
  46.     HasSomethingChanged = true
  47.     return #RenderBuffer
  48. end
  49.  
  50. function updateText(handle, newText)
  51.     RenderBuffer[handle]["message"] = newText
  52. end
  53.  
  54.  
  55.  
  56. -- Display
  57. function flush()
  58.     local count = #RenderBuffer
  59.     for i = 1, count do
  60.         RenderBuffer[i] = nil
  61.     end
  62. end
  63.  
  64. function writeText(message, startX, startY)
  65.     term.setCursorPos(startX, startY)
  66.     term.write(message)
  67. end
  68.  
  69. function display()
  70.     if HasSomethingChanged then
  71.         clear()
  72.         for i = 1, #RenderBuffer do
  73.             local current = RenderBuffer[i]
  74.             local x, y, message = current["posX"], current["posY"], current["message"]
  75.             writeText(message, x, y)
  76.         end
  77.         HasSomethingChanged = false
  78.     end
  79. end
  80.  
  81.  
  82.  
  83. return {
  84.     clear = clear,
  85.     addText = addText,
  86.     addCentralText = addCentralText,
  87.     addCentralXText = addCentralXText,
  88.     updateText = updateText,
  89.     flush = flush,
  90.     display = display,
  91.     getWidth = getWidth,
  92.     getHeight = getHeight
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement