forked from Siarko/ButtonApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton
254 lines (225 loc) · 5.84 KB
/
button
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--Button api for computercraft by Siarko
--Usage:
--
-- FIRST SET MONITOR HANDLE BY
-- button.setMonitor(handle)
--
-- new button object creation:
-- myFancyButton = button.create([label])
-- Methods:
-- setText, setPos, setSize, setColor, setBlinkColor, setActive, onClick
-- return button object so they can be chained eg:
-- myButton = button.create("My button").setSize(5,5).setColor(colors.gray).onClick(function)
-- setText(string, resize-bool) - sets button label; if resize then auto changes size to fit label
-- setSize(int, int), setPos(int, int) - quite self explainatory ;)
-- setColor(color) - sets background color, parameter color - computercraft colors api
-- setBlinkColor(color) - sets background to be drawn while button click
-- setActive(bool) - is button active and can be clicked? inactive - gray background
-- onClick(function) - function called when button is clicked
-- onClickReturn(value) - "await" function returns value instead of executing callback,
-- returns nil if no button was clicked
-- setAlign("left" / "center" / "right") - aligns label in button accordingly;
-- if label doesn't fit, it will be cut to fit, min button width = 4
--Activate waiting for click:
-- button.await(b1, b2, b3, ...)
-- or button.await(buttonArray)
-- b1, b2, b3, - single button objects
-- buttonArray - table containing buttons eg:
-- arr = {b1, b2, b3}
-- "await" function draws supplied buttons and waits for click, if any button is clicked
-- fires callback function set in button by "onClick()" method
-- probably should be called in loop
-- If you want to do something in the background you have to run "button.await" in another thread
-- like parallel.waitForAny(background, buttonAwaitFunction)
local _locales = {
mon = nil
}
--button class
function create(text)
local this = {
x = 1,
y = 1,
w = 0,
h = 1,
text = tostring(text) or "None",
bgcol = colors.lime,
blinkCol = colors.red,
align = "c",
active = true,
callback = nil,
ret = nil,
textColor = colors.black,
disabledColor = colors.gray
}
if text ~= nil then
this.w = #tostring(text)
end
--setters, return this object
function this.setText(text, resize)
this.text = tostring(text)
if resize and this.w < #this.text then
this.w = #this.text
end
return this
end
function this.setTextColor(color)
this.textColor = color
return this
end
function this.setDisabledColor(color)
this.disabledColor = color
return this
end
function this.setAlign(align)
if align == "center" then
this.align = "c"
elseif align == "left" then
this.align = "l"
elseif align == "right" then
this.align = "r"
else
print("Incorrect slign set! ")
error()
end
return this
end
function this.setPos(x, y)
this.x = x
this.y = y
return this
end
function this.setSize(w, h)
this.w = w
this.h = h
return this
end
function this.setColor(color)
this.bgcol = color
return this
end
function this.setBlinkColor(color)
this.blinkCol = color
return this
end
function this.setActive(state)
this.active = state
return this
end
function this.wasClicked(x,y)
if
x >= this.x and
x < this.x+this.w and
y >= this.y and
y < this.y+this.h and
this.active
then
return true
end
return false
end
function this.onClick(callback)
this.callback = callback
return this
end
function this.onClickReturn(value)
this.ret = value
return this
end
function this.fireEvent()
if this.callback ~= nil then
this.callback()
end
end
function this.drawWrapper(bgcol)
if _locales.mon == nil then
print("Monitor not set!")
error()
end
local xpos = this.x+(this.w/2-#this.text/2)
local t = this.text
local bg = _locales.mon.getBackgroundColor()
local tc = _locales.mon.getTextColor()
if this.align == "l" then
xpos = this.x
end
if this.align == "r" then
xpos = this.x+this.w-#this.text
end
if #this.text > this.w then
xpos = this.x
t = string.sub(t,1,this.w-3)..".."..string.sub(t,-1)
end
_locales.mon.setTextColor(this.textColor)
local f = string.rep(" ", this.w)
if this.active then
_locales.mon.setBackgroundColor(bgcol)
else
_locales.mon.setBackgroundColor(this.disabledColor)
end
for i = 1, this.h do
_locales.mon.setCursorPos(this.x, this.y+(i-1))
_locales.mon.write(f)
end
_locales.mon.setCursorPos(xpos,this.y+this.h/2)
_locales.mon.write(t)
_locales.mon.setBackgroundColor(bg)
_locales.mon.setTextColor(tc)
end
function this.draw()
this.drawWrapper(this.bgcol)
end
function this.blink()
this.drawWrapper(this.blinkCol)
sleep(0.2)
this.draw()
end
return this
end
--set Monitor handle to draw on
function setMonitor(mon)
_locales.mon = mon
--MON = mon
end
function clearMon()
_locales.mon.clear()
end
local function isTable(element)
return type(element) == "table"
end
local function isButton(element)
if isTable(element) and element.text ~= nil then
return true
end
return false
end
local function mergeTables(tab1, tab2)
for i in pairs(tab2) do
tab1[#tab1+1] = tab2[i]
end
end
--manage button checks
function await(...)
array = {}
for i in pairs(arg) do
if i ~= "n" then
if isTable(arg[i]) and not isButton(arg[i]) then --table of buttons
mergeTables(array, arg[i])
else --single button
array[#array+1] = arg[i]
end
end
end
for i in pairs(array) do
array[i].draw()
end
e, s, x, y = os.pullEvent("monitor_touch")
for i in pairs(array) do
if array[i].wasClicked(x,y) then
array[i].blink()
if array[i].ret ~= nil then
return array[i].ret
end
array[i].fireEvent()
end
end
end