Advertisement
Shazz

MonAPI - ComputerCraft

Jun 21st, 2013
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.09 KB | None | 0 0
  1. --[[
  2.     monAPI v1.0 - Made by Shazz
  3.     This is a simple API for creating 'sections' on monitors.
  4.     Feel free to share and edit to your needs but try to keep this header intact.
  5. ]]
  6.  
  7. -- Variables
  8. local monitors = {}
  9. local hitboxes = {}
  10.  
  11. -- Functions
  12. function wrapMonitor(side)
  13.     if peripheral.getType(side) == "monitor" then
  14.         monitors[side] = {sections = {}; pH = peripheral.wrap(side)}
  15.         for k, v in pairs(monitors[side]["pH"]) do
  16.             monitors[side][k] = v
  17.         end
  18.         monitors[side]["pH"] = nil
  19.  
  20.         monitors[side]["getHitbox"] =
  21.         function(x, y)
  22.             for k, v in pairs(hitboxes) do
  23.                 if x >= v.startX and y >= v.startY and x <= v.endX and y <= v.endY then
  24.                     return v.name, v.section
  25.                 end
  26.             end
  27.             return nil
  28.         end
  29.         monitors[side]["createSection"] =
  30.         function(startX, startY, endX, endY)
  31.             monitor = monitors[side]
  32.             startX = math.floor(startX)
  33.             startY = math.floor(startY)
  34.             endX = math.floor(endX)
  35.             endY = math.floor(endY)
  36.             if endX < startX then
  37.                 error("endX must be greater than startX")
  38.             end
  39.             if endY < startY then
  40.                 error("endY must be greater than startY")
  41.             end
  42.            
  43.             monitor["sections"][#monitor["sections"]+1] = {startX = startX; startY = startY; endX = endX; endY = endY; textColour = colours.white; backgroundColour = colours.black; cursorPosX = 1; cursorPosY = 1;}
  44.             local sectionID = #monitor["sections"]
  45.             local self = monitor["sections"][sectionID]
  46.             local pH = monitor
  47.             local w, h = self.endX - self.startX + 1, self.endY - self.startY + 1
  48.            
  49.             self.realCursorPosX = self.startX + self.cursorPosX - 1
  50.             self.realCursorPosY = self.startY + self.cursorPosY - 1
  51.  
  52.             local functions = {
  53.                 -- getSize
  54.                 getSize = function()
  55.                     return w, h
  56.                 end;
  57.            
  58.                 -- setPos
  59.                 setCursorPos = function(x, y)
  60.                     if type(x) == "number" and type(y) == "number" then
  61.                         if x <= w and y > 0 and y <= h then
  62.                             self.cursorPosX, self.cursorPosY = x, y
  63.                             self.realCursorPosX = self.startX + self.cursorPosX - 1
  64.                             self.realCursorPosY = self.startY + self.cursorPosY - 1
  65.                         else
  66.                             self.cursorPosX, self.cursorPosY = nil, nil
  67.                         end
  68.                     else
  69.                         error("Expected number")
  70.                     end
  71.                 end;
  72.                
  73.                 -- getPos
  74.                 getCursorPos = function()
  75.                     return cursorPosX, cursorPosY
  76.                 end;
  77.                
  78.                 -- textColour + backgroundColour
  79.                 setTextColour = function(colour)
  80.                     self.textColour = colour
  81.                 end;
  82.                
  83.                 setBackgroundColour = function(colour)
  84.                     self.backgroundColour = colour
  85.                 end;
  86.                
  87.                 setTextColor = function(colour)
  88.                     self.textColour = colour
  89.                 end;
  90.                
  91.                 setBackgroundColor = function(colour)
  92.                     self.backgroundColour = colour
  93.                 end;
  94.                
  95.                 -- write
  96.                 write = function(text)
  97.                     if self.cursorPosX and self.cursorPosY then
  98.                         text = string.sub(text, 1, w - self.cursorPosX + 1) .. ""
  99.                         if self.cursorPosX <= 0 then
  100.                             text = string.sub(text, math.abs(self.cursorPosX)+2, #text) .. ""
  101.                             pH.setCursorPos(self.startX, self.realCursorPosY)
  102.                         else
  103.                             pH.setCursorPos(self.realCursorPosX, self.realCursorPosY)
  104.                         end
  105.                         pH.setTextColour(self.textColour)
  106.                         pH.setBackgroundColour(self.backgroundColour)
  107.                         pH.write(text)
  108.                     end
  109.                 end;
  110.                
  111.                 -- clear
  112.                 clear = function()
  113.                     pH.setBackgroundColour(self.backgroundColour)
  114.                     for i=self.startY, self.endY do
  115.                         pH.setCursorPos(self.startX, i)
  116.                         pH.write(string.rep(" ", w))
  117.                     end
  118.                 end;
  119.                
  120.                 -- clearLine
  121.                 clearLine = function()
  122.                     pH.setBackgroundColour(self.backgroundColour)
  123.                     pH.setCursorPos(self.startX, self.realCursorPosY)
  124.                     pH.write(string.rep(" ", w))
  125.                 end;
  126.                
  127.                 -- drawBorder
  128.                 drawBorder = function(horChar, verChar, cornerChar)
  129.                     pH.setTextColour(self.textColour)
  130.                     pH.setBackgroundColour(self.backgroundColour)
  131.                    
  132.                     pH.setCursorPos(self.startX, self.startY)
  133.                     pH.write(string.rep(string.sub(horChar, 1, 1) .. "", w))
  134.                    
  135.                     pH.setCursorPos(self.startX, self.endY)
  136.                     pH.write(string.rep(string.sub(horChar, 1, 1) .. "", w))
  137.                    
  138.                     for i=1, h do
  139.                         pH.setCursorPos(self.startX, self.startY + i - 1)
  140.                         pH.write(string.sub(verChar, 1, 1) .. "")
  141.                         pH.setCursorPos(self.endX, self.startY + i - 1)
  142.                         pH.write(string.sub(verChar, 1, 1) .. "")
  143.                     end
  144.                    
  145.                     pH.setCursorPos(self.startX, self.startY)
  146.                     pH.write(string.sub(cornerChar, 1, 1) .. "")
  147.                     pH.setCursorPos(self.endX, self.startY)
  148.                     pH.write(string.sub(cornerChar, 1, 1) .. "")
  149.                     pH.setCursorPos(self.startX, self.endY)
  150.                     pH.write(string.sub(cornerChar, 1, 1) .. "")
  151.                     pH.setCursorPos(self.endX, self.endY)
  152.                     pH.write(string.sub(cornerChar, 1, 1) .. "")
  153.                 end;
  154.                
  155.                 -- fill
  156.                 fill = function(char)
  157.                     pH.setBackgroundColour(self.backgroundColour)
  158.                     pH.setTextColour(self.textColour)
  159.                     for i=1, h do
  160.                         pH.setCursorPos(self.startX, self.startY + i - 1)
  161.                         pH.write(string.rep(string.sub(char, 1, 1) .. "", w))
  162.                     end
  163.                 end;
  164.                
  165.                 -- getSectionID
  166.                 getSectionID = function()
  167.                     return sectionID
  168.                 end;
  169.                
  170.                 -- registerHitbox
  171.                 registerHitbox = function(name, startX, startY, endX, endY)
  172.                     table.insert(hitboxes, {name = name; section = sectionID; startX = self.startX + startX - 1; startY = self.startY + startY - 1; endX = self.endX + startX - 1; endY = self.startY + endY - 1;})
  173.                 end;
  174.                
  175.                 -- isColour
  176.                 isColour = function()
  177.                     return pH.isColour()
  178.                 end;
  179.                
  180.                 -- isColor
  181.                 isColor = function()
  182.                     return pH.isColour()
  183.                 end;
  184.                
  185.                 -- clearHitbox
  186.                 clearHitbox = function()
  187.                     local toDelete = {}
  188.                     for k, v in pairs(hitboxes) do
  189.                         if v.section == sectionID then
  190.                             toDelete[#toDelete+1] = k
  191.                         end
  192.                     end
  193.                     for k, v in pairs(toDelete) do
  194.                         hitboxes[v] = nil
  195.                     end
  196.                 end;
  197.                
  198.                 -- delete
  199.                 delete = function()
  200.                     monitor["sections"][sectionID] = nil
  201.                 end;
  202.             }
  203.             return functions
  204.         end
  205.         return monitors[side]
  206.     else
  207.         if peripheral.isPresent(side) then
  208.             error("No peripherals on " .. side)
  209.         else
  210.             error("Peripheral on " .. side .. " not monitor")
  211.         end
  212.     end
  213. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement