mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 20:45:23 +00:00
[adapters] 增加小游戏适配部分源码
This commit is contained in:
24
adapters/platforms/wechat/wrapper/engine/Texture2D.js
Normal file
24
adapters/platforms/wechat/wrapper/engine/Texture2D.js
Normal file
@@ -0,0 +1,24 @@
|
||||
if (cc.Texture2D) {
|
||||
cc.Texture2D.prototype._checkPackable = function () {
|
||||
let dynamicAtlas = cc.dynamicAtlasManager;
|
||||
if (!dynamicAtlas) return;
|
||||
|
||||
if (this._isCompressed()) {
|
||||
this._packable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let w = this.width, h = this.height;
|
||||
if (!this._image ||
|
||||
w > dynamicAtlas.maxFrameSize || h > dynamicAtlas.maxFrameSize ||
|
||||
this._getHash() !== dynamicAtlas.Atlas.DEFAULT_HASH) {
|
||||
this._packable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// HACK: Can't tell if it's a Canvas or an Image by instanceof on WeChat.
|
||||
if (this._image && this._image.getContext) {
|
||||
this._packable = true;
|
||||
}
|
||||
};
|
||||
}
|
339
adapters/platforms/wechat/wrapper/engine/VideoPlayer.js
Normal file
339
adapters/platforms/wechat/wrapper/engine/VideoPlayer.js
Normal file
@@ -0,0 +1,339 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
(function () {
|
||||
if (!(cc && cc.VideoPlayer && cc.VideoPlayer.Impl && __globalAdapter.createVideo)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Mat4 = cc.Mat4;
|
||||
var _worldMat = cc.mat4();
|
||||
var _cameraMat = cc.mat4();
|
||||
|
||||
var _impl = cc.VideoPlayer.Impl;
|
||||
var _p = cc.VideoPlayer.Impl.prototype;
|
||||
|
||||
cc.VideoPlayer.prototype._updateVideoSource = function _updateVideoSource() {
|
||||
let clip = this._clip;
|
||||
if (this.resourceType === cc.VideoPlayer.ResourceType.REMOTE) {
|
||||
this._impl.setURL(this.remoteURL, this._mute || this._volume === 0);
|
||||
}
|
||||
else if (clip) {
|
||||
if (clip._nativeAsset) {
|
||||
this._impl.setURL(clip._nativeAsset, this._mute || this._volume === 0);
|
||||
}
|
||||
else {
|
||||
// deferred loading video clip
|
||||
cc.assetManager.postLoadNative(clip, (err) => {
|
||||
if (err) {
|
||||
console.error(err.message, err.stack);
|
||||
return;
|
||||
}
|
||||
this._impl.setURL(clip._nativeAsset, this._mute || this._volume === 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_p._bindEvent = function () {
|
||||
let video = this._video,
|
||||
self = this;
|
||||
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
|
||||
video.onPlay(function () {
|
||||
if (self._video !== video) return;
|
||||
self._playing = true;
|
||||
self._dispatchEvent(_impl.EventType.PLAYING);
|
||||
});
|
||||
video.onEnded(function () {
|
||||
if (self._video !== video) return;
|
||||
self._playing = false;
|
||||
self._currentTime = self._duration; // ensure currentTime is at the end of duration
|
||||
self._dispatchEvent(_impl.EventType.COMPLETED);
|
||||
});
|
||||
video.onPause(function () {
|
||||
if (self._video !== video) return;
|
||||
self._playing = false;
|
||||
self._dispatchEvent(_impl.EventType.PAUSED);
|
||||
});
|
||||
video.onTimeUpdate(function (res) {
|
||||
self._duration = res.duration;
|
||||
self._currentTime = res.position;
|
||||
});
|
||||
// onStop not supported, implemented in promise returned by video.stop call.
|
||||
};
|
||||
|
||||
_p._unbindEvent = function () {
|
||||
let video = this._video;
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
|
||||
// BUG: video.offPlay(cb) is invalid
|
||||
video.offPlay();
|
||||
video.offEnded();
|
||||
video.offPause();
|
||||
video.offTimeUpdate();
|
||||
// offStop not supported
|
||||
};
|
||||
|
||||
_p.setVisible = function (value) {
|
||||
let video = this._video;
|
||||
if (!video || this._visible === value) {
|
||||
return;
|
||||
}
|
||||
if (value) {
|
||||
video.width = this._actualWidth || 0;
|
||||
}
|
||||
else {
|
||||
video.width = 0; // hide video
|
||||
}
|
||||
this._visible = value;
|
||||
};
|
||||
|
||||
_p.createDomElementIfNeeded = function () {
|
||||
if (!__globalAdapter.createVideo) {
|
||||
cc.warn('VideoPlayer not supported');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._video) {
|
||||
this._video = __globalAdapter.createVideo();
|
||||
this._video.showCenterPlayBtn = false;
|
||||
this._video.controls = false;
|
||||
this._duration = 0;
|
||||
this._currentTime = 0;
|
||||
this._loaded = false;
|
||||
this.setVisible(false);
|
||||
this._bindEvent();
|
||||
}
|
||||
};
|
||||
|
||||
_p.setURL = function (path) {
|
||||
let video = this._video;
|
||||
if (!video || video.src === path) {
|
||||
return;
|
||||
}
|
||||
video.stop();
|
||||
this._unbindEvent();
|
||||
video.autoplay = true; // HACK: to implement onCanplay callback
|
||||
video.src = path;
|
||||
video.muted = true;
|
||||
let self = this;
|
||||
this._loaded = false;
|
||||
function loadedCallback () {
|
||||
video.offPlay();
|
||||
self._bindEvent();
|
||||
video.stop();
|
||||
video.muted = false;
|
||||
self._loaded = true;
|
||||
self._playing = false;
|
||||
self._currentTime = 0;
|
||||
self._dispatchEvent(_impl.EventType.READY_TO_PLAY);
|
||||
video.autoplay = false;
|
||||
}
|
||||
video.onPlay(loadedCallback);
|
||||
};
|
||||
|
||||
_p.getURL = function() {
|
||||
let video = this._video;
|
||||
if (!video) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return video.src;
|
||||
};
|
||||
|
||||
_p.play = function () {
|
||||
let video = this._video;
|
||||
if (!video || !this._visible || this._playing) return;
|
||||
|
||||
video.play();
|
||||
};
|
||||
|
||||
_p.setStayOnBottom = function (enabled) {};
|
||||
|
||||
_p.pause = function () {
|
||||
let video = this._video;
|
||||
if (!this._playing || !video) return;
|
||||
|
||||
video.pause();
|
||||
};
|
||||
|
||||
_p.resume = function () {
|
||||
let video = this._video;
|
||||
if (this._playing || !video) return;
|
||||
|
||||
video.play();
|
||||
};
|
||||
|
||||
_p.stop = function () {
|
||||
let self = this;
|
||||
let video = this._video;
|
||||
if (!video || !this._visible) return;
|
||||
|
||||
video.stop().then(function (res) {
|
||||
if (res.errMsg && !res.errMsg.includes('ok')) {
|
||||
console.error('failed to stop video player');
|
||||
return;
|
||||
}
|
||||
self._currentTime = 0;
|
||||
self._playing = false;
|
||||
self._dispatchEvent(_impl.EventType.STOPPED);
|
||||
});
|
||||
};
|
||||
|
||||
_p.setVolume = function (volume) {
|
||||
// wx not support setting video volume
|
||||
};
|
||||
|
||||
|
||||
_p.seekTo = function (time) {
|
||||
let video = this._video;
|
||||
if (!video || !this._loaded) return;
|
||||
|
||||
video.seek(time);
|
||||
};
|
||||
|
||||
_p.isPlaying = function () {
|
||||
return this._playing;
|
||||
};
|
||||
|
||||
_p.duration = function () {
|
||||
return this._duration;
|
||||
};
|
||||
|
||||
_p.currentTime = function () {
|
||||
return this._currentTime;
|
||||
};
|
||||
|
||||
_p.setKeepAspectRatioEnabled = function (isEnabled) {
|
||||
console.warn('On wechat game videoPlayer is always keep the aspect ratio');
|
||||
};
|
||||
|
||||
_p.isKeepAspectRatioEnabled = function () {
|
||||
return true;
|
||||
};
|
||||
|
||||
_p.isFullScreenEnabled = function () {
|
||||
return this._fullScreenEnabled;
|
||||
};
|
||||
|
||||
_p.setFullScreenEnabled = function (enable) {
|
||||
let video = this._video;
|
||||
if (!video || this._fullScreenEnabled === enable) {
|
||||
return;
|
||||
}
|
||||
if (enable) {
|
||||
video.requestFullScreen();
|
||||
}
|
||||
else {
|
||||
video.exitFullScreen();
|
||||
}
|
||||
this._fullScreenEnabled = enable;
|
||||
};
|
||||
|
||||
_p.enable = function () {
|
||||
this.setVisible(true);
|
||||
};
|
||||
|
||||
_p.disable = function () {
|
||||
if (this._playing) {
|
||||
this._video.pause();
|
||||
}
|
||||
this.setVisible(false);
|
||||
};
|
||||
|
||||
_p.destroy = function () {
|
||||
this.disable();
|
||||
this._unbindEvent();
|
||||
if (this._video) {
|
||||
this._video.destroy();
|
||||
this._video = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
_p.updateMatrix = function (node) {
|
||||
if (!this._video || !this._visible) return;
|
||||
let camera = cc.Camera.findCamera(node);
|
||||
if (!camera) {
|
||||
return;
|
||||
}
|
||||
|
||||
node.getWorldMatrix(_worldMat);
|
||||
if (this._m00 === _worldMat.m[0] && this._m01 === _worldMat.m[1] &&
|
||||
this._m04 === _worldMat.m[4] && this._m05 === _worldMat.m[5] &&
|
||||
this._m12 === _worldMat.m[12] && this._m13 === _worldMat.m[13] &&
|
||||
this._w === node._contentSize.width && this._h === node._contentSize.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update matrix cache
|
||||
this._m00 = _worldMat.m[0];
|
||||
this._m01 = _worldMat.m[1];
|
||||
this._m04 = _worldMat.m[4];
|
||||
this._m05 = _worldMat.m[5];
|
||||
this._m12 = _worldMat.m[12];
|
||||
this._m13 = _worldMat.m[13];
|
||||
this._w = node._contentSize.width;
|
||||
this._h = node._contentSize.height;
|
||||
|
||||
camera.getWorldToScreenMatrix2D(_cameraMat);
|
||||
Mat4.multiply(_cameraMat, _cameraMat, _worldMat);
|
||||
|
||||
|
||||
let viewScaleX = cc.view._scaleX,
|
||||
viewScaleY = cc.view._scaleY;
|
||||
let dpr = cc.view._devicePixelRatio;
|
||||
viewScaleX /= dpr;
|
||||
viewScaleY /= dpr;
|
||||
|
||||
let finalScaleX = _cameraMat.m[0] * viewScaleX,
|
||||
finalScaleY = _cameraMat.m[5] * viewScaleY;
|
||||
|
||||
let finalWidth = this._w * finalScaleX,
|
||||
finalHeight = this._h * finalScaleY;
|
||||
|
||||
let appx = finalWidth * node._anchorPoint.x;
|
||||
let appy = finalHeight * node._anchorPoint.y;
|
||||
|
||||
let viewport = cc.view._viewportRect;
|
||||
let offsetX = viewport.x / dpr,
|
||||
offsetY = viewport.y / dpr;
|
||||
|
||||
let tx = _cameraMat.m[12] * viewScaleX - appx + offsetX,
|
||||
ty = _cameraMat.m[13] * viewScaleY - appy + offsetY;
|
||||
|
||||
var height = cc.view.getFrameSize().height;
|
||||
|
||||
this._video.x = tx;
|
||||
this._video.y = height - finalHeight - ty;
|
||||
this._actualWidth = this._video.width = finalWidth;
|
||||
this._video.height = finalHeight;
|
||||
};
|
||||
})();
|
3
adapters/platforms/wechat/wrapper/engine/index.js
Normal file
3
adapters/platforms/wechat/wrapper/engine/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
require('./VideoPlayer');
|
||||
require('./pc-adapter');
|
||||
require('./Texture2D');
|
170
adapters/platforms/wechat/wrapper/engine/pc-adapter.js
Normal file
170
adapters/platforms/wechat/wrapper/engine/pc-adapter.js
Normal file
@@ -0,0 +1,170 @@
|
||||
const env = wx.getSystemInfoSync();
|
||||
const inputMgr = cc.internal.inputManager;
|
||||
const eventMgr = cc.internal.eventManager;
|
||||
const EventKeyboard = cc.Event.EventKeyboard;
|
||||
const EventMouse = cc.Event.EventMouse;
|
||||
|
||||
// map from CCMacro
|
||||
const key2keyCode = {
|
||||
backspace: 8,
|
||||
tab: 9,
|
||||
enter: 13,
|
||||
shift: 16,
|
||||
control: 17,
|
||||
alt: 18,
|
||||
pause: 19,
|
||||
capslock: 20,
|
||||
escape: 27,
|
||||
' ': 32,
|
||||
pageup: 33,
|
||||
pagedown: 34,
|
||||
end: 35,
|
||||
home: 36,
|
||||
arrowleft: 37,
|
||||
arrowup: 38,
|
||||
arrowright: 39,
|
||||
arrowdown: 40,
|
||||
insert: 45,
|
||||
a: 65,
|
||||
b: 66,
|
||||
c: 67,
|
||||
d: 68,
|
||||
e: 69,
|
||||
f: 70,
|
||||
g: 71,
|
||||
h: 72,
|
||||
i: 73,
|
||||
j: 74,
|
||||
k: 75,
|
||||
l: 76,
|
||||
m: 77,
|
||||
n: 78,
|
||||
o: 79,
|
||||
p: 80,
|
||||
q: 81,
|
||||
r: 82,
|
||||
s: 83,
|
||||
t: 84,
|
||||
u: 85,
|
||||
v: 86,
|
||||
w: 87,
|
||||
x: 88,
|
||||
y: 89,
|
||||
z: 90,
|
||||
'*': 106,
|
||||
'+': 107,
|
||||
'-': 109,
|
||||
'/': 111,
|
||||
f1: 112,
|
||||
f2: 113,
|
||||
f3: 114,
|
||||
f4: 115,
|
||||
f5: 116,
|
||||
f6: 117,
|
||||
f7: 118,
|
||||
f8: 119,
|
||||
f9: 120,
|
||||
f10: 121,
|
||||
f11: 122,
|
||||
f12: 123,
|
||||
numlock: 144,
|
||||
scrolllock: 145,
|
||||
';': 186,
|
||||
'=': 187,
|
||||
',': 188,
|
||||
'.': 190,
|
||||
'`': 192,
|
||||
'[': 219,
|
||||
'\\': 220,
|
||||
']': 221,
|
||||
'\'': 222,
|
||||
};
|
||||
|
||||
const code2KeyCode = {
|
||||
Delete: 46,
|
||||
Digit0: 48,
|
||||
Digit1: 49,
|
||||
Digit2: 50,
|
||||
Digit3: 51,
|
||||
Digit4: 52,
|
||||
Digit5: 53,
|
||||
Digit6: 54,
|
||||
Digit7: 55,
|
||||
Digit8: 56,
|
||||
Digit9: 57,
|
||||
Numpad0: 96,
|
||||
Numpad1: 97,
|
||||
Numpad2: 98,
|
||||
Numpad3: 99,
|
||||
Numpad4: 100,
|
||||
Numpad5: 101,
|
||||
Numpad6: 102,
|
||||
Numpad7: 103,
|
||||
Numpad8: 104,
|
||||
Numpad9: 105,
|
||||
NumpadDecimal: 110,
|
||||
};
|
||||
|
||||
function getKeyCode (res) {
|
||||
let key = res.key.toLowerCase(), code = res.code;
|
||||
// distinguish different numLock states
|
||||
if (/^\d$/.test(key) || key === 'delete') {
|
||||
return code2KeyCode[code];
|
||||
}
|
||||
return key2keyCode[key] || 0;
|
||||
}
|
||||
|
||||
function adaptKeyboadEvent () {
|
||||
wx.onKeyDown(res => eventMgr.dispatchEvent(new EventKeyboard(getKeyCode(res), true)));
|
||||
wx.onKeyUp(res => eventMgr.dispatchEvent(new EventKeyboard(getKeyCode(res), false)));
|
||||
}
|
||||
|
||||
function adaptMouseEvent () {
|
||||
let canvasRect = {
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
};
|
||||
function registerMouseEvent (funcName, engineEventType, handler) {
|
||||
wx[funcName](res => {
|
||||
let mouseEvent = inputMgr.getMouseEvent(res, canvasRect, engineEventType);
|
||||
mouseEvent.setButton(res.button || 0);
|
||||
handler(res, mouseEvent);
|
||||
eventMgr.dispatchEvent(mouseEvent);
|
||||
});
|
||||
}
|
||||
registerMouseEvent('onMouseDown', EventMouse.DOWN, function (res, mouseEvent) {
|
||||
inputMgr._mousePressed = true;
|
||||
inputMgr.handleTouchesBegin([inputMgr.getTouchByXY(res.x, res.y, canvasRect)]);
|
||||
});
|
||||
registerMouseEvent('onMouseUp', EventMouse.UP, function (res, mouseEvent) {
|
||||
inputMgr._mousePressed = false;
|
||||
inputMgr.handleTouchesEnd([inputMgr.getTouchByXY(res.x, res.y, canvasRect)]);
|
||||
});
|
||||
registerMouseEvent('onMouseMove', EventMouse.MOVE, function (res, mouseEvent) {
|
||||
inputMgr.handleTouchesMove([inputMgr.getTouchByXY(res.x, res.y, canvasRect)]);
|
||||
if (!inputMgr._mousePressed) {
|
||||
mouseEvent.setButton(null);
|
||||
}
|
||||
});
|
||||
registerMouseEvent('onWheel', EventMouse.SCROLL, function (res, mouseEvent) {
|
||||
mouseEvent.setScrollData(0, -res.deltaY);
|
||||
});
|
||||
}
|
||||
|
||||
(function () {
|
||||
// TODO: add mac
|
||||
if (__globalAdapter.isSubContext || env.platform !== 'windows') {
|
||||
return;
|
||||
}
|
||||
inputMgr.registerSystemEvent = function () {
|
||||
if (this._isRegisterEvent) {
|
||||
return;
|
||||
}
|
||||
this._glView = cc.view;
|
||||
adaptKeyboadEvent();
|
||||
adaptMouseEvent();
|
||||
this._isRegisterEvent = true;
|
||||
};
|
||||
})();
|
Reference in New Issue
Block a user