Guest User

OpenPeripheral MouseAPI

a guest
Jun 16th, 2013
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Credit to Mikeemoo for creating OpenPeripheral
  2. --API written by Bubba
  3.  
  4. local function load() --This function will automatically wrap the peripherals required for it to run
  5.     if not os.loadAPI("ocs/apis/sensor") then
  6.         print("This program requires an OpenCCSensor to run.")
  7.         return false
  8.     end
  9.     local prox
  10.     local bridge
  11.     for k,v in pairs(rs.getSides()) do
  12.         if peripheral.getType(v) == "sensor" then
  13.             prox = sensor.wrap(v)
  14.         elseif peripheral.getType(v) and string.match(peripheral.getType(v),"bridge") then
  15.             bridge = peripheral.wrap(v)
  16.         end
  17.     end
  18.     return prox, bridge
  19. end
  20.  
  21. local prox, bridge = load()
  22. if not prox then
  23.     print("This program requires an OpenCCSensor with a proximity card in order to run")
  24.     return
  25. end
  26. if not bridge then
  27.     print("This program requires a terminal glasses bridge in order to run")
  28.     return
  29. end
  30.  
  31. scaleFactor = 3.0 --This variable controls the speed of the mouse. A higher number indicates a faster speed.
  32. screenX, screenY = 480, 255 --This is the size of your screen. You can adjust this manually or you can use the built-in function, getScreenSize
  33. mouseX, mouseY = 0,0 --These variables keep track of the mouse position as it moves.
  34.  
  35. local allTargets = {} --This will store the initial Yaw of the player so that the mouse can be centered on the screen
  36.  
  37. local function checkPlayer(name) --This function returns the distance from the left side of the screen as a percentage value
  38.     local target = prox.getTargetDetails(name)
  39.     if not target then
  40.         return false
  41.     end
  42.  
  43.     return target.IsSneaking,
  44.     (scaleFactor * (target.Yaw - allTargets[name].Yaw)/180), -- X percentage
  45.     (scaleFactor * (target.Pitch/90) -- Y percentage
  46. end
  47.  
  48. local mouseOBJ --This is where we create our mouse box
  49.  
  50. function startMouse(size, color, opacity, scale) --This populates the allTargets table and creates the mouse box
  51.     for name, data in pairs(prox.getTargets()) do
  52.         allTargets[name] = prox.getTargetDetails(name)
  53.     end
  54.     mouseOBJ = bridge.addBox(1, 1, size,size, color, opacity)
  55.     scaleFactor = scale or scaleFactor
  56.     return true
  57.  
  58. end
  59. function resetMouse() --This recenters the mouse
  60.     for name, data in pairs(prox.getTargets()) do
  61.         allTargets[name] = prox.getTargetDetails(name)
  62.     end
  63. end
  64.  
  65. function setScreenSize(width, height) --Sets the screen size variables
  66.     screenX,screenY = width, height
  67. end
  68.  
  69. function setScaleFactor(scale) --Sets the mouse speed
  70.     scaleFactor = scale
  71. end
  72.  
  73. function waitForClick(username) --Draws/Updates the mouse and when the player sneaks returns their x/y coordinates
  74.     if not mouseOBJ then
  75.         return false
  76.     end
  77.     while true do
  78.         local isClicked, mX, mY = checkPlayer(username)
  79.         mouseX = math.floor(screenX/2) + math.floor(mX*screenX/2)
  80.         mouseY = math.floor(screenY/2) + math.floor(mY*screenY/2)
  81.         if isClicked then
  82.             return mouseX, mouseY
  83.         end
  84.         mouseOBJ.setX(mouseX)
  85.         mouseOBJ.setY(mouseY)
  86.     end
  87. end
  88.  
  89. function getScreenSize(username) --Creates a red box that moves along with the mouse in order to get the screen size easily.
  90.     for name, target in pairs(prox.getTargets()) do
  91.         allTargets[name] = prox.getTargetDetails(name)
  92.     end
  93.  
  94.     local sX, sY = 1,1
  95.     local screenObject = bridge.addBox(0,0,sX, sY, 0xCC0000, 0.4)
  96.     while true do
  97.         local isClicked, sfX, sfY = checkPlayer(username)
  98.         sX = math.floor(sfX*700)
  99.         sY = math.floor(sfY*400)
  100.  
  101.         screenObject.setWidth(sX)
  102.         screenObject.setHeight(sY)
  103.         if isClicked then
  104.             screenObject.delete()
  105.             return sX, sY
  106.         end
  107.     end
  108. end
Advertisement
Add Comment
Please, Sign In to add comment