Skip to content

Commit

Permalink
Merge pull request #87 from umbrellaPP/231_pc
Browse files Browse the repository at this point in the history
adapt pc wechat game
  • Loading branch information
PPpro committed Feb 18, 2020
2 parents 7b633e0 + 0ce8cda commit 9aa9dae
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 12 deletions.
2 changes: 1 addition & 1 deletion common/engine/DeviceMotionEvent.js
@@ -1,5 +1,5 @@

const inputManager = _cc.inputManager;
const inputManager = cc.internal.inputManager;
const globalAdapter = window.__globalAdapter;

Object.assign(inputManager, {
Expand Down
2 changes: 1 addition & 1 deletion common/engine/Game.js
@@ -1,4 +1,4 @@
const inputManager = _cc.inputManager;
const inputManager = cc.internal.inputManager;
const renderer = cc.renderer;
const game = cc.game;

Expand Down
2 changes: 1 addition & 1 deletion common/engine/InputManager.js
@@ -1,4 +1,4 @@
const mgr = _cc && _cc.inputManager;
const mgr = cc.internal.inputManager;
const canvasPosition = {
left: 0,
top: 0,
Expand Down
9 changes: 0 additions & 9 deletions common/engine/globalAdapter/BaseSystemInfo.js
Expand Up @@ -17,15 +17,6 @@ function adaptSys (sys, env) {
else if (platform === "ios") {
sys.os = sys.OS_IOS;
}
else if (platform === 'devtools') {
sys.isMobile = false;
if (system.indexOf('android') > -1) {
sys.os = sys.OS_ANDROID;
}
else if (system.indexOf('ios') > -1) {
sys.os = sys.OS_IOS;
}
}

// Adaptation to Android P
if (system === 'android p') {
Expand Down
1 change: 1 addition & 0 deletions platforms/wechat/wrapper/engine/index.js
@@ -1,2 +1,3 @@
require('./Loader');
require('./VideoPlayer');
require('./pc-adapter');
158 changes: 158 additions & 0 deletions platforms/wechat/wrapper/engine/pc-adapter.js
@@ -0,0 +1,158 @@
const env = wx.getSystemInfoSync();
const inputMgr = cc.internal.inputManager;
const eventMgr = cc.internal.eventManager;
const EventKeyboard = cc.Event.EventKeyboard;
const EventMouse = cc.Event.EventMouse;

function adaptKeyboadEvent () {
// map from CCMacro
const key2keyCode = {
Backspace: 8,
Tab: 9,
Enter: 13,
Shift: 16,
Control: 17,
Alt: 18,
// pause: 19,
CapsLock: 20,
Escape: 27,
Space: 32,
// pageup: 33,
// pagedown: 34,
// end: 35,
// home: 36,
ArrowLeft: 37,
ArrowUp: 38,
ArrowRight: 39,
ArrowDown: 40,
// select: 41,
Insert: 45,
Delete: 46,
0: 48,
1: 49,
2: 50,
3: 51,
4: 52,
5: 53,
6: 54,
7: 55,
8: 56,
9: 57,
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,
// num0: 96,
// num1: 97,
// num2: 98,
// num3: 99,
// num4: 100,
// num5: 101,
// num6: 102,
// num7: 103,
// num8: 104,
// num9: 105,
'*': 106,
'+': 107,
'-': 109,
// 'numdel': 110,
'/': 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,
};
wx.onKeyDown(res => eventMgr.dispatchEvent(new EventKeyboard(key2keyCode[res.key] || 0, true)));
wx.onKeyUp(res => eventMgr.dispatchEvent(new EventKeyboard(key2keyCode[res.key] || 0, 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 (env.platform !== 'windows') {
return;
}
inputMgr.registerSystemEvent = function () {
if (this._isRegisterEvent) {
return;
}
this._glView = cc.view;
adaptKeyboadEvent();
adaptMouseEvent();
this._isRegisterEvent = true;
};
})();
15 changes: 15 additions & 0 deletions platforms/wechat/wrapper/systemInfo.js
@@ -1,10 +1,25 @@
const adapter = window.__globalAdapter;
const env = wx.getSystemInfoSync();
let adaptSysFunc = adapter.adaptSys;

Object.assign(adapter, {
// Extend adaptSys interface
adaptSys (sys) {
adaptSysFunc.call(this, sys);
// TODO: add mac platform
if (env.platform === 'windows') {
sys.isMobile = false;
sys.os = sys.OS_WINDOWS;
}
else if (env.platform === 'devtools') {
let system = env.system.toLowerCase();
if (system.indexOf('android') > -1) {
sys.os = sys.OS_ANDROID;
}
else if (system.indexOf('ios') > -1) {
sys.os = sys.OS_IOS;
}
}
// wechatgame subdomain
if (!wx.getOpenDataContext) {
sys.platform = sys.WECHAT_GAME_SUB;
Expand Down

0 comments on commit 9aa9dae

Please sign in to comment.