fix editing fbx animation events interface bug (#15718)

* fix the bug of changing select fbx, the preview interface did not update

* fix editing fbx animation events interface bug
This commit is contained in:
黄森斌
2023-07-26 18:00:51 +08:00
committed by GitHub
parent 30061b151d
commit cd90419f2e
6 changed files with 220 additions and 161 deletions
@@ -6,15 +6,15 @@ const eventItem = require('./event-item');
exports.template = `<section v-if="show" id="event-editor" @mousedown.stop>
<header class="flex">
<ui-label class="f1" :value="'Animation Event (frame: ' + RealFrame + ' )'"></ui-label>
<ui-icon value="close" @click="show = false"></ui-icon>
<ui-label class="title" :value="'Current Animation Frame: ' + RealFrame"></ui-label>
<ui-icon value="close" @click="hide" tooltip="Close"></ui-icon>
</header>
<div class="functions">
<div class="tools flex">
<ui-input class="f1" placeholder="i18n:animator.event.func_placeholder"
tooltip="i18n:animator.event.func_placeholder"
:value="newFuncName"
@change.stop="newFuncName = $event.target.value"
@change.stop="newFuncName = $event.target.value.trim()"
@keydown.enter="addFunc"
></ui-input>
<ui-icon value="add"
@@ -29,6 +29,7 @@ exports.template = `<section v-if="show" id="event-editor" @mousedown.stop>
:index="index"
:key="index"
@update="updateValue"
@delete="deleteValue"
></event-item>
</div>
<div v-if="!value.length" class="empty">
@@ -50,8 +51,8 @@ exports.data = function() {
dirty: false,
debounceSave: null,
// time value
frame: 0,
RealFrame: 0,
frame: -1,
RealFrame: -1,
show: false,
};
};
@@ -71,26 +72,27 @@ exports.methods = {
that.showToast(Editor.I18n.t('animator.event.enter_func_name'));
return;
}
that.value.push({
func: that.newFuncName,
params: [],
frame: that.frame,
that.$emit('addFunc', that.frame, that.newFuncName);
setTimeout(() => {
that.newFuncName = '';
});
that.dirty = true;
that.debounceSave();
},
updateValue(eventInfo, index) {
const that = this;
if (!eventInfo) {
that.value.splice(index, 1);
} else {
that.value[index] = eventInfo;
}
that.value[index] = eventInfo;
that.dirty = true;
that.debounceSave();
},
deleteValue(index) {
const that = this;
const eventInfo = that.value[index];
if (eventInfo) {
that.$emit('delFunc', eventInfo.frame, eventInfo);
}
},
showToast(msg, time = 800) {
const that = this;
if (that.toast) {
@@ -106,23 +108,37 @@ exports.methods = {
}, time);
},
async saveData() {
saveData() {
const that = this;
that.$emit('update', that.frame, that.value);
that.$emit('update', that.value);
that.dirty = false;
},
refresh() {
refresh(events) {
const that = this;
let data = that.events.filter((item) => {
return item.frame === that.frame;
});
if (data.length < 1) {
that.value = [];
return;
const infos = [];
if (Array.isArray(events)) {
if (!events.length) {
this.frame = -1;
}
events.forEach((item) => {
if (item.info.frame === that.frame) {
infos.push(item.info);
}
});
}
that.value = JSON.parse(JSON.stringify(data));
that.newFuncName = '';
that.value = infos;
that.show = !!infos.length;
},
hide() {
this.$emit('hide');
},
unselect() {
this.show = false;
},
};
@@ -80,16 +80,6 @@ exports.props = [
'index',
];
exports.computed = {
selectEvent() {
const that = this;
if (!that.selectInfo) {
return null;
}
return that.selectInfo.data;
},
};
exports.methods = {
onConfirm(event) {
const that = this;
@@ -126,27 +116,20 @@ exports.methods = {
const that = this;
const name = event.target.getAttribute('name');
let index = event.target.getAttribute('index');
const eventInfo = that.event;
let dirty = false;
switch (name) {
case 'delFunc':
that.$emit('update', null, that.index);
that.$emit('delete', that.index);
return;
case 'addParams':
dirty = true;
eventInfo.params.splice(eventInfo.params.length - 1, 0, 'param');
that.event.params.splice(that.event.params.length - 1, 0, 'param');
break;
case 'delParams':
dirty = true;
eventInfo.params.splice(index, 1);
that.event.params.splice(index, 1);
break;
case 'clearParams':
if (eventInfo.params.length) {
dirty = true;
eventInfo.params = [];
}
that.event.params = [];
break;
}
dirty && that.$emit('update', that.event, that.index);
that.$emit('update', that.event, that.index);
},
};
@@ -14,8 +14,19 @@ exports.ready = function() {
components: eventEditor.components,
mounted: eventEditor.mounted,
});
this.eventEditorVm.$on('update', (frame, eventInfos) => {
this.events.updateEventInfo.call(this, frame, eventInfos);
this.eventEditorVm.$on('update', (eventInfos) => {
this.events.updateEventInfo.call(this, eventInfos);
});
this.eventEditorVm.$on('addFunc', (frame, newFuncName) => {
this.events.addEvent.call(this, frame, newFuncName);
});
this.eventEditorVm.$on('delFunc', (frame, eventInfo) => {
this.events.delEvent.call(this, frame, eventInfo);
});
this.eventEditorVm.$on('hide', (frame, eventInfo) => {
this.events.unselect.call(this, frame, eventInfo);
});
};
+35 -62
View File
@@ -4,25 +4,16 @@ exports.template = /* html */`
<div class="events" :style="'transform: translateX(' + offset + 'px)'">
<template
v-if="events"
v-for="(info, index) in events"
v-for="(event, index) in events"
>
<ui-icon value="event"
:style="queryKeyStyle(info.x)"
:active="selectInfo.frames.includes(info.frame)"
:style="queryKeyStyle(event.x)"
:active="frame===event.info.frame"
:index="index"
name="event"
@mousedown="onMouseDown($event, info)"
@click.right="onPopMenu($event, info)"
@dblclick="openEventEditor(info)"
></ui-icon>
</template>
<template
v-if="selectEvent"
v-for="(info, index) in selectEvent"
>
<ui-icon value="event" class="preview" color="true"
name="event"
:style="queryKeyStyle(info.x)"
@mousedown.left="onMouseDown($event, event)"
@click.right="onPopMenu($event, event.info)"
@dblclick="openEventEditor(event.info)"
></ui-icon>
</template>
</div>
@@ -31,18 +22,7 @@ exports.template = /* html */`
exports.data = {
events: [],
offset: 0,
selectEvent: [],
eventEditorInfo: {
frame: 0,
events: [{
func: '',
params: [],
frame: 0,
}],
},
selectInfo: {
frames: [],
},
frame: -1,
};
exports.methods = {
@@ -53,45 +33,40 @@ exports.methods = {
onPopMenu(event, eventInfo) {
const that = this;
const menu = [{
label: Editor.I18n.t(`animator.event.edit`),
click() {
that.openEventEditor(eventInfo);
},
}, {
label: Editor.I18n.t(`animator.event.delete`),
click() {
that.$emit('del', eventInfo);
},
},
];
}];
Editor.Menu.popup({
x: event.pageX,
y: event.pageY,
menu,
});
},
onMouseDown(event, info) {
onMouseDown(event, item) {
const that = this;
event.stopPropagation();
const data = JSON.parse(JSON.stringify(info));
let selectIndex = that.selectInfo && that.selectInfo.frames.indexOf(info.frame);
if (typeof selectIndex !== 'number' || selectIndex === -1) {
that.selectInfo = {
startX: event.x,
data: [data],
offset: 0,
offsetFrame: 0,
frames: [info.frame],
};
} else {
that.selectInfo.startX = event.x;
this.frame = item.info.frame;
const clientX = event.clientX;
const startX = item.x;
function mousemove(event) {
const timelineX = startX + (event.clientX - clientX);
that.$emit('move', item, timelineX);
}
function mouseup() {
that.$emit('moveEnd', item);
document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mouseup', mouseup);
}
document.addEventListener('mousemove', mousemove);
document.addEventListener('mouseup', mouseup);
},
openEventEditor(eventInfo) {
// HACK 目前的事件帧会有重复关键帧重叠的情况
this.selectInfo.frames = [eventInfo.frame];
// 目前的事件帧会有重复关键帧重叠的情况
this.frame = eventInfo.frame;
this.$emit('edit', eventInfo);
},
@@ -99,17 +74,15 @@ exports.methods = {
return `transform: translateX(${x || 0}px);`;
},
onDomMouseDown() {
this.selectInfo = {
frames: [],
};
refresh(eventInfos) {
this.events = eventInfos;
if (!eventInfos.some((item) => item.info.frame === this.frame)) {
this.unselect();
}
},
unselect() {
this.frame = -1;
},
};
exports.mounted = function() {
document.addEventListener('mousedown', this.onDomMouseDown);
};
exports.beforeDestroy = function() {
document.removeEventListener('mousedown', this.onDomMouseDown);
};
+81 -28
View File
@@ -20,11 +20,9 @@ exports.ready = function() {
if (this.checkDisabledEditEvent()) {
return;
}
this.eventEditorVm.events = this.eventVm.events;
this.eventEditorVm.frame = eventInfo.frame;
this.eventEditorVm.RealFrame = Math.round(eventInfo && eventInfo.frame * this.curEditClipInfo.fps || 0);
this.eventEditorVm.refresh();
this.eventEditorVm.show = true;
this.eventEditorVm.refresh(this.eventVm.events);
});
this.eventVm.$on('del', (eventInfo) => {
if (this.checkDisabledEditEvent()) {
@@ -32,10 +30,26 @@ exports.ready = function() {
}
this.events.delEvent.call(this, eventInfo.frame);
});
this.eventVm.$on('move', (eventItem, timelineX) => {
if (this.checkDisabledEditEvent()) {
return;
}
this.events.moveEvent.call(this, eventItem, timelineX);
});
this.eventVm.$on('moveEnd', (eventItem) => {
if (this.checkDisabledEditEvent()) {
return;
}
this.eventVm.openEventEditor(eventItem.info);
this.dispatch('snapshot');
});
};
exports.update = function(eventInfo) {
this.eventVm.events = eventInfo;
exports.update = function(eventInfos) {
this.eventVm.refresh(eventInfos);
this.eventEditorVm.refresh(eventInfos);
};
exports.apply = async function() {
@@ -52,45 +66,84 @@ exports.apply = async function() {
this.events.eventsMap = {};
};
exports.addNewEvent = function(time) {
exports.addEvent = function(time, newFuncName = '') {
const newInfo = {
// 注意: frame 是时间
frame: time,
func: '',
func: newFuncName,
params: [],
};
const userData = this.curEditClipInfo.userData;
if (!userData.events) {
userData.events = [newInfo];
} else {
userData.events.push(newInfo);
userData.events.sort((a, b) => a.frame - b.frame);
// 已经存在空记录
if (!newFuncName && userData.events.some(event => event.frame === time && event.func === newFuncName)) {
return;
}
let exist = false;
for (const event of userData.events) {
if (event.frame === time && event.func === '') {
event.func = newFuncName;
exist = true;
break;
}
}
if (!exist) {
userData.events.push(newInfo);
userData.events.sort((a, b) => a.frame - b.frame);
}
}
this.events.eventsMap[this.curEditClipInfo.clipUUID] = userData.events;
this.updateEventInfo();
this.dispatch('change');
this.dispatch('snapshot');
this.eventVm.openEventEditor(newInfo);
};
exports.delEvent = function(frame, info) {
const userData = this.curEditClipInfo.userData;
if (info) {
userData.events = userData.events.filter((item) => item !== info);
} else {
// 删除多个
userData.events = userData.events.filter((item) => item.frame !== frame);
}
this.updateEventInfo();
this.dispatch('change');
this.dispatch('snapshot');
};
exports.delEvent = function(time) {
exports.moveEvent = function(eventItem, timelineX) {
const frame = Math.min(this.$.animationTime._config.max, Math.max(0, Math.round(this.$.animationTime.pixelToValue(timelineX))));
eventItem.info.frame = frame / this.curEditClipInfo.fps;
eventItem.x = this.$.animationTime.valueToPixel(frame);
this.dispatch('change');
};
exports.unselect = function() {
this.eventVm.unselect();
this.eventEditorVm.unselect();
};
exports.updateEventInfo = function(eventInfos) {
const userData = this.curEditClipInfo.userData;
userData.events = userData.events.filter((item) => item.frame !== time);
this.events.eventsMap[this.curEditClipInfo.clipUUID] = userData.events;
if (userData && userData.events && Array.isArray(eventInfos)) {
eventInfos.forEach((eventInfo) => {
if (!userData.events.includes(eventInfo)) {
userData.events.push(eventInfo);
}
});
}
this.updateEventInfo();
this.dispatch('change');
this.dispatch('snapshot');
};
exports.updateEventInfo = function(time, eventInfos) {
const userData = this.curEditClipInfo.userData;
let newEvents = [];
if (userData.events) {
newEvents = userData.events.filter((item) => item.frame !== time);
}
newEvents.push(...eventInfos);
userData.events = newEvents;
this.events.eventsMap[this.curEditClipInfo.clipUUID] = newEvents;
this.updateEventInfo();
this.dispatch('change');
this.dispatch('update');
};
+43 -20
View File
@@ -27,7 +27,7 @@ exports.template = /* html */`
<ui-icon value="event" name="add_event"></ui-icon>
</div>
<div class="time flex toolbar f1">
<ui-label value="Time"></ui-label>
<ui-label value="Frame"></ui-label>
<ui-num-input id="currentTime"></ui-num-input>
<div class="duration"></div>
</div>
@@ -168,6 +168,7 @@ ui-icon {
background: var(--color-normal-fill);
}
#event-editor {
position: relative;
line-height: 20px;
width: 100%;
height: 70%;
@@ -186,6 +187,9 @@ ui-icon {
background: var(--color-normal-fill-emphasis);
padding: 2px 4px;
}
#event-editor > header .title {
color: var(--color-focus-fill);
}
#event-editor > .header .name {
margin: 0 4px;
}
@@ -194,7 +198,6 @@ ui-icon {
margin: 0 2px;
}
#event-editor > .functions {
padding: 4px;
overflow-y: auto;
flex: 1;
height: 100%;
@@ -234,6 +237,7 @@ ui-icon {
#event-editor ui-checkbox,
#event-editor ui-num-input {
flex: 1;
margin-left: 4px;
}
#event-editor ui-section {
width: 100%;
@@ -259,6 +263,15 @@ ui-icon {
font-style: italic;
text-align: center;
}
#event-editor .toast {
position: absolute;
top: 54px;
right: 4px;
z-index: 1;
padding: 0 4px;
background-color: var(--color-normal-fill-emphasis);
color: var(--color-warn-fill);
}
`;
exports.$ = {
@@ -484,7 +497,11 @@ exports.methods = {
async onTabChanged(activeTab) {
if (typeof activeTab === 'string') {
this.activeTab = activeTab;
this.$.animationInfo.style.display = this.activeTab === 'animation' ? 'block' : 'none';
const isAnimationTab = this.activeTab === 'animation';
this.$.animationInfo.style.display = isAnimationTab ? 'block' : 'none';
if (!isAnimationTab) {
this.eventEditorVm.show = false;
}
this.$.modelInfo.style.display = this.activeTab === 'model' ? 'block' : 'none';
await this.stopAnimation();
}
@@ -534,19 +551,22 @@ exports.methods = {
},
addEventToCurTime() {
this.events.addNewEvent.call(this, this.$.animationTime.value / this.curEditClipInfo.fps);
this.events.addEvent.call(this, this.$.animationTime.value / this.curEditClipInfo.fps);
},
updateEventInfo() {
let eventInfos = [];
const events = this.curEditClipInfo.userData.events;
if (Array.isArray(events)) {
eventInfos = events.map((info) => {
return {
...info,
x: this.$.animationTime.valueToPixel(info.frame * this.curEditClipInfo.fps),
};
});
if (this.curEditClipInfo && this.curEditClipInfo.userData) {
const events = this.curEditClipInfo.userData.events;
if (Array.isArray(events)) {
eventInfos = events.map((info) => {
return {
info,
x: this.$.animationTime.valueToPixel(info.frame * this.curEditClipInfo.fps),
};
});
}
}
this.events.update.call(this, eventInfos);
@@ -633,22 +653,16 @@ exports.methods = {
},
async setCurEditClipInfo(clipInfo) {
this.curEditClipInfo = clipInfo;
this.curTotalFrames = 0;
if (clipInfo) {
this.curTotalFrames = Math.round(clipInfo.duration * clipInfo.fps);
this.$.animationTime.setConfig({
max: this.curTotalFrames,
});
this.$.duration.innerHTML = `Duration: ${this.curTotalFrames}`;
// update animation events, clipInfo.clipUUID may be undefined
if (clipInfo.clipUUID) {
const subId = clipInfo.clipUUID.match(/@(.*)/)[1];
this.curEditClipInfo.userData = this.meta.subMetas[subId] && this.meta.subMetas[subId].userData || {};
this.updateEventInfo();
}
if (this.$.animationTimeSlider) {
this.$.animationTimeSlider.max = this.curTotalFrames;
}
await callModelPreviewFunction(
'setPlaybackRange',
clipInfo.from,
@@ -665,6 +679,12 @@ exports.methods = {
await this.stopAnimation();
}
this.$.animationTime.setConfig({
max: this.curTotalFrames,
});
this.$.duration.innerHTML = `Totals: ${this.curTotalFrames}`;
this.updateEventInfo();
},
onAnimationPlayStateChanged(state) {
this.setCurPlayState(state);
@@ -757,7 +777,10 @@ exports.update = async function(assetList, metaList) {
this.splitClipIndex = 0;
const clipInfo = animation.methods.getCurClipInfo.call(this);
await this.onEditClipInfoChanged(clipInfo);
} else {
await this.setCurEditClipInfo();
}
this.eventEditorVm.show = false;
this.setCurPlayState(PLAY_STATE.STOP);
this.isPreviewDataDirty = true;
this.refreshPreview();