View difference between Paste ID: 4YFvPmmj and wW8JRPGS
SHOW: | | - or go back to the newest paste.
1
local default = {text="???", color=14, colorActive=6, colorText=15, w = 4, h = 3}
2
local addMode = false
3
local selected = 0
4
local square = 4
5
local w,h = term.getSize()
6
local colorName = {colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black}
7
local box = {}
8
9
10
function save()
11
	local file = fs.open("redstone","w")
12
		file.writeLine(textutils.serialize(default))
13
		file.writeLine(textutils.serialize(colorName))
14
		file.writeLine(textutils.serialize(box))
15
	file.close()
16
end
17
18
19
function load()
20
	if fs.exists("redstone") then
21
		local file = fs.open("redstone", "r")
22
			default = textutils.unserialize(file.readLine())
23
			colorName = textutils.unserialize(file.readLine())
24
			box = textutils.unserialize(file.readLine())
25
		file.close()
26
	end
27
end
28
29
30
function drawBox(b,id)
31
	id = id or -1
32
	for i=b.x, b.x+b.w do
33
		for j=b.y, (b.y+b.h-1) do
34
			term.setCursorPos(i, j)
35
			if addMode and selected == id then
36
				if i==b.x and j==b.y then
37
					term.setBackgroundColor(colorName[b.color])
38
				else
39
					if b.color+1 > 14 then
40
						term.setBackgroundColor(colorName[1])
41
					else
42
						term.setBackgroundColor(colorName[b.color+1])
43
					end
44
				end
45
			elseif b.active then
46
				term.setBackgroundColor(colorName[b.colorActive])
47
			else
48
				term.setBackgroundColor(colorName[b.color])
49
			end
50
			term.write(" ")
51
		end
52
	end
53
	term.setCursorPos(math.ceil(b.x+b.w/2-#b.text/2), b.y+math.floor(b.h/2))
54
	term.setTextColor(colorName[b.colorText])
55
	term.write(b.text)
56
end
57
58
59
60
61
function draw()
62
	term.setCursorPos(1,1)
63
	term.setBackgroundColor(colors.black)
64
	term.clear()
65
	
66
67
	if addMode then
68
		term.setTextColor(colors.black)
69
		term.setBackgroundColor(colors.red)
70
		term.setCursorPos(1, 1)
71
		term.write( string.rep(' ', w) )
72
		term.setCursorPos(1, h)
73
		term.write( string.rep(' ', w) )
74
		for i=2,h-1 do
75
			term.setCursorPos(1, i)
76
			term.write(" ")
77
			term.setCursorPos(w, i)
78
			term.write(" ")
79
		end
80
		term.setCursorPos(w-27, h)
81
		term.write("by Hannez04 and UNOBTANIUM")
82
	else
83
		term.setTextColor(colors.red)
84
	end
85
	term.setCursorPos(1, 1)
86
	term.write("+")
87
88
	for id,b in pairs(box) do
89
		drawBox(b,id)
90
	end
91
end
92
93
94
function changeBox()
95
	term.setBackgroundColor(colors.black)
96
	term.setTextColor(colors.white)
97
	term.clear()
98
	term.setCursorPos(7,13)
99
	term.write("Look at the computer screen!")
100
101
	repeat
102
  		term.restore()
103
  		local w, h = term.getSize()
104
 	until w == 51 and h == 19
105
106
 	local b = box[selected]
107
 	while true do
108
	 	term.setBackgroundColor(colors.black)
109
	 	term.clear()
110
	 	for i=19, 19+square do
111
			for j=2, (2+math.ceil(square/2)) do
112
				term.setCursorPos(i, j)
113
				term.setBackgroundColor(colorName[b.color])
114
				term.write(" ")
115
			end
116
		end
117
		term.setTextColor(colorName[b.colorText])
118
		write(b.text, math.ceil(19+square/2-#b.text/2), 2+math.ceil(square/2)-1)
119
		for i=27, 27+square do
120
			for j=2, (2+math.ceil(square/2)) do
121
				term.setCursorPos(i, j)
122
				term.setBackgroundColor(colorName[b.colorActive])
123
				term.write(" ")
124
			end
125
		end
126
		term.setTextColor(colorName[b.colorText])
127
		write(b.text, math.ceil(27+square/2-#b.text/2), 2+math.ceil(square/2)-1)
128
129
	 	term.setTextColor(colors.white)
130
	 	term.setBackgroundColor(colors.black)
131
		write("DELETE",6, 3)
132
		write("BACK",40,3)
133
	 	write("+",20,15)
134
	 	write("+",30,15)
135
	 	write("-",20,17)
136
	 	write("-",30,17)
137
	 	write(b.w+1, 20, 16)
138
	 	write(b.h, 30, 16)
139
	 	write("Text (click to edit): " ..  b.text, 1, 7)
140
	 	write("Text Color:", 1,9)
141
	 	for i=1,15 do
142
	 		term.setCursorPos(15+i*2, 9)
143
	 		term.setBackgroundColor(colorName[i])
144
	 		term.write("  ")
145
	 	end
146
	 	term.setBackgroundColor(colors.black)
147
		term.setCursorPos(1,11)
148
	 	term.write("Backgroundcolor:")
149
	 	for i=1,15 do
150
	 		term.setCursorPos(15+i*2, 11)
151
	 		term.setBackgroundColor(colorName[i])
152
	 		term.write("  ")
153
	 	end
154
	 	term.setBackgroundColor(colors.black)
155
		term.setCursorPos(1,13)
156
	 	term.write("Active Color:")
157
	 	for i=1,15 do
158
	 		term.setCursorPos(15+i*2, 13)
159
	 		term.setBackgroundColor(colorName[i])
160
	 		term.write("  ")
161
	 	end
162
	 	term.setBackgroundColor(colors.black)
163
		term.setTextColor(colors.white)
164
165
166
167
		local event = {os.pullEvent()}
168
		if event[1] == "mouse_click" then
169
			local x,y = event[3], event[4]
170
			if y == 3 and x <= 17 then
171
				table.remove(box, selected)
172
				selected = 0
173
				break
174
			elseif y == 3 and x >= 40 then
175
				break
176
			elseif y == 7 then
177
				b.text = betterRead(23,7)
178
			elseif y == 9 and x >= 17 and x < 47 then
179
				b.colorText = math.ceil((x-16)/2)
180
			elseif y == 11 and x >= 17 and x < 47 then
181
				b.color = math.ceil((x-16)/2)
182
			elseif y == 13 and x >= 17 and x < 47 then
183
				b.colorActive = math.ceil((x-16)/2)
184
			elseif y == 15 then
185
				if x < 25 then
186
					b.w = b.w + 1
187
				elseif x > 25 then
188
					b.h = b.h + 1
189
				end
190
			elseif y == 17 then
191
				if x < 25 and b.w > 1 then
192
					b.w = b.w - 1
193
				elseif x > 25 and b.h > 2 then
194
					b.h = b.h - 1
195
				end
196
			end
197
		end
198
	end
199
	term.clear()
200
	term.setCursorPos(1, 1)
201
	term.write("Look at the monitor!")
202
	local monitor = peripheral.wrap("right")
203
	term.redirect(monitor)
204
	w,h = term.getSize()
205
end
206
207
function write(text,x,y)
208
	term.setCursorPos(x, y)
209
	term.write(tostring(text))
210
end
211
212
function betterRead(x,y) -- DONE 1
213
 	term.setTextColor(colors.lightGray)
214
 	term.setCursorBlink(true)
215
  	local s = tostring(box[selected].text)
216
 
217
  	while true do
218
	    term.setCursorPos(x,y)
219
	    term.write( string.rep(' ', w - x + 1) )
220
	    term.setCursorPos(x,y)
221
	    if s:len()+x < w then
222
	      	term.write(s)
223
	    else
224
	     	term.write(s:sub( s:len() - (w-x-2)))
225
	    end
226
	    local e = { os.pullEvent() }
227
		if e[1] == "mouse_click" then
228
			break
229
		elseif e[1] == "char" then
230
			s = s .. e[2]
231
		elseif e[1] == "key" then
232
			if e[2] == keys.enter then
233
		  		break
234
			elseif e[2] == keys.backspace then
235
		  		s = s:sub( 1, s:len() - 1 )
236
			end
237
		end
238
  	end
239
 
240
  	term.setTextColor(colors.white)
241
  	term.setCursorBlink(false)
242
  	return s
243
end
244
245
function countArray(a)
246
	local elements = 0
247
	for k,v in pairs(a) do
248
		elements = elements + 1
249
	end
250
	return elements
251
end
252
253
function main()
254
	while true do
255
		save()
256
		draw()
257
		local event = {os.pullEvent()}
258
		if event[1] == "monitor_touch" then
259
			local Px,Py = event[3], event[4]
260
			if Px == 1 and Py == 1 then -- toggle between mode
261
				addMode = not addMode
262
				selected = 0
263
			elseif addMode then -- edit
264
				local createNew = true
265
				for id,b in pairs(box) do
266
					if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
267
						if selected == id and b.x==Px and b.y ==Py then
268
							changeBox()
269
							createNew = false
270
							break
271
						elseif selected == id then
272
							createNew = false
273
							selected = 0
274
							break
275
						else
276
							selected = id
277
							createNew = false
278
							break
279
						end
280
					elseif selected == id then
281
						box[selected].x = Px
282
						box[selected].y = Py
283
						createNew = false
284
						break
285
					end
286
				end
287
				if createNew and selected == 0 then
288
					table.insert(box, countArray(box)+1, {x=Px,y=Py,w=default.w,h=default.h,text=default.text,color=default.color,colorActive=default.colorActive,colorText=default.colorText,active=false})
289
				end
290
			else -- activate
291
				for id,b in pairs(box) do
292
					if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
293
						b.active = not b.active
294
						rednet.broadcast("" .. b.text .. tostring(b.active))
295
					end
296
				end
297
			end
298
		end
299
		sleep(0)
300
	end
301
end
302
303
function openRednet()
304
	for _, side in pairs(redstone.getSides()) do
305
      if peripheral.getType(side) == "modem" then
306
        rednet.open(side)
307
      end
308
    end
309
end
310
311
openRednet()
312
313
term.clear()
314
term.setCursorPos(1, 1)
315
term.write("Look at the monitor!")
316
local monitor = peripheral.wrap("right")
317
term.redirect(monitor)
318
w,h = term.getSize()
319
320
if not (w == 39 or h == 26) then
321
	repeat
322
  		term.restore()
323
  		local w, h = term.getSize()
324
 	until w == 51 and h == 19
325
 	term.clear()
326
 	term.setCursorPos(1, 1)
327
	print("You are not using a 4x4 monitor!")
328
else
329
	load()
330
	main()
331
end