UNPKG

@mai3/phaser-sdk

Version:

A UI component library based on the Phaser game engine

222 lines (221 loc) 10.9 kB
import { Address } from '../common/external'; import { EVMConnector } from "../game/EVMConnetor"; var Horizontal = [0, 'x', 'h', 'horizontal', 'left-to-right']; var Vertical = [1, 'y', 'v', 'vertical', 'top-to-bottom']; var Utils = { isHorizontal: function (val) { return Horizontal.includes(val); }, isVertical: function (val) { return Vertical.includes(val); }, sleep: function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }, hexColorToNumber: function (color) { return parseInt(color.replace('#', ''), 16); }, numberToHex: function (num, minLength) { if (minLength === void 0) { minLength = 2; } var hex = num.toString(16).toLowerCase(); while (hex.length < minLength) { hex = '0' + hex; } return '#' + hex; }, hexToNumber: function (hex) { return parseInt(hex.replace('#', '0x'), 16); }, rawAddressToFriendly: function (address, shorten) { if (shorten === void 0) { shorten = false; } var result = Address.parseRaw(address).toString(); if (!shorten) { return result; } return result.substring(0, 4) + '...' + result.substring(result.length - 4); }, smoothScale: function (manager, obj, scaleValue, duration) { manager.add({ targets: obj, scaleX: scaleValue, scaleY: scaleValue, duration: duration }); }, drawRoundedRectRenderTexture: function (scene, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 100; } if (height === void 0) { height = 100; } if (borderWidth === void 0) { borderWidth = 0; } if (radius === void 0) { radius = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } if (backgroundAlpha === void 0) { backgroundAlpha = 1; } // 检查宽高是否有效 if (width <= 0 || height <= 0) { console.error("Width and height must be positive values."); return null; // 返回 null 以避免后续调用报错 } var rt = scene.make.renderTexture({ width: width, height: height }); return this.reDrawRoundedRectRenderTexture(scene, rt, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha); }, drawCircleRenderTexture: function (scene, x, y, radius, borderWidth, borderColor, fillColor) { var rt = scene.make.renderTexture({ width: radius * 2, height: radius * 2 }); return this.reDrawCircleRenderTexture(scene, rt, x, y, radius, borderWidth, borderColor, fillColor); }, reDrawRoundedRectRenderTexture: function (scene, rt, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 100; } if (height === void 0) { height = 100; } if (borderWidth === void 0) { borderWidth = 0; } if (radius === void 0) { radius = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } if (backgroundAlpha === void 0) { backgroundAlpha = 1; } // 参数有效性检查 if (width <= 0 || height <= 0) { console.error("Width and height must be positive values."); return rt; } var g = scene && scene.make.graphics(); this.reDrawRoundedRect(g, scene, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha); g.setVisible(false); // 检查 rt 是否为 undefined if (!rt) { rt = scene.make.renderTexture({ width: width, height: height }); } // 销毁之前的RenderTexture对象,确保无内存泄漏 rt.clear(); // 确保没有遗留的内容 // 渲染Graphics对象到RenderTexture rt.draw(g, 0, 0); rt.setOrigin(0); g.destroy(); // 销毁Graphics对象,避免内存泄漏 return rt; }, reDrawCircleRenderTexture: function (scene, rt, x, y, _radius, borderWidth, borderColor, fillColor) { if (_radius === void 0) { _radius = 0; } if (borderWidth === void 0) { borderWidth = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } var radius = _radius !== null && _radius !== void 0 ? _radius : 0; var width = radius * 2; var height = width; var g = scene.make.graphics(); this.reDrawCircle(g, x, y, radius, borderWidth, borderColor, fillColor); g.setVisible(false); rt.clear(); rt.beginDraw(); rt.batchDraw(g, 0, 0); rt.endDraw(); rt.setOrigin(0); rt.setDisplaySize(radius * 2, radius * 2); g.destroy(); return rt; }, drawRoundedRect: function (scene, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 100; } if (height === void 0) { height = 100; } if (borderWidth === void 0) { borderWidth = 0; } if (radius === void 0) { radius = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } if (backgroundAlpha === void 0) { backgroundAlpha = 1; } var g = scene.make.graphics(); return this.reDrawRoundedRect(g, scene, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha); }, drawCircle: function (scene, x, y, radius, borderWidth, borderColor, fillColor) { var g = scene.make.graphics(); this.reDrawCircle(g, x, y, radius, borderWidth, borderColor, fillColor); return g; }, reDrawCircle: function (g, x, y, radius, borderWidth, borderColor, fillColor) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (radius === void 0) { radius = 0; } if (borderWidth === void 0) { borderWidth = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } g.clear(); if (borderWidth > 0) { g.fillStyle(borderColor); g.fillCircle(x, y, radius); } g.fillStyle(fillColor); g.fillCircle(x, y, radius - borderWidth); return g; }, reDrawRoundedRect: function (g, scene, x, y, width, height, borderWidth, radius, borderColor, fillColor, backgroundAlpha) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 100; } if (height === void 0) { height = 100; } if (borderWidth === void 0) { borderWidth = 0; } if (radius === void 0) { radius = 0; } if (borderColor === void 0) { borderColor = 0x000000; } if (fillColor === void 0) { fillColor = 0xffffff; } if (backgroundAlpha === void 0) { backgroundAlpha = 1; } g.clear(); if (backgroundAlpha > 0) g.alpha = backgroundAlpha; if (borderWidth > 0) { g.fillStyle(borderColor); g.fillRoundedRect(x, y, width, height, radius); } g.fillStyle(fillColor); g.fillRoundedRect(x + borderWidth, y + borderWidth, width - borderWidth * 2, height - borderWidth * 2, radius); return g; }, clampX: function (x, sceneWidth, displayWidth) { return Phaser.Math.Clamp(x, 0, sceneWidth - displayWidth); }, clampY: function (y, sceneHeight, displayHeight) { return Phaser.Math.Clamp(y, 0, sceneHeight - displayHeight); }, isNullOrZeroOrEmpty: function (value) { return value === undefined || value === '' || value === 0; }, GetValue: function (source, key, defaultValue, altSource) { return Phaser.Utils.Objects.GetValue(source, key, defaultValue, altSource); }, GetOrDefaultValue: function (value, defaultValue) { return this.isNullOrZeroOrEmpty(value) ? defaultValue : value; }, MergeRight: function (obj1, obj2) { return Phaser.Utils.Objects.MergeRight(obj1, obj2); }, getWorldPosition: function (transformObj) { var worldTransform = transformObj.getWorldTransformMatrix(); return { x: worldTransform.tx, y: worldTransform.ty }; }, getLocalPosition: function (transformObj) { var localTransform = transformObj.getLocalTransformMatrix(); return { x: localTransform.tx, y: localTransform.ty }; }, addTimer: function (scene, delay, callback, loop) { var timer = scene.time.addEvent({ delay: delay !== null && delay !== void 0 ? delay : 50, callback: callback, callbackScope: scene, loop: loop !== null && loop !== void 0 ? loop : true }); return timer; }, getPadding: function (padding) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; var left = (_c = (_b = (_a = padding === null || padding === void 0 ? void 0 : padding.all) !== null && _a !== void 0 ? _a : padding === null || padding === void 0 ? void 0 : padding.x) !== null && _b !== void 0 ? _b : padding === null || padding === void 0 ? void 0 : padding.left) !== null && _c !== void 0 ? _c : 0; var right = (_f = (_e = (_d = padding === null || padding === void 0 ? void 0 : padding.all) !== null && _d !== void 0 ? _d : padding === null || padding === void 0 ? void 0 : padding.x) !== null && _e !== void 0 ? _e : padding === null || padding === void 0 ? void 0 : padding.right) !== null && _f !== void 0 ? _f : 0; var top = (_j = (_h = (_g = padding === null || padding === void 0 ? void 0 : padding.all) !== null && _g !== void 0 ? _g : padding === null || padding === void 0 ? void 0 : padding.y) !== null && _h !== void 0 ? _h : padding === null || padding === void 0 ? void 0 : padding.top) !== null && _j !== void 0 ? _j : 0; var bottom = (_m = (_l = (_k = padding === null || padding === void 0 ? void 0 : padding.all) !== null && _k !== void 0 ? _k : padding === null || padding === void 0 ? void 0 : padding.y) !== null && _l !== void 0 ? _l : padding === null || padding === void 0 ? void 0 : padding.bottom) !== null && _m !== void 0 ? _m : 0; var x = left; var y = top; return { x: x, y: y, left: left, right: right, top: top, bottom: bottom }; }, evmConnector: function () { return EVMConnector; } }; export default Utils;