SHOW:
|
|
- or go back to the newest paste.
1 | ------------------------------------------------- | |
2 | -- This script is designed to handle a touch | |
3 | -- screen controlled mystcraft portal, and | |
4 | -- requires the Touchpoint API to run (pFHeia96), | |
5 | -- saved as "touchpoint" on the computer. | |
6 | -- | |
7 | -- Most important variables to adjust are listed | |
8 | -- first, and the script assumes that you have | |
9 | -- two chests (one for storage, one for removal), | |
10 | -- as well as this computer and a book receptacle | |
11 | -- adjacent to the storage chest. You also need | |
12 | -- an advanced monitor connected to the computer. | |
13 | ------------------------------------------------- | |
14 | os.loadAPI("touchpoint") --Do not adjust. | |
15 | ||
16 | --Use either the side of the computer touching the | |
17 | --object, or the modem connecting them. | |
18 | local monitorLocation = "back" | |
19 | local chestLocation = "iron_4" | |
20 | ||
21 | --Use compass direction from the storage chest to | |
22 | --the listed object (or up/down). | |
23 | local directionToPortal = "up" | |
24 | local directionToEject = "down" | |
25 | ||
26 | --Primary customization options are below. | |
27 | local textSize = 1 | |
28 | local minimumNameLength = 4 | |
29 | local buttonPadding = 1 --Min 1, Space before/after | |
30 | local centerPadding = 0 --Min 0, Space before/after | |
31 | local idleButtonColor = colors.blue | |
32 | local activeButtonColor = colors.cyan | |
33 | local portalDuration = 5 --In Seconds | |
34 | local showEject = true | |
35 | local showPageNum = true | |
36 | local showTotalPages = true | |
37 | local pageSeparator = " of " | |
38 | ||
39 | --Secondary customization options are below. | |
40 | local drawBorders = false | |
41 | local borderSymbol1 = "-" | |
42 | local borderSymbol2 = "+" | |
43 | local pageBack = "Prev Page" | |
44 | local pageFore = "Next Page" | |
45 | local ejectButton = "Eject" | |
46 | local resetButton = "Refresh" | |
47 | ||
48 | --Used internally. Do not adjust. | |
49 | local mon = peripheral.wrap(monitorLocation) | |
50 | local touch = touchpoint.new(monitorLocation) | |
51 | local chest = peripheral.wrap(chestLocation) | |
52 | local monX = 0 | |
53 | local monY = 0 | |
54 | local monMidL = 0 | |
55 | local monMidR = 0 | |
56 | local maxNameLength = 0 | |
57 | local problem = false | |
58 | local iBC = idleButtonColor | |
59 | local aBC = activeButtonColor | |
60 | local names = {} | |
61 | local curPage = 1 | |
62 | local lastPage = 1 | |
63 | local namesPerPage = 1 | |
64 | local pages = {} | |
65 | local eviction = false | |
66 | local pLabel | |
67 | local event, p1 | |
68 | ||
69 | function centerText(cString) | |
70 | local sSize = string.len(cString) | |
71 | offset = math.floor((monX - sSize) / 2) + 1 | |
72 | return offset | |
73 | end | |
74 | ||
75 | function populateNames() | |
76 | names = {} | |
77 | ||
78 | local invSize = chest.getInventorySize() | |
79 | local stackInfo = chest.getAllStacks() | |
80 | for a=1,invSize + 1,1 do | |
81 | if stackInfo[a] ~= nil then | |
82 | names[#names + 1] = {name = stackInfo[a]["myst_book"]["destination"],slot = a} | |
83 | print(#names..": "..names[#names]["name"]..", "..names[#names]["slot"]) | |
84 | end | |
85 | end | |
86 | end | |
87 | ||
88 | function returnDir(myDir) | |
89 | if myDir == "north" then | |
90 | return "south" | |
91 | elseif myDir == "south" then | |
92 | return "north" | |
93 | elseif myDir == "east" then | |
94 | return "west" | |
95 | elseif myDir == "west" then | |
96 | return "east" | |
97 | elseif myDir == "down" then | |
98 | return "up" | |
99 | elseif myDir == "up" then | |
100 | return "down" | |
101 | else | |
102 | problem = true | |
103 | return "oops..." | |
104 | end | |
105 | end | |
106 | ||
107 | function prepareScreen() | |
108 | mon.clear() | |
109 | mon.setTextScale(textSize) | |
110 | monX, monY = mon.getSize() | |
111 | ||
112 | if (monY < 5) or (monX < (minimumNameLength * 2) + 4) then | |
113 | mon.setBackgroundColor(colors.red) | |
114 | mon.setTextColor(colors.black) | |
115 | mon.setCursorPos(1,1) | |
116 | mon.write("!!!") | |
117 | print("Screen too small with current resolution, please adjust.") | |
118 | problem = true | |
119 | else | |
120 | if math.floor(monX/2) == math.ceil(monX/2) then --Even | |
121 | monMidR = monX/2 + 1 | |
122 | monMidL = monMidR - 1 | |
123 | maxNameLength = (monX - 4)/2 - (centerPadding + buttonPadding) * 2 | |
124 | else --Odd | |
125 | monMidR = math.floor(monX/2) + 1 | |
126 | monMidL = monMidR | |
127 | maxNameLength = (monX - 3)/2 - (centerPadding + buttonPadding) * 2 | |
128 | end | |
129 | namesPerPage = math.ceil((monY - 4) / 2) * 2 | |
130 | end | |
131 | end | |
132 | ||
133 | function checkDirections() | |
134 | if directionToEject == "oops..." then | |
135 | print("Direction to Eject isn't a valid forge direction.") | |
136 | print("Please use north, south, east, west, up or down.") | |
137 | end | |
138 | ||
139 | if directionToPortal == "oops..." then | |
140 | print("Direction to Portal isn't a valid forge direction.") | |
141 | print("Please use north, south, east, west, up or down.") | |
142 | end | |
143 | end | |
144 | ||
145 | function initialPull() | |
146 | chest.pullItemIntoSlot(directionToPortal,1,1,1) | |
147 | end | |
148 | ||
149 | function determineLength(myString) | |
150 | local theLength = 0 | |
151 | if string.len(myString) > maxNameLength then | |
152 | theLength = maxNameLength + buttonPadding * 2 | |
153 | else | |
154 | theLength = string.len(myString) + buttonPadding * 2 | |
155 | end | |
156 | ||
157 | return theLength | |
158 | end | |
159 | ||
160 | function drawRuler(vert) | |
161 | local toggle = true | |
162 | for a=1,monX,1 do | |
163 | mon.setCursorPos(a,vert) | |
164 | if toggle then | |
165 | mon.write(borderSymbol1) | |
166 | toggle = false | |
167 | else | |
168 | mon.write(borderSymbol2) | |
169 | toggle = true | |
170 | end | |
171 | end | |
172 | end | |
173 | ||
174 | function drawLabels() | |
175 | mon.setBackgroundColor(colors.black) | |
176 | ||
177 | if showPageNum then | |
178 | if showTotalPages then | |
179 | pLabel = curPage..pageSeparator..#pages | |
180 | else | |
181 | pLabel = curPage | |
182 | end | |
183 | ||
184 | mon.setCursorPos(centerText(pLabel),1) | |
185 | mon.write(pLabel) | |
186 | end | |
187 | ||
188 | if drawBorders then | |
189 | drawRuler(2) | |
190 | drawRuler(monY - 1) | |
191 | end | |
192 | end | |
193 | ||
194 | function prevPage() | |
195 | lastPage = curPage | |
196 | ||
197 | if eviction then | |
198 | evictBook() | |
199 | end | |
200 | ||
201 | pages[lastPage]:toggleButton(pageBack) | |
202 | drawLabels() | |
203 | os.sleep(0.125) | |
204 | ||
205 | curPage = curPage - 1 | |
206 | if curPage < 1 then | |
207 | curPage = #pages | |
208 | end | |
209 | pages[lastPage]:toggleButton(pageBack) | |
210 | drawLabels() | |
211 | end | |
212 | ||
213 | function nextPage() | |
214 | lastPage = curPage | |
215 | ||
216 | if eviction then | |
217 | evictBook() | |
218 | end | |
219 | ||
220 | pages[lastPage]:toggleButton(pageFore) | |
221 | drawLabels() | |
222 | os.sleep(0.125) | |
223 | ||
224 | curPage = curPage + 1 | |
225 | if curPage > #pages then | |
226 | curPage = 1 | |
227 | end | |
228 | pages[lastPage]:toggleButton(pageFore) | |
229 | pages[curPage]:draw() | |
230 | drawLabels() | |
231 | end | |
232 | ||
233 | function doAction() | |
234 | pages[curPage]:toggleButton(p1) | |
235 | drawLabels() | |
236 | for k,v in ipairs(names) do | |
237 | if v["name"] == p1 then | |
238 | curSlot = v["slot"] | |
239 | break | |
240 | end | |
241 | end | |
242 | ||
243 | if eviction then | |
244 | chest.pushItemIntoSlot(directionToEject,curSlot,1,1) | |
245 | os.sleep(0.125) | |
246 | refresh() | |
247 | eviction = false | |
248 | else | |
249 | chest.pushItemIntoSlot(directionToPortal,curSlot,1,1) | |
250 | os.sleep(portalDuration) | |
251 | chest.pullItemIntoSlot(directionToPortal,1,1,curSlot) | |
252 | pages[curPage]:toggleButton(p1) | |
253 | drawLabels() | |
254 | end | |
255 | end | |
256 | ||
257 | ||
258 | function refresh() | |
259 | print("Collecting Names.") | |
260 | populateNames() | |
261 | ||
262 | pages = {} | |
263 | ||
264 | local myPage = 1 | |
265 | ||
266 | if #names > 0 then | |
267 | while (myPage - 1) * namesPerPage < #names do | |
268 | print("Populating page "..myPage..".") | |
269 | buildPage(myPage) | |
270 | myPage = myPage + 1 | |
271 | end | |
272 | else | |
273 | buildPage(1) | |
274 | end | |
275 | ||
276 | if curPage > #pages then | |
277 | curPage = #pages | |
278 | end | |
279 | ||
280 | pages[curPage]:draw() | |
281 | drawLabels() | |
282 | end | |
283 | ||
284 | function rebuildPages() | |
285 | pages[curPage]:toggleButton(resetButton) | |
286 | drawLabels() | |
287 | refresh() | |
288 | end | |
289 | ||
290 | function evictBook() | |
291 | pages[curPage]:toggleButton(ejectButton) | |
292 | if eviction then | |
293 | eviction = false | |
294 | else | |
295 | eviction = true | |
296 | end | |
297 | drawLabels() | |
298 | end | |
299 | ||
300 | function buildPage(pN) | |
301 | pages[pN] = touchpoint.new(monitorLocation) | |
302 | pages[pN]:add(pageBack, prevPage, 1, 1, determineLength(pageBack), 1, iBC, aBC) | |
303 | pages[pN]:add(pageFore, nextPage, monX - determineLength(pageFore) + 1, 1, monX, 1, iBC, aBC) | |
304 | ||
305 | if #names > 0 then | |
306 | local curIndex = (pN - 1) * namesPerPage + 1 | |
307 | for y = 3, monY - 2, 2 do | |
308 | if curIndex < #names + 1 then | |
309 | pages[pN]:add(names[curIndex]["name"], doAction, 2, y, monMidL - 1 - centerPadding, y, iBC, aBC) | |
310 | else | |
311 | break | |
312 | end | |
313 | curIndex = curIndex + 1 | |
314 | if curIndex < #names + 1 then | |
315 | pages[pN]:add(names[curIndex]["name"], doAction, monMidR + 1 + centerPadding, y, monX - 1, y, iBC, aBC) | |
316 | else | |
317 | break | |
318 | end | |
319 | curIndex = curIndex + 1 | |
320 | end | |
321 | end | |
322 | ||
323 | pages[pN]:add(resetButton, rebuildPages, 1, monY, determineLength(resetButton), monY, iBC, aBC) | |
324 | if showEject then | |
325 | pages[pN]:add(ejectButton, evictBook, monX - determineLength(ejectButton) + 1, monY, monX, monY, iBC, aBC) | |
326 | end | |
327 | end | |
328 | ||
329 | prepareScreen() | |
330 | checkDirections() | |
331 | ||
332 | if not problem then | |
333 | initialPull() | |
334 | refresh() | |
335 | print("Starting main control loop.") | |
336 | while true do | |
337 | pages[curPage]:draw() | |
338 | drawLabels() | |
339 | event, p1 = pages[curPage]:handleEvents(os.pullEvent()) | |
340 | if event == "button_click" then | |
341 | pages[curPage].buttonList[p1].func() | |
342 | end | |
343 | end | |
344 | end |