skeleton-preview.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. 'use strict';
  2. exports.template = /* html */`
  3. <ui-section header="i18n:ENGINE.inspector.preview.header" class="preview-section config no-padding" expand>
  4. <div class="preview">
  5. <div class="info">
  6. <ui-label value="JointCount:0" class="joint-count"></ui-label>
  7. </div>
  8. <div class="image">
  9. <canvas class="canvas"></canvas>
  10. </div>
  11. </div>
  12. </ui-section>
  13. `;
  14. exports.style = /* css */`
  15. .preview-section {
  16. margin-top: 0px;
  17. }
  18. .preview {
  19. border-top: 1px solid var(--color-normal-border);
  20. }
  21. .preview > .info {
  22. padding: 4px 4px 0 4px;
  23. }
  24. .preview > .info > ui-label {
  25. margin-right: 6px;
  26. }
  27. .preview > .image {
  28. height: var(--inspector-footer-preview-height, 200px);
  29. overflow: hidden;
  30. display: flex;
  31. flex: 1;
  32. }
  33. .preview >.image > .canvas {
  34. flex: 1;
  35. }
  36. `;
  37. exports.$ = {
  38. container: '.preview',
  39. jointCount: '.joint-count',
  40. image: '.image',
  41. canvas: '.canvas',
  42. };
  43. async function callSkeletonPreviewFunction(funcName, ...args) {
  44. return await Editor.Message.request('scene', 'call-preview-function', 'scene:skeleton-preview', funcName, ...args);
  45. }
  46. const Elements = {
  47. preview: {
  48. ready() {
  49. const panel = this;
  50. let _isPreviewDataDirty = false;
  51. Object.defineProperty(panel, 'isPreviewDataDirty', {
  52. get() {
  53. return _isPreviewDataDirty;
  54. },
  55. set(value) {
  56. if (value !== _isPreviewDataDirty) {
  57. _isPreviewDataDirty = value;
  58. value && panel.refreshPreview();
  59. }
  60. },
  61. });
  62. panel.$.canvas.addEventListener('mousedown', async (event) => {
  63. await callSkeletonPreviewFunction('onMouseDown', { x: event.x, y: event.y, button: event.button });
  64. async function mousemove(event) {
  65. await callSkeletonPreviewFunction('onMouseMove', {
  66. movementX: event.movementX,
  67. movementY: event.movementY,
  68. });
  69. panel.isPreviewDataDirty = true;
  70. }
  71. async function mouseup(event) {
  72. await callSkeletonPreviewFunction('onMouseUp', {
  73. x: event.x,
  74. y: event.y,
  75. });
  76. document.removeEventListener('mousemove', mousemove);
  77. document.removeEventListener('mouseup', mouseup);
  78. panel.isPreviewDataDirty = false;
  79. }
  80. document.addEventListener('mousemove', mousemove);
  81. document.addEventListener('mouseup', mouseup);
  82. panel.isPreviewDataDirty = true;
  83. });
  84. panel.$.canvas.addEventListener('wheel', async (event) => {
  85. await callSkeletonPreviewFunction('onMouseWheel', {
  86. wheelDeltaY: event.wheelDeltaY,
  87. wheelDeltaX: event.wheelDeltaX,
  88. });
  89. panel.isPreviewDataDirty = true;
  90. });
  91. const GlPreview = Editor._Module.require('PreviewExtends').default;
  92. panel.glPreview = new GlPreview('scene:skeleton-preview', 'query-skeleton-preview-data');
  93. function observer() {
  94. panel.isPreviewDataDirty = true;
  95. }
  96. panel.resizeObserver = new window.ResizeObserver(observer);
  97. panel.resizeObserver.observe(panel.$.image);
  98. observer();
  99. },
  100. async update() {
  101. const panel = this;
  102. if (!panel.$.canvas) {
  103. return;
  104. }
  105. await panel.glPreview.init({ width: panel.$.canvas.clientWidth, height: panel.$.canvas.clientHeight });
  106. const info = await callSkeletonPreviewFunction('setSkeleton', panel.asset.uuid);
  107. panel.infoUpdate(info);
  108. this.isPreviewDataDirty = true;
  109. },
  110. close() {
  111. const panel = this;
  112. panel.resizeObserver.unobserve(panel.$.image);
  113. },
  114. },
  115. info: {
  116. ready() {
  117. const panel = this;
  118. panel.infoUpdate = Elements.info.update.bind(panel);
  119. },
  120. update(info) {
  121. if (!info) {
  122. return;
  123. }
  124. const panel = this;
  125. panel.$.jointCount.value = 'JointCount:' + info.jointCount;
  126. panel.isPreviewDataDirty = true;
  127. },
  128. close() {
  129. callSkeletonPreviewFunction('hide');
  130. },
  131. },
  132. };
  133. exports.methods = {
  134. async refreshPreview() {
  135. const panel = this;
  136. // After await, the panel no longer exists
  137. if (!panel.$.canvas) {
  138. return;
  139. }
  140. const doDraw = async () => {
  141. try {
  142. const canvas = panel.$.canvas;
  143. const image = panel.$.image;
  144. const width = image.clientWidth;
  145. const height = image.clientHeight;
  146. if (canvas.width !== width || canvas.height !== height) {
  147. canvas.width = width;
  148. canvas.height = height;
  149. await panel.glPreview.initGL(canvas, { width, height });
  150. await panel.glPreview.resizeGL(width, height);
  151. }
  152. const info = await panel.glPreview.queryPreviewData({
  153. width: canvas.width,
  154. height: canvas.height,
  155. });
  156. panel.glPreview.drawGL(info);
  157. } catch (e) {
  158. console.warn(e);
  159. }
  160. };
  161. requestAnimationFrame(async () => {
  162. await doDraw();
  163. panel.isPreviewDataDirty = false;
  164. });
  165. },
  166. };
  167. exports.ready = function() {
  168. for (const prop in Elements) {
  169. const element = Elements[prop];
  170. if (element.ready) {
  171. element.ready.call(this);
  172. }
  173. }
  174. };
  175. exports.update = function(assetList, metaList) {
  176. this.assetList = assetList;
  177. this.metaList = metaList;
  178. this.asset = assetList[0];
  179. this.meta = metaList[0];
  180. // 如何多选就隐藏预览
  181. if (assetList.length > 1) {
  182. this.$.container.style.display = 'none';
  183. } else {
  184. this.$.container.style.display = 'block';
  185. }
  186. for (const prop in Elements) {
  187. const element = Elements[prop];
  188. if (element.update) {
  189. element.update.call(this);
  190. }
  191. }
  192. };
  193. exports.close = function() {
  194. for (const prop in Elements) {
  195. const element = Elements[prop];
  196. if (element.close) {
  197. element.close.call(this);
  198. }
  199. }
  200. };