2ed169cce048377227be18ccef0e5d688c1e2465.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. System.register(["cc", "cc/env"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, UITransform, size, screen, view, v4, EDITOR, JSB, Adapter, _crd, ccclass, property, DeviceDirection, SAFE_SIZE, OUTSIDE_SIZE;
  4. _export("Adapter", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. _decorator = _cc._decorator;
  11. Component = _cc.Component;
  12. UITransform = _cc.UITransform;
  13. size = _cc.size;
  14. screen = _cc.screen;
  15. view = _cc.view;
  16. v4 = _cc.v4;
  17. }, function (_ccEnv) {
  18. EDITOR = _ccEnv.EDITOR;
  19. JSB = _ccEnv.JSB;
  20. }],
  21. execute: function () {
  22. _crd = true;
  23. _cclegacy._RF.push({}, "bd749XlRlxBuozLdQ22l+pP", "Adapter", undefined);
  24. __checkObsolete__(['_decorator', 'Component', 'UITransform', 'size', 'Size', 'screen', 'view', 'v4']);
  25. ({
  26. ccclass,
  27. property
  28. } = _decorator);
  29. /**
  30. * @description 该适配方案参考 https://forum.cocos.org/t/cocos-creator/74001
  31. */
  32. /**
  33. * 屏幕分辨率下 的像素值
  34. */
  35. /**@description 设备方向 */
  36. DeviceDirection = /*#__PURE__*/function (DeviceDirection) {
  37. DeviceDirection[DeviceDirection["Unknown"] = 0] = "Unknown";
  38. DeviceDirection[DeviceDirection["LandscapeLeft"] = 1] = "LandscapeLeft";
  39. DeviceDirection[DeviceDirection["LandscapeRight"] = 2] = "LandscapeRight";
  40. DeviceDirection[DeviceDirection["Portrait"] = 3] = "Portrait";
  41. DeviceDirection[DeviceDirection["UpsideDown"] = 4] = "UpsideDown";
  42. return DeviceDirection;
  43. }(DeviceDirection || {});
  44. SAFE_SIZE = size(0, 0);
  45. OUTSIDE_SIZE = size(0, 0);
  46. _export("Adapter", Adapter = class Adapter extends Component {
  47. constructor() {
  48. super(...arguments);
  49. this._func = null;
  50. /**@description 适配完成回调 */
  51. this.onAdapterComplete = null;
  52. this._isFullScreenAdaption = true;
  53. }
  54. set width(value) {
  55. var trans = this.getComponent(UITransform);
  56. if (!trans) {
  57. return;
  58. }
  59. trans.width = value;
  60. }
  61. get width() {
  62. var trans = this.getComponent(UITransform);
  63. if (!trans) {
  64. return 0;
  65. }
  66. return trans.width;
  67. }
  68. set height(value) {
  69. var trans = this.getComponent(UITransform);
  70. if (!trans) {
  71. return;
  72. }
  73. trans.height = value;
  74. }
  75. get height() {
  76. var trans = this.getComponent(UITransform);
  77. if (!trans) {
  78. return 0;
  79. }
  80. return trans.height;
  81. }
  82. static get windowSize() {
  83. if (EDITOR) {
  84. return view.getDesignResolutionSize();
  85. }
  86. return screen.windowSize;
  87. }
  88. static get visibleSize() {
  89. return view.getVisibleSize();
  90. }
  91. doOnAdapterComplete() {
  92. if (this.onAdapterComplete) {
  93. this.onAdapterComplete();
  94. }
  95. }
  96. onLoad() {
  97. super.onLoad && super.onLoad();
  98. this.onChangeSize();
  99. }
  100. onEnable() {
  101. super.onEnable && super.onEnable();
  102. this.addEvents();
  103. }
  104. onDisable() {
  105. this.removeEvents();
  106. super.onDisable && super.onDisable();
  107. }
  108. onDestroy() {
  109. this.removeEvents();
  110. super.onDestroy && super.onDestroy();
  111. }
  112. addEvents() {
  113. if (this._func) {
  114. return;
  115. }
  116. this._func = this.onChangeSize.bind(this);
  117. window.addEventListener("resize", this._func);
  118. window.addEventListener("orientationchange", this._func);
  119. }
  120. removeEvents() {
  121. if (this._func) {
  122. window.removeEventListener("resize", this._func);
  123. window.removeEventListener("orientationchange", this._func);
  124. }
  125. this._func = null;
  126. }
  127. /**
  128. * @description 视图发生大小变化
  129. */
  130. onChangeSize() {
  131. this.doOnAdapterComplete();
  132. }
  133. /**@description 获取当前设备方向 */
  134. get direction() {
  135. var str = "未知";
  136. var result = DeviceDirection.Unknown;
  137. var orientation = window.orientation;
  138. if (JSB) {
  139. orientation = jsb.device.getDeviceOrientation();
  140. }
  141. if (orientation != undefined || orientation != null) {
  142. if (orientation == 90) {
  143. str = "\u6A2A\u5C4F\u5411\u5DE6";
  144. result = DeviceDirection.LandscapeLeft;
  145. } else if (orientation == -90) {
  146. str = "\u6A2A\u5C4F\u5411\u53F3";
  147. result = DeviceDirection.LandscapeRight;
  148. } else if (orientation == 0) {
  149. str = "竖屏向上";
  150. result = DeviceDirection.Portrait;
  151. } else if (orientation == 180) {
  152. str = "竖屏向下";
  153. result = DeviceDirection.UpsideDown;
  154. }
  155. } // console.log(`当前设备方向:${str}`)
  156. return result;
  157. }
  158. static get safeAreaEdge() {
  159. if (JSB) {
  160. return jsb.device.getSafeAreaEdge();
  161. } else {
  162. return v4(0, 0, 0, 0);
  163. }
  164. }
  165. static set safeArea(value) {
  166. this._safeArea = value;
  167. }
  168. static screenPxToDesignPx(screenPx) {
  169. return screenPx / this.safeArea.designPxToScreenPxRatio;
  170. }
  171. static designPxToScreenPx(designPx) {
  172. return designPx * this.safeArea.designPxToScreenPxRatio;
  173. }
  174. /**
  175. * 基于屏幕尺寸的安全区域
  176. *
  177. * 可以通过 screenPxToDesignPx 转换为基于设计画布尺寸的像素大小
  178. */
  179. static get safeArea() {
  180. if (this._safeArea == null || this._safeArea == undefined) {
  181. // 初始屏幕宽高像素
  182. var screenWidth = Adapter.windowSize.width;
  183. var screenHeight = Adapter.windowSize.height; // 「设计分辨率」像素值转换到 「屏幕分辨率」 下的像素比
  184. var designWidth = Adapter.visibleSize.width;
  185. var designHeight = Adapter.visibleSize.height;
  186. var designPxToScreenPxRatio = Math.min(screenWidth / designWidth, screenHeight / designHeight);
  187. OUTSIDE_SIZE.width = 0;
  188. OUTSIDE_SIZE.height = 0;
  189. SAFE_SIZE.width = screenWidth - OUTSIDE_SIZE.width;
  190. SAFE_SIZE.height = screenHeight - OUTSIDE_SIZE.height;
  191. if (JSB) {
  192. OUTSIDE_SIZE.width = this.safeAreaEdge.y + this.safeAreaEdge.w;
  193. OUTSIDE_SIZE.height = this.safeAreaEdge.x + this.safeAreaEdge.z;
  194. SAFE_SIZE.width = screenWidth - OUTSIDE_SIZE.width;
  195. SAFE_SIZE.height = screenHeight - OUTSIDE_SIZE.height;
  196. OUTSIDE_SIZE.width = OUTSIDE_SIZE.width / 2;
  197. OUTSIDE_SIZE.height = OUTSIDE_SIZE.height / 2;
  198. }
  199. this._safeArea = {
  200. width: screenWidth,
  201. height: screenHeight,
  202. outside: OUTSIDE_SIZE,
  203. safe: SAFE_SIZE,
  204. designPxToScreenPxRatio: designPxToScreenPxRatio
  205. };
  206. }
  207. return this._safeArea;
  208. }
  209. /** 是否全屏*/
  210. get isFullScreenAdaption() {
  211. return this._isFullScreenAdaption;
  212. }
  213. set isFullScreenAdaption(value) {
  214. this._isFullScreenAdaption = value;
  215. }
  216. /**
  217. * 获取是否为16:9的常规屏幕尺寸
  218. */
  219. static isNormalScreen() {
  220. return screen.windowSize.height / screen.windowSize.width < 1335 / 750;
  221. }
  222. });
  223. Adapter.direction = DeviceDirection;
  224. Adapter._safeArea = null;
  225. _cclegacy._RF.pop();
  226. _crd = false;
  227. }
  228. };
  229. });
  230. //# sourceMappingURL=2ed169cce048377227be18ccef0e5d688c1e2465.js.map