SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ * Buttons API by LBPHacker, 2013 | |
| 2 | * You can use it whenever and whereever you want, | |
| 3 | but give credit! (at least something like 'LBP') | |
| 4 | ]] | |
| 5 | ||
| 6 | --edited by Shazbot | |
| 7 | ||
| 8 | - | local defaultColorT = colors.white |
| 8 | + | |
| 9 | - | local defaultColorB = colors.black |
| 9 | + | |
| 10 | local bgColorDefault = colors.black | |
| 11 | local textColorDefault = colors.white | |
| 12 | ||
| 13 | local forcedEvent | |
| 14 | local getClickEvent = function() | |
| 15 | local clickEvent | |
| 16 | if forcedEvent then | |
| 17 | clickEvent = forcedEvent | |
| 18 | else | |
| 19 | --[[ * Below is a pretty way to determine wheter | |
| 20 | the terminal is redirected to a monitor or not. | |
| 21 | * As you might know, .setTextScale can be found | |
| 22 | in monitors only. | |
| 23 | ]] | |
| 24 | if term.setTextScale then | |
| 25 | clickEvent = "monitor_touch" | |
| 26 | else | |
| 27 | clickEvent = "mouse_click" | |
| 28 | end | |
| 29 | end | |
| 30 | return clickEvent | |
| 31 | - | register = function(xorg, yorg, width, height, textColor, backgroundColor, text, func) |
| 31 | + | |
| 32 | ||
| 33 | register = function(xorg, yorg, width, height, color, text, func) | |
| 34 | local newButtonID = false | |
| 35 | for i = 1, #container do if not container[i].alive and not newButtonID then newButtonID = i end end | |
| 36 | if not newButtonID then | |
| 37 | newButtonID = #container + 1 | |
| 38 | container[newButtonID] = {}
| |
| 39 | end | |
| 40 | container[newButtonID].alive = true | |
| 41 | container[newButtonID].xorg = xorg | |
| 42 | container[newButtonID].yorg = yorg | |
| 43 | - | container[newButtonID].textColor = textColor |
| 43 | + | |
| 44 | - | container[newButtonID].backgroundColor = backgroundColor |
| 44 | + | |
| 45 | container[newButtonID].color = color | |
| 46 | container[newButtonID].text = text | |
| 47 | container[newButtonID].func = func | |
| 48 | container[newButtonID].enabled = true | |
| 49 | container[newButtonID].visible = true | |
| 50 | return newButtonID | |
| 51 | end | |
| 52 | ||
| 53 | exists = function(buttonID) | |
| 54 | if container[buttonID] then | |
| 55 | return container[buttonID].alive | |
| 56 | else | |
| 57 | return false | |
| 58 | end | |
| 59 | end | |
| 60 | ||
| 61 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 61 | + | |
| 62 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 63 | container[buttonID].alive = false | |
| 64 | end | |
| 65 | ||
| 66 | event = function(eventArray) | |
| 67 | if eventArray[1] == getClickEvent() then | |
| 68 | local button, mousex, mousey = eventArray[2], eventArray[3], eventArray[4] | |
| 69 | for i = 1, #container do | |
| 70 | - | if container[i].enabled and mousex > container[i].xorg - 1 and mousey > container[i].yorg - 1 and mousex < container[i].xorg + container[i].width and mousey < container[i].yorg + container[i].height then container[i].func() end |
| 70 | + | |
| 71 | if container[i].enabled and (button == 1 or getClickEvent() == "monitor_touch") and mousex > container[i].xorg - 1 and mousey > container[i].yorg - 1 and mousex < container[i].xorg + container[i].width and mousey < container[i].yorg + container[i].height then container[i].func() end | |
| 72 | end | |
| 73 | end | |
| 74 | end | |
| 75 | end | |
| 76 | ||
| 77 | - | term.setBackgroundColor(defaultColorB) |
| 77 | + | |
| 78 | term.setBackgroundColor(32768) | |
| 79 | term.clear() | |
| 80 | for i = 1, #container do | |
| 81 | if container[i].alive and container[i].visible then | |
| 82 | for j = container[i].yorg, container[i].yorg + container[i].height - 1 do | |
| 83 | - | term.setTextColor(container[i].textColor) |
| 83 | + | |
| 84 | - | term.setBackgroundColor(container[i].backgroundColor) |
| 84 | + | term.setTextColor(2 ^ (container[i].color % 16)) |
| 85 | term.setBackgroundColor(2 ^ math.floor(container[i].color / 16)) | |
| 86 | term.write(string.rep(" ", container[i].width))
| |
| 87 | end | |
| 88 | term.setCursorPos(container[i].xorg + math.floor((container[i].width - #container[i].text) / 2), container[i].yorg + math.floor((container[i].height - 1) / 2)) | |
| 89 | term.write(container[i].text) | |
| 90 | end | |
| 91 | - | term.setTextColor(defaultColorT) |
| 91 | + | |
| 92 | - | term.setBackgroundColor(defaultColorB) |
| 92 | + | term.setTextColor(textColorDefault) |
| 93 | term.setBackgroundColor(bgColorDefault) | |
| 94 | end | |
| 95 | ||
| 96 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 96 | + | |
| 97 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 98 | container[buttonID].enabled = bEnabled | |
| 99 | end | |
| 100 | ||
| 101 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 101 | + | |
| 102 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 103 | container[buttonID].visible = bVisible | |
| 104 | end | |
| 105 | ||
| 106 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 106 | + | |
| 107 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 108 | container[buttonID].xorg = xorg | |
| 109 | container[buttonID].yorg = yorg | |
| 110 | container[buttonID].width = width | |
| 111 | container[buttonID].height = height | |
| 112 | end | |
| 113 | ||
| 114 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 114 | + | |
| 115 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 116 | container[buttonID].text = sText | |
| 117 | end | |
| 118 | ||
| 119 | setColor = function(buttonID, color) | |
| 120 | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID), 2) end
| |
| 121 | container[buttonID].color = color | |
| 122 | - | setColor = function(buttonID, colorT, colorB) |
| 122 | + | |
| 123 | - | if not exists(buttonID) then error("Invalid identifier: " .. tostring(buttonID)) end
|
| 123 | + | |
| 124 | - | container[buttonID].textColor = colorT |
| 124 | + | |
| 125 | - | container[buttonID].backgroundColor = colorB |
| 125 | + | |
| 126 | end | |
| 127 | ||
| 128 | - | setDefaultColor = function(buttonID, colorT, colorB) |
| 128 | + | setBgColorDefault = function(color) |
| 129 | - | defaultColorT = colorT |
| 129 | + | bgColorDefault = color |
| 130 | - | defaultColorB = colorB |
| 130 | + | |
| 131 | ||
| 132 | setTextColorDefault = function(color) | |
| 133 | textColorDefault = color | |
| 134 | end |