init
This commit is contained in:
153
animator-editor/assets/script/editor/parameters/ParamCtr.ts
Normal file
153
animator-editor/assets/script/editor/parameters/ParamCtr.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import Events, { EventName, preloadEvent } from "../../common/util/Events";
|
||||
import RecyclePool from "../../common/util/RecyclePool";
|
||||
import Res from "../../common/util/Res";
|
||||
import { ParameterData } from "../../constant/BaseConst";
|
||||
import { ResUrl } from "../../constant/ResUrl";
|
||||
import ParamItem from "./ParamItem";
|
||||
|
||||
const { ccclass, property } = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class ParamCtr extends cc.Component {
|
||||
@property(cc.Node) ParamContent: cc.Node = null;
|
||||
@property(cc.Node) SelectPosNode: cc.Node = null;
|
||||
|
||||
protected onLoad() {
|
||||
Events.targetOn(this);
|
||||
}
|
||||
|
||||
protected onEnable() {
|
||||
// 屏蔽scrollView内部touch事件,防止与拖拽行为产生冲突
|
||||
let scrollView = this.getComponent(cc.ScrollView);
|
||||
scrollView.node.off(cc.Node.EventType.TOUCH_START, scrollView['_onTouchBegan'], scrollView, true);
|
||||
scrollView.node.off(cc.Node.EventType.TOUCH_MOVE, scrollView['_onTouchMoved'], scrollView, true);
|
||||
scrollView.node.off(cc.Node.EventType.TOUCH_END, scrollView['_onTouchEnded'], scrollView, true);
|
||||
scrollView.node.off(cc.Node.EventType.TOUCH_CANCEL, scrollView['_onTouchCancelled'], scrollView, true);
|
||||
}
|
||||
|
||||
protected onDestroy() {
|
||||
Events.targetOff(this);
|
||||
}
|
||||
|
||||
//#region import and export
|
||||
/**
|
||||
* 导入数据
|
||||
*/
|
||||
public import(arr: ParameterData[]) {
|
||||
arr.forEach((data: ParameterData) => {
|
||||
let node = this.getParamItem();
|
||||
this.ParamContent.addChild(node);
|
||||
let pi = node.getComponent(ParamItem);
|
||||
pi.onInit(data.param, data.type, data.init);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据
|
||||
*/
|
||||
public export(): ParameterData[] {
|
||||
let arr: ParameterData[] = [];
|
||||
this.ParamContent.children.forEach((e) => {
|
||||
let pi = e.getComponent(ParamItem);
|
||||
let data: ParameterData = {
|
||||
param: pi.paramName,
|
||||
type: pi.type,
|
||||
init: pi.init
|
||||
};
|
||||
arr.push(data);
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
public getParamMap() {
|
||||
let map: Map<string, ParamItem> = new Map();
|
||||
this.ParamContent.children.forEach((e) => {
|
||||
let pi = e.getComponent(ParamItem);
|
||||
map.set(pi.paramName, pi);
|
||||
});
|
||||
return map;
|
||||
}
|
||||
|
||||
private getParamItem() {
|
||||
let prefab = Res.getLoaded(ResUrl.PREFAB.PARAM_ITEM);
|
||||
let node: cc.Node = RecyclePool.get(ParamItem) || cc.instantiate(prefab);
|
||||
node.width = this.ParamContent.width;
|
||||
return node;
|
||||
}
|
||||
|
||||
private putParamItem(node: cc.Node) {
|
||||
RecyclePool.put(ParamItem, node);
|
||||
}
|
||||
|
||||
public getParamItemByName(name: string) {
|
||||
for (let i = 0; i < this.ParamContent.childrenCount; i++) {
|
||||
let pi = this.ParamContent.children[i].getComponent(ParamItem);
|
||||
if (name === pi.paramName) {
|
||||
return pi;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个不重复的参数名
|
||||
*/
|
||||
public getParamName(paramItem: ParamItem, name: string = 'param'): string {
|
||||
let index = 0;
|
||||
let findName = false;
|
||||
|
||||
while (!findName) {
|
||||
findName = true;
|
||||
for (let i = 0; i < this.ParamContent.childrenCount; i++) {
|
||||
let pi = this.ParamContent.children[i].getComponent(ParamItem);
|
||||
if (pi === paramItem) {
|
||||
continue;
|
||||
}
|
||||
if (pi.paramName === `${name}${index > 0 ? index : ''}`) {
|
||||
index++;
|
||||
findName = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return `${name}${index > 0 ? index : ''}`;
|
||||
}
|
||||
|
||||
private onClickAddParam(event: cc.Event, typeStr: string) {
|
||||
Events.emit(EventName.CLOSE_MENU);
|
||||
|
||||
let type = parseInt(typeStr);
|
||||
let node = this.getParamItem();
|
||||
this.ParamContent.addChild(node);
|
||||
let pi = node.getComponent(ParamItem);
|
||||
pi.onInit(this.getParamName(pi), type);
|
||||
}
|
||||
|
||||
private onClickOpenSelect() {
|
||||
Events.emit(EventName.SHOW_PARAM_ADD, this.SelectPosNode.parent.convertToWorldSpaceAR(this.SelectPosNode.position));
|
||||
}
|
||||
|
||||
@preloadEvent(EventName.PARAM_DELETE)
|
||||
private onEventParamDelete(item: ParamItem) {
|
||||
this.putParamItem(item.node);
|
||||
}
|
||||
|
||||
@preloadEvent(EventName.PARAM_SELECT)
|
||||
private onEventParamSelect(item: ParamItem) {
|
||||
this.ParamContent.children.forEach((e) => {
|
||||
let ti = e.getComponent(ParamItem);
|
||||
ti.select(ti === item);
|
||||
});
|
||||
}
|
||||
|
||||
@preloadEvent(EventName.RESIZE)
|
||||
private onEventResize(node: cc.Node) {
|
||||
if (node !== this.node) {
|
||||
return;
|
||||
}
|
||||
this.ParamContent.children.forEach((e) => {
|
||||
e.width = this.ParamContent.width;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "86c89dff-cf3e-406e-a9d5-3da7cd5e86cf",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
128
animator-editor/assets/script/editor/parameters/ParamItem.ts
Normal file
128
animator-editor/assets/script/editor/parameters/ParamItem.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import Events, { EventName } from "../../common/util/Events";
|
||||
import { RecycleNode } from "../../common/util/RecyclePool";
|
||||
import { ParamType } from "../../constant/BaseConst";
|
||||
import Editor from "../Editor";
|
||||
|
||||
const { ccclass, property } = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class ParamItem extends cc.Component implements RecycleNode {
|
||||
@property(cc.Node) Bg: cc.Node = null;
|
||||
@property(cc.EditBox) NameEdit: cc.EditBox = null;
|
||||
@property(cc.Node) InitValue: cc.Node = null;
|
||||
|
||||
/** 组件是否初始化完成 */
|
||||
private _hasInit: boolean = false;
|
||||
|
||||
private _paramName: string = '';
|
||||
/** 参数名 */
|
||||
public get paramName() {
|
||||
return this._paramName;
|
||||
}
|
||||
public set paramName(v: string) {
|
||||
if (this._paramName === v) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._paramName = v;
|
||||
this.NameEdit.string = v;
|
||||
Events.emit(EventName.PARAM_NAME_CHANGED, this);
|
||||
}
|
||||
|
||||
/** 参数类型 */
|
||||
public type: ParamType = null;
|
||||
/** 初始值 */
|
||||
public init: number = 0;
|
||||
|
||||
public reuse() {
|
||||
}
|
||||
|
||||
public unuse() {
|
||||
this._hasInit = false;
|
||||
}
|
||||
|
||||
public onInit(param: string, type: ParamType, init: number = 0) {
|
||||
this.Bg.opacity = 0;
|
||||
this.paramName = param;
|
||||
this.type = type;
|
||||
this.init = init;
|
||||
this.showInitValue();
|
||||
|
||||
this._hasInit = true;
|
||||
}
|
||||
|
||||
protected onLoad() {
|
||||
this.Bg.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
|
||||
}
|
||||
|
||||
private showInitValue() {
|
||||
this.InitValue.children.forEach((e, index) => {
|
||||
e.active = (this.type === (index + 1));
|
||||
if (e.active) {
|
||||
if (this.type === ParamType.NUMBER) {
|
||||
e.getComponent(cc.EditBox).string = `${this.init}`;
|
||||
} else {
|
||||
e.getComponent(cc.Toggle).isChecked = this.init === 1 ? true : false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private onTouchStart() {
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选中
|
||||
*/
|
||||
public select(value: boolean) {
|
||||
this.Bg.opacity = value ? 255 : 0;
|
||||
}
|
||||
|
||||
private onNameEditBegan() {
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
private onNameChanged() {
|
||||
if (this.NameEdit.string === '') {
|
||||
this.NameEdit.string = this.paramName;
|
||||
return;
|
||||
}
|
||||
|
||||
this.NameEdit.string = Editor.Inst.ParamCtr.getParamName(this, this.NameEdit.string);
|
||||
this.paramName = this.NameEdit.string;
|
||||
}
|
||||
|
||||
private onNumberEditBegan() {
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
private onNumberChanged(str: string, edit: cc.EditBox) {
|
||||
str = str.replace(/[^-.\d]/g, '');
|
||||
if (str === '') {
|
||||
return;
|
||||
}
|
||||
let num = parseFloat(str);
|
||||
this.init = isNaN(num) ? 0 : num;
|
||||
edit.string = `${this.init}`;
|
||||
}
|
||||
|
||||
private onBooleanCheckd(toggle: cc.Toggle) {
|
||||
this.init = toggle.isChecked ? 1 : 0;
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
private onTriggerCheckd(toggle: cc.Toggle) {
|
||||
this.init = toggle.isChecked ? 1 : 0;
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
private onAutoTriggerCheckd(toggle: cc.Toggle) {
|
||||
this.init = toggle.isChecked ? 1 : 0;
|
||||
this._hasInit && Events.emit(EventName.PARAM_SELECT, this);
|
||||
}
|
||||
|
||||
private onClickDelete() {
|
||||
Events.emit(EventName.PARAM_DELETE, this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "37ea8c36-974d-41ce-bcae-451ac3ca5776",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
Reference in New Issue
Block a user