diff --git a/flixel/system/frontEnds/HTML5FrontEnd.hx b/flixel/system/frontEnds/HTML5FrontEnd.hx index e30ef67a4d..3c73e88e6c 100644 --- a/flixel/system/frontEnds/HTML5FrontEnd.hx +++ b/flixel/system/frontEnds/HTML5FrontEnd.hx @@ -1,27 +1,35 @@ package flixel.system.frontEnds; import flixel.math.FlxPoint; +import flixel.util.FlxStringUtil; #if js import js.Browser; class HTML5FrontEnd { - public var browser(get, never):FlxBrowser; + public var browser(default, null):FlxBrowser; + public var platform(default, null):FlxPlatform; + public var isMobile(default, null):Bool; public var browserWidth(get, never):Int; public var browserHeight(get, never):Int; public var browserPosition(get, null):FlxPoint; @:allow(flixel.FlxG) - private function new() {} + private function new() + { + browser = getBrowser(); + platform = getPlatform(); + isMobile = getIsMobile(); + } - private function get_browser():FlxBrowser + private function getBrowser():FlxBrowser { - if (Browser.navigator.userAgent.indexOf(" OPR/") > -1) + if (userAgentContains(" OPR/")) { return OPERA; } - else if (Browser.navigator.userAgent.toLowerCase().indexOf("chrome") > -1) + else if (userAgentContains("chrome", true)) { return CHROME; } @@ -37,7 +45,64 @@ class HTML5FrontEnd { return SAFARI; } - return UNKNOWN; + return FlxBrowser.UNKNOWN; + } + + private function getPlatform():FlxPlatform + { + + if (userAgentContains("Win")) + { + return WINDOWS; + } + else if (userAgentContains("Mac")) + { + return MAC; + } + else if (userAgentContains("Linux") + && !userAgentContains("Android")) + { + return LINUX; + } + else if (userAgentContains("IEMobile")) + { + return WINDOWS_PHONE; + } + else if (userAgentContains("Android")) + { + return ANDROID; + } + else if (userAgentContains("BlackBerry")) + { + return BLACKBERRY; + } + else if (userAgentContains("iPhone")) + { + return IOS(IPHONE); + } + else if (userAgentContains("iPad")) + { + return IOS(IPAD); + } + else if (userAgentContains("iPod")) + { + return IOS(IPOD); + } + else return FlxPlatform.UNKNOWN; + } + + private inline function getIsMobile():Bool + { + var mobilePlatforms:Array = [ANDROID, BLACKBERRY, WINDOWS_PHONE, IOS(IPHONE), IOS(IPAD), IOS(IPOD)]; + return mobilePlatforms.indexOf(platform) != -1; + } + + private inline function userAgentContains(substring:String, toLowerCase:Bool = false) + { + var userAgent = Browser.navigator.userAgent; + if (toLowerCase) + userAgent = userAgent.toLowerCase(); + return FlxStringUtil.contains(userAgent, substring); } private function get_browserPosition():FlxPoint @@ -70,4 +135,22 @@ enum FlxBrowser OPERA; UNKNOWN; } + +enum FlxPlatform +{ + WINDOWS; + LINUX; + MAC; + ANDROID; + BLACKBERRY; + WINDOWS_PHONE; + IOS(device:FlxIOSDevice); + UNKNOWN; +} +enum FlxIOSDevice +{ + IPHONE; + IPAD; + IPOD; +} #end \ No newline at end of file