SHOW:
|
|
- or go back to the newest paste.
| 1 | --DIREWOLF20's BUTTON API | |
| 2 | local mon = peripheral.wrap("top")
| |
| 3 | mon.setTextScale(1) | |
| 4 | mon.setTextColor(colors.white) | |
| 5 | local button={}
| |
| 6 | mon.setBackgroundColor(colors.black) | |
| 7 | ||
| 8 | function clearTable() | |
| 9 | button = {}
| |
| 10 | mon.clear() | |
| 11 | end | |
| 12 | ||
| 13 | function setTable(name, func, xmin, xmax, ymin, ymax) | |
| 14 | button[name] = {}
| |
| 15 | button[name]["func"] = func | |
| 16 | button[name]["active"] = false | |
| 17 | button[name]["xmin"] = xmin | |
| 18 | button[name]["ymin"] = ymin | |
| 19 | button[name]["xmax"] = xmax | |
| 20 | button[name]["ymax"] = ymax | |
| 21 | end | |
| 22 | ||
| 23 | function funcName() | |
| 24 | print("You clicked buttonText")
| |
| 25 | end | |
| 26 | ||
| 27 | function fillTable() | |
| 28 | setTable("ButtonText", funcName, 5, 25, 4, 8)
| |
| 29 | end | |
| 30 | ||
| 31 | function fill(text, color, bData) | |
| 32 | mon.setBackgroundColor(color) | |
| 33 | local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2) | |
| 34 | local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1 | |
| 35 | for j = bData["ymin"], bData["ymax"] do | |
| 36 | mon.setCursorPos(bData["xmin"], j) | |
| 37 | if j == yspot then | |
| 38 | for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do | |
| 39 | if k == xspot then | |
| 40 | mon.write(text) | |
| 41 | else | |
| 42 | mon.write(" ")
| |
| 43 | end | |
| 44 | end | |
| 45 | else | |
| 46 | for i = bData["xmin"], bData["xmax"] do | |
| 47 | mon.write(" ")
| |
| 48 | end | |
| 49 | end | |
| 50 | end | |
| 51 | mon.setBackgroundColor(colors.black) | |
| 52 | end | |
| 53 | ||
| 54 | function screen() | |
| 55 | local currColor | |
| 56 | for name,data in pairs(button) do | |
| 57 | local on = data["active"] | |
| 58 | if on == true then currColor = colors.lime else currColor = colors.red end | |
| 59 | fill(name, currColor, data) | |
| 60 | end | |
| 61 | end | |
| 62 | ||
| 63 | function toggleButton(name) | |
| 64 | button[name]["active"] = not button[name]["active"] | |
| 65 | screen() | |
| 66 | end | |
| 67 | ||
| 68 | function flash(name) | |
| 69 | toggleButton(name) | |
| 70 | screen() | |
| 71 | sleep(0.15) | |
| 72 | toggleButton(name) | |
| 73 | screen() | |
| 74 | end | |
| 75 | ||
| 76 | function checkxy(x, y) | |
| 77 | for name, data in pairs(button) do | |
| 78 | if y>=data["ymin"] and y <= data["ymax"] then | |
| 79 | if x>=data["xmin"] and x<= data["xmax"] then | |
| 80 | data["func"]() | |
| 81 | return true | |
| 82 | --data["active"] = not data["active"] | |
| 83 | --print(name) | |
| 84 | end | |
| 85 | end | |
| 86 | end | |
| 87 | return false | |
| 88 | end | |
| 89 | ||
| 90 | function heading(text) | |
| 91 | w, h = mon.getSize() | |
| 92 | mon.setCursorPos((w-string.len(text))/2+1, 1) | |
| 93 | mon.write(text) | |
| 94 | end | |
| 95 | ||
| 96 | function label(w, h, text) | |
| 97 | mon.setCursorPos(w, h) | |
| 98 | mon.write(text) | |
| 99 | end |