Guest User

Device Info - v1.2 Cleaned

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