|
| 1 | +/* |
| 2 | + Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd. |
| 3 | +
|
| 4 | + https://www.cocos.com/ |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights to |
| 9 | + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | + of the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +import { IMiniGame } from 'pal/minigame'; |
| 26 | +import { checkPalIntegrity, withImpl } from '../integrity-check'; |
| 27 | +import { Orientation } from '../screen-adapter/enum-type'; |
| 28 | +import { cloneObject, createInnerAudioContextPolyfill } from '../utils'; |
| 29 | +import { warn } from '../../cocos/core/platform/debug'; |
| 30 | + |
| 31 | +declare let qg: any; |
| 32 | + |
| 33 | +const minigame: IMiniGame = {} as IMiniGame; |
| 34 | +cloneObject(minigame, qg); |
| 35 | + |
| 36 | +// #region SystemInfo |
| 37 | +const systemInfo = minigame.getSystemInfoSync(); |
| 38 | +minigame.isDevTool = false; |
| 39 | + |
| 40 | +minigame.isLandscape = systemInfo.screenWidth > systemInfo.screenHeight; |
| 41 | +// init landscapeOrientation as LANDSCAPE_RIGHT |
| 42 | +const landscapeOrientation = Orientation.LANDSCAPE_RIGHT; |
| 43 | +// NOTE: onDeviceOrientationChange is not supported on this platform |
| 44 | +// qg.onDeviceOrientationChange((res) => { |
| 45 | +// if (res.value === 'landscape') { |
| 46 | +// landscapeOrientation = Orientation.LANDSCAPE_RIGHT; |
| 47 | +// } else if (res.value === 'landscapeReverse') { |
| 48 | +// landscapeOrientation = Orientation.LANDSCAPE_LEFT; |
| 49 | +// } |
| 50 | +// }); |
| 51 | +Object.defineProperty(minigame, 'orientation', { |
| 52 | + get () { |
| 53 | + return minigame.isLandscape ? landscapeOrientation : Orientation.PORTRAIT; |
| 54 | + }, |
| 55 | +}); |
| 56 | +// #endregion SystemInfo |
| 57 | + |
| 58 | +// #region TouchEvent |
| 59 | +minigame.onTouchStart = (cb): void => { |
| 60 | + window.canvas.ontouchstart = cb; |
| 61 | +}; |
| 62 | +minigame.onTouchMove = (cb): void => { |
| 63 | + window.canvas.ontouchmove = cb; |
| 64 | +}; |
| 65 | +minigame.onTouchEnd = (cb): void => { |
| 66 | + window.canvas.ontouchend = cb; |
| 67 | +}; |
| 68 | +minigame.onTouchCancel = (cb): void => { |
| 69 | + window.canvas.ontouchcancel = cb; |
| 70 | +}; |
| 71 | +// #endregion TouchEvent |
| 72 | + |
| 73 | +// // Keyboard |
| 74 | +// globalAdapter.showKeyboard = function (res) { |
| 75 | +// res.confirmHold = true; // HACK: confirmHold not working on Xiaomi platform |
| 76 | +// qg.showKeyboard(res); |
| 77 | +// }; |
| 78 | + |
| 79 | +// #region Accelerometer |
| 80 | +let _customAccelerometerCb: AccelerometerChangeCallback | undefined; |
| 81 | +let _innerAccelerometerCb: AccelerometerChangeCallback | undefined; |
| 82 | +minigame.onAccelerometerChange = (cb: AccelerometerChangeCallback): void => { |
| 83 | + // qg.offAccelerometerChange() is not supported. |
| 84 | + // so we can only register AccelerometerChange callback, but can't unregister. |
| 85 | + if (!_innerAccelerometerCb) { |
| 86 | + _innerAccelerometerCb = (res: any): void => { |
| 87 | + let x = res.x; |
| 88 | + let y = res.y; |
| 89 | + if (minigame.isLandscape) { |
| 90 | + const orientationFactor = (landscapeOrientation === Orientation.LANDSCAPE_RIGHT ? 1 : -1); |
| 91 | + const tmp = x; |
| 92 | + x = -y * orientationFactor; |
| 93 | + y = tmp * orientationFactor; |
| 94 | + } |
| 95 | + |
| 96 | + const standardFactor = -0.1; |
| 97 | + x *= standardFactor; |
| 98 | + y *= standardFactor; |
| 99 | + const resClone = { |
| 100 | + x, |
| 101 | + y, |
| 102 | + z: res.z, |
| 103 | + }; |
| 104 | + _customAccelerometerCb?.(resClone); |
| 105 | + }; |
| 106 | + qg.onAccelerometerChange(_innerAccelerometerCb); |
| 107 | + } |
| 108 | + _customAccelerometerCb = cb; |
| 109 | +}; |
| 110 | +minigame.offAccelerometerChange = (cb?: AccelerometerChangeCallback): void => { |
| 111 | + // qg.offAccelerometerChange() is not supported. |
| 112 | + _customAccelerometerCb = undefined; |
| 113 | +}; |
| 114 | +// #endregion Accelerometer |
| 115 | + |
| 116 | +// #region InnerAudioContext |
| 117 | +minigame.createInnerAudioContext = createInnerAudioContextPolyfill(qg, { |
| 118 | + onPlay: true, |
| 119 | + onPause: true, |
| 120 | + onStop: true, |
| 121 | + onSeek: false, |
| 122 | +}); |
| 123 | +const originalCreateInnerAudioContext = minigame.createInnerAudioContext; |
| 124 | +minigame.createInnerAudioContext = (): InnerAudioContext => { |
| 125 | + const audioContext = originalCreateInnerAudioContext.call(minigame); |
| 126 | + const originalStop = audioContext.stop; |
| 127 | + Object.defineProperty(audioContext, 'stop', { |
| 128 | + configurable: true, |
| 129 | + value (): void { |
| 130 | + // NOTE: stop won't seek to 0 when audio is paused on Xiaomi platform. |
| 131 | + audioContext.seek(0); |
| 132 | + originalStop.call(audioContext); |
| 133 | + }, |
| 134 | + }); |
| 135 | + return audioContext; |
| 136 | +}; |
| 137 | +// #endregion InnerAudioContext |
| 138 | + |
| 139 | +// #region SafeArea |
| 140 | +minigame.getSafeArea = (): SafeArea => { |
| 141 | + warn('getSafeArea is not supported on this platform'); |
| 142 | + const systemInfo = minigame.getSystemInfoSync(); |
| 143 | + return { |
| 144 | + top: 0, |
| 145 | + left: 0, |
| 146 | + bottom: systemInfo.screenHeight, |
| 147 | + right: systemInfo.screenWidth, |
| 148 | + width: systemInfo.screenWidth, |
| 149 | + height: systemInfo.screenHeight, |
| 150 | + }; |
| 151 | +}; |
| 152 | +// #endregion SafeArea |
| 153 | + |
| 154 | +export { minigame }; |
| 155 | + |
| 156 | +checkPalIntegrity<typeof import('pal/minigame')>(withImpl<typeof import('./xiaomi')>()); |
0 commit comments