Guest User

OpenPeripheral MouseAPI

a guest
Jun 15th, 2013
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. --Credit to Mikeemoo for creating OpenPeripheral
  2. --API written by Bubba
  3.  
  4. local function load()
  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 or not bridge then
  23.     print("Ending program...")
  24.     return
  25. end
  26.  
  27. scaleFactor = 3.0
  28. screenX, screenY = 480, 255
  29. mouseX, mouseY = 0,0
  30.  
  31. local allTargets = {}
  32.  
  33. local function checkPlayer(name)
  34.     local target = prox.getTargetDetails(name)
  35.     if not target then
  36.         return false
  37.     end
  38.  
  39.     return target.IsSneaking,
  40.     (scaleFactor * (target.Yaw - allTargets[name].Yaw)/180), -- X percentage
  41.     (scaleFactor * (target.Pitch - allTargets[name].Pitch)/90) -- Y percentage
  42. end
  43.  
  44. local mouseOBJ
  45.  
  46. function startMouse(size, color, opacity, scale)
  47.     for name, data in pairs(prox.getTargets()) do
  48.         allTargets[name] = prox.getTargetDetails(name)
  49.     end
  50.     mouseOBJ = bridge.addBox(1, 1, size,size, color, opacity)
  51.     scaleFactor = scale or scaleFactor
  52.     return true
  53.  
  54. end
  55. function resetMouse()
  56.     for name, data in pairs(prox.getTargets()) do
  57.         allTargets[name] = prox.getTargetDetails(name)
  58.     end
  59. end
  60.  
  61. function setScreenSize(width, height)
  62.     screenX,screenY = width, height
  63. end
  64.  
  65. function setScaleFactor(scale)
  66.     scaleFactor = scale
  67. end
  68.  
  69. function waitForClick(username)
  70.     if not mouseOBJ then
  71.         return false
  72.     end
  73.     while true do
  74.         local isClicked, mX, mY = checkPlayer(username)
  75.         mouseX = math.floor(screenX/2) + math.floor(mX*screenX/2)
  76.         mouseY = math.floor(screenY/2) + math.floor(mY*screenY/2)
  77.         if isClicked then
  78.             return mouseX, mouseY
  79.         end
  80.         mouseOBJ.setX(mouseX)
  81.         mouseOBJ.setY(mouseY)
  82.     end
  83. end
  84.  
  85. function getScreenSize(username)
  86.     for name, target in pairs(prox.getTargets()) do
  87.         allTargets[name] = prox.getTargetDetails(name)
  88.     end
  89.  
  90.     local sX, sY = 1,1
  91.     local screenObject = bridge.addBox(0,0,sX, sY, 0xCC0000, 0.4)
  92.     while true do
  93.         local isClicked, sfX, sfY = checkPlayer(username)
  94.         sX = math.floor(sfX*700)
  95.         sY = math.floor(sfY*400)
  96.  
  97.         screenObject.setWidth(sX)
  98.         screenObject.setHeight(sY)
  99.         if isClicked then
  100.             screenObject.delete()
  101.             return sX, sY
  102.         end
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment