Advertisement
Guest User

butapi

a guest
Apr 3rd, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.68 KB | None | 0 0
  1. pSides = {"left","right","top","bottom","back","front"}
  2. debug = false
  3. buttons = {}
  4.  
  5. w = 0
  6. h = 0
  7. hReal = 0
  8.  
  9. wQty = 0
  10. hQty = 0
  11. wSize = 0
  12. hSize = 0
  13.  
  14. wMin = 0
  15. wMax = 0
  16. hMin = 0
  17. hMax = 0
  18.  
  19. maxPerPage = 1
  20. currentPage = 1
  21. maxPages = math.ceil(((#buttons-2)/maxPerPage)+0.5)
  22. buttonsOnPage = 1
  23. pageSet = 1
  24.  
  25. function scan()
  26.     for i = 1, #pSides do
  27.         if peripheral.getType(pSides[i]) == "monitor" then
  28.             m = peripheral.wrap(pSides[i])
  29.             w,h = m.getSize()
  30.             hReal = h
  31.             wReal = w
  32.             h = h-2
  33.             m.clear()
  34.             m.setCursorPos(1,1)
  35.             if debug then
  36.                 print("monitor wrapped and set size is "..w,":"..h)
  37.             else
  38.             end
  39.         elseif peripheral.getType(pSides[i]) == "modem" then
  40.             rednet.open(pSides[i])
  41.         else
  42.         end
  43.     end
  44. end
  45.  
  46. function howManyButtons(w1,h1)
  47.     wQty = w1
  48.     hQty = h1
  49.     wSize = math.floor((w-(wQty-1))/wQty)
  50.     hSize = math.floor((h-(hQty-1))/hQty)
  51.     wMin = 1
  52.     wMax = wSize
  53.     hMin = 1
  54.     hMax = hSize
  55.     maxPerPage = wQty * hQty
  56.     if debug then
  57.         print(wSize,":"..hSize,":"..wMin,":"..wMax,":"..hMin,":"..hMax)
  58.     else
  59.     end
  60. end
  61.  
  62. function addPagesButtons()
  63.     name = "Next"
  64.     buttons[name] = {}
  65.     buttons[name]["page"] = currentPage
  66.     buttons[name]["name"] = name
  67.     buttons[name]["func"] = pageUp
  68.     buttons[name]["boolen"] = false
  69.     buttons[name]["wMin"] = (wReal-6)
  70.     buttons[name]["wMax"] = wReal
  71.     buttons[name]["hMin"] = hReal - 1
  72.     buttons[name]["hMax"] = hReal
  73.    
  74.     name2 = "Prev"
  75.     buttons[name2] = {}
  76.     buttons[name2]["page"] = currentPage
  77.     buttons[name2]["name"] = name2
  78.     buttons[name2]["func"] = pageDown
  79.     buttons[name2]["boolen"] = false
  80.     buttons[name2]["wMin"] = 1
  81.     buttons[name2]["wMax"] = 6
  82.     buttons[name2]["hMin"] = hReal-1
  83.     buttons[name2]["hMax"] = hReal
  84. end
  85.  
  86. function pageDown()
  87.     if currentPage > 1 then
  88.         currentPage = currentPage -1
  89.         m.clear()
  90.     else
  91.     end
  92. end
  93.  
  94. function pageUp()
  95.     if currentPage < maxPages then
  96.         currentPage = currentPage +1
  97.         m.clear()
  98.     else
  99.     end
  100. end
  101.  
  102. function setNextButton()
  103.     wMin = wMin + wSize + 1
  104.     wMax = wMax + wSize + 1
  105.     if  wMax > w then
  106.         wMin = 1
  107.         wMax = wSize
  108.         hMin = hMin + hSize + 1
  109.         hMax = hMax + hSize + 1
  110.     else
  111.     end
  112.     if hMax > h then
  113.         wMin = 1
  114.         wMax = wSize
  115.         hMin = 1
  116.         hMax = hSize
  117.         pageSet = pageSet +1
  118.     else
  119.     end
  120.     if debug then
  121.         print("setting next button at: "..wMin,":"..wMax,":"..hMin,":"..hMax)
  122.     else
  123.     end
  124. end
  125.  
  126. function setTable(name,func,boolen)
  127.     if debug then
  128.         print("setting table "..name,":"..pageSet)
  129.     else
  130.     end
  131.     buttons[name]={}
  132.     buttons[name]["page"] = pageSet
  133.     buttons[name]["name"] = name
  134.     buttons[name]["func"] = func
  135.     buttons[name]["boolen"] = boolen
  136.     buttons[name]["wMin"] = wMin
  137.     buttons[name]["wMax"] = wMax
  138.     buttons[name]["hMin"] = hMin
  139.     buttons[name]["hMax"] = hMax
  140.     setNextButton()
  141. end
  142.  
  143. function getTotalButtons()
  144.     totalButtons = 0
  145.     for d,v in pairs(buttons) do
  146.         totalButtons = totalButtons +1
  147.     end
  148. end
  149.  
  150. function drawScreen()
  151.     addPagesButtons()
  152.     getTotalButtons()
  153.     maxPages = math.ceil(((totalButtons)/maxPerPage)+0.5)
  154.     for data,value in pairs(buttons) do
  155.         screenPrint(buttons[data]["wMin"],buttons[data]["wMax"],buttons[data]["hMin"],buttons[data]["hMax"],buttons[data]["boolen"],buttons[data]["name"],buttons[data]["page"])
  156.         if debug then
  157.             print(buttons[data]["func"])
  158.         else
  159.         end
  160.     end
  161. end
  162.  
  163. function screenPrint(wMin,wMax,hMin,hMax,boolen,name,page)
  164.     if debug then
  165.         print("Printing button: "..name)
  166.     else
  167.     end
  168.     if currentPage == page then
  169.         if boolen then
  170.             m.setBackgroundColor(colors.green)
  171.         else
  172.             m.setBackgroundColor(colors.red)
  173.         end
  174.         for i = wMin,wMax do
  175.             for j = hMin,hMax do
  176.                 m.setCursorPos(i,j)
  177.                 m.write(" ")
  178.             end
  179.         end
  180.         hSpot = math.floor((hMax - hMin)/2+hMin)
  181.         wSpot = math.floor( (wMax - wMin -  string.len(name) )/2+wMin)+1
  182.         m.setCursorPos(wSpot,hSpot)
  183.         m.write(name)
  184.         m.setBackgroundColor(colors.black)
  185.     else
  186.     end
  187. end
  188.  
  189. function watchForClick()
  190.     while true do
  191.         drawScreen()
  192.         event, side, w,h = os.pullEvent("monitor_touch")
  193.         if debug then
  194.             print("Button clicked")
  195.         else
  196.         end
  197.         for data,value in pairs(buttons) do
  198.             if currentPage == buttons[data]["page"] then
  199.                 if w >= buttons[data]["wMin"] and w <= buttons[data]["wMax"] then
  200.                     if h >= buttons[data]["hMin"] and h <=buttons[data]["hMax"]  then
  201.                         if debug then
  202.                             print(buttons[data]["name"])
  203.                         else
  204.                         end
  205.                         fname = buttons[data]["func"]
  206.                         if debug then
  207.                             print(fname)
  208.                         else
  209.                         end    
  210.                         fname()
  211.                         if debug then
  212.                             print("done with fname")
  213.                         else
  214.                         end
  215.                         buttons[data]["boolen"] = not buttons[data]["boolen"]
  216.                     else
  217.                     end
  218.                 else
  219.                 end
  220.             else
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226. function none()
  227.     print("butAPI test")
  228. end
  229.  
  230. scan()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement