//所有UI控制脚本

export abstract class UICtrl extends cc.Component {
	protected view: {};//保存所有节点信息

	onLoad() {
		this.view = {};
		this.loadAllObject(this.node, "");
	}

	//加载所有节点
	loadAllObject(root: any, path: string) {
		for (let i = 0; i < root.children.length; i++) {
			//添加监听事件
			this.handlePartAdded(root.children[i], root.children[i].name);

			//把每个节点存起来
			this.view[path + root.children[i].name] = root.children[i];
			this.loadAllObject(root.children[i], path + root.children[i].name + "/");
		}
	}

	//添加监听事件
	handlePartAdded(addHandNode: Node, name: string) {
		//如果以_tap_+节点名字 并且有按钮组件的 默认注册点击监听事件
		let itemClick = this["_tap_" + name];
		if (itemClick) {
			this.addBtnListen(addHandNode, this, itemClick);
		}
	}

	//添加按钮监听事件
	public addBtnListen(nodView, caller, cb) {
		var button = nodView.getComponent(cc.Button);
		if (!button) {
			return;
		}
		nodView.on("click", cb, caller);
	}

	public get m_view() {
		return this.view;
	}


	//图片适配
	public photoScare(node, maxNum = 110) {
		// var maxNum = 110;
		let maxSize = Math.min(maxNum / node.height, maxNum / node.width);
		if (node.perScale == undefined) {
			node.perScale = node.scaleX;
		} else {
			node.scaleX = node.perScale;
			node.scaleY = node.perScale;
		}
		// node.scaleX *= maxSize;
		// node.scaleY *= maxSize;
		node.width = maxSize * node.width;
		node.height = maxSize * node.height;
	}

}