Advertisement
Guest User

gui

a guest
Feb 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.40 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 localPad = 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.         if width < 3 then
  128.             localPad = 0
  129.         end
  130.         if #label > width - localPad then
  131.             label = label:sub(1,width - localPad)
  132.         end
  133.        
  134.         -- draw label
  135.         display.setCursorPos(startColumn + math.floor((width - #label)/2),startRow + math.floor((height-1)/2))
  136.         display.write(label)
  137.         display.setBackgroundColor(button.defaultBackgroundColor)
  138.         display.setTextColor(button.defaultTextColor)
  139.     end
  140.                  
  141.     return button
  142. end
  143.  
  144.  
  145. local attachedMonitor = peripheral.wrap("top")
  146. attachedMonitor.clear()
  147. attachedMonitor.setTextScale(1)
  148. local HQ_button = Button(
  149.                             10,3,  --width,height
  150.                             "HQ", --label
  151.                             colors.blue,colors.red, -- background colors
  152.                             colors.white,colors.yellow, -- text colors
  153.                             true,colors.lightBlue, colors.pink, -- hasBorder, border colors
  154.                             10,10, -- starting location
  155.                             false, -- isPressed?
  156.                             colors.black,colors.white -- default colors
  157.                             )
  158. rednet.open("back")
  159. HQ_button.draw(attachedMonitor)
  160. while true do
  161.  
  162.   event = {os.pullEvent()}
  163.   trainPresent = rs.getInput("left")
  164.  
  165.   if event[1] == "monitor_touch" and not trainPresent then
  166.     attachedMonitor.clear()
  167.     attachedMonitor.setCursorPos(1,1)
  168.     attachedMonitor.write("> Requesting train.")
  169.     HQ_button.draw(attachedMonitor,true)
  170.     rednet.broadcast("HQ", "dispatch_request")
  171.     id, message = rednet.receive("dispatch_confirmation",5)
  172.     if not (message == nil) then
  173.       attachedMonitor.clear()
  174.       attachedMonitor.setCursorPos(1,1)
  175.       attachedMonitor.write("> Inbound from: "..message)      
  176.     else
  177.       attachedMonitor.clear()
  178.       attachedMonitor.setCursorPos(1,1)
  179.       attachedMonitor.write("> No response from depot.")  
  180.     end
  181.     HQ_button.draw(attachedMonitor, false)
  182.   elseif event[1] == "rednet_message" and event[4] == "dispatch_request" and trainPresent then    
  183.     rednet.broadcast("Desert Station", "dispatch_confirmation")
  184.     rs.setOutput("bottom", true)
  185.     sleep(5)
  186.     rs.setOutput("bottom", true)
  187.   end
  188.  
  189. end
  190. attachedMonitor.setCursorPos(1,9)
  191. print("Done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement