Advertisement
Guest User

Spawner

a guest
Mar 29th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.67 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.green
  23.     button.backgroundColorPressed=backgroundColorPressed or colors.red
  24.     button.textColorNormal=textColorNormal or colors.black
  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.clicked(column,row)
  36.       return (column >= button.startColumn and column < button.startColumn + button.width and row >= button.startRow and row < button.startRow + button.height)
  37.     end
  38.  
  39.     function button.draw(display,isPressed,startColumn,startRow)
  40.                            
  41.         button.startColumn = startColumn or button.startColumn
  42.         button.startRow = startRow or button.startRow
  43.         display = display or term
  44.         if isPressed == false or isPressed then
  45.             button.isPressed = isPressed
  46.         else isPressed = button.isPressed
  47.         end
  48.         local width = button.width
  49.         local height = button.height
  50.         startRow = button.startRow
  51.         startColumn = button.startColumn
  52.        
  53.         local label = button.label
  54.         local labelPad = 2
  55.        
  56.         -- set border params and draw border if hasBorder
  57.         if button.hasBorder == true then
  58.             -- button must be at least 3x3, if not, make it so
  59.             if width < 3 then
  60.                 width = 3
  61.             end
  62.             if height < 3 then
  63.                 height = 3
  64.             end
  65.            
  66.             -- set border colors
  67.             if not isPressed then
  68.                 if not display.isColor() then
  69.                     display.setBackgroundColor(colors.white)
  70.                 else
  71.                     display.setBackgroundColor(button.borderColorNormal)
  72.                 end
  73.             else
  74.                 if not display.isColor() then
  75.                     display.setBackgroundColor(colors.white)
  76.                 else
  77.                     display.setBackgroundColor(button.borderColorPressed)
  78.                 end
  79.             end
  80.            
  81.             -- draw button border (inset)
  82.             display.setCursorPos(startColumn,startRow)
  83.             display.write(string.rep(" ",width))
  84.             for row = 2,height-1 do
  85.                 display.setCursorPos(startColumn,button.startRow+row -1)
  86.                 display.write(" ")
  87.                 display.setCursorPos(startColumn+width -1 ,startRow + row-1)
  88.                 display.write(" ")
  89.             end
  90.             display.setCursorPos(startColumn,startRow+height-1)
  91.             display.write(string.rep(" ",width))
  92.            
  93.             -- reset startColumn,startRow,width,column to inset button and label
  94.             startColumn=startColumn+1
  95.             startRow = startRow +1
  96.             width = width - 2
  97.             height = height - 2
  98.         end
  99.        
  100.         --set button background and text colors
  101.         if not isPressed then
  102.             if not display.isColor() then
  103.                 display.setBackgroundColor(colors.black)
  104.                 display.setTextColor(colors.white)
  105.             else
  106.                 display.setBackgroundColor(button.backgroundColorNormal)
  107.                 display.setTextColor(button.textColorNormal)
  108.             end
  109.         else
  110.             if not display.isColor() then
  111.                 display.setBackgroundColor(colors.white)
  112.                 display.setTextColor(colors.black)
  113.             else
  114.                 display.setBackgroundColor(button.backgroundColorPressed)
  115.                 display.setTextColor(button.textColorPressed)
  116.             end
  117.         end
  118.        
  119.         -- draw button background (will be inside border if there is one)
  120.         for row = 1,height do
  121.             --print(tostring(startColumn)..","..tostring(startRow-row))
  122.             display.setCursorPos(startColumn,startRow + row -1)
  123.             display.write(string.rep(" ",width))
  124.         end
  125.        
  126.         -- prepare label, truncate label if necessary
  127.        
  128.         -- prepare label, truncate label if necessary
  129.         if width < 3 then
  130.             labelPad = 0
  131.         end
  132.         if #label > width - labelPad then
  133.             label = label:sub(1,width - labelPad)
  134.         end
  135.        
  136.         -- draw label
  137.         display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  138.         display.write(label)
  139.         display.setBackgroundColor(button.defaultBackgroundColor)
  140.         display.setTextColor(button.defaultTextColor)
  141.     end
  142.  
  143.     button.getLabel = function ()
  144.       return button.label
  145.     end
  146.  
  147.     button.toggle = function ()
  148.       button.isPressed = not button.isPressed
  149.       return button.isPressed
  150.     end
  151.  
  152.     return button
  153. end
  154.  
  155. sSide = "bottom"
  156. local function toogleCableOutput(color, signal)
  157.   if signal then
  158.     redstone.setBundledOutput(sSide,colors.subtract(redstone.getBundledOutput(sSide), color))
  159.   else
  160.     redstone.setBundledOutput(sSide,colors.combine(redstone.getBundledOutput(sSide),color))
  161.   end
  162. end
  163.  
  164. -- color codes at the end
  165. local function resetCableOutput()
  166.     for i = 0, 15, 1 do
  167.       redstone.setBundledOutput(sSide,colors.combine(redstone.getBundledOutput(sSide),2^i))
  168.     end
  169. end
  170.  
  171.  
  172. --  start of the program
  173. attachedMonitor = peripheral.wrap("right")
  174. attachedMonitor.setTextScale(1)
  175. resetCableOutput() -- disable all output, set redstone signal to true
  176. buttons = {}
  177.  
  178. -- define buttons
  179. buttons.ghast= Button(9,6,"Ghast",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,1,false,nil,nil)
  180. buttons.empty2 = Button(9,7,"Empty2",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,7,false,nil,nil)
  181. buttons.empty3 = Button(9,6,"Empty3",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,1,14,false,nil,nil)
  182.  
  183. buttons.empty4 = Button(11,6,"Empty4",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,1,false,nil,nil)
  184. buttons.empty5 = Button(11,7,"Empty5",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,7,false,nil,nil)
  185. buttons.empty6 = Button(11,6,"Empty6",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,10,14,false,nil,nil)
  186.  
  187. buttons.empty7 = Button(9,6,"Empty7",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,1,false,nil,nil)
  188. buttons.empty8 = Button(9,7,"Empty8",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,7,false,nil,nil)
  189. buttons.empty9 = Button(9,6,"Empty9",colors.lime,colors.red,colors.white,colors.yellow,true,colors.green,colors.pink,21,14,false,nil,nil)
  190.  
  191.  
  192. -- display buttons on attachedMonitor
  193. attachedMonitor.clear()
  194. for key,button in pairs(buttons) do
  195.   button.draw(attachedMonitor)
  196. end
  197.  
  198. -- event loop
  199. while true do
  200.   event = {os.pullEvent()}
  201.   if event[1] =="monitor_touch" then
  202.     for key,button in pairs(buttons) do
  203.       if button.clicked(event[3],event[4]) then -- column,row
  204.         if button.getLabel() == "Ghast" then
  205.           toogleCableOutput(colors.white, button.toggle())
  206.           button.draw(attachedMonitor)
  207.           break -- we found on, so we don't need to keep looking
  208.         end
  209.       end
  210.     end
  211.   end
  212. end
  213.  
  214. --[[ color codes
  215. 0       nothing
  216. 1       white
  217. 2       orange
  218. 4       magenta
  219. 8       light blue
  220. 16      yellow
  221. 32      lime
  222. 64      pink
  223. 128     gray
  224. 256     light gray
  225. 512     cyan
  226. 1024    purple
  227. 2048    blue
  228. 4096    brown
  229. 8192    green
  230. 16384   red
  231. 32768   black
  232. 65535   all ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement