le_Fish

DigMonitor API V4

May 4th, 2020
2,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. Mon = {
  2.     textScale = 0.5,
  3.     textWidth = 15,
  4.     width = 15,
  5.     height = 10,
  6.     data = {},
  7.     screen = 1,
  8.     totalScreens = 0,
  9.     monitor,
  10.     view = "gps"
  11. }
  12.  
  13. function Mon:new()
  14.     if #peripheral.getNames() == 0 then
  15.         error("No peripherals found to check")
  16.     end
  17.     for k, v in pairs(peripheral.getNames()) do
  18.         if peripheral.getType(v) == "monitor" then
  19.             self.monitor = peripheral.wrap(v)
  20.             break
  21.         end
  22.     end
  23.     if not self.monitor then
  24.         error("couldn't find a monitor to wrap")
  25.     end
  26.     setmetatable({}, Mon)
  27.     local x, y = self.monitor.getSize()
  28.     self.width = x
  29.     self.height = y
  30.     self.monitor.setTextScale(self.textScale)
  31.     self.monitor.setCursorPos(1, 1)
  32.     return self
  33. end
  34.  
  35. function Mon:fillScreen(data, multiScreen)
  36.     local height = self.height
  37.     multiScreen = multiScreen or false
  38.  
  39.     if multiScreen then
  40.         height = height - 2
  41.     end
  42.  
  43.     local width = self.width
  44.     local cursorPos = {[1] = 1, [2] = 1}
  45.     local spaces = 3
  46.     local counter = 0
  47.  
  48.     local screenText = ""
  49.     local dummyText = ""
  50.     local workingText = ""
  51.     for k, v in pairs(data) do
  52.         if data[k][2] + 600 > os.clock() then
  53.             workingText = "on"
  54.         elseif data[k][2] + 600 < os.clock() then
  55.             workingText = "off"
  56.         else
  57.             workingText = "un"
  58.         end
  59.         -- 3+1+5+5+7+4+2 = 31 = +1 = 32
  60.         if type(data[k][1]) == "table" then
  61.             dummyText = self:spaceNumber(k, 3) .. ":"
  62.             if self.view == "chunk" then
  63.                 dummyText =
  64.                     dummyText ..
  65.                     self:spaceNumber(math.floor(data[k][1].x / 16), 5) ..
  66.                         "," .. self:spaceNumber(math.floor(data[k][1].y / 16), 5) .. ","
  67.             else
  68.                 dummyText =
  69.                     dummyText .. self:spaceNumber(data[k][1].x, 5) .. "," .. self:spaceNumber(data[k][1].y, 5) .. ","
  70.             end
  71.  
  72.             dummyText =
  73.                 dummyText .. self:spaceNumber(data[k][1].z, 3) .. "-" .. self:spaceNumber(workingText, 4) .. "| "
  74.         else
  75.             dummyText =
  76.                 self:spaceNumber(k, 3) ..
  77.                 ":" .. self:spaceNumber(data[k][1], 15) .. "-" .. self:spaceNumber(workingText, 4) .. "| "
  78.         end
  79.         counter = counter + 1
  80.         screenText = screenText .. dummyText
  81.  
  82.         if counter == math.floor(width / 25) then
  83.             screenText = screenText .. "\n"
  84.             counter = 0
  85.         end
  86.     end
  87.     self.monitor.setCursorPos(1, 1)
  88.     self.monitor.clear()
  89.     local oldTerm = term.redirect(self.monitor)
  90.     write(screenText)
  91.     term.redirect(oldTerm)
  92. end
  93.  
  94. function Mon:spaceNumber(number, totalSpace)
  95.     local spaceText = ""
  96.     local number = number
  97.     if number == nil then
  98.         number = "nan"
  99.     end
  100.     local totalSpace = totalSpace or 0
  101.     spaces = totalSpace - #tostring(number)
  102.     for i = 1, spaces do
  103.         spaceText = spaceText .. " "
  104.     end
  105.     return number .. spaceText
  106. end
  107.  
  108. function Mon:setData(data)
  109.     local something = {}
  110.     for k, v in pairs(data) do
  111.         something[#something + 1] = k
  112.     end
  113.     self.data = data
  114. end
  115. function Mon:addData(data)
  116.     for i = 1, #data do
  117.         self.data[#self.data + 1] = data[i]
  118.     end
  119. end
  120.  
  121. function Mon:showData(screen)
  122.     local displayData = {}
  123.     local columns = math.floor(self.width / 25)
  124.     if columns == 0 then
  125.         columns = 1
  126.     end
  127.  
  128.     local screen = screen or nil
  129.     local rows = self.height
  130.     local totalSpots = (columns * rows) - 5
  131.     if #self.data > totalSpots and self.totalScreens ~= 1 then
  132.         rows = rows - 2
  133.         -- data = {[i] = {1, x, y}}
  134.         if screen then
  135.             self.screen = screen
  136.         end
  137.         self.totalScreens = math.ceil(#self.data / totalSpots)
  138.  
  139.         local multiplier = self.screen - 1
  140.         for i = 1 + (multiplier * totalSpots), (totalSpots + (multiplier * totalSpots)) do
  141.             if self.data[i] ~= nil then
  142.                 if type(self.data[i][3]) == nil then
  143.                     displayData[self.data[i][1]] = {self.data[i][2], self.data[i][3]}
  144.                 else
  145.                     displayData[self.data[i][1]] = {self.data[i][2], self.data[i][3]}
  146.                 end
  147.             end
  148.         end
  149.         self:fillScreen(displayData, true)
  150.         self.monitor.setCursorPos(2, self.height)
  151.         self.monitor.write("PREV")
  152.         local middleText = self.screen .. "/" .. self.totalScreens
  153.         self.monitor.setCursorPos(math.floor((self.width / 2) - (#middleText / 2)), self.height)
  154.         self.monitor.write(tostring(middleText))
  155.         self.monitor.setCursorPos((self.width - 11), self.height)
  156.         self.monitor.write("NEXT")
  157.         self.monitor.setCursorPos((self.width - 5), self.height)
  158.         if self.view == "chunk" then
  159.             self.monitor.write("CHUNK")
  160.         else
  161.             self.monitor.write("GPS")
  162.         end
  163.     else
  164.         for k, v in pairs(self.data) do
  165.             if v[3] then
  166.                 displayData[v[1]] = {v[2], v[3]}
  167.             else
  168.                 displayData[v[1]] = {v[2]}
  169.             end
  170.         end
  171.         self:fillScreen(displayData, false)
  172.         self.monitor.setCursorPos((self.width - 5), self.height)
  173.         if self.view == "chunk" then
  174.             self.monitor.write("GPS  ")
  175.         else
  176.             self.monitor.write("CHUNK")
  177.         end
  178.     end
  179. end
  180.  
  181. function Mon:listen()
  182.     while true do
  183.         local event, side, x, y = os.pullEvent("monitor_touch")
  184.         local button1 = {[1] = true, [2] = true, [3] = true, [4] = true, [5] = true}
  185.         local button2 = {
  186.             [self.width] = true,
  187.             [self.width - 1] = true,
  188.             [self.width - 2] = true,
  189.             [self.width - 3] = true,
  190.             [self.width - 4] = true
  191.         }
  192.         local button3 = {
  193.             [self.width - 5] = true,
  194.             [self.width - 6] = true,
  195.             [self.width - 7] = true,
  196.             [self.width - 8] = true,
  197.             [self.width - 9] = true
  198.         }
  199.         if button1[x] and y == self.height then
  200.             self.screen = self.screen - 1
  201.             if self.screen <= 0 then
  202.                 self.screen = self.totalScreens
  203.             end
  204.             self:showData()
  205.         elseif button2[x] and y == self.height then
  206.             if self.view == "chunk" then
  207.                 self.view = "gps"
  208.             else
  209.                 self.view = "chunk"
  210.             end
  211.             self:showData()
  212.         elseif button3[x] and y == self.height then
  213.             self.screen = self.screen + 1
  214.             if self.screen > self.totalScreens then
  215.                 self.screen = 1
  216.             end
  217.             self:showData()
  218.         end
  219.     end
  220. end
Advertisement
Add Comment
Please, Sign In to add comment