Advertisement
Inksaver

Computercraft Monitor Class

Mar 23rd, 2016
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.54 KB | None | 0 0
  1. -- Creates a monitor class with these methods:
  2. -- Finds the position of the monitor if directly connected
  3. -- Finds the monitor if connected by modem and network cable
  4. -- Get the monitor size
  5. -- Get/Set monitor text scale. Size is re-calculated
  6. -- Get Advanced (Colour)/Normal (Monochrome) status
  7. -- Write to monitor at specific location, text colour and back colour
  8. -- Divide monitor into a grid for calculating monitor_touch locations, eg 4x2
  9. -- Read monitor_touch from either grid coordinates or actual, eg 40x12 cols/rows or 4x2 grid
  10. function clsMonitor()
  11.     clsMonitor = {}
  12.     clsMonitor.__index = clsMonitor
  13.  
  14.     local self = setmetatable({}, clsMonitor)
  15.     self.monitor = {}
  16.     self.x = 0
  17.     self.y = 0
  18.     self.gridX = 1
  19.     self.gridY = 1
  20.     self.width = 0
  21.     self.height = 0
  22.     self.textColour = colors.white
  23.     self.backColour = colors.black
  24.     self.textScale = 1
  25.     self.isColour = false
  26.     self.gridWidth = 1
  27.     self.gridHeight = 1
  28.     self.side = ""
  29.    
  30.     local strSides = {"front","back","top", "bottom","right","left","monitor_0"}
  31.     local bFound = false
  32.     for i = 1, 7 do
  33.         if peripheral.getType(strSides[i]) == 'monitor' then
  34.             self.side = strSides[i]
  35.             bFound = true
  36.             break
  37.         end
  38.     end
  39.     if bFound then
  40.         self.monitor = peripheral.wrap(self.side)
  41.         self.width, self.height = self.monitor.getSize()
  42.         if self.monitor.isColor() then
  43.             self.isColour = true
  44.         end
  45.         print("monitor found: "..self.side..", cols="..self.width..", rows="..self.height)
  46.     end
  47.    
  48.     function clsMonitor.setTextScale(self,  iScale)
  49.         self.monitor.setTextScale(iScale)
  50.         self.width, self.height = self.monitor.getSize()
  51.         print("monitor textScale set: "..iScale..", cols="..self.width..", rows="..self.height)
  52.     end
  53.    
  54.     function clsMonitor.getTextScale(self)
  55.         return self.monitor.getTextScale()
  56.     end
  57.    
  58.     function clsMonitor.clearMonitor(self, fColour, bColour)
  59.         self.monitor.clear()
  60.         self.monitor.setCursorPos(1,1)
  61.         self.textColour = colors.white --white
  62.         self.backColour = colors.black --black
  63.     end
  64.    
  65.     function clsMonitor.write(self, x, y, text, fColour, bColour)
  66.         fColour = fColour or self.textColour
  67.         bColour = bColour or self.backColour
  68.         if self.isColour then
  69.             self.monitor.setTextColor(fColour)
  70.             self.monitor.setBackgroundColor(bColour)
  71.         else
  72.             if fColour > 0 then
  73.                 self.textColour = 1
  74.                 self.backColour = 0
  75.             else
  76.                 self.textColour = 0
  77.                 self.backColour = 1
  78.             end
  79.         end
  80.         self.monitor.setCursorPos(1,1)
  81.         self.monitor.write(text)
  82.         self.monitor.setTextColor(self.textColour)
  83.         self.monitor.setBackgroundColor(self.backColour)
  84.     end
  85.    
  86.     function clsMonitor.getTouch(self)
  87.         local touch = false
  88.        
  89.         while not touch do
  90.             local event, side, x, y  = os.pullEvent()
  91.             if event == "monitor_touch" then
  92.                 touch = true
  93.                 self.x = x
  94.                 self.y = y
  95.             end
  96.         end
  97.        
  98.         return self.x, self.y
  99.     end
  100.    
  101.     function clsMonitor.getGridRef(self)
  102.         local touch = false
  103.        
  104.         while not touch do
  105.             local event, side, x, y  = os.pullEvent()
  106.            
  107.             if event == "monitor_touch" then
  108.                 -- debug
  109.                 -- print(tostring(event)..", x="..tostring(x)..", y="..tostring(y))
  110.                 touch = true
  111.                 for i = self.gridWidth - 1, 0, -1 do
  112.                     if x >= (self.width / self.gridWidth) * i then --eg 4x2 39/4 =10
  113.                         self.gridX = i + 1
  114.                         break
  115.                     end
  116.                 end
  117.                 for i = self.gridHeight - 1, 0 , -1 do
  118.                     if y >= (self.height / self.gridHeight) * i then --eg 4x2 39/4 =10
  119.                         self.gridY = i + 1
  120.                         break
  121.                     end
  122.                 end
  123.             end
  124.             sleep(0.1)
  125.         end
  126.        
  127.         return self.gridX, self.gridY
  128.     end
  129.    
  130.     function clsMonitor.setGrid(self, width, height)
  131.         self.gridWidth = width
  132.         self.gridHeight = height       
  133.         print("Monitor grid set to "..width.."x"..height)
  134.     end
  135.    
  136.     function clsMonitor.drawGrid(self)
  137.         -- write spaces of alternating background colours over the monitor
  138.         local backcolour = colors.white
  139.         -- get the number of columns of each cell
  140.         local numSpaces = math.ceil(self.width / self.gridWidth)
  141.         -- Get the number of rows of each cell
  142.         local numRows = math.ceil(self.height / self.gridHeight)
  143.         -- Create a string of spaces the width of each cell
  144.         local spaceBar = ""
  145.         for i = 1, numSpaces do
  146.             spaceBar = spaceBar.." "
  147.         end
  148.         -- Draw each cell as a series of rows of spaces using alternate black/white colours
  149.         for i = 0, self.gridWidth - 1 do -- run 4x in 4x2 grid
  150.             if i == 0 or i % 2 == 0 then
  151.                 backcolour = colors.white
  152.             else
  153.                 backcolour = colors.black
  154.             end
  155.             for j = 0, self.gridHeight - 1 do -- run 2x in 4x2 grid
  156.                 for square = 1, numRows do
  157.                     self.monitor.setCursorPos(i * (self.width / self.gridWidth) + 1, j * numRows + square)
  158.                     self.monitor.setBackgroundColor(backcolour)
  159.                     self.monitor.write(spaceBar)
  160.                 end
  161.                 -- change background for next square
  162.                 if backcolour == colors.white then
  163.                     backcolour = colors.black
  164.                 else
  165.                     backcolour = colors.white
  166.                 end
  167.             end
  168.         end
  169.     end
  170.  
  171.     return self
  172. end
  173.  
  174. function main()
  175.     -- Example for demonstration/testing purposes
  176.     -- Build a monitor 4 blocks wide x 2 blocks high
  177.     mon = clsMonitor()      -- create monitor object
  178.     mon:clearMonitor(0,1)   -- clear monitor and set text/background colours
  179.     mon:setGrid(4,3)        -- set grid size eg 4 rows x 2 cols (does not have to be same as physical size)
  180.     mon:drawGrid()          -- Draw demo grid of 4x2
  181.     while true do           -- test routine to output coordinates of the grid touched
  182.         local x, y = mon:getGridRef()
  183.         -- mon:write(cursorPos X, cursorPos Y, Text, text colour, background colour)
  184.         mon:write(1, 1, "x="..x..", y="..y, 2, 1)-- write to monitor at 1,1, using orange text on white background
  185.     end
  186. end
  187.  
  188. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement