Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pSides = {"left","right","top","bottom","back","front"}
- debug = false
- buttons = {}
- w = 0
- h = 0
- hReal = 0
- wQty = 0
- hQty = 0
- wSize = 0
- hSize = 0
- wMin = 0
- wMax = 0
- hMin = 0
- hMax = 0
- maxPerPage = 1
- currentPage = 1
- maxPages = math.ceil(((#buttons-2)/maxPerPage)+0.5)
- buttonsOnPage = 1
- pageSet = 1
- function scan()
- for i = 1, #pSides do
- if peripheral.getType(pSides[i]) == "monitor" then
- m = peripheral.wrap(pSides[i])
- w,h = m.getSize()
- hReal = h
- wReal = w
- h = h-2
- m.clear()
- m.setCursorPos(1,1)
- if debug then
- print("monitor wrapped and set size is "..w,":"..h)
- else
- end
- elseif peripheral.getType(pSides[i]) == "modem" then
- rednet.open(pSides[i])
- else
- end
- end
- end
- function howManyButtons(w1,h1)
- wQty = w1
- hQty = h1
- wSize = math.floor((w-(wQty-1))/wQty)
- hSize = math.floor((h-(hQty-1))/hQty)
- wMin = 1
- wMax = wSize
- hMin = 1
- hMax = hSize
- maxPerPage = wQty * hQty
- if debug then
- print(wSize,":"..hSize,":"..wMin,":"..wMax,":"..hMin,":"..hMax)
- else
- end
- end
- function addPagesButtons()
- name = "Next"
- buttons[name] = {}
- buttons[name]["page"] = currentPage
- buttons[name]["name"] = name
- buttons[name]["func"] = pageUp
- buttons[name]["boolen"] = false
- buttons[name]["wMin"] = (wReal-6)
- buttons[name]["wMax"] = wReal
- buttons[name]["hMin"] = hReal - 1
- buttons[name]["hMax"] = hReal
- name2 = "Prev"
- buttons[name2] = {}
- buttons[name2]["page"] = currentPage
- buttons[name2]["name"] = name2
- buttons[name2]["func"] = pageDown
- buttons[name2]["boolen"] = false
- buttons[name2]["wMin"] = 1
- buttons[name2]["wMax"] = 6
- buttons[name2]["hMin"] = hReal-1
- buttons[name2]["hMax"] = hReal
- end
- function pageDown()
- if currentPage > 1 then
- currentPage = currentPage -1
- m.clear()
- else
- end
- end
- function pageUp()
- if currentPage < maxPages then
- currentPage = currentPage +1
- m.clear()
- else
- end
- end
- function setNextButton()
- wMin = wMin + wSize + 1
- wMax = wMax + wSize + 1
- if wMax > w then
- wMin = 1
- wMax = wSize
- hMin = hMin + hSize + 1
- hMax = hMax + hSize + 1
- else
- end
- if hMax > h then
- wMin = 1
- wMax = wSize
- hMin = 1
- hMax = hSize
- pageSet = pageSet +1
- else
- end
- if debug then
- print("setting next button at: "..wMin,":"..wMax,":"..hMin,":"..hMax)
- else
- end
- end
- function setTable(name,func,boolen)
- if debug then
- print("setting table "..name,":"..pageSet)
- else
- end
- buttons[name]={}
- buttons[name]["page"] = pageSet
- buttons[name]["name"] = name
- buttons[name]["func"] = func
- buttons[name]["boolen"] = boolen
- buttons[name]["wMin"] = wMin
- buttons[name]["wMax"] = wMax
- buttons[name]["hMin"] = hMin
- buttons[name]["hMax"] = hMax
- setNextButton()
- end
- function getTotalButtons()
- totalButtons = 0
- for d,v in pairs(buttons) do
- totalButtons = totalButtons +1
- end
- end
- function drawScreen()
- addPagesButtons()
- getTotalButtons()
- maxPages = math.ceil(((totalButtons)/maxPerPage)+0.5)
- for data,value in pairs(buttons) do
- screenPrint(buttons[data]["wMin"],buttons[data]["wMax"],buttons[data]["hMin"],buttons[data]["hMax"],buttons[data]["boolen"],buttons[data]["name"],buttons[data]["page"])
- if debug then
- print(buttons[data]["func"])
- else
- end
- end
- end
- function screenPrint(wMin,wMax,hMin,hMax,boolen,name,page)
- if debug then
- print("Printing button: "..name)
- else
- end
- if currentPage == page then
- if boolen then
- m.setBackgroundColor(colors.green)
- else
- m.setBackgroundColor(colors.red)
- end
- for i = wMin,wMax do
- for j = hMin,hMax do
- m.setCursorPos(i,j)
- m.write(" ")
- end
- end
- hSpot = math.floor((hMax - hMin)/2+hMin)
- wSpot = math.floor( (wMax - wMin - string.len(name) )/2+wMin)+1
- m.setCursorPos(wSpot,hSpot)
- m.write(name)
- m.setBackgroundColor(colors.black)
- else
- end
- end
- function watchForClick()
- while true do
- drawScreen()
- event, side, w,h = os.pullEvent("monitor_touch")
- if debug then
- print("Button clicked")
- else
- end
- for data,value in pairs(buttons) do
- if currentPage == buttons[data]["page"] then
- if w >= buttons[data]["wMin"] and w <= buttons[data]["wMax"] then
- if h >= buttons[data]["hMin"] and h <=buttons[data]["hMax"] then
- if debug then
- print(buttons[data]["name"])
- else
- end
- fname = buttons[data]["func"]
- if debug then
- print(fname)
- else
- end
- fname()
- if debug then
- print("done with fname")
- else
- end
- buttons[data]["boolen"] = not buttons[data]["boolen"]
- else
- end
- else
- end
- else
- end
- end
- end
- end
- function none()
- print("butAPI test")
- end
- scan()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement