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
60 changes: 59 additions & 1 deletion flixel/system/frontEnds/HTML5FrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class HTML5FrontEnd
public var browserWidth(get, never):Int;
public var browserHeight(get, never):Int;
public var browserPosition(get, null):FlxPoint;
public var platform(get, never):FlxPlatform;
public var isMobile(get, never):Bool;

@:allow(flixel.FlxG)
private function new() {}
Expand All @@ -37,7 +39,7 @@ class HTML5FrontEnd
{
return SAFARI;
}
return UNKNOWN;
return FlxBrowser.UNKNOWN;
}

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

private inline function get_platform():FlxPlatform
Copy link
Member

Choose a reason for hiding this comment

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

This function is way to long to be a good candidate for inline.

Copy link
Member

Choose a reason for hiding this comment

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

We could probably just run this entire thing once in new() and cache the result? It's not like it's gonna change at runtime, right? :)

{

if (Browser.navigator.userAgent.indexOf("Win") > -1)
Copy link
Member

Choose a reason for hiding this comment

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

Should probably wrap Browser.navigator.userAgent.indexOf() in a helper function, it's used quite often here.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, and instead of indexOf() you can use .contains() via using flixel.util.FlxStringUtil.

{
return WINDOWS;
}
else if (Browser.navigator.userAgent.indexOf("Linux") > -1
&& Browser.navigator.userAgent.indexOf("Android") == -1)
{
return LINUX;
}
else if (Browser.navigator.userAgent.indexOf("X11") > -1)
{
return UNIX;
}
else if (Browser.navigator.userAgent.indexOf("Android") > -1)
{
return ANDROID;
}
else if (Browser.navigator.userAgent.indexOf("BlackBerry") > -1)
{
return BLACKBERRY;
}
else if (Browser.navigator.userAgent.indexOf("iPhone") > -1
|| Browser.navigator.userAgent.indexOf("iPad") > -1
|| Browser.navigator.userAgent.indexOf("iPod") > -1)
{
return IOS;
}
else if (Browser.navigator.userAgent.indexOf("IEMobile") > -1)
{
return WINDOWS_PHONE;
}
else return FlxPlatform.UNKNOWN;
}

private inline function get_isMobile():Bool
{
var platform = this.platform;
return platform == ANDROID || platform == BLACKBERRY || platform == IOS || platform == WINDOWS_PHONE;
}
}

enum FlxBrowser
Expand All @@ -70,4 +115,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