preview.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Hide or show an HTML element based on the value.
  3. * 根据值隐藏或显示 HTML 元素。
  4. *
  5. * @param {HTMLElement} element - The HTML element to hide or show. 要隐藏或显示的 HTML 元素。
  6. * @param {boolean} value - If true, the element is hidden; otherwise, it is shown. 如果为 true,则隐藏元素;否则显示。
  7. */
  8. function hideElement(element, value) {
  9. element.style.display = value ? 'none' : 'block';
  10. }
  11. /**
  12. * get panel frame element
  13. * 获取 panel frame 元素
  14. * @param container
  15. * @returns {*}
  16. */
  17. function getPanelFrameElement(container) {
  18. let element = container;
  19. while (element) {
  20. element = element.parentElement || element.getRootNode().host;
  21. if (element && element.tagName === 'PANEL-FRAME' && element.getAttribute('name') === 'inspector') {
  22. break;
  23. }
  24. }
  25. return element;
  26. }
  27. const UI_SECTION_BAR_HEIGHT = 26;
  28. /**
  29. * 处理展示预览以及操作预览面板的辅助类
  30. * Helper classes that handle the Showcase Preview and Manipulation Preview panels
  31. */
  32. class PreviewControl {
  33. _gLPreviewConfig = {
  34. name: '',
  35. method: '',
  36. };
  37. _animationId = -1;
  38. _isDirty = false;
  39. _glPreview = null;
  40. // html element
  41. _container = null;
  42. _image = null;
  43. _canvas = null;
  44. _resetCamera = null;
  45. _viewToggleButton = null;
  46. _resizeObserver = null;
  47. _observerBind = this._observer.bind(this);
  48. _onMouseDownBind = this._onMouseDown.bind(this);
  49. _onMouseWheelBind = this._onMouseWheel.bind(this);
  50. _onResetCameraBind = this._onResetCamera.bind(this);
  51. _onViewToggleBind = this._onViewToggle.bind(this);
  52. async _updateToolElements() {
  53. if (!this._container) { return; }
  54. this.queryPreviewFunction('queryViewToolState')
  55. .then(state => {
  56. if (!state) { return; }
  57. let toolRight = 10;
  58. if (state.enableResetCamera) {
  59. this._resetCamera = this._container.querySelector('.reset-camera');
  60. if (!this._resetCamera) {
  61. this._resetCamera = document.createElement('ui-button');
  62. this._resetCamera.classList.add('reset-camera');
  63. this._container.appendChild(this._resetCamera);
  64. this._resetCamera.setAttribute('type', 'icon');
  65. this._resetCamera.setAttribute('tooltip', 'i18n:ENGINE.inspector.preview.resetCameraView');
  66. this._resetCamera.innerHTML = `<ui-icon value="reset"></ui-icon>`;
  67. this._resetCamera.addEventListener('click', this._onResetCameraBind);
  68. }
  69. this._resetCamera.style = `
  70. position: absolute;
  71. right: ${toolRight}px;
  72. bottom: 10px;
  73. height: 24px;
  74. width: 24px;
  75. `;
  76. toolRight += 28;
  77. }
  78. if (state.enableViewToggle) {
  79. this._viewToggleButton = this._container.querySelector('.view-toggle');
  80. if (!this._viewToggleButton) {
  81. this._viewToggleButton = document.createElement('ui-button');
  82. this._container.appendChild(this._viewToggleButton);
  83. this._viewToggleButton.classList.add('view-toggle');
  84. this._viewToggleButton.setAttribute('type', 'icon');
  85. this._viewToggleButton.setAttribute('tooltip', 'i18n:ENGINE.inspector.preview.viewToggle');
  86. this._viewToggleButton.addEventListener('click', this._onViewToggleBind);
  87. }
  88. this._viewToggleButton.style = `
  89. position: absolute;
  90. right: ${toolRight}px;
  91. bottom: 10px;
  92. height: 24px;
  93. width: 24px;
  94. `;
  95. this._updateViewToggleIcon();
  96. }
  97. });
  98. this._updateViewToggleIcon();
  99. }
  100. _updateViewToggleIcon() {
  101. if (!this._viewToggleButton) { return; }
  102. this.queryPreviewFunction('is2DView').then(is2D => {
  103. this._viewToggleButton.innerHTML = `<ui-icon value="${is2D ? '2D' : '3D'}"></ui-icon>`;
  104. });
  105. }
  106. constructor(name, method, container) {
  107. this._gLPreviewConfig = {
  108. name: name,
  109. method: method,
  110. };
  111. this._container = container;
  112. this._createCanvas();
  113. }
  114. async queryPreviewFunction(funcName, ...args) {
  115. try {
  116. return await Editor.Message.request('scene', 'call-preview-function', this._gLPreviewConfig.name, funcName, ...args);
  117. } catch (e) {
  118. console.error(e);
  119. return null;
  120. }
  121. }
  122. async callPreviewFunction(funcName, ...args) {
  123. try {
  124. const result = await Editor.Message.request('scene', 'call-preview-function', this._gLPreviewConfig.name, funcName, ...args);
  125. this.doRefreshDirty();
  126. return result;
  127. } catch (e) {
  128. console.error(e);
  129. return null;
  130. }
  131. }
  132. _createCanvas() {
  133. if (!this._container) { return; }
  134. // image for dragging inspector-resize-preview to resize the preview view.
  135. this._image = document.createElement('div');
  136. this._image.classList.add('image');
  137. this._container.appendChild(this._image);
  138. this._image.style = `
  139. height: var(--inspector-footer-preview-height, 200px);
  140. position: absolute;
  141. overflow: hidden;
  142. display: flex;
  143. flex: 1;
  144. width: 100%;
  145. pointer-events: none;
  146. `;
  147. this._canvas = document.createElement('canvas');
  148. this._canvas.classList.add('canvas');
  149. this._canvas.style = `
  150. flex: 1;
  151. `;
  152. this._container.appendChild(this._canvas);
  153. this._canvas.addEventListener('mousedown', this._onMouseDownBind);
  154. this._canvas.addEventListener('wheel', this._onMouseWheelBind);
  155. }
  156. async init() {
  157. const GLPreview = Editor._Module.require('PreviewExtends').default;
  158. this._glPreview = new GLPreview(this._gLPreviewConfig.name, this._gLPreviewConfig.method);
  159. await this._glPreview.init({
  160. width: this._canvas.clientWidth,
  161. height: this._canvas.clientHeight,
  162. });
  163. this._resizeObserver = new window.ResizeObserver(this._observerBind);
  164. this._resizeObserver.observe(this._image);
  165. await this._refresh();
  166. }
  167. doRefreshDirty() {
  168. this._observer();
  169. void this._refresh();
  170. }
  171. close() {
  172. this._resizeObserver && this._resizeObserver.unobserve(this._image);
  173. cancelAnimationFrame(this._animationId);
  174. }
  175. _observer() {
  176. this._isDirty = true;
  177. }
  178. /**
  179. * Calculate canvas max size to avoid overflow when dragging inspector-resize-preview.
  180. * @returns {number}
  181. */
  182. panelFrameElement = null;
  183. getMaxHeight() {
  184. if (!this.panelFrameElement) {
  185. this.panelFrameElement = getPanelFrameElement(this._container);
  186. }
  187. if (!this.panelFrameElement) {
  188. return 0;
  189. }
  190. const $content = this._container.querySelector('.content');
  191. if (!$content) {
  192. return this.panelFrameElement.clientHeight * 0.7 - UI_SECTION_BAR_HEIGHT;
  193. } else {
  194. return this.panelFrameElement.clientHeight * 0.7 - $content.clientHeight - UI_SECTION_BAR_HEIGHT;
  195. }
  196. }
  197. async _refresh() {
  198. if (this._isDirty) {
  199. try {
  200. this._isDirty = false;
  201. const canvas = this._canvas;
  202. const image = this._image;
  203. const width = image.clientWidth;
  204. let height = image.clientHeight;
  205. const maxHeight = this.getMaxHeight();
  206. if (height >= maxHeight) {
  207. height = maxHeight;
  208. }
  209. if (canvas.width !== width || canvas.height !== height) {
  210. canvas.width = width;
  211. canvas.height = height;
  212. await this._glPreview.initGL(canvas, {
  213. width, height,
  214. });
  215. await this._glPreview.resizeGL(width, height);
  216. }
  217. this._updateToolElements();
  218. const info = await this._glPreview.queryPreviewData({
  219. width: canvas.width,
  220. height: canvas.height,
  221. });
  222. this._glPreview.drawGL(info);
  223. } catch (e) {
  224. console.warn(e);
  225. }
  226. }
  227. cancelAnimationFrame(this._animationId);
  228. this._animationId = requestAnimationFrame(() => {
  229. this._refresh();
  230. });
  231. }
  232. async _onMouseDown(event) {
  233. await this.callPreviewFunction('onMouseDown', {
  234. x: event.x,
  235. y: event.y,
  236. button: event.button,
  237. });
  238. const mousemove = async (event) => {
  239. await this.callPreviewFunction('onMouseMove', {
  240. movementX: event.movementX,
  241. movementY: event.movementY,
  242. });
  243. this._isDirty = true;
  244. };
  245. const mouseup = async (event) => {
  246. await this.callPreviewFunction('onMouseUp', {
  247. x: event.x,
  248. y: event.y,
  249. });
  250. document.removeEventListener('mousemove', mousemove);
  251. document.removeEventListener('mouseup', mouseup);
  252. this._isDirty = false;
  253. };
  254. document.addEventListener('mousemove', mousemove);
  255. document.addEventListener('mouseup', mouseup);
  256. this._isDirty = true;
  257. }
  258. async _onMouseWheel(event) {
  259. await this.callPreviewFunction('onMouseWheel', {
  260. wheelDeltaY: event.deltaY,
  261. wheelDeltaX: event.deltaX,
  262. });
  263. this._isDirty = true;
  264. }
  265. async _onResetCamera() {
  266. await this.callPreviewFunction('resetCameraView');
  267. }
  268. async _onViewToggle() {
  269. await this.callPreviewFunction('viewToggle');
  270. }
  271. }
  272. module.exports = {
  273. hideElement,
  274. PreviewControl,
  275. };