Thor_s_Crafter

touchpoint

Mar 27th, 2016
13,946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. -- Reaktor- and Turbinen control by Thor_s_Crafter --
  2. -- Version 2.3 --
  3. -- Touchpoint API by Lyqyd - Slightly changed --
  4.  
  5. local backgroundColor
  6. local textColor
  7.  
  8. function loadOptions()
  9. local optionList = {}
  10. local file = fs.open("/reactor-turbine-program/config/options.txt","r")
  11. local listElement = file.readLine()
  12. while listElement do
  13. table.insert(optionList,listElement)
  14. listElement = file.readLine()
  15. end
  16. file.close()
  17.  
  18. backgroundColor = tonumber(optionList[7])
  19. textColor = tonumber(optionList[9])
  20. end
  21.  
  22. loadOptions()
  23.  
  24. local function setupLabel(buttonLen, minY, maxY, name)
  25. local labelTable = {}
  26. if type(name) == "table" then
  27. for i = 1, #name do
  28. labelTable[i] = name[i]
  29. end
  30. name = name.label
  31. elseif type(name) == "string" then
  32. local buttonText = string.sub(name, 1, buttonLen - 2)
  33. if #buttonText < #name then
  34. buttonText = " "..buttonText.." "
  35. else
  36. local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
  37. buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
  38. end
  39. for i = 1, maxY - minY + 1 do
  40. if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
  41. labelTable[i] = buttonText
  42. else
  43. labelTable[i] = string.rep(" ", buttonLen)
  44. end
  45. end
  46. end
  47. return labelTable, name
  48. end
  49.  
  50. local Button = {
  51. draw = function(self)
  52. local old = term.redirect(self.mon)
  53. term.setTextColor(textColor)
  54. term.setBackgroundColor(backgroundColor)
  55. term.clear()
  56. for name, buttonData in pairs(self.buttonList) do
  57. if buttonData.active then
  58. term.setBackgroundColor(buttonData.activeColor)
  59. term.setTextColor(buttonData.activeText)
  60. else
  61. term.setBackgroundColor(buttonData.inactiveColor)
  62. term.setTextColor(buttonData.inactiveText)
  63. end
  64. for i = buttonData.yMin, buttonData.yMax do
  65. term.setCursorPos(buttonData.xMin, i)
  66. term.write(buttonData.label[i - buttonData.yMin + 1])
  67. end
  68. end
  69. if old then
  70. term.redirect(old)
  71. else
  72. term.restore()
  73. end
  74. end,
  75. add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText)
  76. local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
  77. if self.buttonList[name] then error("button already exists", 2) end
  78. local x, y = self.mon.getSize()
  79. if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
  80. self.buttonList[name] = {
  81. func = func,
  82. xMin = xMin,
  83. yMin = yMin,
  84. xMax = xMax,
  85. yMax = yMax,
  86. active = false,
  87. inactiveColor = inactiveColor or colors.red,
  88. activeColor = activeColor or colors.lime,
  89. inactiveText = inactiveText or colors.white,
  90. activeText = activeText or colors.white,
  91. label = label,
  92. }
  93. for i = xMin, xMax do
  94. for j = yMin, yMax do
  95. if self.clickMap[i][j] ~= nil then
  96. --undo changes
  97. for k = xMin, xMax do
  98. for l = yMin, yMax do
  99. if self.clickMap[k][l] == name then
  100. self.clickMap[k][l] = nil
  101. end
  102. end
  103. end
  104. self.buttonList[name] = nil
  105. error("overlapping button", 2)
  106. end
  107. self.clickMap[i][j] = name
  108. end
  109. end
  110. end,
  111. remove = function(self, name)
  112. if self.buttonList[name] then
  113. local button = self.buttonList[name]
  114. for i = button.xMin, button.xMax do
  115. for j = button.yMin, button.yMax do
  116. self.clickMap[i][j] = nil
  117. end
  118. end
  119. self.buttonList[name] = nil
  120. end
  121. end,
  122. run = function(self)
  123. while true do
  124. self:draw()
  125. local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
  126. if event[1] == "button_click" then
  127. self.buttonList[event[2]].func()
  128. end
  129. end
  130. end,
  131. handleEvents = function(self, ...)
  132. local event = {...}
  133. if #event == 0 then event = {os.pullEvent()} end
  134. if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then
  135. local clicked = self.clickMap[event[3]][event[4]]
  136. if clicked and self.buttonList[clicked] then
  137. return "button_click", clicked
  138. end
  139. end
  140. return unpack(event)
  141. end,
  142. toggleButton = function(self, name, noDraw)
  143. self.buttonList[name].active = not self.buttonList[name].active
  144. if not noDraw then self:draw() end
  145. end,
  146. flash = function(self, name, duration)
  147. self:toggleButton(name)
  148. sleep(tonumber(duration) or 0.15)
  149. self:toggleButton(name)
  150. end,
  151. rename = function(self, name, newName)
  152. self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName)
  153. if not self.buttonList[name] then error("no such button", 2) end
  154. if name ~= newName then
  155. self.buttonList[newName] = self.buttonList[name]
  156. self.buttonList[name] = nil
  157. for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do
  158. for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do
  159. self.clickMap[i][j] = newName
  160. end
  161. end
  162. end
  163. self:draw()
  164. end,
  165. }
  166.  
  167. function new(monSide)
  168. local buttonInstance = {
  169. side = monSide or "term",
  170. mon = monSide and peripheral.wrap(monSide) or term.current(),
  171. buttonList = {},
  172. clickMap = {},
  173. }
  174. local x, y = buttonInstance.mon.getSize()
  175. for i = 1, x do
  176. buttonInstance.clickMap[i] = {}
  177. end
  178. setmetatable(buttonInstance, {__index = Button})
  179. return buttonInstance
  180. end
Advertisement
Add Comment
Please, Sign In to add comment