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 | + | |
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 | local old = 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 | term.setTextColor(buttonData.activeText) | |
37 | else | |
38 | term.setBackgroundColor(buttonData.inactiveColor) | |
39 | term.setTextColor(buttonData.inactiveText) | |
40 | end | |
41 | for i = buttonData.yMin, buttonData.yMax do | |
42 | term.setCursorPos(buttonData.xMin, i) | |
43 | term.write(buttonData.label[i - buttonData.yMin + 1]) | |
44 | end | |
45 | end | |
46 | if old then | |
47 | term.redirect(old) | |
48 | else | |
49 | term.restore() | |
50 | end | |
51 | end, | |
52 | add = function(self, name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor, inactiveText, activeText) | |
53 | local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name) | |
54 | if self.buttonList[name] then error("button already exists", 2) end | |
55 | local x, y = self.mon.getSize() | |
56 | if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of bounds", 2) end | |
57 | self.buttonList[name] = { | |
58 | func = func, | |
59 | xMin = xMin, | |
60 | yMin = yMin, | |
61 | xMax = xMax, | |
62 | yMax = yMax, | |
63 | active = false, | |
64 | inactiveColor = inactiveColor or colors.red, | |
65 | activeColor = activeColor or colors.lime, | |
66 | inactiveText = inactiveText or colors.white, | |
67 | activeText = activeText or colors.white, | |
68 | label = label, | |
69 | } | |
70 | for i = xMin, xMax do | |
71 | for j = yMin, yMax do | |
72 | if self.clickMap[i][j] ~= nil then | |
73 | --undo changes | |
74 | for k = xMin, xMax do | |
75 | for l = yMin, yMax do | |
76 | if self.clickMap[k][l] == name then | |
77 | self.clickMap[k][l] = nil | |
78 | end | |
79 | end | |
80 | end | |
81 | self.buttonList[name] = nil | |
82 | error("overlapping button", 2) | |
83 | end | |
84 | self.clickMap[i][j] = name | |
85 | end | |
86 | end | |
87 | end, | |
88 | remove = function(self, name) | |
89 | if self.buttonList[name] then | |
90 | local button = self.buttonList[name] | |
91 | for i = button.xMin, button.xMax do | |
92 | for j = button.yMin, button.yMax do | |
93 | self.clickMap[i][j] = nil | |
94 | end | |
95 | end | |
96 | self.buttonList[name] = nil | |
97 | end | |
98 | end, | |
99 | run = function(self) | |
100 | while true do | |
101 | self:draw() | |
102 | local event = {self:handleEvents(os.pullEvent(self.side == "term" and "mouse_click" or "monitor_touch"))} | |
103 | if event[1] == "button_click" then | |
104 | self.buttonList[event[2]].func() | |
105 | end | |
106 | end | |
107 | end, | |
108 | handleEvents = function(self, ...) | |
109 | local event = {...} | |
110 | if #event == 0 then event = {os.pullEvent()} end | |
111 | if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term" and event[1] == "monitor_touch" and event[2] == self.side) then | |
112 | local clicked = self.clickMap[event[3]][event[4]] | |
113 | if clicked and self.buttonList[clicked] then | |
114 | return "button_click", clicked | |
115 | end | |
116 | end | |
117 | return unpack(event) | |
118 | end, | |
119 | toggleButton = function(self, name, noDraw) | |
120 | self.buttonList[name].active = not self.buttonList[name].active | |
121 | if not noDraw then self:draw() end | |
122 | end, | |
123 | flash = function(self, name, duration) | |
124 | self:toggleButton(name) | |
125 | sleep(tonumber(duration) or 0.15) | |
126 | self:toggleButton(name) | |
127 | end, | |
128 | rename = function(self, name, newName) | |
129 | self.buttonList[name].label, newName = setupLabel(self.buttonList[name].xMax - self.buttonList[name].xMin + 1, self.buttonList[name].yMin, self.buttonList[name].yMax, newName) | |
130 | if not self.buttonList[name] then error("no such button", 2) end | |
131 | if name ~= newName then | |
132 | self.buttonList[newName] = self.buttonList[name] | |
133 | self.buttonList[name] = nil | |
134 | for i = self.buttonList[newName].xMin, self.buttonList[newName].xMax do | |
135 | for j = self.buttonList[newName].yMin, self.buttonList[newName].yMax do | |
136 | self.clickMap[i][j] = newName | |
137 | end | |
138 | end | |
139 | end | |
140 | self:draw() | |
141 | end, | |
142 | } | |
143 | ||
144 | function new(monSide) | |
145 | local buttonInstance = { | |
146 | side = monSide or "term", | |
147 | mon = monSide and peripheral.wrap(monSide) or term.current(), | |
148 | buttonList = {}, | |
149 | clickMap = {}, | |
150 | } | |
151 | local x, y = buttonInstance.mon.getSize() | |
152 | for i = 1, x do | |
153 | buttonInstance.clickMap[i] = {} | |
154 | end | |
155 | setmetatable(buttonInstance, {__index = Button}) | |
156 | return buttonInstance | |
157 | end |