Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. local function Button(
  2. width,
  3. height,
  4. label,
  5. backgroundColorNormal,
  6. backgroundColorPressed,
  7. textColorNormal,
  8. textColorPressed,
  9. hasBorder,
  10. borderColorNormal,
  11. borderColorPressed,
  12. startColumn,
  13. startRow,
  14. isPressed,
  15. defaultBackgroundColor,
  16. defaultTextColor
  17. )
  18. local button = {}
  19. button.height=height or 1
  20. button.width=width or 1
  21. button.label=label or ""
  22. button.backgroundColorNormal=backgroundColorNormal or colors.black
  23. button.backgroundColorPressed=backgroundColorPressed or colors.white
  24. button.textColorNormal=textColorNormal or colors.white
  25. button.textColorPressed=textColorPressed or colors.black
  26. button.hasBorder = hasBorder or false
  27. button.borderColorNormal = borderColorNormal or backGroundColorNormal
  28. button.borderColorPressed = borderColorPressed or backGroundColorPressed
  29. button.defaultBackgroundColor = defaultBackgroundColor or colors.black
  30. button.defaultTextColor = defaultTextColor or colors.white
  31. button.startColumn = startColumn or 1
  32. button.startRow = startRow or 1
  33. button.isPressed=isPressed or false
  34. function button.press()
  35. button.isPressed = not button.isPressed
  36. end
  37.  
  38. function button.draw(display,isPressed,startColumn,startRow)
  39.  
  40. button.startColumn = startColumn or button.startColumn
  41. button.startRow = startRow or button.startRow
  42. display = display or term
  43. if isPressed == false or isPressed then
  44. button.isPressed = isPressed
  45. else isPressed = button.isPressed
  46. end
  47. local width = button.width
  48. local height = button.height
  49. startRow = button.startRow
  50. startColumn = button.startColumn
  51.  
  52. local label = button.label
  53. local labelPad = 2
  54.  
  55. -- set border params and draw border if hasBorder
  56. if button.hasBorder == true then
  57. -- button must be at least 3x3, if not, make it so
  58. if width < 3 then
  59. width = 3
  60. end
  61. if height < 3 then
  62. height = 3
  63. end
  64.  
  65. -- set border colors
  66. if not isPressed then
  67. if not display.isColor() then
  68. display.setBackgroundColor(colors.white)
  69. else
  70. display.setBackgroundColor(button.borderColorNormal)
  71. end
  72. else
  73. if not display.isColor() then
  74. display.setBackgroundColor(colors.white)
  75. else
  76. display.setBackgroundColor(button.borderColorPressed)
  77. end
  78. end
  79.  
  80. -- draw button border (inset)
  81. display.setCursorPos(startColumn,startRow)
  82. display.write(string.rep(" ",width))
  83. for row = 2,height-1 do
  84. display.setCursorPos(startColumn,button.startRow+row -1)
  85. display.write(" ")
  86. display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  87. display.write(" ")
  88. end
  89. display.setCursorPos(startColumn,startRow+height-1)
  90. display.write(string.rep(" ",width))
  91.  
  92. -- reset startColumn,startRow,width,column to inset button and label
  93. startColumn=startColumn+1
  94. startRow = startRow +1
  95. width = width - 2
  96. height = height - 2
  97. end
  98.  
  99. --set button background and text colors
  100. if not isPressed then
  101. if not display.isColor() then
  102. display.setBackgroundColor(colors.black)
  103. display.setTextColor(colors.white)
  104. else
  105. display.setBackgroundColor(button.backgroundColorNormal)
  106. display.setTextColor(button.textColorNormal)
  107. end
  108. else
  109. if not display.isColor() then
  110. display.setBackgroundColor(colors.white)
  111. display.setTextColor(colors.black)
  112. else
  113. display.setBackgroundColor(button.backgroundColorPressed)
  114. display.setTextColor(button.textColorPressed)
  115. end
  116. end
  117.  
  118. -- draw button background (will be inside border if there is one)
  119. for row = 1,height do
  120. --print(tostring(startColumn)..","..tostring(startRow-row))
  121. display.setCursorPos(startColumn,startRow + row -1)
  122. display.write(string.rep(" ",width))
  123. end
  124.  
  125. -- prepare label, truncate label if necessary
  126.  
  127. -- prepare label, truncate label if necessary
  128. if width < 3 then
  129. labelPad = 0
  130. end
  131. if #label > width - labelPad then
  132. label = label:sub(1,width - labelPad)
  133. end
  134.  
  135. -- draw label
  136. display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  137. display.write(label)
  138. display.setBackgroundColor(button.defaultBackgroundColor)
  139. display.setTextColor(button.defaultTextColor)
  140. end
  141. button.toggle = function ()
  142. button.isPressed = not button.isPressed
  143. return button.isPressed
  144. end
  145. return button
  146. end
  147.  
  148. -- Start of test Program
  149.  
  150. monitor1 = peripheral.wrap("left")
  151.  
  152. buttons = {}
  153. buttons.left= Button(10,3,"Left",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,1,false,nil,nil)
  154. buttons.right = Button(10,3,"Right",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,4,false,nil,nil)
  155. buttons.bottom = Button(10,3,"Bottom",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,7,false,nil,nil)
  156.  
  157. local quit = Button(8,1,"Quit",colors.gray,colors.black,colors.black,colors.white,false,nil,nil,4,10,false,nil,nil)
  158.  
  159. -- Display buttons on monitor1
  160. monitor1.clear()
  161. for key,button in pairs(buttons) do
  162. button.draw(monitor1)
  163. end
  164. quit.draw()
  165. -- this is our event loop
  166. while true do
  167. event ={os.pullEvent()}
  168. if event[1] =="monitor_touch" then
  169. for key,button in pairs(buttons)
  170. if button.clicked(event[3],event[4] then -- column,row
  171. redstone.setOutput(key,button.toggle())
  172. button.draw(monitor1)
  173. break -- we found on, so we don't need to keep looking
  174. end
  175. end
  176. if quit.clicked(event[3],event[4]) == true then
  177. os.queueEvent("key",keys.q)
  178. end
  179. elseif event[1] =="key" and event[2]==keys.q then
  180. break
  181. end
  182. end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement