Skip to content

Commit

Permalink
inspector adds prefab and spine previews (cocos#17855)
Browse files Browse the repository at this point in the history
  • Loading branch information
knoxHuang authored and qiuguohua committed Nov 26, 2024
1 parent 17b31fe commit 94c4b63
Show file tree
Hide file tree
Showing 9 changed files with 677 additions and 34 deletions.
1 change: 0 additions & 1 deletion cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,6 @@ export class Skeleton extends UIRenderer {
*/
public updateAnimation (dt: number): void {
this.markForUpdateRenderData();
if (EDITOR_NOT_IN_PREVIEW) return;
if (this.paused) return;
if (this.isAnimationCached()) {
// On realTime mode, dt is multiplied at native side.
Expand Down
8 changes: 8 additions & 0 deletions editor/i18n/en/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module.exports = {
inspector: {
cloneToEdit: 'Clone it. Use and go into edit.',
cloneToDirectoryIllegal: 'Please limit the saved path to the current project assets path',
preview: {
header: 'Preview',
},
spine: {
skin: 'Skin',
animation: 'Animation',
loop: 'loop',
},
},

assets: {
Expand Down
8 changes: 8 additions & 0 deletions editor/i18n/zh/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module.exports = {
inspector: {
cloneToEdit: '克隆出新资源,使用并编辑',
cloneToDirectoryIllegal: '保存路径请限制在当前项目 /assets 路径内',
preview: {
header: '预览',
},
spine: {
skin: '皮肤',
animation: '动画',
loop: '循环',
},
},

assets: {
Expand Down
3 changes: 3 additions & 0 deletions editor/inspector/assets-footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ module.exports = {
gltf: join(__dirname, './assets/fbx/preview.js'), // reuse
'gltf-mesh': join(__dirname, './assets/mesh-preview.js'),
'gltf-skeleton': join(__dirname, './assets/skeleton-preview.js'),
'gltf-scene': join(__dirname, './assets/prefab-preview.js'),
prefab: join(__dirname, './assets/prefab-preview.js'),
'spine-data': join(__dirname, './assets/spine-preview.js'),
};
25 changes: 18 additions & 7 deletions editor/inspector/assets/image-preview.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
'use strict';

exports.template = /* html */`
<div class="image-preview">
<ui-asset-image class="image" size="origin"></ui-asset-image>
<div class="label">
<span class="size"></span>
<ui-section header="i18n:ENGINE.inspector.preview.header" class="preview-section config no-padding" expand>
<div class="image-preview">
<div class="image-content">
<ui-asset-image class="image" size="origin"></ui-asset-image>
</div>
<div class="label">
<span class="size"></span>
</div>
</div>
</div>
</ui-section>
`;

exports.style = /* css */`
.preview-section {
margin-top: 0px;
}
.image-preview {
position: relative;
display: flex;
}
.image-preview > .image-content {
height: var(--inspector-footer-preview-height, 200px);
background: var(--color-normal-fill-emphasis);
display: flex;
position: relative;
box-sizing: border-box;
}
.image-preview > .image {
width: 100%;
height: 100%;
Expand Down Expand Up @@ -85,6 +95,7 @@ exports.update = function(assetList, metaList) {
this.asset = assetList[0];
this.meta = metaList[0];

// 如何多选就隐藏预览
if (assetList.length > 1) {
this.$.container.style.display = 'none';
} else {
Expand Down
49 changes: 30 additions & 19 deletions editor/inspector/assets/mesh-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@


exports.template = /* html */`
<div class="preview">
<div class="info">
<ui-label value="Vertices:0" class="vertices"></ui-label>
<ui-label value="Triangles:0" class="triangles"></ui-label>
<div class="select-box">
<ui-select class="preview-channel">
</ui-select>
<ui-select class="preview-type">
</ui-select>
<ui-section header="i18n:ENGINE.inspector.preview.header" class="preview-section config no-padding" expand>
<div class="preview">
<div class="info">
<ui-label value="Vertices:0" class="vertices"></ui-label>
<ui-label value="Triangles:0" class="triangles"></ui-label>
<div class="select-box">
<ui-select class="preview-channel">
</ui-select>
<ui-select class="preview-type">
</ui-select>
</div>
<div>
<ui-label value="" class="minPosLabel"></ui-label>
</div>
<div>
<ui-label value="" class="maxPosLabel"></ui-label>
</div>
</div>
<div>
<ui-label value="" class="minPosLabel"></ui-label>
<div class="image">
<canvas class="canvas"></canvas>
</div>
<div>
<ui-label value="" class="maxPosLabel"></ui-label>
</div>
</div>
<div class="image">
<canvas class="canvas"></canvas>
</div>
</div>
</ui-section>
`;

exports.style = /* css */`
.preview-section {
margin-top: 0px;
}
.preview {
border-top: 1px solid var(--color-normal-border);
}
.preview > .info {
Expand Down Expand Up @@ -292,6 +296,13 @@ exports.update = function(assetList, metaList) {
this.asset = assetList[0];
this.meta = metaList[0];

// 如何多选就隐藏预览
if (assetList.length > 1) {
this.$.container.style.display = 'none';
} else {
this.$.container.style.display = 'block';
}

for (const prop in Elements) {
const element = Elements[prop];
if (element.update) {
Expand Down
206 changes: 206 additions & 0 deletions editor/inspector/assets/prefab-preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
'use strict';

exports.template = /* html */`
<ui-section header="i18n:ENGINE.inspector.preview.header" class="preview-section config no-padding" expand>
<div class="preview">
<div class="image">
<canvas class="canvas"></canvas>
</div>
</div>
</ui-section>
`;

exports.style = /* css */`
.preview-section {
margin-top: 0px;
}
.preview { }
.preview > .image {
height: var(--inspector-footer-preview-height, 200px);
overflow: hidden;
display: flex;
flex: 1;
}
.preview >.image > .canvas {
flex: 1;
}
`;


exports.$ = {
container: '.preview',
canvas: '.canvas',
image: '.image',
};

async function callFunction(funcName, ...args) {
return await Editor.Message.request('scene', 'call-preview-function', 'scene:prefab-preview', funcName, ...args);
}

const Elements = {
preview: {
ready() {
const panel = this;

let _isPreviewDataDirty = false;
Object.defineProperty(panel, 'isPreviewDataDirty', {
get() {
return _isPreviewDataDirty;
},
set(value) {
if (value !== _isPreviewDataDirty) {
_isPreviewDataDirty = value;
value && panel.refreshPreview();
}
},
});
panel.$.canvas.addEventListener('mousedown', async (event) => {
await callFunction('onMouseDown', { x: event.x, y: event.y, button: event.button });

async function mousemove(event) {
await callFunction('onMouseMove', {
movementX: event.movementX,
movementY: event.movementY,
});

panel.isPreviewDataDirty = true;
}

async function mouseup(event) {
await callFunction('onMouseUp', {
x: event.x,
y: event.y,
});

document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mouseup', mouseup);

panel.isPreviewDataDirty = false;
}

document.addEventListener('mousemove', mousemove);
document.addEventListener('mouseup', mouseup);


panel.isPreviewDataDirty = true;
});

panel.$.canvas.addEventListener('wheel', async (event) => {
await callFunction('onMouseWheel', {
wheelDeltaY: event.wheelDeltaY,
wheelDeltaX: event.wheelDeltaX,
});
panel.isPreviewDataDirty = true;
});


const GlPreview = Editor._Module.require('PreviewExtends').default;
panel.glPreview = new GlPreview('scene:prefab-preview', 'query-prefab-preview-data');

function observer() {
panel.isPreviewDataDirty = true;
}

panel.resizeObserver = new window.ResizeObserver(observer);
panel.resizeObserver.observe(panel.$.image);
observer();
},
async update() {
const panel = this;

if (!panel.$.canvas) {
return;
}

await panel.glPreview.init({ width: panel.$.canvas.clientWidth, height: panel.$.canvas.clientHeight });
await callFunction('setPrefab', panel.asset.uuid);
this.isPreviewDataDirty = true;
},
close() {
const panel = this;

panel.resizeObserver.unobserve(panel.$.image);
},
},
};

exports.methods = {
async refreshPreview() {
const panel = this;

// After await, the panel no longer exists
if (!panel.$.canvas) {
return;
}

const doDraw = async () => {
try {
const canvas = panel.$.canvas;
const image = panel.$.image;

const width = image.clientWidth;
const height = image.clientHeight;
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;

await panel.glPreview.initGL(canvas, { width, height });
await panel.glPreview.resizeGL(width, height);
}

const info = await panel.glPreview.queryPreviewData({
width: canvas.width,
height: canvas.height,
});

panel.glPreview.drawGL(info);
} catch (e) {
console.warn(e);
}
};

requestAnimationFrame(async () => {
await doDraw();
panel.isPreviewDataDirty = false;
});
},
};

exports.ready = function() {
for (const prop in Elements) {
const element = Elements[prop];
if (element.ready) {
element.ready.call(this);
}
}
};

exports.update = function(assetList, metaList) {
this.assetList = assetList;
this.metaList = metaList;
this.asset = assetList[0];
this.meta = metaList[0];

// 如何多选就隐藏预览
if (assetList.length > 1) {
this.$.container.style.display = 'none';
} else {
this.$.container.style.display = 'block';
}

for (const prop in Elements) {
const element = Elements[prop];
if (element.update) {
element.update.call(this);
}
}
};

exports.close = function() {
for (const prop in Elements) {
const element = Elements[prop];
if (element.close) {
element.close.call(this);
}
}
};
Loading

0 comments on commit 94c4b63

Please sign in to comment.