Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 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.  
  35. function button.draw(display,isPressed,startColumn,startRow)
  36.  
  37. button.startColumn = startColumn or button.startColumn
  38. button.startRow = startRow or button.startRow
  39. display = display or term
  40. if isPressed == false or isPressed then
  41. button.isPressed = isPressed
  42. else isPressed = button.isPressed
  43. end
  44. local width = button.width
  45. local height = button.height
  46. startRow = button.startRow
  47. startColumn = button.startColumn
  48.  
  49. local label = button.label
  50. local labelPad = 2
  51.  
  52. -- set border params and draw border if hasBorder
  53. if button.hasBorder == true then
  54. -- button must be at least 3x3, if not, make it so
  55. if width < 3 then
  56. width = 3
  57. end
  58. if height < 3 then
  59. height = 3
  60. end
  61.  
  62. -- set border colors
  63. if not isPressed then
  64. if not display.isColor() then
  65. display.setBackgroundColor(colors.white)
  66. else
  67. display.setBackgroundColor(button.borderColorNormal)
  68. end
  69. else
  70. if not display.isColor() then
  71. display.setBackgroundColor(colors.white)
  72. else
  73. display.setBackgroundColor(button.borderColorPressed)
  74. end
  75. end
  76.  
  77. -- draw button border (inset)
  78. display.setCursorPos(startColumn,startRow)
  79. display.write(string.rep(" ",width))
  80. for row = 2,height-1 do
  81. display.setCursorPos(startColumn,button.startRow+row -1)
  82. display.write(" ")
  83. display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  84. display.write(" ")
  85. end
  86. display.setCursorPos(startColumn,startRow+height-1)
  87. display.write(string.rep(" ",width))
  88.  
  89. -- reset startColumn,startRow,width,column to inset button and label
  90. startColumn=startColumn+1
  91. startRow = startRow +1
  92. width = width - 2
  93. height = height - 2
  94. end
  95.  
  96. --set button background and text colors
  97. if not isPressed then
  98. if not display.isColor() then
  99. display.setBackgroundColor(colors.black)
  100. display.setTextColor(colors.white)
  101. else
  102. display.setBackgroundColor(button.backgroundColorNormal)
  103. display.setTextColor(button.textColorNormal)
  104. end
  105. else
  106. if not display.isColor() then
  107. display.setBackgroundColor(colors.white)
  108. display.setTextColor(colors.black)
  109. else
  110. display.setBackgroundColor(button.backgroundColorPressed)
  111. display.setTextColor(button.textColorPressed)
  112. end
  113. end
  114.  
  115. -- draw button background (will be inside border if there is one)
  116. for row = 1,height do
  117. --print(tostring(startColumn)..","..tostring(startRow-row))
  118. display.setCursorPos(startColumn,startRow + row -1)
  119. display.write(string.rep(" ",width))
  120. end
  121.  
  122. -- prepare label, truncate label if necessary
  123.  
  124. -- prepare label, truncate label if necessary
  125. if width < 3 then
  126. labelPad = 0
  127. end
  128. if #label > width - labelPad then
  129. label = label:sub(1,width - labelPad)
  130. end
  131.  
  132. -- draw label
  133. display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  134. display.write(label)
  135. display.setBackgroundColor(button.defaultBackgroundColor)
  136. display.setTextColor(button.defaultTextColor)
  137. end
  138.  
  139. funtion button.toggle()
  140. button.isPressed = not button.isPressed
  141. return button.isPressed
  142. end
  143.  
  144. monitor1 = peripheral.wrap("top")
  145. monitor1.setTextScale(0.5)
  146. buttons = {}
  147.  
  148. buttons.left= Button(7,3,"<--",colors.lime,colors.pink,colors.white,colors.yellow,true,colors.green,colors.red,1,1,false,nil,nil)
  149.  
  150. buttons.right = Button(7,3,"-->",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,9,1,false,nil,nil)
  151.  
  152. buttons.bottom = Button(10,3,"Bottom",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,3,5,false,nil,nil)
  153.  
  154. monitor1.clear()
  155. for i,button in ipairs(buttons) do
  156. button.draw(monitor1)
  157. end
  158.  
  159. return button
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement