ModelInspector.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import InspectorWidget = require("./InspectorWidget");
  23. import ArrayEditWidget = require("./ArrayEditWidget");
  24. import InspectorUtils = require("./InspectorUtils");
  25. import EditorEvents = require("editor/EditorEvents");
  26. class ModelInspector extends InspectorWidget {
  27. constructor() {
  28. super();
  29. this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  30. }
  31. handleWidgetEvent(ev: Atomic.UIWidgetEvent):boolean {
  32. return false;
  33. }
  34. onApply() {
  35. this.importer.scale = Number(this.scaleEdit.text);
  36. this.importer.importAnimations = this.importAnimationBox.value ? true : false;
  37. this.importer.setImportMaterials(this.importMaterials.value ? true : false);
  38. for (var i = 0; i < this.importer.animationCount; i++) {
  39. var info = this.importer.getAnimationInfo(i);
  40. var nameEdit = this.nameEdits[i];
  41. var startEdit = this.startEdits[i];
  42. var endEdit = this.endEdits[i];
  43. info.name = nameEdit.text;
  44. // guard against NAN
  45. var _startTime = Number(startEdit.text);
  46. var _endTime = Number(endEdit.text);
  47. if (isNaN(_startTime)) _startTime = 0;
  48. if (isNaN(_endTime)) _endTime = 0;
  49. info.startTime = _startTime;
  50. info.endTime = _endTime;
  51. }
  52. this.asset.import();
  53. this.asset.save();
  54. }
  55. inspect(asset: ToolCore.Asset) {
  56. this.asset = asset;
  57. this.importer = <ToolCore.ModelImporter> asset.importer;
  58. // node attr layout
  59. var rootLayout = this.rootLayout;
  60. // Model Section
  61. var modelLayout = this.createSection(rootLayout, "Model", 1);
  62. var editField = InspectorUtils.createAttrEditField("Name", modelLayout);
  63. var lp = new Atomic.UILayoutParams();
  64. editField.readOnly = true;
  65. editField.text = asset.name;
  66. //This should preferably be onClick
  67. editField.subscribeToEvent(editField, "UIWidgetFocusChanged", (ev: Atomic.UIWidgetFocusChangedEvent) => {
  68. if (ev.widget == editField && editField.focus) {
  69. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": asset.getRelativePath() });
  70. }
  71. });
  72. this.scaleEdit = InspectorUtils.createAttrEditField("Scale", modelLayout);
  73. this.scaleEdit.text = this.importer.scale.toString();
  74. this.importMaterials = this.createAttrCheckBox("Import Materials", modelLayout);
  75. this.importMaterials.value = this.importer.getImportMaterials() ? 1 : 0;
  76. // Animations Section
  77. var animationLayout = this.createSection(rootLayout, "Animation", 1);
  78. this.importAnimationBox = this.createAttrCheckBox("Import Animations", animationLayout);
  79. this.importAnimationBox.value = this.importer.importAnimations ? 1 : 0;
  80. this.importAnimationArray = new ArrayEditWidget("Animation Count");
  81. animationLayout.addChild(this.importAnimationArray);
  82. this.importAnimationArray.onCountChanged = (count) => this.onAnimationCountChanged(count);
  83. var nlp = new Atomic.UILayoutParams();
  84. nlp.width = 310;
  85. var animLayout = this.animationInfoLayout = new Atomic.UILayout();
  86. animLayout.spacing = 4;
  87. animLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  88. animLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  89. animLayout.layoutParams = nlp;
  90. animLayout.axis = Atomic.UI_AXIS_Y;
  91. animLayout.gravity = Atomic.UI_GRAVITY_ALL;
  92. animationLayout.addChild(animLayout);
  93. this.createAnimationEntries();
  94. // apply button
  95. rootLayout.addChild(this.createApplyButton());
  96. }
  97. createAnimationEntries() {
  98. var layout = this.animationInfoLayout;
  99. layout.deleteAllChildren();
  100. var count = this.importer.animationCount;
  101. this.importAnimationArray.countEdit.text = count.toString();
  102. this.nameEdits = [];
  103. this.startEdits = [];
  104. this.endEdits = [];
  105. for (var i = 0; i < count; i++) {
  106. var animInfo = this.importer.getAnimationInfo(i);
  107. var name = InspectorUtils.createAttrName("Animation " + i.toString() + ":");
  108. layout.addChild(name);
  109. var nameEdit = InspectorUtils.createAttrEditField("Name", layout);
  110. nameEdit.text = animInfo.name;
  111. var startEdit = InspectorUtils.createAttrEditField("Start", layout);
  112. startEdit.text = animInfo.startTime.toString();
  113. var endEdit = InspectorUtils.createAttrEditField("End", layout);
  114. endEdit.text = animInfo.endTime.toString();
  115. this.nameEdits.push(nameEdit);
  116. this.startEdits.push(startEdit);
  117. this.endEdits.push(endEdit);
  118. InspectorUtils.createSeparator(layout);
  119. }
  120. }
  121. onAnimationCountChanged(count: number) {
  122. if (this.importer.animationCount == count) {
  123. return;
  124. }
  125. this.importer.animationCount = count;
  126. this.createAnimationEntries();
  127. }
  128. // model
  129. scaleEdit: Atomic.UIEditField;
  130. // animation
  131. importAnimationBox: Atomic.UICheckBox;
  132. importMaterials: Atomic.UICheckBox;
  133. importAnimationArray: ArrayEditWidget;
  134. animationInfoLayout: Atomic.UILayout;
  135. nameEdits: Atomic.UIEditField[];
  136. startEdits: Atomic.UIEditField[];
  137. endEdits: Atomic.UIEditField[];
  138. asset: ToolCore.Asset;
  139. importer: ToolCore.ModelImporter;
  140. }
  141. export = ModelInspector;