Advertisement
MrHG

Touchscreen Menu

May 13th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. -- Touchscreen Program using Touchscreen API
  2. if viewportAPI then os.unloadAPI("viewportAPI") end
  3. os.loadAPI("viewportAPI")
  4. if buttonAPI then os.unloadAPI("buttonAPI") end
  5. os.loadAPI("buttonAPI")
  6. if eventDispatcherAPI then os.unloadAPI("eventDispatcherAPI") end
  7. os.loadAPI("eventDispatcherAPI")
  8.  
  9. -- Declaring Vars
  10. rSide = "top"
  11. amenuItems = {}
  12. abuttons = {}
  13. aCurrentOrder = {}
  14. foodAmount = 1
  15.  
  16. -- Filesystem Setup
  17. if not fs.exists("/menu") then
  18.    fs.makeDir("/menu")
  19.    print("Created menu folder. Please enter items before running again.")
  20.    return
  21. end
  22.  
  23. -- Creating Viewport for Buttons
  24. viewport = viewportAPI.new({term = term})
  25.  
  26. -- Function for Button
  27. function clickHandler(element, posX, posY)
  28.   for i=1,#amenuItems do
  29.    if element.text == amenuItems[i] then
  30.     cfile = fs.open("menu/"..amenuItems[i], "r")
  31.     cfileColor = cfile.readLine()
  32.     for i=1,foodAmount do
  33.      aCurrentOrder[#aCurrentOrder+1] = cfileColor
  34.     end
  35.     cfile.close()
  36.    end
  37.   end
  38.   element.backgroundColor = colors.lightGray
  39.   viewport:redraw()
  40.   --redstone.setBundledOutput(rSide, colors.white)
  41.   sleep(0.1)
  42.   element.backgroundColor = colors.white
  43.   --redstone.setBundledOutput(rSide,0)
  44.   return true
  45. end
  46.  
  47. function sendOrder(element, posX, posY)
  48.  element.backgroundColor = colors.gray
  49.  element.text = "Processing"
  50.  viewport:redraw()
  51.  for i=1,#aCurrentOrder do
  52.   color = aCurrentOrder[i]-0
  53.   redstone.setBundledOutput(rSide, color)
  54.   sleep(0.1)
  55.   redstone.setBundledOutput(rSide, 0)
  56.   sleep(0.1)
  57.  end
  58.  aCurrentOrder = {}
  59.  element.backgroundColor = colors.lightGray
  60.  element.text = "Checkout"
  61.  viewport:redraw()
  62.  return true
  63. end
  64.  
  65. function cycleFoodAmount(element, posX, posY)
  66.  element.backgroundColor = colors.gray
  67.  viewport:redraw()
  68.  sleep(0.1)
  69.  element.backgroundColor = colors.lightGray
  70.  if foodAmount == 1 then foodAmount = 8 element.text = foodAmount.."" return true
  71.  elseif foodAmount == 8 then foodAmount = 16 element.text = foodAmount.."" return true
  72.  else foodAmount = 1 element.text = foodAmount.."" return true end
  73. end
  74.  
  75. topButton = buttonAPI.new({
  76.     x = 2,
  77.     y = 2,
  78.     width = buttonAPI.width(0.95),
  79.     height = 2,
  80.     backgroundColor = colors.purple,
  81.     textColor = colors.white,
  82.     text = "Hyps Fried Chicken Menu" })
  83. viewport:addElement(topButton)
  84.  
  85. checkoutButton = buttonAPI.new({
  86.     x = buttonAPI.width(0.20),
  87.     y = buttonAPI.height(0.90),
  88.     width = buttonAPI.width(0.80),
  89.     height = 2,
  90.     backgroundColor = colors.lightGray,
  91.     textColor = colors.white,
  92.     callback = sendOrder,
  93.     text = "Checkout" })
  94. viewport:addElement(checkoutButton)
  95.  
  96. foodAmountButton = buttonAPI.new({
  97.     x = 2,
  98.     y = buttonAPI.height(0.90),
  99.     width = buttonAPI.width(0.10),
  100.     height = 2,
  101.     backgroundColor = colors.lightGray,
  102.     textColor = colors.white,
  103.     callback = cycleFoodAmount,
  104.     text = foodAmount.."" })
  105. viewport:addElement(foodAmountButton)
  106.  
  107. amenuItems = fs.list("/menu")
  108. yStep = 2
  109. if amenuItems ~= nil then
  110.  for i=1,#amenuItems do
  111.   buttonX = 2
  112.   buttonY = 3*yStep
  113.   if i % 2 == 0 then
  114.    buttonX = buttonAPI.width(0.55)
  115.    yStep = yStep + 1
  116.   end
  117.   abuttons[i] = buttonAPI.new({
  118.     x = buttonX,
  119.     y = buttonY,
  120.     width = buttonAPI.width(0.45),
  121.     height = 2,
  122.     callback = clickHandler,
  123.     text = amenuItems[i] })
  124.  end
  125. else
  126.  error()
  127. end
  128.  
  129. for i=1,#abuttons do
  130.  viewport:addElement(abuttons[i])
  131. end
  132. viewport:redraw()
  133.  
  134. eventDispatcherAPI.addHandler("mouse_click", function(event, side, xPos, yPos)
  135.     viewport:handleClick(xPos, yPos)
  136. end)
  137.  
  138. eventDispatcherAPI.runDispatchLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement