forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCScreen.js
158 lines (143 loc) · 4.92 KB
/
CCScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
* The fullscreen API provides an easy way for web content to be presented using the user's entire screen.
* It's invalid on safari,QQbrowser and android browser
* @class
* @extends cc.Class
*/
cc.Screen = cc.Class.extend({
_supportsFullScreen: false,
// the pre fullscreenchange function
_preOnFullScreenChange: null,
_touchEvent: "",
_fn: null,
// Function mapping for cross browser support
_fnMap: [
[
'requestFullscreen',
'exitFullscreen',
'fullscreenchange',
'fullscreenEnabled',
'fullscreenElement'
],
[
'webkitRequestFullScreen',
'webkitCancelFullScreen',
'webkitfullscreenchange',
'webkitIsFullScreen',
'webkitCurrentFullScreenElement'
],
[
'mozRequestFullScreen',
'mozCancelFullScreen',
'mozfullscreenchange',
'mozFullScreen',
'mozFullScreenElement'
],
[
'msRequestFullscreen',
'msExitFullscreen',
'MSFullscreenChange',
'msFullscreenEnabled',
'msFullscreenElement'
]
],
init: function () {
this._fn = {};
var i, val, map = this._fnMap, valL;
for (i = 0, l = map.length; i < l; i++ ) {
val = map[ i ];
if ( val && val[1] in document ) {
for ( i = 0, valL = val.length; i < valL; i++ ) {
this._fn[ map[0][ i ] ] = val[ i ];
}
break;
}
}
this._supportsFullScreen = (this._fn.requestFullscreen != undefined);
this._touchEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown';
},
/**
* return true if it's full now.
* @returns {Boolean}
*/
fullScreen: function() {
return this._supportsFullScreen && document[ this._fn.fullscreenEnabled ];
},
/**
* change the screen to full mode.
* @param {Element} element
* @param {Function} onFullScreenChange
* @returns {*}
*/
requestFullScreen: function (element, onFullScreenChange) {
if (!this._supportsFullScreen) return;
element = element || document.documentElement;
element[ this._fn.requestFullscreen ]();
if (onFullScreenChange) {
var eventName = this._fn.fullscreenchange;
if (this._preOnFullScreenChange)
document.removeEventListener(eventName, this._preOnFullScreenChange);
this._preOnFullScreenChange = onFullScreenChange;
document.addEventListener(eventName, onFullScreenChange, false);
}
return element[ this._fn.requestFullscreen ]();
},
/**
* exit the full mode.
* @returns {*}
*/
exitFullScreen: function () {
return this._supportsFullScreen ? document[ this._fn.exitFullscreen ]() : true;
},
/**
* Automatically request full screen with a touch/click event
* @param {Element} element
* @param {Function} onFullScreenChange
*/
autoFullScreen: function (element, onFullScreenChange) {
element = element || document.body;
var touchTarget = cc.canvas || element;
var theScreen = this;
// Function bind will be too complicated here because we need the callback function's reference to remove the listener
function callback() {
theScreen.requestFullScreen(element, onFullScreenChange);
touchTarget.removeEventListener(theScreen._touchEvent, callback);
}
this.requestFullScreen(element, onFullScreenChange);
touchTarget.addEventListener(this._touchEvent, callback);
}
});
/**
* returns a shared instance of the cc.Screen
* @function
* @return {cc.Screen}
*/
cc.Screen.getInstance = function () {
if (!this._instance){
var screen = new cc.Screen();
screen.init();
this._instance = screen;
}
return this._instance;
};