"use strict"; cc._RF.push(module, 'b49c6NO5SJA7Kw6NTIWAoKp', 'nx.gui'); // Scripts/nx/kernel/nx.gui.js "use strict"; var _cc$Class; /******************************************************************************* * * Nx UI相关方法 * * * * 2021.12.10 ******************************************************************************/ var NxObject = require("nx.object"); var nxGUI = cc.Class((_cc$Class = { "extends": NxObject, name: "nxGUI", // 初始化 initialize: function initialize(_args) { // USPER if (!this._super(_args)) { return false; } // 全局服务开启 nx.gui = this; return true; }, // 销毁 uninitialize: function uninitialize() { // 全局服务关闭 nx.gui = null; // USPER return this._super(); }, // 子节点查找 // _names为cc.find参数格式 若_names空,则为_refnode find: function find(_refnode, _names) { if (!_refnode || !_refnode.isValid) { return null; } if (_refnode instanceof cc.Component) { _refnode = _refnode.node; } if (typeof _names != "string" || _names === "") { return _refnode; } return cc.find(_names, _refnode); }, // 获取子节点组件 getComponent: function getComponent(_refnode, _names, _cmpName, _create) { if (_create === void 0) { _create = false; } var node = this.find(_refnode, _names); if (!node) { return null; } var cmp = node.getComponent(_cmpName); if (!cmp && _create) { cmp = node.addComponent(_cmpName); } return cmp; }, // 节点是否可见 isActive: function isActive(_refnode, _names) { var node = this.find(_refnode, _names); return node ? node.active : false; }, // 节点有效 setActive: function setActive(_refnode, _names, _active) { var node = this.find(_refnode, _names); if (node) { node.active = !!_active; } return node; }, // 节点透明度 setOpacity: function setOpacity(_refnode, _names, _opacity) { var node = this.find(_refnode, _names); if (node) { node.opacity = _opacity; } return node; }, // 设置是否有效 setEnable: function setEnable(_refnode, _name, _enable) { // 按钮 var btn = this.getComponent(_refnode, _name, cc.Button); if (btn) { btn.enabled = _enable; return btn.node; } return null; }, // 位置设定 setPosition: function setPosition(_refnode, _names, _pos) { var node = this.find(_refnode, _names); if (node) { node.setPosition(_pos); } return node; }, // 是否屏幕内可见 // _ofs[上,下,左,右] isInFrameView: function isInFrameView(_refnode, _names, _ofs) { var node = this.find(_refnode, _names); if (!node) { return false; } _ofs = _ofs || [0, 0, 0, 0]; var range = cc.view.getVisibleSize(); var pos = node.convertToWorldSpaceAR(cc.Vec2.ZERO); var top = pos.y - node.height / 2; var bottom = pos.y + node.height / 2; var left = pos.x + node.width / 2; var right = pos.x - node.height / 2; return top <= range.height - _ofs[0] && bottom >= _ofs[1] && left >= _ofs[2] && right <= range.width - _ofs[3]; }, // 设置文本 setString: function setString(_refnode, _names, _text) { var cmp = this.getComponent(_refnode, _names, cc.Label); if (!cmp) { cmp = this.getComponent(_refnode, _names, cc.RichText); if (!cmp) { cmp = this.getComponent(_refnode, _names, cc.EditBox); } } if (cmp) { _text += ""; cmp.string = _text || ""; } return cmp; }, // 设置富文本 setStringRich: function setStringRich(_refnode, _names, _text) { var cmp = this.getComponent(_refnode, _names, cc.RichText); if (cmp) { _text += ""; cmp.string = _text == null ? "" : _text; } return cmp; }, // 设置颜色 setColor: function setColor(_refnode, _names, _color) { var node = this.find(_refnode, _names); if (node) { node.color = _color; } return node; }, // 设置样式 setCSS: function setCSS(_refnode, _names, _cssKey) { var cmp = this.getComponent(_refnode, _names, "nx.vb.theme"); if (cmp) { cmp.setCSS(_cssKey); } return cmp; }, // 设置描边色 setOutlineColor: function setOutlineColor(_refnode, _names, _color) { var cmp = this.getComponent(_refnode, _names, cc.LabelOutline); if (cmp) { cmp.color = _color; } return cmp; }, // 精灵褪色 fadeSpriteColor: function fadeSpriteColor(_refnode, _names, _isfade) { var cmp = this.getComponent(_refnode, _names, cc.Sprite); if (!cmp) { return null; } cmp.setState(_isfade ? 1 : 0); return cmp; }, // 节点全色设置 setColorDeep: function setColorDeep(_refnode, _names, _color) { var _setclr = function _setclr(_nd, _color) { if (!_nd) { return; } _nd.color = _color; var children = _nd.children; for (var i = 0; i < children.length; ++i) { _setclr(children[i], _color); } }; var node = this.find(_refnode, _names); if (node) { _setclr(node, _color); } }, // 设置WEB图片 setWebImage: function setWebImage(_refnode, _names, _url, _ext) { if (_ext === void 0) { _ext = ".jpg"; } var cmp = this.getComponent(_refnode, _names, cc.Sprite); if (!cmp) { return null; } cc.loader.load(_url, function (_err, _tex) { if (_err) { nx.warn("[远程]loadRemote加载失败:", _err.errorMessage || _err); return; } nx.dt.trycatch(function () { if (cmp) { cmp.spriteFrame = new cc.SpriteFrame(_tex, new cc.Rect(0, 0, _tex.width, _tex.height), false, cc.Vec2.ZERO, new cc.Size(_tex.width, _tex.height)); } }); }); return cmp; }, // 设置精灵 setSpriteFrame: function setSpriteFrame(_refnode, _names, _sfkey, _cb) { if (_cb === void 0) { _cb = nx.dt.fnEmpty; } var sprite = this.getComponent(_refnode, _names, "nx.fx.sprite", true); if (!sprite) { nx.warn("[GUI]设置精灵失败,不是有效组件:%s", _names); return null; } sprite.setSpriteFrame(_sfkey, _cb); return this.getComponent(sprite, "", cc.Sprite); }, // 设置合图精灵 setAtlasFrame: function setAtlasFrame(_refnode, _names, _sfkey, _opacity) { if (_opacity === void 0) { _opacity = false; } var cmp = this.getComponent(_refnode, _names, cc.Sprite); if (!cmp) { return null; } // 直接设置返回 if (typeof _sfkey != "string") { cmp.spriteFrame = _sfkey; return cmp; } // 频繁设置 if (cmp.loading) { nx.warn("[GUI]设置合图频繁:"); nx.warn("\t\t进行中:", nx.dt.enjson(cmp.loading)); nx.warn("\t\t顶替为:", nx.dt.enjson({ names: _names, rkey: _sfkey })); cmp.loading = null; } // 记录 cmp.loading = { names: _names, rkey: _sfkey }; if (_opacity) { cmp.node.opacity = op; } nx.res.loadAtlasFrame(_sfkey, function (_error, _sframe) { // 记录还原 cmp.loading = null; if (_opacity) { cmp.node.opacity = op; } cmp.spriteFrame = _sframe; }); return cmp; }, // 设置子图 setLocAtlas: function setLocAtlas(_refnode, _names, _key) { var cmp = this.getComponent(_refnode, _names, "xLocalAtlas"); if (!cmp) { nx.warn("子图设定失败,错误的组件:" + _key); return null; } cmp.set(_key); return cmp; }, // 设置本地文本 setLocText: function setLocText(_refnode, _names, _key) { var cmp = this.getComponent(_refnode, _names, "xLocalText"); if (!cmp) { nx.warn("文本设定失败,错误的组件:" + _key); return null; } cmp.set(_key); return cmp; }, // 按钮锁&解锁 setLocked: function setLocked(_refnode, _names, _locked) { var cmp = this.getComponent(_refnode, _names, "nx.fx.button"); if (!cmp) { cmp = this.getComponent(_refnode, _names, "nx.fx.button.wnd"); if (!cmp) { nx.warn("按钮锁&解锁,错误的组件:" + _locked); return null; } } cmp.lock(!!_locked); return cmp; }, setCdTxt: function setCdTxt(_node, _names, _sec, _endCall) { var cmp = nx.gui.getComponent(_node, _names, "nx.fx.cd", true); if (!cmp) { nx.warn("计时组件不存在:" + _names); return; } cmp.setSecs(_sec, _endCall); return cmp; }, // 子节点全隐藏 hideAllChildren: function hideAllChildren(_refnode, _names) { var parent = this.find(_refnode, _names); if (parent) { for (var i = 0; i < parent.childrenCount; i++) { this.setActive(parent.children[i], null, false); } } return parent; }, // 清空子节点 cleanAllChildren: function cleanAllChildren(_refnode, _names) { var parent = this.find(_refnode, _names); if (parent) { var chd = parent.children; if (chd.length > 0) { for (var i = 0; i < chd.length; i++) { if (chd[i]) { chd[i].destroy(); } } } } return parent; }, // 子节点批量补全 gocChildren: function gocChildren(_refnode, _names, _count, _prefab, _justHide) { if (_justHide === void 0) { _justHide = true; } var parent = this.find(_refnode, _names); if (!parent) { nx.error("子节点补全失败,无效父节点!"); return; } // 需要补全 if (_count >= parent.childrenCount) { _prefab = _prefab || parent.children[0]; if (!_prefab) { nx.error("子节点补全失败,无效模板!"); return; } // 实例化缺少数量的节点 var cloneCount = _count - parent.childrenCount; for (var i = 0; i < cloneCount; i++) { parent.addChild(cc.instantiate(_prefab)); } if (_justHide) { parent.children.forEach(function (_node) { if (!_node.active) { _node.active = true; } }); } return; } // 统计多余的个数 if (_justHide) { for (var _i in parent.children) { var node = parent.children[_i]; node.active = _i < _count; } } else { var nodes = []; var count = parent.childrenCount - _count; for (var _i2 = 1; _i2 <= count; ++_i2) { var idx = parent.children.length - _i2; nodes.push(parent.children[idx]); } nodes.forEach(function (_nd) { _nd.removeFromParent(true); }); } }, // 执行某方法 doSth: function doSth(_refnode, _names, _func, _data) { var node = this.find(_refnode, _names); if (node && nx.dt.fnGood(_func)) { _func(node, _data); } return node; }, // 对齐方式 // Key: center, lt, ld, rt, rd setAligment: function setAligment(_refnod, _names, _key, _p1, _p2) { if (_p1 === void 0) { _p1 = 0; } if (_p2 === void 0) { _p2 = 0; } var widget = nx.gui.getComponent(_refnod, _names, cc.Widget); if (!widget) { nx.error("[GUI]对齐失败,找不到Widget组件!"); return; } switch (_key) { case "lt": { widget.isAlignLeft = true; widget.isAlignTop = true; widget.isAlignBottom = false; widget.isAlignRight = false; widget.left = _p1 || 0; widget.top = _p2 || 0; } break; case "ld": { widget.isAlignLeft = true; widget.isAlignTop = false; widget.isAlignBottom = true; widget.isAlignRight = false; widget.left = _p1 || 0; widget.bottom = _p2 || 0; } break; case "rt": { widget.isAlignLeft = false; widget.isAlignTop = true; widget.isAlignBottom = false; widget.isAlignRight = true; widget.right = _p1 || 0; widget.top = _p2 || 0; } break; case "rd": { widget.isAlignLeft = false; widget.isAlignTop = false; widget.isAlignBottom = true; widget.isAlignRight = true; widget.right = _p1 || 0; widget.bottom = _p2 || 0; } break; default: { widget.isAlignVerticalCenter = true; widget.isAlignHorizontalCenter = true; widget.horizontalCenter = _p1 || 0; widget.verticalCenter = _p2 || 0; } break; } widget.updateAlignment(); return widget; }, // 按钮无效化 // 背景褪色,子节点调整透明度 btnFadeColorAndAlphaChildren: function btnFadeColorAndAlphaChildren(_refnode, _names, _isfade) { var nd = this.find(_refnode, _names); nd.color = _isfade ? cc.Color.GRAY : cc.Color.WHITE; // 子节点调整透明度 var children = nd.children; for (var i = 0; i < children.length; ++i) { if (children[i].name !== "bg") { children[i].opacity = _isfade ? 100 : 255; } } }, // 按钮锁定(用于QA模式,避免重复触发) lockButton: function lockButton(_refnode, _names, _uifunc) { var cmp = this.getComponent(_refnode, _names, "xButton"); if (cmp) { cmp.setInteract(false, _uifunc || this.btnFadeColorAndAlphaChildren.bind(this)); } return cmp; }, // 按钮解锁(用于QA模式,避免重复触发) unlockButton: function unlockButton(_refnode, _names, _uifunc) { var cmp = this.getComponent(_refnode, _names, "xButton"); if (cmp) { cmp.setInteract(true, _uifunc || this.btnFadeColorAndAlphaChildren.bind(this)); } return cmp; }, // 视图绑定-文本 vbindText: function vbindText(_refnode, _names, _target, _key, _fmt) { var cmp = this.getComponent(_refnode, _names, "nx.vb.text"); if (cmp) { cmp.setTarget(_target, _key, _fmt); } return cmp; }, // 视图绑定-富文本 vbindRichText: function vbindRichText(_refnode, _names, _target, _key, _fmt) { var cmp = this.getComponent(_refnode, _names, "nx.vb.richText"); if (cmp) { cmp.setTarget(_target, _key, _fmt); } return cmp; }, // 视图绑定-图片 vbindImage: function vbindImage(_refnode, _names, _target, _key) { var cmp = this.getComponent(_refnode, _names, "nx.vb.sprite"); if (cmp) { cmp.setTarget(_target, _key); } return cmp; }, // 视图绑定-进度条 vbindProgress: function vbindProgress(_refnode, _names, _target, _key1, _key2) { var cmp = this.getComponent(_refnode, _names, "nx.vb.progress"); if (cmp) { cmp.setTarget(_target, _key1, _key2); } return cmp; }, // 视图绑定-显示控制 vbindVisible: function vbindVisible(_refnode, _names, _target, _key) { var cmp = this.getComponent(_refnode, _names, "nx.vb.visible"); if (cmp) { cmp.setTarget(_target, _key); } return cmp; } }, _cc$Class["setAtlasFrame"] = function setAtlasFrame(_refnode, _names, _sfkey) { var cmp = this.getComponent(_refnode, _names, cc.Sprite); if (!cmp) { return null; } if (typeof _sfkey == "string") { nx.res.loadAtlasFrame(_sfkey, function (_error, _sframe) { cmp.spriteFrame = _sframe; }); } else { cmp.spriteFrame = _sfkey; } return cmp; }, _cc$Class.setFrameAnimation = function setFrameAnimation(_refnode, _names, _rkey, _cb, _loop, _speed) { if (_loop === void 0) { _loop = true; } if (_speed === void 0) { _speed = 0; } var cmp = this.getComponent(_refnode, _names, "nx.fx.animFrames", true); if (!cmp) { nx.error("[GUI]找不到帧动画播放组件!"); return null; } // 播放 cmp.isLoop = _loop; if (_speed > 0) { cmp.speed = _speed; } cmp.load2Play(_rkey, _cb); return cmp; }, _cc$Class.playDragonDones = function playDragonDones(_refnode, _names, _rkey, _action, _cb, _loop) { if (_loop === void 0) { _loop = true; } var cmp = this.getComponent(_refnode, _names, "nx.fx.dragonBones", true); if (!cmp) { nx.error("[GUI]找不到DragonBones组件!"); return null; } // 播放 cmp.play(_rkey, _action, _cb, _loop); return cmp; }, _cc$Class.playSpine = function playSpine(_refnode, _names, _rkey, _action, _cb, _loop) { if (_loop === void 0) { _loop = true; } var cmp = this.getComponent(_refnode, _names, "nx.fx.spine", true); if (!cmp) { nx.error("[GUI]找不到Spine组件!"); return null; } // 播放 cmp.play(_rkey, _action, _cb, _loop); return cmp; }, _cc$Class)); // 模块导出 module.exports = nxGUI; cc._RF.pop();