DorianH935

Untitled

Jul 18th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. local obsidianOS = {}
  2.  
  3. -- Encryption function
  4. function obsidianOS.encrypt(data, key)
  5. local encrypted = {}
  6. for i = 1, #data do
  7. local char = string.byte(data, i)
  8. local kchar = string.byte(key, (i - 1) % #key + 1)
  9. table.insert(encrypted, string.char(bit.bxor(char, kchar)))
  10. end
  11. return table.concat(encrypted)
  12. end
  13.  
  14. -- Decryption function
  15. function obsidianOS.decrypt(data, key)
  16. return obsidianOS.encrypt(data, key) -- Symmetric encryption
  17. end
  18.  
  19. -- Compression function
  20. function obsidianOS.compress(data)
  21. return textutils.serializeJSON(textutils.unserialize(data))
  22. end
  23.  
  24. -- Decompression function
  25. function obsidianOS.decompress(data)
  26. return textutils.serialize(textutils.unserializeJSON(data))
  27. end
  28.  
  29. -- Write Tixel function
  30. function obsidianOS.writeTixel(x, y, color, monitor)
  31. monitor = monitor or term
  32. monitor.setCursorPos(x, y)
  33. monitor.setBackgroundColor(color)
  34. monitor.write(" ")
  35. monitor.setBackgroundColor(colors.black) -- Reset background color
  36. end
  37.  
  38. -- Draw Box function
  39. function obsidianOS.drawBox(x1, y1, x2, y2, color, monitor)
  40. monitor = monitor or term
  41. local minX, maxX = math.min(x1, x2), math.max(x1, x2)
  42. local minY, maxY = math.min(y1, y2), math.max(y1, y2)
  43. for y = minY, maxY do
  44. for x = minX, maxX do
  45. obsidianOS.writeTixel(x, y, color, monitor)
  46. end
  47. end
  48. end
  49.  
  50. -- Button creation function
  51. function obsidianOS.createButton(x, y, x2, y2, text, color, outlineColor, textColor, textBackgroundColor, callOnClick)
  52. local button = {
  53. x = x,
  54. y = y,
  55. x2 = x2,
  56. y2 = y2,
  57. text = text,
  58. color = color,
  59. outlineColor = outlineColor,
  60. textColor = textColor or colors.white,
  61. textBackgroundColor = textBackgroundColor or color,
  62. callOnClick = callOnClick
  63. }
  64. return button
  65. end
  66.  
  67. -- Function to draw a button
  68. function obsidianOS.drawButton(button, monitor)
  69. monitor = monitor or term
  70. -- Draw outline
  71. for x = button.x, button.x2 do
  72. obsidianOS.writeTixel(x, button.y, button.outlineColor, monitor)
  73. obsidianOS.writeTixel(x, button.y2, button.outlineColor, monitor)
  74. end
  75. for y = button.y, button.y2 do
  76. obsidianOS.writeTixel(button.x, y, button.outlineColor, monitor)
  77. obsidianOS.writeTixel(button.x2, y, button.outlineColor, monitor)
  78. end
  79. -- Fill button area
  80. for y = button.y + 1, button.y2 - 1 do
  81. for x = button.x + 1, button.x2 - 1 do
  82. obsidianOS.writeTixel(x, y, button.color, monitor)
  83. end
  84. end
  85. -- Draw text centered
  86. local textX = button.x + math.floor((button.x2 - button.x - #button.text) / 2)
  87. local textY = button.y + math.floor((button.y2 - button.y) / 2)
  88. monitor.setBackgroundColor(button.textBackgroundColor)
  89. monitor.setTextColor(button.textColor)
  90. monitor.setCursorPos(textX, textY)
  91. monitor.write(button.text)
  92. end
  93.  
  94. -- Function to handle button clicks
  95. function obsidianOS.handleButtonClick(buttons, x, y, monitor)
  96. monitor = monitor or term
  97. x, y = obsidianOS.getAbsoluteCoordinates(monitor, x, y)
  98. for _, button in ipairs(buttons) do
  99. if x >= button.x and x <= button.x2 and y >= button.y and y <= button.y2 then
  100. button.callOnClick()
  101. return true
  102. end
  103. end
  104. return false
  105. end
  106.  
  107. -- Function to get absolute coordinates on the screen from monitor-relative coordinates
  108. function obsidianOS.getAbsoluteCoordinates(monitor, x, y)
  109. local monitorX, monitorY = monitor.getPosition()
  110. return x + monitorX - 1, y + monitorY - 1
  111. end
  112.  
  113. return obsidianOS
  114.  
Advertisement
Add Comment
Please, Sign In to add comment