/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies 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.
****************************************************************************/
var __BrowserGetter = {
init: function(){
this.html = document.getElementsByTagName("html")[0];
},
availWidth: function(frame){
if(!frame || frame === this.html)
return window.innerWidth;
else
return frame.clientWidth;
},
availHeight: function(frame){
if(!frame || frame === this.html)
return window.innerHeight;
else
return frame.clientHeight;
},
meta: {
"width": "device-width"
},
adaptationType: cc.sys.browserType
};
if(window.navigator.userAgent.indexOf("OS 8_1_") > -1) //this mistake like MIUI, so use of MIUI treatment method
__BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_MIUI;
if(cc.sys.os === cc.sys.OS_IOS) // All browsers are WebView
__BrowserGetter.adaptationType = cc.sys.BROWSER_TYPE_SAFARI;
switch(__BrowserGetter.adaptationType){
case cc.sys.BROWSER_TYPE_SAFARI:
__BrowserGetter.meta["minimal-ui"] = "true";
__BrowserGetter.availWidth = function(frame){
return frame.clientWidth;
};
__BrowserGetter.availHeight = function(frame){
return frame.clientHeight;
};
break;
case cc.sys.BROWSER_TYPE_CHROME:
__BrowserGetter.__defineGetter__("target-densitydpi", function(){
return cc.view._targetDensityDPI;
});
case cc.sys.BROWSER_TYPE_SOUGOU:
case cc.sys.BROWSER_TYPE_UC:
__BrowserGetter.availWidth = function(frame){
return frame.clientWidth;
};
__BrowserGetter.availHeight = function(frame){
return frame.clientHeight;
};
break;
case cc.sys.BROWSER_TYPE_MIUI:
__BrowserGetter.init = function(view){
if(view.__resizeWithBrowserSize) return;
var resize = function(){
view.setDesignResolutionSize(
view._designResolutionSize.width,
view._designResolutionSize.height,
view._resolutionPolicy
);
window.removeEventListener("resize", resize, false);
};
window.addEventListener("resize", resize, false);
};
break;
}
/**
* cc.view is the singleton object which represents the game window.
* It's main task include:
* - Apply the design resolution policy
* - Provide interaction with the window, like resize event on web, retina display support, etc...
* - Manage the game view port which can be different with the window
* - Manage the content scale and translation
*
* Since the cc.view is a singleton, you don't need to call any constructor or create functions,
* the standard way to use it is by calling:
* - cc.view.methodName();
*
* @class View
*/
var View = cc._Class.extend({
_delegate: null,
// Size of parent node that contains cc.container and cc.game.canvas
_frameSize: null,
// resolution size, it is the size appropriate for the app resources.
_designResolutionSize: null,
_originalDesignResolutionSize: null,
// Viewport is the container's rect related to content's coordinates in pixel
_viewPortRect: null,
// The visible rect in content's coordinate in point
_visibleRect: null,
_retinaEnabled: false,
_autoFullScreen: true,
// The device's pixel ratio (for retina displays)
_devicePixelRatio: 1,
// the view name
_viewName: "",
// Custom callback for resize event
_resizeCallback: null,
_scaleX: 1,
_originalScaleX: 1,
_scaleY: 1,
_originalScaleY: 1,
_indexBitsUsed: 0,
_maxTouches: 5,
_resolutionPolicy: null,
_rpExactFit: null,
_rpShowAll: null,
_rpNoBorder: null,
_rpFixedHeight: null,
_rpFixedWidth: null,
_initialized: false,
_captured: false,
_wnd: null,
_hDC: null,
_hRC: null,
_supportTouch: false,
_contentTranslateLeftTop: null,
_frameZoomFactor: 1.0,
__resizeWithBrowserSize: false,
_isAdjustViewPort: true,
_targetDensityDPI: null,
/**
* Constructor of View
*/
ctor: function () {
var _t = this, d = document, _strategyer = cc.ContainerStrategy, _strategy = cc.ContentStrategy;
__BrowserGetter.init(this);
_t._frameSize = cc.size(0, 0);
_t._initFrameSize();
var w = cc.game.canvas.width, h = cc.game.canvas.height;
_t._designResolutionSize = cc.size(w, h);
_t._originalDesignResolutionSize = cc.size(w, h);
_t._viewPortRect = cc.rect(0, 0, w, h);
_t._visibleRect = cc.rect(0, 0, w, h);
_t._contentTranslateLeftTop = {left: 0, top: 0};
_t._viewName = "Cocos2dHTML5";
var sys = cc.sys;
_t.enableRetina(sys.os === sys.OS_IOS || sys.os === sys.OS_OSX);
cc.visibleRect && cc.visibleRect.init(_t._visibleRect);
// Setup system default resolution policies
_t._rpExactFit = new cc.ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.EXACT_FIT);
_t._rpShowAll = new cc.ResolutionPolicy(_strategyer.PROPORTION_TO_FRAME, _strategy.SHOW_ALL);
_t._rpNoBorder = new cc.ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.NO_BORDER);
_t._rpFixedHeight = new cc.ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_HEIGHT);
_t._rpFixedWidth = new cc.ResolutionPolicy(_strategyer.EQUAL_TO_FRAME, _strategy.FIXED_WIDTH);
_t._hDC = cc.game.canvas;
_t._hRC = cc._renderContext;
_t._targetDensityDPI = cc.macro.DENSITYDPI_HIGH;
},
// Resize helper functions
_resizeEvent: function () {
var view;
if(this.setDesignResolutionSize){
view = this;
}else{
view = cc.view;
}
// Check frame size changed or not
var prevFrameW = view._frameSize.width, prevFrameH = view._frameSize.height;
view._initFrameSize();
if (view._frameSize.width === prevFrameW && view._frameSize.height === prevFrameH)
return;
// Frame size changed, do resize works
var width = view._originalDesignResolutionSize.width;
var height = view._originalDesignResolutionSize.height;
if (width > 0)
view.setDesignResolutionSize(width, height, view._resolutionPolicy);
cc.eventManager.dispatchCustomEvent('canvas-resize');
if (view._resizeCallback) {
view._resizeCallback.call();
}
},
/**
*
* Sets view's target-densitydpi for android mobile browser. it can be set to:
* 1. cc.macro.DENSITYDPI_DEVICE, value is "device-dpi"
* 2. cc.macro.DENSITYDPI_HIGH, value is "high-dpi" (default value)
* 3. cc.macro.DENSITYDPI_MEDIUM, value is "medium-dpi" (browser's default value)
* 4. cc.macro.DENSITYDPI_LOW, value is "low-dpi"
* 5. Custom value, e.g: "480"
*
cc.ContainerStrategy class is the root strategy class of container's scale strategy, * it controls the behavior of how to scale the cc.container and cc.game.canvas object
* * @class ContainerStrategy */ cc.ContainerStrategy = cc._Class.extend(/** @lends cc.ContainerStrategy# */{ /** * Manipulation before appling the strategy * @method preApply * @param {View} view - The target view */ preApply: function (view) { }, /** * Function to apply this strategy * @method apply * @param {View} view * @param {Size} designedResolution */ apply: function (view, designedResolution) { }, /** * Manipulation after applying the strategy * @method postApply * @param {View} view The target view */ postApply: function (view) { }, _setupContainer: function (view, w, h) { var frame = cc.game.frame; if (cc.view._autoFullScreen && cc.sys.isMobile && frame === document.documentElement) { // Automatically full screen when user touches on mobile version cc.screen.autoFullScreen(frame); } var locCanvasElement = cc.game.canvas, locContainer = cc.game.container; // Setup container locContainer.style.width = locCanvasElement.style.width = w + "px"; locContainer.style.height = locCanvasElement.style.height = h + "px"; // Setup pixel ratio for retina display var devicePixelRatio = view._devicePixelRatio = 1; if (view.isRetinaEnabled()) devicePixelRatio = view._devicePixelRatio = window.devicePixelRatio || 1; // Setup canvas locCanvasElement.width = w * devicePixelRatio; locCanvasElement.height = h * devicePixelRatio; cc._renderContext.resetCache && cc._renderContext.resetCache(); var body = document.body, style; if (body && (style = body.style)) { style.paddingTop = style.paddingTop || "0px"; style.paddingRight = style.paddingRight || "0px"; style.paddingBottom = style.paddingBottom || "0px"; style.paddingLeft = style.paddingLeft || "0px"; style.borderTop = style.borderTop || "0px"; style.borderRight = style.borderRight || "0px"; style.borderBottom = style.borderBottom || "0px"; style.borderLeft = style.borderLeft || "0px"; style.marginTop = style.marginTop || "0px"; style.marginRight = style.marginRight || "0px"; style.marginBottom = style.marginBottom || "0px"; style.marginLeft = style.marginLeft || "0px"; } }, _fixContainer: function () { // Add container to document body document.body.insertBefore(cc.container, document.body.firstChild); // Set body's width height to window's size, and forbid overflow, so that game will be centered var bs = document.body.style; bs.width = window.innerWidth + "px"; bs.height = window.innerHeight + "px"; bs.overflow = "hidden"; // Body size solution doesn't work on all mobile browser so this is the aleternative: fixed container var contStyle = cc.container.style; contStyle.position = "fixed"; contStyle.left = contStyle.top = "0px"; // Reposition body document.body.scrollTop = 0; } }); /** *cc.ContentStrategy class is the root strategy class of content's scale strategy, * it controls the behavior of how to scale the scene and setup the viewport for the game
* * @class ContentStrategy */ cc.ContentStrategy = cc._Class.extend(/** @lends cc.ContentStrategy# */{ _result: { scale: [1, 1], viewport: null }, _buildResult: function (containerW, containerH, contentW, contentH, scaleX, scaleY) { // Makes content fit better the canvas Math.abs(containerW - contentW) < 2 && (contentW = containerW); Math.abs(containerH - contentH) < 2 && (contentH = containerH); var viewport = cc.rect(Math.round((containerW - contentW) / 2), Math.round((containerH - contentH) / 2), contentW, contentH); // Translate the content if (cc._renderType === cc.game.RENDER_TYPE_CANVAS){ //TODO: modify something for setTransform //cc._renderContext.translate(viewport.x, viewport.y + contentH); } this._result.scale = [scaleX, scaleY]; this._result.viewport = viewport; return this._result; }, /** * Manipulation before applying the strategy * @method preApply * @param {View} view - The target view */ preApply: function (view) { }, /** * Function to apply this strategy * The return value is {scale: [scaleX, scaleY], viewport: {cc.Rect}}, * The target view can then apply these value to itself, it's preferred not to modify directly its private variables * @method apply * @param {View} view * @param {Size} designedResolution * @return {Object} scaleAndViewportRect */ apply: function (view, designedResolution) { return {"scale": [1, 1]}; }, /** * Manipulation after applying the strategy * @method postApply * @param {View} view - The target view */ postApply: function (view) { } }); (function () { // Container scale strategys /** * @class EqualToFrame * @extends ContainerStrategy */ var EqualToFrame = cc.ContainerStrategy.extend({ apply: function (view) { this._setupContainer(view, view._frameSize.width, view._frameSize.height); } }); /** * @class ProportionalToFrame * @extends ContainerStrategy */ var ProportionalToFrame = cc.ContainerStrategy.extend({ apply: function (view, designedResolution) { var frameW = view._frameSize.width, frameH = view._frameSize.height, containerStyle = cc.container.style, designW = designedResolution.width, designH = designedResolution.height, scaleX = frameW / designW, scaleY = frameH / designH, containerW, containerH; scaleX < scaleY ? (containerW = frameW, containerH = designH * scaleX) : (containerW = designW * scaleY, containerH = frameH); // Adjust container size with integer value var offx = Math.round((frameW - containerW) / 2); var offy = Math.round((frameH - containerH) / 2); containerW = frameW - 2 * offx; containerH = frameH - 2 * offy; this._setupContainer(view, containerW, containerH); // Setup container's margin containerStyle.marginLeft = offx + "px"; containerStyle.marginRight = offx + "px"; containerStyle.marginTop = offy + "px"; containerStyle.marginBottom = offy + "px"; } }); /** * @class EqualToWindow * @extends EqualToFrame */ var EqualToWindow = EqualToFrame.extend({ preApply: function (view) { this._super(view); cc.game.frame = document.documentElement; }, apply: function (view) { this._super(view); this._fixContainer(); } }); /** * @class ProportionalToWindow * @extends ProportionalToFrame */ var ProportionalToWindow = ProportionalToFrame.extend({ preApply: function (view) { this._super(view); cc.game.frame = document.documentElement; }, apply: function (view, designedResolution) { this._super(view, designedResolution); this._fixContainer(); } }); /** * @class OriginalContainer * @extends ContainerStrategy */ var OriginalContainer = cc.ContainerStrategy.extend({ apply: function (view) { this._setupContainer(view, cc.game.canvas.width, cc.game.canvas.height); } }); // #NOT STABLE on Android# Alias: Strategy that makes the container's size equals to the window's size // cc.ContainerStrategy.EQUAL_TO_WINDOW = new EqualToWindow(); // #NOT STABLE on Android# Alias: Strategy that scale proportionally the container's size to window's size // cc.ContainerStrategy.PROPORTION_TO_WINDOW = new ProportionalToWindow(); // Alias: Strategy that makes the container's size equals to the frame's size cc.ContainerStrategy.EQUAL_TO_FRAME = new EqualToFrame(); // Alias: Strategy that scale proportionally the container's size to frame's size cc.ContainerStrategy.PROPORTION_TO_FRAME = new ProportionalToFrame(); // Alias: Strategy that keeps the original container's size cc.ContainerStrategy.ORIGINAL_CONTAINER = new OriginalContainer(); // Content scale strategys var ExactFit = cc.ContentStrategy.extend({ apply: function (view, designedResolution) { var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, scaleX = containerW / designedResolution.width, scaleY = containerH / designedResolution.height; return this._buildResult(containerW, containerH, containerW, containerH, scaleX, scaleY); } }); var ShowAll = cc.ContentStrategy.extend({ apply: function (view, designedResolution) { var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, designW = designedResolution.width, designH = designedResolution.height, scaleX = containerW / designW, scaleY = containerH / designH, scale = 0, contentW, contentH; scaleX < scaleY ? (scale = scaleX, contentW = containerW, contentH = designH * scale) : (scale = scaleY, contentW = designW * scale, contentH = containerH); return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); } }); var NoBorder = cc.ContentStrategy.extend({ apply: function (view, designedResolution) { var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, designW = designedResolution.width, designH = designedResolution.height, scaleX = containerW / designW, scaleY = containerH / designH, scale, contentW, contentH; scaleX < scaleY ? (scale = scaleY, contentW = designW * scale, contentH = containerH) : (scale = scaleX, contentW = containerW, contentH = designH * scale); return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); } }); var FixedHeight = cc.ContentStrategy.extend({ apply: function (view, designedResolution) { var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, designH = designedResolution.height, scale = containerH / designH, contentW = containerW, contentH = containerH; return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); }, postApply: function (view) { cc.director._winSizeInPoints = view.getVisibleSize(); } }); var FixedWidth = cc.ContentStrategy.extend({ apply: function (view, designedResolution) { var containerW = cc.game.canvas.width, containerH = cc.game.canvas.height, designW = designedResolution.width, scale = containerW / designW, contentW = containerW, contentH = containerH; return this._buildResult(containerW, containerH, contentW, contentH, scale, scale); }, postApply: function (view) { cc.director._winSizeInPoints = view.getVisibleSize(); } }); // Alias: Strategy to scale the content's size to container's size, non proportional cc.ContentStrategy.EXACT_FIT = new ExactFit(); // Alias: Strategy to scale the content's size proportionally to maximum size and keeps the whole content area to be visible cc.ContentStrategy.SHOW_ALL = new ShowAll(); // Alias: Strategy to scale the content's size proportionally to fill the whole container area cc.ContentStrategy.NO_BORDER = new NoBorder(); // Alias: Strategy to scale the content's height to container's height and proportionally scale its width cc.ContentStrategy.FIXED_HEIGHT = new FixedHeight(); // Alias: Strategy to scale the content's width to container's width and proportionally scale its height cc.ContentStrategy.FIXED_WIDTH = new FixedWidth(); })(); /** *cc.ResolutionPolicy class is the root strategy class of scale strategy, * its main task is to maintain the compatibility with Cocos2d-x
* * @class ResolutionPolicy */ /** * @method ResolutionPolicy * @param {ContainerStrategy} containerStg The container strategy * @param {ContentStrategy} contentStg The content strategy */ cc.ResolutionPolicy = cc._Class.extend(/** @lends cc.ResolutionPolicy# */{ _containerStrategy: null, _contentStrategy: null, /** * Constructor of cc.ResolutionPolicy * @param {ContainerStrategy} containerStg * @param {ContentStrategy} contentStg */ ctor: function (containerStg, contentStg) { this.setContainerStrategy(containerStg); this.setContentStrategy(contentStg); }, /** * Manipulation before applying the resolution policy * @method preApply * @param {View} view The target view */ preApply: function (view) { this._containerStrategy.preApply(view); this._contentStrategy.preApply(view); }, /** * Function to apply this resolution policy * The return value is {scale: [scaleX, scaleY], viewport: {cc.Rect}}, * The target view can then apply these value to itself, it's preferred not to modify directly its private variables * @method apply * @param {View} view - The target view * @param {Size} designedResolution - The user defined design resolution * @return {Object} An object contains the scale X/Y values and the viewport rect */ apply: function (view, designedResolution) { this._containerStrategy.apply(view, designedResolution); return this._contentStrategy.apply(view, designedResolution); }, /** * Manipulation after appyling the strategy * @method postApply * @param {View} view - The target view */ postApply: function (view) { this._containerStrategy.postApply(view); this._contentStrategy.postApply(view); }, /** * Setup the container's scale strategy * @method setContainerStrategy * @param {ContainerStrategy} containerStg */ setContainerStrategy: function (containerStg) { if (containerStg instanceof cc.ContainerStrategy) this._containerStrategy = containerStg; }, /** * Setup the content's scale strategy * @method setContentStrategy * @param {ContentStrategy} contentStg */ setContentStrategy: function (contentStg) { if (contentStg instanceof cc.ContentStrategy) this._contentStrategy = contentStg; } }); cc.js.get(cc.ResolutionPolicy.prototype, "canvasSize", function () { return cc.v2(cc.game.canvas.width, cc.game.canvas.height); }); /** * The entire application is visible in the specified area without trying to preserve the original aspect ratio.