Skip to content

Commit

Permalink
HTML5FrontEnd: Add platform and mobile detection (HaxeFlixel#1897)
Browse files Browse the repository at this point in the history
  • Loading branch information
DleanJeans authored and Aurel300 committed Apr 17, 2018
1 parent eb74694 commit 4f288e9
Showing 1 changed file with 89 additions and 6 deletions.
95 changes: 89 additions & 6 deletions flixel/system/frontEnds/HTML5FrontEnd.hx
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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<FlxPlatform> = [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
Expand Down Expand Up @@ -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

0 comments on commit 4f288e9

Please sign in to comment.