Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML5FrontEnd: Add platform and mobile detection #1897

Merged
merged 8 commits into from
Aug 4, 2016
87 changes: 79 additions & 8 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 browserWidth(get, never):Int;
public var browserHeight(get, never):Int;
public var browser(default, null):FlxBrowser;
public var browserWidth(get, null):Int;
public var browserHeight(get, null):Int;
public var browserPosition(get, null):FlxPoint;
public var platform(default, null):FlxPlatform;
public var isMobile(default, null):Bool;

@: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,7 @@ class HTML5FrontEnd
{
return SAFARI;
}
return UNKNOWN;
return FlxBrowser.UNKNOWN;
}

private function get_browserPosition():FlxPoint
Expand All @@ -59,6 +67,56 @@ class HTML5FrontEnd
{
return Browser.window.innerHeight;
}

private function getPlatform():FlxPlatform
{

if (userAgentContains("Win"))
{
return WINDOWS;
}
else if (userAgentContains("Linux")
&& !userAgentContains("Android"))
{
return LINUX;
}
else if (userAgentContains("X11"))
{
return UNIX;
}
else if (userAgentContains("Android"))
{
return ANDROID;
}
else if (userAgentContains("BlackBerry"))
{
return BLACKBERRY;
}
else if (userAgentContains("iPhone")
|| userAgentContains("iPad")
|| userAgentContains("iPod"))
{
return IOS;
}
else if (userAgentContains("IEMobile"))
{
return WINDOWS_PHONE;
}
else return FlxPlatform.UNKNOWN;
}

private inline function getIsMobile():Bool
{
return platform == ANDROID || platform == BLACKBERRY || platform == IOS || platform == WINDOWS_PHONE;
}

private inline function userAgentContains(substring:String, toLowerCase:Bool = false)
{
var userAgent = Browser.navigator.userAgent;
if (toLowerCase)
userAgent = userAgent.toLowerCase();
return FlxStringUtil.contains(userAgent, substring);
}
}

enum FlxBrowser
Expand All @@ -70,4 +128,17 @@ enum FlxBrowser
OPERA;
UNKNOWN;
}

enum FlxPlatform
{
WINDOWS;
LINUX;
UNIX;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does UNIX mean in this context? As I understand, both Linux and Mac are technically UNIX systems. That wouldn't make much sense in an enum though.

MAC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... MAC isn't used anywhere?

ANDROID;
BLACKBERRY;
WINDOWS_PHONE;
IOS;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems "iPhone", "iPad" and "iPod" all end up as IOS. Would it be useful to the able to distuingish them?

UNKNOWN;
}
#end