SHOW:
|
|
- or go back to the newest paste.
| 1 | - | --[[ |
| 1 | + | |
| 2 | - | The MIT License (MIT) |
| 2 | + | |
| 3 | - | |
| 3 | + | |
| 4 | - | Copyright (c) 2013 Lyqyd |
| 4 | + | |
| 5 | - | |
| 5 | + | |
| 6 | - | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + | |
| 7 | - | of this software and associated documentation files (the "Software"), to deal |
| 7 | + | |
| 8 | - | in the Software without restriction, including without limitation the rights |
| 8 | + | |
| 9 | - | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + | |
| 10 | - | copies of the Software, and to permit persons to whom the Software is |
| 10 | + | |
| 11 | - | furnished to do so, subject to the following conditions: |
| 11 | + | |
| 12 | - | |
| 12 | + | |
| 13 | - | The above copyright notice and this permission notice shall be included in |
| 13 | + | |
| 14 | - | all copies or substantial portions of the Software. |
| 14 | + | |
| 15 | - | |
| 15 | + | |
| 16 | - | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + | |
| 17 | - | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + | if maxY == minY or i == math.floor((maxY - minY + 1) / 2) then |
| 18 | - | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + | |
| 19 | - | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + | |
| 20 | - | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + | |
| 21 | - | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + | |
| 22 | - | THE SOFTWARE. |
| 22 | + | |
| 23 | - | --]] |
| 23 | + | |
| 24 | return labelTable, name | |
| 25 | end | |
| 26 | ||
| 27 | local Button = {
| |
| 28 | draw = function(self) | |
| 29 | term.redirect(self.mon) | |
| 30 | term.setTextColor(colors.white) | |
| 31 | term.setBackgroundColor(colors.black) | |
| 32 | term.clear() | |
| 33 | for name, buttonData in pairs(self.buttonList) do | |
| 34 | if buttonData.active then | |
| 35 | term.setBackgroundColor(buttonData.activeColor) | |
| 36 | else | |
| 37 | term.setBackgroundColor(buttonData.inactiveColor) | |
| 38 | end | |
| 39 | for i = buttonData.yMin, buttonData.yMax do | |
| 40 | term.setCursorPos(buttonData.xMin, i) | |
| 41 | - | if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then |
| 41 | + | |
| 42 | end | |
| 43 | end | |
| 44 | term.restore() | |
| 45 | end, | |
| 46 | add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor) | |
| 47 | local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name) | |
| 48 | if self.buttonList[name] then error("button already exists", 2) end
| |
| 49 | local x, y = self.mon.getSize() | |
| 50 | if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end
| |
| 51 | self.buttonList[name] = {
| |
| 52 | func = func, | |
| 53 | - | local old = term.redirect(self.mon) |
| 53 | + | |
| 54 | yMin = yMin, | |
| 55 | xMax = xMax, | |
| 56 | yMax = yMax, | |
| 57 | active = false, | |
| 58 | inactiveColor = inactiveColor or colors.red, | |
| 59 | activeColor = activeColor or colors.lime, | |
| 60 | - | term.setTextColor(buttonData.activeText) |
| 60 | + | |
| 61 | } | |
| 62 | for i = xMin, xMax do | |
| 63 | - | term.setTextColor(buttonData.inactiveText) |
| 63 | + | |
| 64 | if self.clickMap[i][j] ~= nil then | |
| 65 | --undo changes | |
| 66 | for k = xMin, xMax do | |
| 67 | for l = yMin, yMax do | |
| 68 | if self.clickMap[k][l] == name then | |
| 69 | self.clickMap[k][l] = nil | |
| 70 | - | if old then |
| 70 | + | |
| 71 | - | term.redirect(old) |
| 71 | + | |
| 72 | end | |
| 73 | - | term.restore() |
| 73 | + | |
| 74 | error("overlapping button", 2)
| |
| 75 | end | |
| 76 | - | add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText) |
| 76 | + | |
| 77 | end | |
| 78 | end | |
| 79 | end, | |
| 80 | run = function(self) | |
| 81 | while true do | |
| 82 | self:draw() | |
| 83 | local event = {os.pullEvent("monitor_touch")}
| |
| 84 | if event[2] == self.side then | |
| 85 | local clicked = self.clickMap[event[3]][event[4]] | |
| 86 | if clicked and self.buttonList[clicked] and self.buttonList[clicked].func then | |
| 87 | self.buttonList[clicked].func() | |
| 88 | end | |
| 89 | end | |
| 90 | - | inactiveText = inactiveText or colors.white, |
| 90 | + | |
| 91 | - | activeText = activeText or colors.white, |
| 91 | + | |
| 92 | handleEvents = function(self, ...) | |
| 93 | local event = {...}
| |
| 94 | if #event == 0 then event = {os.pullEvent()} end
| |
| 95 | if event[1] == "monitor_touch" and event[2] == self.side then | |
| 96 | local clicked = self.clickMap[event[3]][event[4]] | |
| 97 | if clicked and self.buttonList[clicked] then | |
| 98 | return "button_click", clicked | |
| 99 | end | |
| 100 | end | |
| 101 | return unpack(event) | |
| 102 | end, | |
| 103 | toggleButton = function(self, name, noDraw) | |
| 104 | self.buttonList[name].active = not self.buttonList[name].active | |
| 105 | if not noDraw then self:draw() end | |
| 106 | end, | |
| 107 | flash = function(self, name) | |
| 108 | self:toggleButton(name) | |
| 109 | sleep(0.15) | |
| 110 | self:toggleButton(name) | |
| 111 | end, | |
| 112 | - | remove = function(self, name) |
| 112 | + | |
| 113 | - | if self.buttonList[name] then |
| 113 | + | |
| 114 | - | local button = self.buttonList[name] |
| 114 | + | |
| 115 | - | for i = button.xMin, button.xMax do |
| 115 | + | self.buttonList[newName] = self.buttonList[name] |
| 116 | - | for j = button.yMin, button.yMax do |
| 116 | + | self.buttonList[name] = nil |
| 117 | - | self.clickMap[i][j] = nil |
| 117 | + | for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do |
| 118 | for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do | |
| 119 | self.clickMap[i][j] = newName | |
| 120 | - | self.buttonList[name] = nil |
| 120 | + | |
| 121 | end | |
| 122 | self:draw() | |
| 123 | end, | |
| 124 | } | |
| 125 | ||
| 126 | - | local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))}
|
| 126 | + | |
| 127 | - | if event[1] == "button_click" then |
| 127 | + | |
| 128 | - | self.buttonList[event[2]].func() |
| 128 | + | side = monSide, |
| 129 | mon = peripheral.wrap(monSide), | |
| 130 | buttonList = {},
| |
| 131 | clickMap = {},
| |
| 132 | } | |
| 133 | local x, y = buttonInstance.mon.getSize() | |
| 134 | for i = 1, x do | |
| 135 | - | if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then |
| 135 | + | |
| 136 | end | |
| 137 | setmetatable(buttonInstance, {__index = Button})
| |
| 138 | return buttonInstance | |
| 139 | end |