physics-material.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. exports.template = /* html */`
  3. <section class="asset-physics-material">
  4. <ui-label class="multiple-warn-tip" value="i18n:ENGINE.assets.multipleWarning"></ui-label>
  5. </section>
  6. `;
  7. exports.style = /* css */`
  8. .asset-physics-material {
  9. padding-right: 4px;
  10. }
  11. .asset-physics-material[multiple-invalid] > *:not(.multiple-warn-tip) {
  12. display: none!important;
  13. }
  14. .asset-physics-material[multiple-invalid] > .multiple-warn-tip {
  15. display: block;
  16. }
  17. .asset-physics-material .multiple-warn-tip {
  18. display: none;
  19. text-align: center;
  20. color: var(--color-focus-contrast-weakest);
  21. margin-top: 8px;
  22. }
  23. `;
  24. exports.$ = {
  25. container: '.asset-physics-material',
  26. };
  27. exports.methods = {
  28. record() {
  29. return JSON.stringify(this.queryData);
  30. },
  31. async restore(record) {
  32. record = JSON.parse(record);
  33. if (!record || typeof record !== 'object') {
  34. return false;
  35. }
  36. this.queryData = record;
  37. await this.change();
  38. return true;
  39. },
  40. async query(uuid) {
  41. return await Editor.Message.request('scene', 'query-physics-material', uuid);
  42. },
  43. async apply() {
  44. this.reset();
  45. await Editor.Message.request('scene', 'apply-physics-material', this.asset.uuid, this.queryData);
  46. },
  47. abort() {
  48. this.reset();
  49. },
  50. reset() {
  51. /**
  52. * reset 环节只需把 uuid 清空
  53. * 会重新进入 panel.update 周期,根据 uuid 为空的条件,把 this.dirtyData.origin 重新填充
  54. */
  55. this.dirtyData.uuid = '';
  56. },
  57. async change() {
  58. this.queryData = await Editor.Message.request('scene', 'change-physics-material', this.queryData);
  59. this.updateInterface();
  60. this.setDirtyData();
  61. this.dispatch('change');
  62. },
  63. snapshot() {
  64. this.dispatch('snapshot');
  65. },
  66. updateInterface() {
  67. for (const key in this.queryData) {
  68. const dump = this.queryData[key];
  69. if (!dump.visible) {
  70. continue;
  71. }
  72. // reuse
  73. if (!this.$[key]) {
  74. this.$[key] = document.createElement('ui-prop');
  75. this.$[key].setAttribute('type', 'dump');
  76. this.$.container.appendChild(this.$[key]);
  77. }
  78. if (this.asset.readonly) {
  79. dump.readonly = true;
  80. }
  81. this.$[key].render(dump);
  82. }
  83. },
  84. setDirtyData() {
  85. this.dirtyData.realtime = JSON.stringify(this.queryData);
  86. if (!this.dirtyData.origin) {
  87. this.dirtyData.origin = this.dirtyData.realtime;
  88. }
  89. },
  90. isDirty() {
  91. const isDirty = this.dirtyData.origin !== this.dirtyData.realtime;
  92. return isDirty;
  93. },
  94. };
  95. exports.ready = function() {
  96. this.$.container.addEventListener('change-dump', () => {
  97. this.change();
  98. });
  99. this.$.container.addEventListener('confirm-dump', () => {
  100. this.snapshot();
  101. });
  102. // Used to determine whether the material has been modified in isDirty()
  103. this.dirtyData = {
  104. uuid: '',
  105. origin: '',
  106. realtime: '',
  107. };
  108. };
  109. exports.update = async function(assetList, metaList) {
  110. this.assetList = assetList;
  111. this.metaList = metaList;
  112. this.asset = assetList[0];
  113. this.meta = metaList[0];
  114. if (assetList.length > 1) {
  115. this.$.container.setAttribute('multiple-invalid', '');
  116. return;
  117. } else {
  118. this.$.container.removeAttribute('multiple-invalid');
  119. }
  120. if (this.dirtyData.uuid !== this.asset.uuid) {
  121. this.dirtyData.uuid = this.asset.uuid;
  122. this.dirtyData.origin = '';
  123. }
  124. this.queryData = await this.query(this.asset.uuid);
  125. this.updateInterface();
  126. this.setDirtyData();
  127. };
  128. exports.close = function() {
  129. // Used to determine whether the material has been modified in isDirty()
  130. this.dirtyData = {
  131. uuid: '',
  132. origin: '',
  133. realtime: '',
  134. };
  135. };