View difference between Paste ID: FFaMyXYV and YgrL3hPc
SHOW: | | - or go back to the newest paste.
1
local function main()
2
  --Data that gets saved to a file.
3
  local legend = {
4
    color = {},
5
    pattern = {},
6
    name = {},
7
    vendor = {},
8
    blurb = {}
9
  }
10
  --Data that is constant, never changes.
11
  local m = peripheral.wrap("left") -- Monitor Controller
12
  local p = peripheral.wrap("right") -- Printer Controller
13
  local t = term.current() -- Terminal Controller
14
  local mx, my = m.getSize() -- Size of Monitor
15
  local tx, ty = t.getSize() -- Size of Terminal
16
  local color = {
17
    [0] = "White", [1] = "Orange", [2] = "Magenta", [3] = "Light Blue", [4] = "Yellow", [5] = "Lime", [6] = "Pink", [7] = "Gray",
18
    [8] = "Light Gray", [9] = "Cyan", [10] = "Purple", [11] = "Blue", [12] = "Brown", [13] = "Green", [14] = "Red", [15] = "Black"
19
  }
20
  local pattern = {[0] = "Solid", [1] = "Horizontal Stripe", [2] = "Vertical Stripe", [3] = "Checkerboard"}
21
  --Data that is variable, changes based on user input.
22
  local termScroll = 0
23
  local monScroll = 0
24
  local monScreen = 0
25
  local editing = 0
26
  local editMode = "nil"
27
  local editLegend = {
28
    color = {0,0},
29
    pattern = 0,
30
    name = "",
31
    vendor = "",
32
    blurb = ""
33
  }
34
  local xPos, yPos = 0, 0
35
  local deleting = 0
36
  local sel = 0
37
  local printerError = 0
38
  local pCoolDown = 10
39
  local coolDownTimer = os.startTimer(10)
40
  local helper = 0
41
42
  if fs.exists("mapLegend.sav") then -- Loads the legend data from the save file if it exists.
43
    local file = fs.open("mapLegend.sav","r")
44
    legend = textutils.unserialize(file.readAll())
45
    file.close()
46
  end
47
48
  while true do
49
    -- Terminal Display
50
    if editing > 0 then xPos, yPos = editItem(editLegend, t, color, pattern, editMode)
51
    else
52
      if helper > 0 then helpScreen(helper, t)
53
      else
54
        drawTerm(termScroll, legend, t, sel)
55
        if deleting > 0 and deleting-termScroll >= 1 and deleting-termScroll <= ty-1 then
56
          term.setCursorPos(tx-6,deleting-termScroll)
57
          term.blit("Delete","000000","eeeeee")
58
        end
59
      end
60
    end
61
    -- Monitor Display
62
    if monScreen > 0 then
63
      if printerError > 0 then drawPrintError(printerError, m)
64
      else drawSub(monScreen, legend, m, pCoolDown) end
65
    else drawMain(monScroll, legend, m) end
66
67
    local event = {os.pullEvent()} -- Catches system events such as timers or user input
68
    if event[1] == "mouse_up" and event[2] == 1 then -- Releasing left mouse click
69
      if editing > 0 then
70
        if event[4] == 2 then
71
          editMode = "nil"
72
          if event[3] == 16 then
73
            editLegend.color[1] = editLegend.color[1] - 1
74
            if editLegend.color[1] < 0 then editLegend.color[1] = 15 end
75
          elseif event[3] == 34 then
76
            editLegend.color[1] = editLegend.color[1] + 1
77
            if editLegend.color[1] > 15 then editLegend.color[1] = 0 end
78
          end
79
        elseif event[4] == 3 and editLegend.pattern > 0 then
80
          editMode = "nil"
81
          if event[3] == 16 then
82
            editLegend.color[2] = editLegend.color[2] - 1
83
            if editLegend.color[2] < 0 then editLegend.color[2] = 15 end
84
          elseif event[3] == 34 then
85
            editLegend.color[2] = editLegend.color[2] + 1
86
            if editLegend.color[2] > 15 then editLegend.color[2] = 0 end
87
          end
88
        elseif event[4] == 4 then
89
          editMode = "nil"
90
          if event[3] == 16 then
91
            editLegend.pattern = editLegend.pattern - 1
92
            if editLegend.pattern < 0 then editLegend.pattern = #pattern end
93
          elseif event[3] == 34 then
94
            editLegend.pattern = editLegend.pattern + 1
95
            if editLegend.pattern > #pattern then editLegend.pattern = 0 end
96
          end
97
        elseif event[4] >= 8 and event[4] <= 9 and event[3] >= 2 and event[3] <= 14 then editMode = "name"
98
        elseif event[4] >= 12 and event[4] <= 13 and event[3] >= 2 and event[3] <= 14 then editMode = "vendor"
99
        elseif event[4] >= 8 and event[4] <= 13 and event[3] >= 17 and event[3] <= 34 then editMode = "blurb"
100
        elseif event[4] == 15 then
101
          editMode = "nil"
102
          if event[3] >= 2 and event[3] <= 5 then legend, editing = doneEdit(legend, editLegend, editing)
103
          elseif event[3] >= 9 and event[3] <= 14 then
104
            editing = 0
105
            term.setCursorBlink(false)
106
          end
107
        else editMode = "nil"
108
        end
109
      else
110
        if not (event[3] >= tx-6 and event[3] <= tx-1 and event[4] == deleting-termScroll) then deleting = 0 end
111
        if helper > 0 then
112
          if event[3] >= 2 and event[3] <= 10 and event[4] == 11 and helper == 1 then helper = 2
113
          elseif event[3] >= 2 and event[3] <= 16 and event[4] == 13 and helper == 1 then helper = 3
114
          elseif event[3] >= 2 and event[3] <= 8 and event[4] == 15 and helper == 1 then helper = 4
115
          elseif event[3] >= 1 and event[3] <= 6 and event[4] == ty then
116
            if helper > 1 then helper = 1
117
            else helper = 0 end
118
          end
119
        else
120
          if sel > 0 then sel = 0
121
          else
122
            if event[4] < ty then
123
              if event[3] >= tx-11 and event[3] <= tx-8 and event[4]+termScroll <= #legend.color then
124
                editing = event[4]+termScroll
125
                editLegend = {
126
                  color = {0,0},
127
                  pattern = legend.pattern[editing],
128
                  name = legend.name[editing],
129
                  blurb = legend.blurb[editing],
130
                  vendor = legend.vendor[editing]
131
                }
132
                editLegend.color = convertColors(legend.color[editing], editLegend.color)
133
              elseif event[3] >= tx-6 and event[3] <= tx-1 then
134
                if deleting > 0 then
135
                  if event[4] == deleting-termScroll then
136
                    table.remove(legend.color,deleting)
137
                    table.remove(legend.pattern,deleting)
138
                    table.remove(legend.name,deleting)
139
                    table.remove(legend.vendor,deleting)
140
                    table.remove(legend.blurb,deleting)
141
                    saveData(legend)
142
                    if monScreen == deleting then monScreen = 0 end
143
                  end
144
                  deleting = 0
145
                elseif event[4]+termScroll <= #legend.color then
146
                  deleting = event[4] + termScroll
147
                end
148
              end
149
            else
150
              if event[3] >= 2 and event[3] <= 5 then
151
                t.clear()
152
                t.setCursorPos(1,1)
153
                m.clear()
154
                m.setCursorPos(1,1)
155
                error()
156
              elseif event[3] >= 7 and event[3] <= 12 then
157
                os.reboot()
158
              elseif event[3] >= math.floor(tx/2)-3 and event[3] <= math.floor(tx/2)-1 and termScroll > 0 then
159
                termScroll = termScroll - ty + 1
160
                if termScroll < 0 then termScroll = 0 end
161
              elseif event[3] >= math.floor(tx/2)+1 and event[3] <= math.floor(tx/2)+3 and termScroll+ty-1 < #legend.color then
162
                termScroll = termScroll + ty - 1
163
                if termScroll+ty-1 > #legend.color then termScroll = #legend.color - ty + 1 end
164
              elseif event[3] >= tx-11 and event[3] <= tx-8 then helper = 1
165
              elseif event[3] >= tx-6 and event[3] <= tx-1 then
166
                editing = #legend.color+1
167
                editLegend = {
168
                  color = {0,0},
169
                  pattern = 0,
170
                  name = "",
171
                  blurb = "",
172
                  vendor = ""
173
                }
174
              end
175
            end
176
          end
177
        end
178
      end
179
    elseif event[1] == "mouse_click" and event[2] == 1 and event[3] < tx-11 and event[4] < ty then sel = event[4]+termScroll -- Pressing left mouse click
180
    elseif event[1] == "mouse_drag" and event[2] == 1 and event[4] < ty and editing == 0 and sel > 0 and event[4]+termScroll > 0 and event[4]+termScroll <= #legend.color then
181
      -- Dragging left mouse click
182
      if event[4]+termScroll > sel then
183
        table.insert(legend.color,event[4]+termScroll+1,legend.color[sel])
184
        table.insert(legend.pattern,event[4]+termScroll+1,legend.pattern[sel])
185
        table.insert(legend.name,event[4]+termScroll+1,legend.name[sel])
186
        table.insert(legend.vendor,event[4]+termScroll+1,legend.vendor[sel])
187
        table.insert(legend.blurb,event[4]+termScroll+1,legend.blurb[sel])
188
        table.remove(legend.color,sel)
189
        table.remove(legend.pattern,sel)
190
        table.remove(legend.name,sel)
191
        table.remove(legend.vendor,sel)
192
        table.remove(legend.blurb,sel)
193
      elseif event[4]+termScroll < sel then
194
        table.insert(legend.color,event[4]+termScroll,legend.color[sel])
195
        table.insert(legend.pattern,event[4]+termScroll,legend.pattern[sel])
196
        table.insert(legend.name,event[4]+termScroll,legend.name[sel])
197
        table.insert(legend.vendor,event[4]+termScroll,legend.vendor[sel])
198
        table.insert(legend.blurb,event[4]+termScroll,legend.blurb[sel])
199
        table.remove(legend.color,sel+1)
200
        table.remove(legend.pattern,sel+1)
201
        table.remove(legend.name,sel+1)
202
        table.remove(legend.vendor,sel+1)
203
        table.remove(legend.blurb,sel+1)
204
      end
205
      if monScreen > 0 then
206
        if monScreen > sel and monScreen <= event[4]+termScroll then monScreen = monScreen - 1
207
        elseif monScreen >= event[4]+termScroll and monScreen < sel then monScreen = monScreen + 1
208
        elseif monScreen == sel then monScreen = event[4]+termScroll end
209
      end
210
      sel = event[4]+termScroll
211
      saveData(legend)
212
    elseif event[1] == "mouse_scroll" and editing == 0 and ((event[2] == 1 and termScroll+ty-1 < #legend.color) or (event[2] == -1 and termScroll > 0)) then
213
      -- Scroll Wheel
214
      termScroll = termScroll + event[2]
215
    elseif event[1] == "monitor_touch" and event[2] == "left" then -- Left monitor user touch
216
      if monScreen > 0 then
217
        if printerError > 0 and event[3] >= mx-3 and event[3] <= mx and event[4] == my then printerError = 0
218
        else
219
          if event[4] == my then
220
            if event[3] >= 1 and event[3] <= 3 then monScreen = 0
221
            elseif event[3] >= 5 and event[3] <= 7 and monScreen > 1 then
222
              monScreen = monScreen - 1
223
              if monScreen < monScroll+1 then monScroll = monScroll - my + 1 end
224
            elseif event[3] >= 9 and event[3] <= 11 and monScreen < #legend.color then
225
              monScreen = monScreen + 1
226
              if monScreen > monScroll+my-1 then monScroll = monScroll + my - 1 end
227
            elseif event[3] >= mx-4 and event[3] <= mx then
228
              if p.getPaperLevel() == 0 then
229
                if p.getInkLevel() == 0 then printerError = 3
230
                else printerError = 1 end
231
              elseif p.getInkLevel() == 0 then printerError = 2
232
              elseif pCoolDown == 0 then
233
                local colorIndex = convertColors(legend.color[monScreen],{0,0})
234
                local colorPattern = ""
235
                p.newPage()
236
                local xSize,ySize = p.getPageSize()
237
                p.setPageTitle(legend.name[monScreen].." Info Sheet")
238
                pPrint(legend.name[monScreen].." Info Sheet",1,1,xSize,ySize,p)
239
                colorPattern = color[colorIndex[1] ]
240
                if legend.pattern[monScreen] > 0 then colorPattern = colorPattern.." & "..color[colorIndex[2] ] end
241
                colorPattern = colorPattern.." "..pattern[legend.pattern[monScreen]].." Roof"
242
                pPrint(colorPattern,1,4,xSize,ySize,p)
243
                p.setCursorPos(1,7)
244
                p.write("Vendor:")
245
                pPrint(legend.vendor[monScreen],1,8,xSize,ySize,p)
246
                p.setCursorPos(1,11)
247
                p.write("Description:")
248
                pPrint(legend.blurb[monScreen],1,12,xSize,ySize,p)
249
                p.endPage()
250
                printerError = 4
251
                pCoolDown = 10
252
                coolDownTimer = os.startTimer(10)
253
              end
254
            end
255
          end
256
        end
257
      else
258
        if event[4] < my and event[4]+monScroll <= #legend.color then monScreen = event[4] + monScroll
259
        elseif event[4] == my then
260
          if event[3] >= 5 and event[3] <= 7 and monScroll > 0 then monScroll = monScroll - my + 1
261
          elseif event[3] >= 9 and event[3] <= 11 and monScroll+my-1 < #legend.color then monScroll = monScroll + my -1
262
          end
263
        end
264
      end
265
    elseif event[1] == "char" and editing > 0 then -- Character input from keyboard
266
      if editMode == "name" and not (xPos == 14 and yPos == 9) then editLegend.name = editLegend.name..event[2]
267
      elseif editMode == "vendor" and not (xPos == 14 and yPos == 13) then editLegend.vendor = editLegend.vendor..event[2]
268
      elseif editMode == "blurb" and not (xPos == 34 and yPos == 13) then editLegend.blurb = editLegend.blurb..event[2]
269
      end
270
    elseif event[1] == "key" then -- Non-character input from keyboard
271
      if editing > 0 then
272
        if event[2] == keys.backspace then
273
          if editMode == "name" then editLegend.name = string.sub(editLegend.name,1,string.len(editLegend.name)-1)
274
          elseif editMode == "vendor" then editLegend.vendor = string.sub(editLegend.vendor,1,string.len(editLegend.vendor)-1)
275
          elseif editMode == "blurb" then editLegend.blurb = string.sub(editLegend.blurb,1,string.len(editLegend.blurb)-1)
276
          end
277
        elseif event[2] == keys.tab then
278
          if editMode == "name" then editMode = "vendor"
279
          elseif editMode == "vendor" then editMode = "blurb"
280
          elseif editMode == "blurb" then editMode = "nil"
281
          elseif editMode == "nil" then editMode = "name"
282
          end
283
        elseif event[2] == keys.enter then legend, editing = doneEdit(legend, editLegend, editing)
284
        end
285
      else
286
        if event[2] == keys.pageUp then
287
          termScroll = termScroll - ty + 1
288
          if termScroll < 0 then termScroll = 0 end
289
        elseif event[2] == keys.pageDown then
290
          termScroll = termScroll + ty - 1
291
          if termScroll+ty-1 > #legend.color then termScroll = #legend.color - ty + 1 end
292
        end
293
      end
294
    elseif event[1] == "timer" and event[2] == coolDownTimer then pCoolDown = 0 -- Printer cooldown timer
295
    elseif event[1] == "term_resize" or event[1] == "monitor_resize" then -- Retrieves the size of the monitor and terminal if the size of either changes
296
      mx, my = m.getSize()
297
      tx, ty = t.getSize()
298
    end
299
  end
300
end
301
302
function drawTerm(scrollPos, legend, t, sel) -- Draws the main menu on the terminal
303
  local oldTerm = term.redirect(t)
304
  local xSize, ySize = term.getSize()
305
  term.setBackgroundColor(colors.black)
306
  term.setTextColor(colors.white)
307
  term.clear()
308
309
  for a = 1, ySize-1 do
310
    term.setCursorPos(1,a)
311
    if a+scrollPos <= #legend.color then
312
      local hexCol = "f"
313
      if sel == a+scrollPos then
314
        term.setBackgroundColor(colors.blue)
315
        term.clearLine()
316
        hexCol = "b"
317
      else term.setBackgroundColor(colors.black) end
318
      drawSymbol(legend.color[a+scrollPos], legend.pattern[a+scrollPos])
319
      write(" "..legend.name[a+scrollPos])
320
      term.setCursorPos(xSize-11,a)
321
      term.blit("Edit Delete","fffffffffff","8888"..hexCol.."888888")
322
    end
323
  end
324
  term.setCursorPos(2,ySize)
325
  term.blit("Exit Reboot","fffffffffff","8888f888888")
326
  term.setCursorPos(math.floor(xSize/2)-3,ySize)
327
  if scrollPos == 0 then term.blit(" \30  ","888f","777f")
328
  else term.blit(" \30  ","ffff","888f") end
329
  if scrollPos+ySize-1 >= #legend.color then term.blit(" \31 ","888","777")
330
  else term.blit(" \31 ","fff","888") end
331
  term.setCursorPos(xSize-11,ySize)
332
  term.blit("Help New...","fffffffffff","8888f888888")
333
334
  term.redirect(oldTerm)
335
end
336
337
function editItem(legend, t, color, pattern, editMode) -- Draws the new/edit screen on the teriminal
338
  local oldTerm = term.redirect(t)
339
  local mainTerm = term.current()
340
  local xSize, ySize = term.getSize()
341
  local xPos, yPos = 0, 0
342
  local name = window.create(term.current(),2,8,13,2)
343
  local vendor = window.create(term.current(),2,12,13,2)
344
  local blurb = window.create(term.current(),17,8,18,6)
345
  term.setCursorBlink(false)
346
  term.setBackgroundColor(colors.black)
347
  term.setTextColor(colors.white)
348
  term.clear()
349
  name.setBackgroundColor(colors.white)
350
  name.setTextColor(colors.black)
351
  name.clear()
352
  vendor.setBackgroundColor(colors.white)
353
  vendor.setTextColor(colors.black)
354
  vendor.clear()
355
  blurb.setBackgroundColor(colors.white)
356
  blurb.setTextColor(colors.black)
357
  blurb.clear()
358
359
  drawPattern({2^legend.color[1], 2^legend.color[2]}, legend.pattern, 2, 2)
360
  for a = 1, 2 do
361
    if a == 1 or (a == 2 and legend.pattern > 0) then
362
      term.setCursorPos(7,a+1)
363
      term.setBackgroundColor(colors.black)
364
      term.setTextColor(colors.white)
365
      write("Color "..a..": ")
366
      paintutils.drawLine(17, a+1, 33, a+1, colors.white)
367
      term.setCursorPos(16, a+1)
368
      term.blit("\17","f","8")
369
      term.setTextColor(colors.black)
370
      write(color[legend.color[a] ])
371
      term.setCursorPos(34, a+1)
372
      term.blit("\16","f","8")
373
    end
374
  end
375
  term.setCursorPos(7,4)
376
  term.setBackgroundColor(colors.black)
377
  term.setTextColor(colors.white)
378
  write("Pattern: ")
379
  paintutils.drawLine(17, 4, 33, 4, colors.white)
380
  term.setCursorPos(16, 4)
381
  term.blit("\17","f","8")
382
  term.setTextColor(colors.black)
383
  write(pattern[legend.pattern ])
384
  term.setCursorPos(34, 4)
385
  term.blit("\16","f","8")
386
  term.setCursorPos(7,5)
387
  term.setBackgroundColor(colors.black)
388
  term.setTextColor(colors.white)
389
  write("Symbol: ")
390
  drawSymbol({2^legend.color[1], 2^legend.color[2]}, legend.pattern)
391
  term.setCursorPos(2,7)
392
  write("Name:")
393
  term.setCursorPos(2,11)
394
  write("Vendor:")
395
  term.setCursorPos(17,7)
396
  write("Description:")
397
  term.setCursorPos(2,15)
398
  term.blit("Save   Cancel","fffffffffffff","8888fff888888")
399
400
  term.redirect(name)
401
  term.setCursorPos(1,1)
402
  write(legend.name)
403
  if editMode == "name" then xPos, yPos = t.getCursorPos() end
404
  term.redirect(vendor)
405
  term.setCursorPos(1,1)
406
  write(legend.vendor)
407
  if editMode == "vendor" then xPos, yPos = t.getCursorPos() end
408
  term.redirect(blurb)
409
  term.setCursorPos(1,1)
410
  write(legend.blurb)
411
  if editMode == "blurb" then xPos, yPos = t.getCursorPos() end
412
413
  if xPos > 0 and yPos > 0 then
414
    t.setCursorPos(xPos, yPos)
415
    t.setCursorBlink(true)
416
    t.setTextColor(colors.black)
417
  end
418
  term.redirect(oldTerm)
419
  return xPos, yPos
420
end
421
422
function helpScreen(h, t) -- Draws the help screens on the terminal
423
  local oldTerm = term.redirect(t)
424
  local xSize,ySize = term.getSize()
425
  term.setCursorPos(1,1)
426
  term.setBackgroundColor(colors.black)
427
  term.clear()
428
429
  if h == 1 then
430
    term.setTextColor(colors.white)
431
    print("This is mapLegend.lua, a program made by Raiu for Drako that displays an interactive legend for the town map with info about each location.\n")
432
    print("This section is for the adiministrator to look up how to use this program, as there are some features less obvious than others.\n")
433
    print("Click any of the buttons below to get started.")
434
    term.setCursorPos(1,11)
435
    term.blit(" Main Menu","ffffffffff","f888888888")
436
    print("\n")
437
    term.blit(" New/Edit Screen","ffffffffffffffff","f888888888888888")
438
    print("\n")
439
    term.blit(" Monitor","ffffffff","f8888888")
440
  elseif h == 2 then
441
    term.setTextColor(colors.cyan)
442
    print("Terminal: Main Menu Guide\n")
443
    term.setTextColor(colors.white)
444
    print("Click the 'New...' button to add a new item.")
445
    term.setTextColor(colors.yellow)
446
    print("Click the 'Edit' button next to any item in the menu to edit its properties.")
447
    term.setTextColor(colors.white)
448
    print("Click the 'Delete' button to mark an item for deletion. Click it again to confirm.")
449
    term.setTextColor(colors.yellow)
450
    print("Click the up and down arrow buttons or press the 'Page Up' or 'Page Down' keys to scroll through the menu in pages.")
451
    term.setTextColor(colors.white)
452
    print("Alternatively, you can use the scroll wheel to scroll one item at a time.")
453
    term.setTextColor(colors.yellow)
454
    print("Click the 'Exit' button to exit and restart the program or run a different program.")
455
    term.setTextColor(colors.white)
456
    print("Click the 'Reboot' button to reboot the computer.")
457
  elseif h == 3 then
458
    term.setTextColor(colors.cyan)
459
    print("Terminal: New/Edit Guide\n")
460
    term.setTextColor(colors.white)
461
    print("Use the left and right arrow buttons to cycle through the color and pattern options. The 2nd color option will be taken way while the pattern option is set to 'Solid'.")
462
    term.setTextColor(colors.yellow)
463
    print("Click on the text box for 'Name', 'Vendor' or 'Description' to start editing them. Use can use the 'Tab' key to cycle through the text boxes.")
464
    term.setTextColor(colors.white)
465
    print("Click the 'Save' button or press the 'Enter' or 'Return' key to save the information to the legend and return to the main menu.")
466
    term.setTextColor(colors.yellow)
467
    print("Click the 'Cancel' button to discard all information and return to the main menu.")
468
  elseif h == 4 then
469
    term.setTextColor(colors.cyan)
470
    print("Monitor: Legend Guide")
471
    term.setTextColor(colors.white)
472
    print("Tap on any item in the legend to view its info.")
473
    term.setTextColor(colors.yellow)
474
    print("If there are more items than can be shown, up and down arrow buttons will appear at the bottom of the screen. Tap those to scroll through the pages.")
475
    term.setTextColor(colors.cyan)
476
    print("\nMonitor: Location Info Guide")
477
    term.setTextColor(colors.white)
478
    print("Tap the left arrow button to return to the legend.")
479
    term.setTextColor(colors.yellow)
480
    print("When there is more than 1 item in the legend, up and down arrow buttons will appear at the bottom of the screen. Tap those to scroll through each location.")
481
    term.setTextColor(colors.white)
482
    print("Tap the 'Print' button to print a page with info about the currently viewed location. The page will be in the storage crate under the monitor.")
483
  end
484
  term.setCursorPos(1,ySize)
485
  term.blit("\27 Back","ffffff","888888")
486
487
  term.redirect(oldTerm)
488
end
489
490
function drawMain(scrollPos, legend, m) -- Draws the Legend on the monitor
491
  local oldTerm = term.redirect(m)
492
  local xSize, ySize = term.getSize()
493
  term.setBackgroundColor(colors.black)
494
  term.setTextColor(colors.white)
495
  term.clear()
496
497
  for a = 1, ySize-1 do
498
    term.setCursorPos(1,a)
499
    if a+scrollPos <= #legend.color then
500
      drawSymbol(legend.color[a+scrollPos], legend.pattern[a+scrollPos])
501
      write(" "..legend.name[a+scrollPos])
502
    end
503
  end
504
505
  if #legend.color > ySize-1 then
506
    term.setCursorPos(5,ySize)
507
    if scrollPos == 0 then term.blit(" \30  ","888f","777f")
508
    else term.blit(" \30  ","ffff","888f") end
509
    if scrollPos+ySize-1 > #legend.color then term.blit(" \31 ","888","777")
510
    else term.blit(" \31 ","fff","888") end
511
  end
512
513
  term.redirect(oldTerm)
514
end
515
516
function drawSub(shop, legend, m, coolDown) -- Draws the location info on the monitor
517
  local oldTerm = term.redirect(m)
518
  local xSize, ySize = term.getSize()
519
  local title = window.create(term.current(),6,1,xSize-6,4)
520
  term.setBackgroundColor(colors.black)
521
  term.setTextColor(colors.white)
522
  term.clear()
523
524
  drawPattern(legend.color[shop], legend.pattern[shop], 1, 1)
525
  term.setCursorPos(1, 6)
526
  write(legend.blurb[shop])
527
528
  term.setCursorPos(1,ySize)
529
  term.blit(" \27 ","fff","888")
530
  if #legend.color > 1 then
531
    term.setCursorPos(5,ySize)
532
    if shop == 1 then term.blit(" \30  ","888f","777f")
533
    else term.blit(" \30  ","ffff","888f") end
534
    if shop == #legend.color then term.blit(" \31 ","888","777")
535
    else term.blit(" \31 ","fff","888") end
536
  end
537
  term.setCursorPos(xSize-4,ySize)
538
  if coolDown > 0 then term.blit("Print","88888","77777")
539
  else term.blit("Print","fffff","88888") end
540
541
  term.redirect(title)
542
  term.setCursorPos(1,1)
543
  term.setTextColor(colors.white)
544
  write(legend.name[shop])
545
  term.setCursorPos(1,3)
546
  term.setTextColor(colors.yellow)
547
  write(legend.vendor[shop])
548
  term.setTextColor(colors.white)
549
550
  term.redirect(oldTerm)
551
end
552
553
function drawPrintError(printerError, m) -- Draws the printer info on the monitor
554
  local oldTerm = term.redirect(m)
555
  local xSize, ySize = term.getSize()
556
  term.setBackgroundColor(colors.black)
557
  term.setTextColor(colors.white)
558
  term.clear()
559
560
  term.setCursorPos(1,1)
561
  if printerError == 1 then write("The printer is out of paper.\n\n\n\n\nPlease contact an administrator.")
562
  elseif printerError == 2 then write("The printer is out of ink.\n\n\n\n\nPlease contact an administrator.")
563
  elseif printerError == 3 then write("The printer is out of paper.\n\nThe printer is also out of ink.\n\nPlease contact an administrator.")
564
  elseif printerError == 4 then print("Your document has been printed. Check the storage crate below the monitor.\n\nIf it did not print, please contact an administrator.")
565
  end
566
  write("")
567
  term.setCursorPos(xSize-3, ySize)
568
  term.blit("Okay","ffff","8888")
569
570
  term.redirect(oldTerm)
571
end
572
573
function drawSymbol(color, pattern) -- Writes the symbol for a location
574
  local symbols = {[0] = " ", [1] = "\140", [2] = "\149", [3] = "\153"}
575
  local oldBack, oldText = term.getBackgroundColor(), term.getTextColor()
576
  term.setBackgroundColor(color[1])
577
  term.setTextColor(color[2])
578
  write(symbols[pattern])
579
  term.setBackgroundColor(oldBack)
580
  term.setTextColor(oldText)
581
end
582
583
function drawPattern(color, pattern, xPos, yPos) -- Draws the pattern for a location
584
  local oldBack = term.getBackgroundColor()
585
  if pattern == 0 then -- Solid
586
    paintutils.drawFilledBox(xPos, yPos, xPos+3, yPos+3, color[1])
587
  elseif pattern == 1 then -- Horizontal Stripe
588
    for a = 0, 3, 2 do
589
      paintutils.drawLine(xPos, yPos+a, xPos+3, yPos+a, color[1])
590
      paintutils.drawLine(xPos, yPos+a+1, xPos+3, yPos+a+1, color[2])
591
    end
592
  elseif pattern == 2 then -- Vertical Stripe
593
    for a = 0, 3, 2 do
594
      paintutils.drawLine(xPos+a, yPos, xPos+a, yPos+3, color[1])
595
      paintutils.drawLine(xPos+a+1, yPos, xPos+a+1, yPos+3, color[2])
596
    end
597
  elseif pattern == 3 then -- Checkerboard
598
    for a = 0, 3 do
599
      for b = 0, 3 do
600
        paintutils.drawPixel(xPos+a, yPos+b, color[(a+b)%2+1])
601
      end
602
    end
603
  end
604
  term.setBackgroundColor(oldBack)
605
end
606
607
function doneEdit(legend, editLegend, editing) -- Applies the edit to an item in the legend
608
  legend.color[editing] = {}
609
  legend.color[editing][1] = 2 ^ editLegend.color[1]
610
  legend.color[editing][2] = 2 ^ editLegend.color[2]
611
  legend.pattern[editing] = editLegend.pattern
612
  legend.name[editing] = editLegend.name
613
  legend.vendor[editing] = editLegend.vendor
614
  legend.blurb[editing] = editLegend.blurb
615
  editing = 0
616
  term.setCursorBlink(false)
617
  saveData(legend)
618
  return legend, editing
619
end
620
621
function convertColors(value, index) -- Conversion of color data for dynamic use
622
  for a = 1, 2 do
623
    for b = 0, 15 do
624
      if 2^b == value[a] then index[a] = b end
625
    end
626
  end
627
  return index
628
end
629
630
function pPrint(text, xPos, yPos, xSize, ySize,p) -- Text wrapper for printer paper
631
  local line, space, start = 0, 0, 1
632
  for a = 1, string.len(text) do
633
    if string.sub(text,a+1,a+1) == " " or a+1 > string.len(text) then space = a+1 end
634
    p.setCursorPos(xPos,yPos+line)
635
    if a%(xSize+1)+1 == xSize+1 then
636
      if start >= space+1 then
637
        p.write(string.sub(text,start,a))
638
        start = a + 1
639
      else
640
        p.write(string.sub(text,start,space-1))
641
        start = space + 1
642
      end
643
      line = line + 1
644
    elseif a == string.len(text) then
645
      p.write(string.sub(text,start,a))
646
    end
647
  end
648
end
649
650
function saveData(legend) -- Saves legend data to a file
651
  local file = fs.open("mapLegend.sav","w")
652
  file.write(textutils.serialize(legend))
653
  file.close()
654
end
655
656
main()