Guest User

Device Info - v1.2

a guest
Aug 10th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.26 KB | None | 0 0
  1.  
  2. --[[
  3.     File name: DeviceInfo.lua
  4.     Author: ItzEthanPlayz_YT
  5.    
  6.     https://realethanplayzdev.github.io/Device%20Info/Device%20Info/
  7.    
  8.     V1.2
  9.    
  10.     Allows platform, screen size, input type, device type, and device orientation detection.
  11. --]]
  12. local module = {}
  13. --// If not on client, errors (because this module only works on the client)
  14. assert(not game:GetService("RunService"):IsServer(), "[DeviceInfo]: DeviceInfo can only be used to the server.")
  15. --// Important services
  16. local userInputService = game:GetService("UserInputService")
  17. local guiService = game:GetService("GuiService")
  18. --// Variables
  19. local lastInputTypeWas
  20. local lastResolutionWas
  21. local lastOrientationWas
  22. local lastGraphicQualityLevelWas
  23. --// Event creation
  24. local inputChanged = Instance.new("BindableEvent")
  25. local resolutionChanged = Instance.new("BindableEvent")
  26. local orientationChanged = Instance.new("BindableEvent")
  27. local graphicsQualityChanged = Instance.new("BindableEvent")
  28. --// Custom Enum creation
  29. module.Enum = {
  30.     PlatformType = {
  31.         Computer = "PlatformType_Computer",
  32.         Console = "PlatformType_Console",
  33.         Mobile = "PlatformType_Mobile"
  34.     },
  35.     InputType = {
  36.         Touchscreen = "InputType_Touchscreen",
  37.         KeyboardMouse = "InputType_KeyboardMouse",
  38.         Gamepad = "InputType_Gamepad",
  39.         VR = "InputType_VR",
  40.         Keyboard = "InputType_Keyboard",
  41.         Mouse = "InputType_Mouse"
  42.     },
  43.     DeviceType = {
  44.         Computer = "DeviceType_Computer",
  45.         Phone = "DeviceType_Phone",
  46.         Tablet = "DeviceType_Tablet",
  47.         Console = "DeviceType_Console",
  48.         TouchscreenComputer = "DeviceType_TouchscreenComputer"
  49.     },
  50.     DeviceOrientation = {
  51.         Landscape = "DeviceOrientation_Landscape",
  52.         Portrait = "DeviceOrientation_Portrait"
  53.     },
  54.     GraphicsQuality = {
  55.         Automatic = "GraphicsQuality_Automatic",
  56.         Level01 = "GraphicsQuality_Level01",
  57.         Level02 = "GraphicsQuality_Level02",
  58.         Level03 = "GraphicsQuality_Level03",
  59.         Level04 = "GraphicsQuality_Level04",
  60.         Level05 = "GraphicsQuality_Level05",
  61.         Level06 = "GraphicsQuality_Level06",
  62.         Level07 = "GraphicsQuality_Level07",
  63.         Level08 = "GraphicsQuality_Level08",
  64.         Level09 = "GraphicsQuality_Level09",
  65.         Level10 = "GraphicsQuality_Level10",
  66.         --[[
  67.         Level11 = "GraphicsQuality_Level11",
  68.         Level12 = "GraphicsQuality_Level12",
  69.         Level13 = "GraphicsQuality_Level13",
  70.         Level14 = "GraphicsQuality_Level14",
  71.         Level15 = "GraphicsQuality_Level15",
  72.         Level16 = "GraphicsQuality_Level16",
  73.         Level17 = "GraphicsQuality_Level17",
  74.         Level18 = "GraphicsQuality_Level18",
  75.         Level19 = "GraphicsQuality_Level19",
  76.         Level20 = "GraphicsQuality_Level20",
  77.         Level21 = "GraphicsQuality_Level21"
  78.         --]]
  79.     }
  80. }
  81. --// Functions
  82. function module.getDeviceScreenSize()
  83.     lastResolutionWas = Vector2.new(game.Workspace.CurrentCamera.ViewportSize.X, game.Workspace.CurrentCamera.ViewportSize.Y)
  84.     return Vector2.new(game.Workspace.CurrentCamera.ViewportSize.X, game.Workspace.CurrentCamera.ViewportSize.Y)
  85. end
  86. function module.getDevicePlatform()
  87.     if guiService:IsTenFootInterface() and userInputService.GamepadEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then
  88.         return module.Enum.PlatformType.Console
  89.     elseif userInputService.TouchEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then
  90.         return module.Enum.PlatformType.Mobile
  91.     else
  92.         return module.Enum.PlatformType.Computer
  93.     end
  94. end
  95. function module.getInputType()
  96.     if userInputService.KeyboardEnabled and userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  97.         lastInputTypeWas = module.Enum.InputType.KeyboardMouse
  98.         return module.Enum.InputType.KeyboardMouse
  99.     elseif userInputService.KeyboardEnabled and not userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  100.         lastInputTypeWas = module.Enum.InputType.Keyboard
  101.         return module.Enum.InputType.Keyboard
  102.     elseif not userInputService.KeyboardEnabled and userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  103.         lastInputTypeWas = module.Enum.InputType.Mouse
  104.         return module.Enum.InputType.Mouse
  105.     elseif not userInputService.KeyboardEnabled and not userInputService.MouseEnabled and userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  106.         lastInputTypeWas = module.Enum.InputType.Gamepad
  107.         return module.Enum.InputType.Gamepad
  108.     elseif userInputService.VREnabled then
  109.         lastInputTypeWas = module.Enum.InputType.VR
  110.         return module.Enum.InputType.VR
  111.     else
  112.         lastInputTypeWas = module.Enum.InputType.Touchscreen
  113.         return module.Enum.InputType.Touchscreen
  114.     end
  115. end
  116. function module.getDeviceOrientation()
  117.     if module.getDevicePlatform() ~= module.Enum.PlatformType.Mobile then return end
  118.     local isPortrait = game.Workspace.Camera.ViewportSize.X < game.Workspace.Camera.ViewportSize.Y
  119.     if isPortrait then
  120.         return module.Enum.DeviceOrientation.Portrait
  121.     else
  122.         return module.Enum.DeviceOrientation.Landscape
  123.     end
  124. end
  125. function module.getDeviceType()
  126.     if guiService:IsTenFootInterface() and userInputService.GamepadEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then
  127.         return module.Enum.DeviceType.Console
  128.     elseif not guiService:IsTenFootInterface() and userInputService.TouchEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then
  129.         local deviceOrientation = module.getDeviceOrientation()
  130.         if deviceOrientation == module.Enum.DeviceOrientation.Landscape then
  131.             if game.Workspace.CurrentCamera.ViewportSize.Y < 600 then
  132.                 return module.Enum.DeviceType.Phone
  133.             else
  134.                 return module.Enum.DeviceType.Tablet
  135.             end
  136.         elseif deviceOrientation ==  module.Enum.DeviceOrientation.Portrait then
  137.             if game.Workspace.CurrentCamera.ViewportSize.X < 600 then
  138.                 return module.Enum.DeviceType.Phone
  139.             else
  140.                 return module.Enum.DeviceType.Tablet
  141.             end
  142.         end
  143.     elseif userInputService.TouchEnabled and userInputService.KeyboardEnabled and userInputService.MouseEnabled then
  144.         return module.Enum.DeviceType.TouchscreenComputer
  145.     else
  146.         return module.Enum.DeviceType.Computer
  147.     end
  148. end
  149. function module.getGraphicsQuality()
  150.     local quality = UserSettings().GameSettings.SavedQualityLevel
  151.     if quality == Enum.SavedQualitySetting.Automatic then
  152.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.Automatic
  153.         return module.Enum.GraphicsQuality.Automatic
  154.     elseif quality == Enum.SavedQualitySetting.QualityLevel1 then
  155.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel1
  156.         return module.Enum.GraphicsQuality.Level01
  157.     elseif quality == Enum.SavedQualitySetting.QualityLevel2 then
  158.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel2
  159.         return module.Enum.GraphicsQuality.Level02
  160.     elseif quality == Enum.SavedQualitySetting.QualityLevel3 then
  161.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel3
  162.         return module.Enum.GraphicsQuality.Level03
  163.     elseif quality == Enum.SavedQualitySetting.QualityLevel4 then
  164.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel4
  165.         return module.Enum.GraphicsQuality.Level04
  166.     elseif quality == Enum.SavedQualitySetting.QualityLevel5 then
  167.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel5
  168.         return module.Enum.GraphicsQuality.Level05
  169.     elseif quality == Enum.SavedQualitySetting.QualityLevel6 then
  170.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel6
  171.         return module.Enum.GraphicsQuality.Level06
  172.     elseif quality == Enum.SavedQualitySetting.QualityLevel7 then
  173.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel7
  174.         return module.Enum.GraphicsQuality.Level07
  175.     elseif quality == Enum.SavedQualitySetting.QualityLevel8 then
  176.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel8
  177.         return module.Enum.GraphicsQuality.Level08
  178.     elseif quality == Enum.SavedQualitySetting.QualityLevel9 then
  179.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel9
  180.         return module.Enum.GraphicsQuality.Level09
  181.     elseif quality == Enum.SavedQualitySetting.QualityLevel10 then
  182.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel10
  183.         return module.Enum.GraphicsQuality.Level10
  184.     end
  185. end
  186. --// Initialize events
  187. userInputService.LastInputTypeChanged:Connect(function()
  188.     if userInputService.KeyboardEnabled and userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  189.         if lastInputTypeWas == module.Enum.InputType.KeyboardMouse then return end
  190.         inputChanged:Fire(module.Enum.InputType.KeyboardMouse)
  191.     elseif userInputService.KeyboardEnabled and not userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  192.         if lastInputTypeWas == module.Enum.InputType.Keyboard then return end
  193.         inputChanged:Fire(module.Enum.InputType.Keyboard)
  194.     elseif not userInputService.KeyboardEnabled and userInputService.MouseEnabled and not userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  195.         if lastInputTypeWas == module.Enum.InputType.Mouse then return end
  196.         inputChanged:Fire(module.Enum.InputType.Mouse)
  197.     elseif not userInputService.KeyboardEnabled and not userInputService.MouseEnabled and userInputService.GamepadEnabled and not userInputService.TouchEnabled then
  198.         if lastInputTypeWas == module.Enum.InputType.Gamepad then return end
  199.         inputChanged:Fire(module.Enum.InputType.Gamepad)
  200.     elseif userInputService.VREnabled then
  201.         if lastInputTypeWas == module.Enum.InputType.VR then return end
  202.         inputChanged:Fire(module.Enum.InputType.VR)
  203.     else
  204.         if lastInputTypeWas == module.Enum.InputType.Touchscreen then return end
  205.         inputChanged:Fire(module.Enum.InputType.Touchscreen)
  206.     end
  207. end)
  208. game.Workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(function()
  209.     --// Size changed
  210.     coroutine.resume(coroutine.create(function()
  211.         if game.Workspace.CurrentCamera.ViewportSize.X == lastResolutionWas.X and game.Workspace.CurrentCamera.ViewportSize.X == lastResolutionWas.Y then return end
  212.         lastResolutionWas = Vector2.new(game.Workspace.CurrentCamera.ViewportSize.X, game.Workspace.CurrentCamera.ViewportSize.Y)
  213.         resolutionChanged:Fire(Vector2.new(game.Workspace.CurrentCamera.ViewportSize.X, game.Workspace.CurrentCamera.ViewportSize.Y))
  214.     end))
  215.    
  216.     --// Orientation changed
  217.     coroutine.resume(coroutine.create(function()
  218.         local newOrientation = module.getDeviceOrientation()
  219.         if newOrientation == lastOrientationWas then return end
  220.         if newOrientation == module.Enum.DeviceOrientation.Portrait then
  221.             orientationChanged:Fire(module.Enum.DeviceOrientation.Portrait)
  222.         elseif newOrientation == module.Enum.DeviceOrientation.Landscape then
  223.             orientationChanged:Fire(module.Enum.DeviceOrientation.Landscape)
  224.         end
  225.     end))
  226. end)
  227. UserSettings().GameSettings.Changed:Connect(function()
  228.     local quality = UserSettings().GameSettings.SavedQualityLevel
  229.     if quality == lastGraphicQualityLevelWas then return end
  230.     if quality == Enum.SavedQualitySetting.Automatic then
  231.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.Automatic
  232.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Automatic)
  233.     elseif quality == Enum.SavedQualitySetting.QualityLevel1 then
  234.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel1
  235.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level01)
  236.     elseif quality == Enum.SavedQualitySetting.QualityLevel2 then
  237.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel2
  238.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level02)
  239.     elseif quality == Enum.SavedQualitySetting.QualityLevel3 then
  240.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel3
  241.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level03)
  242.     elseif quality == Enum.SavedQualitySetting.QualityLevel4 then
  243.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel4
  244.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level04)
  245.     elseif quality == Enum.SavedQualitySetting.QualityLevel5 then
  246.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel5
  247.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level05)
  248.     elseif quality == Enum.SavedQualitySetting.QualityLevel6 then
  249.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel6
  250.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level06)
  251.     elseif quality == Enum.SavedQualitySetting.QualityLevel7 then
  252.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel7
  253.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level07)
  254.     elseif quality == Enum.SavedQualitySetting.QualityLevel8 then
  255.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel8
  256.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level08)
  257.     elseif quality == Enum.SavedQualitySetting.QualityLevel9 then
  258.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel9
  259.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level09)
  260.     elseif quality == Enum.SavedQualitySetting.QualityLevel10 then
  261.         lastGraphicQualityLevelWas = Enum.SavedQualitySetting.QualityLevel10
  262.         graphicsQualityChanged:Fire(module.Enum.GraphicsQuality.Level10)
  263.     end
  264. end)
  265. --// Direct events
  266. module.inputTypeChanged = inputChanged.Event
  267. module.screenSizeChanged = resolutionChanged.Event
  268. module.screenOrientationChanged = orientationChanged.Event
  269. module.graphicsQualityChanged = graphicsQualityChanged.Event
  270. return module
Add Comment
Please, Sign In to add comment