-
Notifications
You must be signed in to change notification settings - Fork 435
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
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c57766b
HTML5FrontEnd: Add platform and mobile detection
DleanJeans 108933a
HTML5FrontEnd: Refactor and cache results
DleanJeans bb81af4
HTML5FrontEnd: Refactor and cache results
DleanJeans ce735a7
HTML5FrontEnd: Undo caching browserWidth and browserHeight
DleanJeans 1602b0a
HTML5FrontEnd: Add and remove some platforms
DleanJeans e873a91
HTML5FrontEnd: Add IOSPlatform
DleanJeans 162d77a
HTML5FrontEnd: Add iOS as enum constructor
DleanJeans 19b7c37
browserWidth/Height (get, null) to (get, never)
DleanJeans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
@@ -37,7 +45,7 @@ class HTML5FrontEnd | |
{ | ||
return SAFARI; | ||
} | ||
return UNKNOWN; | ||
return FlxBrowser.UNKNOWN; | ||
} | ||
|
||
private function get_browserPosition():FlxPoint | ||
|
@@ -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 | ||
|
@@ -70,4 +128,17 @@ enum FlxBrowser | |
OPERA; | ||
UNKNOWN; | ||
} | ||
|
||
enum FlxPlatform | ||
{ | ||
WINDOWS; | ||
LINUX; | ||
UNIX; | ||
MAC; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm... |
||
ANDROID; | ||
BLACKBERRY; | ||
WINDOWS_PHONE; | ||
IOS; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems |
||
UNKNOWN; | ||
} | ||
#end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.