Nic_Nacs

firstButton

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