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