View difference between Paste ID: zRM3grRR and Hfkga8c8
SHOW: | | - or go back to the newest paste.
1
local idleColor=256
2
local activatedColor=8192
3
local backgroundColor=32768
4
local textColor=1
5
local buttons={}
6
local monitor=nil
7
local debugMode=false
8
local monitorSide=nil
9
10
function addButton(x,y,width,height,text,toRunFunction,toggle,relative,idleColor,activeColor,textColor)
11
	if monitor==nil then
12
		if debugMode==true then
13
			print("Monitor is not yet defined! Use setMonitorSide(side)")
14
		end
15
		return false,"Monitor is not yet defined! Use setMonitorSide(side)"
16
	elseif x==nil or y==nil or width==nil or height==nil or text==nil then
17
		if debugMode==true then
18
			print("Not all required arguments are specified.")
19
		end
20
		return false,"Not all required arguments are specified."
21
	else
22
		buttonID=#buttons+1
23
		if string.len(text)>width and relative~=true then
24
			text=string.sub(text,0,width)
25
		elseif string.len(text)>width*screenx then
26
			text=string.sub(text,0,width*screenx)
27
		end
28
		buttons[buttonID]={['x']=x,['y']=y,['width']=width,['height']=height,['text']=text,['active']=false,['function']=toRunFunction,['toggle']=toggle,['relative']=relative,['activeColor']=activeColor,['idleColor']=idleColor,['textColor']=textColor}
29
		drawAllButtons{}
30
		return buttonID
31
	end
32
end
33
34
function removeButton(buttonID)
35
	if monitor==nil then
36
		if debugMode==true then
37
			print("Monitor is not yet defined! Use setMonitorSide(side)")
38
		end
39
		return false,"Monitor is not yet defined! Use setMonitorSide(side)"
40
	else
41
		if buttonID==nil then
42
			if debugMode==true then
43
				print("Not all required arguments are specified.")
44
			end
45
			return false,"Not all required arguments are specified."
46
		else
47
			if buttons[buttonID]==nil then
48
				if debugMode==true then
49
					print("That button ID does not correspond with a button!")
50
				end
51
				return false,"That button ID does not correspond with a button!"
52
			else
53
				buttons[buttonID]={"Removed"}
54
			end
55
		end	
56
	end
57
end
58
59
60
function drawAllButtons()
61
	if monitor==nil then
62
		if debugMode==true then
63
			print("Monitor is not yet defined! Use setMonitorSide(side)")
64
		end
65
		return false,"Monitor is not yet defined! Use setMonitorSide(side)"
66
	else
67
		monitor.setBackgroundColor(backgroundColor)
68
		monitor.clear()
69
		for buttonID,buttonInfo in pairs(buttons) do
70
			local x=buttonInfo['x']
71
			local y=buttonInfo['y']
72
			local width=buttonInfo['width']
73
			local height=buttonInfo['height']
74
			if buttonInfo['relative']==true then
75
				x=math.floor(x*screenx+0.5)
76
				y=math.floor(y*screeny+0.5)
77
				width=math.floor(width*screenx+0.5)
78
				height=math.floor(height*screeny+0.5)
79
			end
80
			monitor.setCursorPos(x,y)
81
			if buttonInfo['active']==true then	
82
				if buttonInfo['activeColor']==nil then
83
					monitor.setBackgroundColor(activatedColor)
84
				else
85
					monitor.setBackgroundColor(buttonInfo['activeColor'])
86
				end
87
			else
88
				if buttonInfo['idleColor']==nil then
89
					monitor.setBackgroundColor(idleColor)
90
				else
91
					monitor.setBackgroundColor(buttonInfo['idleColor'])
92
				end
93
			end
94
			for i=0,height-1 do
95
				monitor.setCursorPos(x,y+i)
96
				for i=1,width do
97
					monitor.write(" ")
98
				end
99
			end
100
			if buttonInfo['textColor']==nil then
101
				monitor.setTextColor(textColor)
102
			else
103
				monitor.setTextColor(buttonInfo['textColor'])
104
			end
105
			stringX=x+(width/2-string.len(buttonInfo['text'])/2)
106
			stringY=y+(math.floor(height)/2)
107
			monitor.setCursorPos(stringX,stringY)
108
			monitor.write(buttonInfo['text'])
109
		end	
110
	end
111
end
112
113
function getButtonInfo(buttonID)
114
	if monitor==nil then
115
		if debugMode==true then
116
			print("Monitor is not yet defined! Use setMonitorSide(side)")
117
		end
118
		return false,"Monitor is not yet defined! Use setMonitorSide(side)"
119
	else
120
		if buttonID==nil then
121
			if debugMode==true then
122
				print("Not all required arguments are specified.")
123
			end
124
			return false,"Not all required arguments are specified."
125
		else
126
			if buttons[buttonID]==nil then
127
				if debugMode==true then
128
					print("That button ID does not correspond with a button!")
129
				end
130
				return false,"That button ID does not correspond with a button!"
131
			else
132
				if debugMode==true then
133
					for infoID,info in pairs(buttons[buttonID]) do
134
						if type(info)=="boolean" then
135
							if info==false then
136
								info="false"
137
							else
138
								info="true"
139
							end
140
						elseif type(info)=="function" then
141
							info="function"
142
						end
143
							print(infoID.."	:"..info)
144
						
145
					end
146
				end
147
				return buttons[buttonID]
148
			end
149
		end	
150
	end
151
end
152
153
function setButtonInfo(buttonID,infoID,info)
154
	if monitor==nil then
155
		if debugMode==true then
156
			print("Monitor is not yet defined! Use setMonitorSide(side)")
157
		end
158
		return false,"Monitor is not yet defined! Use setMonitorSide(side)"
159
	else
160
		if buttonID==nil then
161
			if debugMode==true then
162
				print("Not all required arguments are specified.")
163
			end
164
			return false,"Not all required arguments are specified."
165
		else
166
			if buttons[buttonID]==nil then
167
				if debugMode==true then
168
					print("That button ID does not correspond with a button!")
169
				end
170
				return false,"That button ID does not correspond with a button!"
171
			else
172
				buttons[buttonID][infoID]=info
173
			end
174
		end	
175
	end
176
end
177
178-
function run()
178+
function run(event,side,clickX,clickY)
179
	print("Pos:"..clickX..","..clickY)
180
	for buttonID,buttonInfo in pairs(buttons) do
181
		local x=buttonInfo['x']
182
		local y=buttonInfo['y']
183
		local width=buttonInfo['width']
184
		local height=buttonInfo['height']
185
		if buttonInfo['relative']==true then
186
			x=math.floor(x*screenx+0.5)
187
			y=math.floor(y*screeny+0.5)
188
			width=math.floor(width*screenx+0.5)
189
			height=math.floor(height*screeny+0.5)
190
		end
191
		if debugMode==true then
192
			print("Pos:"..x..","..y)
193
		end
194
		if clickX>x and clickX<x+width and clickY>y and clickY<y+height then
195
			if debugMode==true then
196
				print("Clicked :"..buttonID)
197
			end
198
			if buttonInfo['toggle']==true then
199
				buttonInfo['active']=not buttonInfo['active']
200
				if buttonInfo['active']==true then
201
					buttonInfo['function'](true)
202
				else
203
					buttonInfo['function'](false)
204
				end
205
				drawAllButtons()
206
			else
207
				buttonInfo['function']()
208
				buttonInfo['active']=true
209
				drawAllButtons()
210
				sleep(0.5)
211
				if buttonInfo~=nil then
212
					buttonInfo['active']=false
213
				end	
214
				drawAllButtons()
215
			end
216
			
217
		end
218
	end	
219
end
220
221
function main()
222
	event,side,clickX,clickY=os.pullEvent()
223
	if event=="monitor_touch" then
224
		for buttonID,buttonInfo in pairs(buttons) do
225
			local x=buttonInfo['x']
226
			local y=buttonInfo['y']
227
			local width=buttonInfo['width']
228
			local height=buttonInfo['height']
229
			if buttonInfo['relative']==true then
230
				x=math.floor(x*screenx+0.5)
231
				y=math.floor(y*screeny+0.5)
232
				width=math.floor(width*screenx+0.5)
233
				height=math.floor(height*screeny+0.5)
234
			end
235
			if debugMode==true then
236
				print("Pos:"..x..","..y)
237
			end
238
			if clickX>x and clickX<x+width and clickY>y and clickY<y+height then
239
				if debugMode==true then
240
					print("Clicked :"..buttonID)
241
				end
242
				if buttonInfo['toggle']==true then
243
					buttonInfo['active']=not buttonInfo['active']
244
					if buttonInfo['active']==true then
245
						buttonInfo['function'](true)
246
					else
247
						buttonInfo['function'](false)
248
					end
249
					drawAllButtons()
250
				else
251
					buttonInfo['function']()
252
					buttonInfo['active']=true
253
					drawAllButtons()
254
					sleep(0.5)
255
					if buttonInfo~=nil then
256
						buttonInfo['active']=false
257
					end	
258
					drawAllButtons()
259
				end
260
				
261
			end
262
		end	
263
	elseif event=="monitor_resize" then
264
		if side==monitorSide then
265
			setMonitorSide(side)
266
			drawAllButtons()
267
		end
268
	end
269
end
270
271
--[[
272
function updateButtonPositions()
273
	for buttonID,buttonInfo in pairs(buttons) do
274
		if buttonInfo['relative']==true then
275
			x=
276
			y=
277
			width=
278
			height=
279
			x=math.floor(x*screenx+0.5)
280
			y=math.floor(y*screeny+0.5)
281
			width=math.floor(width*screenx+0.5)
282
			height=math.floor(height*screeny+0.5)
283
		end		
284
	end
285
end
286
]]
287
288
function setMonitorSide(side)
289
	if side==nil then
290
		if debugMode==true then
291
			print("Not all required arguments are specified.")
292
		end
293
		return false,"Not all required arguments are specified."
294
	else
295
		monitor=peripheral.wrap(side)
296
		screenx,screeny=monitor.getSize()
297
		monitorSide=side
298
	end
299
end
300
301
function setDebugMode(bool)
302
	if bool==nil then
303
		if debugMode==true then
304
			print("Not all required arguments are specified.")
305
		end
306
		return false,"Not all required arguments are specified."
307
	else
308
		debugMode=bool
309
	end
310
end
311
312
function setIdleColor(color)
313
	if color==nil then
314
		if debugMode==true then
315
			print("Not all required arguments are specified.")
316
		end
317
		return false,"Not all required arguments are specified."
318
	else
319
		idleColor=color
320
		drawAllButtons()
321
	end
322
end
323
324
function setActiveColor(color)
325
	if color==nil then
326
		if debugMode==true then
327
			print("Not all required arguments are specified.")
328
		end
329
		return false,"Not all required arguments are specified."
330
	else
331
		activatedColor=color
332
		drawAllButtons()
333
	end
334
end
335
336
function setBackgroundColor(color)
337
	if color==nil then
338
		if debugMode==true then
339
			print("Not all required arguments are specified.")
340
		end
341
		return false,"Not all required arguments are specified."
342
	else
343
		backgroundColor=color
344
		drawAllButtons()
345
	end
346
end
347
348
function setTextColor(color)
349
	if color==nil then
350
		if debugMode==true then
351
			print("Not all required arguments are specified.")
352
		end
353
		return false,"Not all required arguments are specified."
354
	else
355
		textColor=color
356
		drawAllButtons()
357
	end
358
end