diff --git a/editor/i18n/en/assets.js b/editor/i18n/en/assets.js
index 41ca76762d..65033f0e9e 100644
--- a/editor/i18n/en/assets.js
+++ b/editor/i18n/en/assets.js
@@ -11,6 +11,7 @@ module.exports = {
preview: {
header: 'Preview',
resetCameraView: 'Reset camera view',
+ viewToggle: 'Toggle the 2D/3D view',
},
spine: {
skin: 'Skin',
diff --git a/editor/i18n/zh/assets.js b/editor/i18n/zh/assets.js
index ba4edd7a6d..ade9b30745 100644
--- a/editor/i18n/zh/assets.js
+++ b/editor/i18n/zh/assets.js
@@ -11,6 +11,7 @@ module.exports = {
preview: {
header: '预览',
resetCameraView: '复位摄像机视图',
+ viewToggle: '切换 2D/3D 视图',
},
spine: {
skin: '皮肤',
diff --git a/editor/inspector/utils/preview.js b/editor/inspector/utils/preview.js
index 82ffa6bc19..3ba52590a2 100644
--- a/editor/inspector/utils/preview.js
+++ b/editor/inspector/utils/preview.js
@@ -9,6 +9,25 @@ function hideElement(element, value) {
element.style.display = value ? 'none' : 'block';
}
+/**
+ * get panel frame element
+ * 获取 panel frame 元素
+ * @param container
+ * @returns {*}
+ */
+function getPanelFrameElement(container) {
+ let element = container;
+ while (element) {
+ element = element.parentElement || element.getRootNode().host;
+ if (element && element.tagName === 'PANEL-FRAME' && element.getAttribute('name') === 'inspector') {
+ break;
+ }
+ }
+ return element;
+}
+
+const UI_SECTION_BAR_HEIGHT = 26;
+
/**
* 处理展示预览以及操作预览面板的辅助类
* Helper classes that handle the Showcase Preview and Manipulation Preview panels
@@ -29,8 +48,8 @@ class PreviewControl {
_image = null;
_canvas = null;
- _toolbar = null;
_resetCamera = null;
+ _viewToggleButton = null;
_resizeObserver = null;
@@ -39,58 +58,66 @@ class PreviewControl {
_onMouseWheelBind = this._onMouseWheel.bind(this);
_onResetCameraBind = this._onResetCamera.bind(this);
+ _onViewToggleBind = this._onViewToggle.bind(this);
- enabledResetCamera = true;
+ async _updateToolElements() {
+ if (!this._container) { return; }
- /**
- * create
- * image
- * - toolbar
- * - button:reset-camera
- * - canvas
- * @param container - preview root
- * @private
- */
- _createElements(container) {
- this._container = container;
+ this.queryPreviewFunction('queryViewToolState')
+ .then(state => {
+ if (!state) { return; }
- this._image = document.createElement('div');
- this._image.classList.add('image');
- this._image.style = `
- height: var(--inspector-footer-preview-height, 200px);
- overflow: hidden;
- display: flex;
- flex: 1;
- `;
- this._container.appendChild(this._image);
+ let toolRight = 10;
+ if (state.enableResetCamera) {
+ this._resetCamera = this._container.querySelector('.reset-camera');
+ if (!this._resetCamera) {
+ this._resetCamera = document.createElement('ui-button');
+ this._resetCamera.classList.add('reset-camera');
+ this._container.appendChild(this._resetCamera);
+ this._resetCamera.setAttribute('type', 'icon');
+ this._resetCamera.setAttribute('tooltip', 'i18n:ENGINE.inspector.preview.resetCameraView');
+ this._resetCamera.innerHTML = ``;
+ this._resetCamera.addEventListener('click', this._onResetCameraBind);
+ }
+ this._resetCamera.style = `
+ position: absolute;
+ right: ${toolRight}px;
+ bottom: 10px;
+ height: 24px;
+ width: 24px;
+ `;
+ toolRight += 28;
+ }
- this._toolbar = document.createElement('div');
- this._toolbar.classList.add('toolbar');
- this._toolbar.style = `
- display: flex;
- `;
- this._image.appendChild(this._toolbar);
+ if (state.enableViewToggle) {
+ this._viewToggleButton = this._container.querySelector('.view-toggle');
+ if (!this._viewToggleButton) {
+ this._viewToggleButton = document.createElement('ui-button');
+ this._container.appendChild(this._viewToggleButton);
+ this._viewToggleButton.classList.add('view-toggle');
+ this._viewToggleButton.setAttribute('type', 'icon');
+ this._viewToggleButton.setAttribute('tooltip', 'i18n:ENGINE.inspector.preview.viewToggle');
+ this._viewToggleButton.addEventListener('click', this._onViewToggleBind);
+ }
+ this._viewToggleButton.style = `
+ position: absolute;
+ right: ${toolRight}px;
+ bottom: 10px;
+ height: 24px;
+ width: 24px;
+ `;
+ this._updateViewToggleIcon();
+ }
+ });
+ this._updateViewToggleIcon();
+ }
- if (this.enabledResetCamera) {
- this._resetCamera = document.createElement('ui-button');
- this._resetCamera.classList.add('reset-camera');
- this._resetCamera.setAttribute('type', 'icon');
- this._resetCamera.setAttribute('tooltip', 'i18n:ENGINE.inspector.preview.resetCameraView');
- this._resetCamera.style = `
- position: absolute;
- right: 10px;
- bottom: 10px;
- `;
- this._resetCamera.innerHTML = ``;
- this._toolbar.appendChild(this._resetCamera);
- }
+ _updateViewToggleIcon() {
+ if (!this._viewToggleButton) { return; }
- this._canvas = document.createElement('canvas');
- this._canvas.style = `
- flex: 1;
- `;
- this._canvas.classList.add('canvas');
- this._image.appendChild(this._canvas);
+ this.queryPreviewFunction('is2DView').then(is2D => {
+ this._viewToggleButton.innerHTML = ``;
+ });
}
constructor(name, method, container) {
@@ -98,7 +125,17 @@ class PreviewControl {
name: name,
method: method,
};
- this._createElements(container);
+ this._container = container;
+ this._createCanvas();
+ }
+
+ async queryPreviewFunction(funcName, ...args) {
+ try {
+ return await Editor.Message.request('scene', 'call-preview-function', this._gLPreviewConfig.name, funcName, ...args);
+ } catch (e) {
+ console.error(e);
+ return null;
+ }
}
async callPreviewFunction(funcName, ...args) {
@@ -112,6 +149,34 @@ class PreviewControl {
}
}
+ _createCanvas() {
+ if (!this._container) { return; }
+
+ // image for dragging inspector-resize-preview to resize the preview view.
+ this._image = document.createElement('div');
+ this._image.classList.add('image');
+ this._container.appendChild(this._image);
+ this._image.style = `
+ height: var(--inspector-footer-preview-height, 200px);
+ position: absolute;
+ overflow: hidden;
+ display: flex;
+ flex: 1;
+ width: 100%;
+ pointer-events: none;
+ `;
+
+ this._canvas = document.createElement('canvas');
+ this._canvas.classList.add('canvas');
+ this._canvas.style = `
+ flex: 1;
+ `;
+ this._container.appendChild(this._canvas);
+
+ this._canvas.addEventListener('mousedown', this._onMouseDownBind);
+ this._canvas.addEventListener('wheel', this._onMouseWheelBind);
+ }
+
async init() {
const GLPreview = Editor._Module.require('PreviewExtends').default;
this._glPreview = new GLPreview(this._gLPreviewConfig.name, this._gLPreviewConfig.method);
@@ -121,38 +186,43 @@ class PreviewControl {
});
this._resizeObserver = new window.ResizeObserver(this._observerBind);
this._resizeObserver.observe(this._image);
- this._registerEventListener();
-
await this._refresh();
}
doRefreshDirty() {
this._observer();
- this._refresh();
+ void this._refresh();
}
close() {
this._resizeObserver && this._resizeObserver.unobserve(this._image);
cancelAnimationFrame(this._animationId);
- this._unregisterEventListener();
- }
-
- _registerEventListener() {
- this._resetCamera.addEventListener('click', this._onResetCameraBind);
- this._canvas.addEventListener('mousedown', this._onMouseDownBind);
- this._canvas.addEventListener('wheel', this._onMouseWheelBind);
- }
-
- _unregisterEventListener() {
- this._resetCamera.removeEventListener('click', this._onResetCameraBind);
- this._canvas.removeEventListener('mousedown', this._onMouseDownBind);
- this._canvas.removeEventListener('wheel', this._onMouseWheelBind);
}
_observer() {
this._isDirty = true;
}
+ /**
+ * Calculate canvas max size to avoid overflow when dragging inspector-resize-preview.
+ * @returns {number}
+ */
+ panelFrameElement = null;
+ getMaxHeight() {
+ if (!this.panelFrameElement) {
+ this.panelFrameElement = getPanelFrameElement(this._container);
+ }
+ if (!this.panelFrameElement) {
+ return 0;
+ }
+ const $content = this._container.querySelector('.content');
+ if (!$content) {
+ return this.panelFrameElement.clientHeight * 0.7 - UI_SECTION_BAR_HEIGHT;
+ } else {
+ return this.panelFrameElement.clientHeight * 0.7 - $content.clientHeight - UI_SECTION_BAR_HEIGHT;
+ }
+ }
+
async _refresh() {
if (this._isDirty) {
try {
@@ -161,7 +231,13 @@ class PreviewControl {
const image = this._image;
const width = image.clientWidth;
- const height = image.clientHeight;
+ let height = image.clientHeight;
+
+ const maxHeight = this.getMaxHeight();
+ if (height >= maxHeight) {
+ height = maxHeight;
+ }
+
if (canvas.width !== width || canvas.height !== height) {
canvas.width = width;
canvas.height = height;
@@ -172,6 +248,8 @@ class PreviewControl {
await this._glPreview.resizeGL(width, height);
}
+ this._updateToolElements();
+
const info = await this._glPreview.queryPreviewData({
width: canvas.width,
height: canvas.height,
@@ -232,11 +310,12 @@ class PreviewControl {
this._isDirty = true;
}
- _onResetCamera() {
- this.callPreviewFunction('resetCameraView')
- .then(() => {
- this.doRefreshDirty();
- });
+ async _onResetCamera() {
+ await this.callPreviewFunction('resetCameraView');
+ }
+
+ async _onViewToggle() {
+ await this.callPreviewFunction('viewToggle');
}
}