Advertisement
Sanwi

OpenComputers Button Library

Sep 9th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. -- Please forgive the problems with this script, I wrote it a long time ago when I was learning Lua.
  2.  
  3. component = require("component")
  4. term = require("term")
  5. text = require("text")
  6. event = require("event")
  7.  
  8. button = {}
  9.  
  10. local function splitMessage(message, del) -- Splits a string by del (delimiter)
  11. if not text then text = require("text") end
  12. -- Default to whitespace
  13. if del == nil then
  14. del = "%s"
  15. end
  16.  
  17. local t={} ; i=1
  18. for str in string.gmatch(message, "([^"..del.."]+)") do
  19. t[i] = str
  20. i = i + 1
  21. end
  22. return t
  23. end
  24.  
  25. -- Generates a new button
  26. -- Usage:
  27. -- buttonList = button.newButton(buttonList: table, label: string, action: function, width: number, height: number, paddings: string[ e.g. "left:right:top:bottom"], colorSet: string [e.g. "0x00AA00:0xAA0000"])
  28. function button.newButton(buttonList, label, action, actionArgT, width, height, paddings, colorSet, xBorder, yBorder) -- format colors as color:color
  29. local tX, tY = component.gpu.getResolution()
  30. local buttonData = {}
  31.  
  32. -- Screen margins (defaults for first button)
  33. local minX = 1
  34. local maxX = 0
  35. local minY = 2
  36. local maxY = 0
  37.  
  38. -- Determine button placement
  39. if buttonList[#buttonList] then -- Add button to existing table
  40. -- Row end?
  41. local startX = buttonList[#buttonList].spatialT.maxX + width
  42.  
  43. if startX > tX then -- New row
  44. minY = buttonList[#buttonList].spatialT.maxY
  45. minX = minX
  46. else -- Same row
  47. minX = buttonList[#buttonList].spatialT.maxX
  48. minY = buttonList[#buttonList].spatialT.minY
  49. end
  50. else -- Initial button on table
  51. minX = minX
  52. minY = minY
  53. end
  54.  
  55. local maxX = minX + width
  56. local maxY = minY + height
  57.  
  58. -- Set padding
  59. local paddingT = splitMessage(paddings, ":")
  60.  
  61. -- Set render area
  62. local xMinRender = minX + (tonumber(text.trim(paddingT[1])) * 2)
  63. local xMaxRender = maxX - (tonumber(text.trim(paddingT[2])) * 2)
  64. local yMinRender = minY + tonumber(text.trim(paddingT[3]))
  65. local yMaxRender = maxY - tonumber(text.trim(paddingT[4]))
  66.  
  67. local renderWidth = xMaxRender - xMinRender
  68. local renderHeight = yMaxRender - yMinRender
  69.  
  70. -- Set colors
  71. local colorT = splitMessage(colorSet, ":")
  72.  
  73.  
  74. local spatialT =
  75. {
  76. minX=minX,
  77. maxX=maxX,
  78. minY=minY,
  79. maxY=maxY,
  80.  
  81. width=width,
  82. height=height,
  83.  
  84. xMinPadding=xMinPadding,
  85. xMaxPadding=xMaxPadding,
  86. yMinPadding=yMinPadding,
  87. yMaxPadding=yMaxPadding,
  88.  
  89. xMinRender=xMinRender,
  90. xMaxRender=xMaxRender,
  91. yMinRender=yMinRender,
  92. yMaxRender=yMaxRender,
  93.  
  94. renderWidth=renderWidth,
  95. renderHeight=renderHeight,
  96. }
  97.  
  98. local buttonData =
  99. {
  100. label = label,
  101. action = action,
  102. actionArgT=actionArgT,
  103. state = false,
  104. colorT = colorT,
  105. spatialT=spatialT,
  106. xBorder=xBorder,
  107. yBorder=yBorder,
  108. }
  109.  
  110. for i=1, #buttonData do
  111. assert(buttonData[i], "Missing buttonData "..i)
  112. end
  113. for i=1, #spatialT do
  114. assert(spatialT[i], "Missing spatialT"..i)
  115. end
  116.  
  117. table.insert(buttonList, buttonData)
  118. return buttonList
  119. end
  120.  
  121. function button.render(buttonData)
  122. local bG = component.gpu.getBackground()
  123.  
  124. -- Create string of spaces
  125. local spacers = ""
  126. for i=1, buttonData.spatialT.renderWidth do
  127. spacers = spacers.." "
  128. end
  129.  
  130. -- Create string of margin spaces for label
  131. local margin = ""
  132. for i=1, (buttonData.spatialT.renderWidth - string.len(buttonData.label))/2 do
  133. margin = margin.." "
  134. end
  135.  
  136.  
  137. -- Wrap label in margins
  138. local spacedLabel = margin..buttonData.label..margin
  139. if string.len(spacedLabel) < buttonData.spatialT.renderWidth then spacedLabel = spacedLabel.." " end
  140.  
  141.  
  142. -- Set color from state
  143. if buttonData.state then
  144. component.gpu.setBackground(tonumber(buttonData.colorT[1]))
  145. else
  146. component.gpu.setBackground(tonumber(buttonData.colorT[2]))
  147. end
  148.  
  149. -- Write button to terminal
  150. for i=0, buttonData.spatialT.renderHeight-1 do
  151. term.setCursor(buttonData.spatialT.xMinRender, buttonData.spatialT.yMinRender+i)
  152.  
  153. if i == math.floor((buttonData.spatialT.renderHeight/2)-1) then -- If we're at the center/high-center
  154. term.write(spacedLabel)
  155. else
  156. term.write(spacers)
  157. end
  158. end
  159.  
  160. component.gpu.setBackground(bG)
  161. end
  162.  
  163. function button.renderAll(buttonList)
  164. term.clear()
  165. for i=1, #buttonList do
  166. button.render(buttonList[i])
  167. end
  168. end
  169.  
  170. function button.checkTouch(buttonList, x, y)
  171. local buttonList = buttonList
  172. local cButton = {}
  173.  
  174. local onButton = false
  175. for i=1, #buttonList do
  176. if (x >= buttonList[i].spatialT.xMinRender) and (x <= buttonList[i].spatialT.xMaxRender) and (y >= buttonList[i].spatialT.yMinRender) and (y <= buttonList[i].spatialT.yMaxRender) then
  177. onButton = true
  178.  
  179. -- Execute function with current button as arg
  180. -- Make sure the function returns current button table
  181. cButton = buttonList[i].action(buttonList[i], buttonList[i].actionArgT)
  182.  
  183. -- Rewrite current button with table returned by action
  184. buttonList[i] = cButton
  185. end
  186. end
  187. return buttonList, cButton, onButton
  188. end
  189.  
  190. function button.toggle(cButton, argT)
  191. if cButton.state then cButton.state = false else cButton.state = true end
  192. return cButton
  193. end
  194.  
  195. function button.setAllStates(buttonList, argT)
  196. local buttonList = buttonList
  197. for i=1, #buttonList do
  198. buttonList[i].state = argT.state
  199. end
  200. return buttonList
  201. end
  202.  
  203. return button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement