AnimationToolbar.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 EditorUI = require("ui/EditorUI");
  23. import HierarchyFrame = require("ui/frames/HierarchyFrame");
  24. import InspectorUtils = require("ui/frames/inspector/InspectorUtils");
  25. import ResourceOps = require("resources/ResourceOps");
  26. class AnimationToolbar extends Atomic.UIWidget {
  27. constructor(parent: Atomic.UIWidget, properties: Atomic.UIWidget, asset: ToolCore.Asset) {
  28. super();
  29. this.load("AtomicEditor/editor/ui/animationtoolbar.tb.txt");
  30. this.asset = asset;
  31. this.leftAnimContainer = <Atomic.UILayout>this.getWidget("leftanimcontainer");
  32. this.rightAnimContainer = <Atomic.UILayout>this.getWidget("rightanimcontainer");
  33. this.subscribeToEvent(this, Atomic.UIWidgetEvent((ev) => this.handleWidgetEvent(ev)));
  34. this.subscribeToEvent(Editor.EditorActiveSceneEditorChangeEvent((data) => this.handleActiveSceneEditorChanged(data)));
  35. this.subscribeToEvent(Editor.EditorSceneClosedEvent((data) => this.handleSceneClosed(data)));
  36. var leftAnimationField = InspectorUtils.createAttrEditFieldWithSelectButton("Animation A", this.leftAnimContainer);
  37. leftAnimationField.selectButton.onClick = function () { this.openAnimationSelectionBox(leftAnimationField.editField, this.leftAnim); }.bind(this);
  38. var rightAnimationField = InspectorUtils.createAttrEditFieldWithSelectButton("Animation B", this.rightAnimContainer);
  39. rightAnimationField.selectButton.onClick = function () { this.openAnimationSelectionBox(rightAnimationField.editField, this.rightAnim); }.bind(this);
  40. this.leftAnimEditfield = leftAnimationField.editField;
  41. this.rightAnimEditfield = rightAnimationField.editField;
  42. var leftStateContainer = <Atomic.UILayout>this.getWidget("leftstatedropdown");
  43. var rightStateContainer = <Atomic.UILayout>this.getWidget("rightstatedropdown");
  44. ResourceOps.CreateNewAnimationPreviewScene();
  45. this.populateScene();
  46. parent.addChild(this);
  47. //Animation properties bar
  48. this.animationPropertiesContainer = new Atomic.UILayout();
  49. this.animationSpeed = InspectorUtils.createAttrEditField("Playback Speed:", this.animationPropertiesContainer);
  50. this.animationSpeed.setAdaptToContentSize(true);
  51. this.blendSpeed = InspectorUtils.createAttrEditField("Blend Speed:", this.animationPropertiesContainer);
  52. this.blendSpeed.setAdaptToContentSize(true);
  53. //Set default values
  54. this.animationSpeed.setText("1");
  55. this.blendSpeed.setText("0");
  56. properties.addChild(this.animationPropertiesContainer);
  57. }
  58. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  59. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  60. if (this.animationController != null) {
  61. if (ev.target.id == "play_left") {
  62. if (this.animationController.playExclusive(this.leftAnimEditfield.text, 0, true))
  63. this.animationController.setSpeed(this.leftAnimEditfield.text, Number(this.animationSpeed.text));
  64. else
  65. this.showAnimationWarning();
  66. return true;
  67. }
  68. if (ev.target.id == "play_right") {
  69. if (this.animationController.playExclusive(this.rightAnimEditfield.text, 0, true))
  70. this.animationController.setSpeed(this.rightAnimEditfield.text, Number(this.animationSpeed.text));
  71. else
  72. this.showAnimationWarning();
  73. return true;
  74. }
  75. if (ev.target.id == "blend_left") {
  76. if (this.animationController.playExclusive(this.leftAnimEditfield.text, 0, true, Number(this.blendSpeed.text)))
  77. this.animationController.setSpeed(this.leftAnimEditfield.text, Number(this.animationSpeed.text));
  78. else
  79. this.showAnimationWarning();
  80. return true;
  81. }
  82. if (ev.target.id == "blend_right") {
  83. if (this.animationController.playExclusive(this.rightAnimEditfield.text, 0, true, Number(this.blendSpeed.text)))
  84. this.animationController.setSpeed(this.rightAnimEditfield.text, Number(this.animationSpeed.text));
  85. else
  86. EditorUI.getModelOps().showError("Animation Toolbar Warning", "The animation cannot be played. Please make sure the animation you are trying to play exists in the AnimationController Component.");
  87. return true;
  88. }
  89. if (ev.target.id == "stop") {
  90. this.animationController.stopAll();
  91. return true;
  92. }
  93. }
  94. }
  95. return true;
  96. }
  97. handleSceneClosed(ev: Editor.EditorSceneClosedEvent) {
  98. if (ev.scene == this.scene) {
  99. Atomic.fileSystem.delete(this.sceneAssetPath);
  100. if (this.animationPropertiesContainer)
  101. this.animationPropertiesContainer.remove();
  102. this.remove();
  103. }
  104. }
  105. closeViewer() {
  106. Atomic.fileSystem.delete(this.sceneAssetPath);
  107. this.sceneEditor.close();
  108. if (this.animationPropertiesContainer)
  109. this.animationPropertiesContainer.remove();
  110. this.remove();
  111. }
  112. handleActiveSceneEditorChanged(event: Editor.EditorActiveSceneEditorChangeEvent) {
  113. if (!event.sceneEditor)
  114. return;
  115. this.sceneEditor = event.sceneEditor;
  116. this.scene = event.sceneEditor.scene;
  117. if (this.scene) {
  118. this.unsubscribeFromEvents(this.scene);
  119. return;
  120. }
  121. if (!event.sceneEditor)
  122. return;
  123. }
  124. populateScene() {
  125. this.scene.setUpdateEnabled(true);
  126. var modelNode = this.asset.instantiateNode(this.scene, this.asset.name);
  127. this.modelNode = modelNode;
  128. this.sceneEditor.selection.addNode(modelNode, true);
  129. this.sceneEditor.sceneView3D.frameSelection();
  130. this.animatedModel = <Atomic.AnimatedModel>modelNode.getComponent("AnimatedModel");
  131. this.animationController = <Atomic.AnimationController>modelNode.getComponent("AnimationController");
  132. if ( this.animatedModel != null && this.animationController != null ) {
  133. var model = this.animatedModel.model;
  134. this.animatedModel.setBoneCreationOverride(true);
  135. this.animatedModel.setModel(model, true);
  136. var animComp = new Atomic.AnimatedModel();
  137. var animContComp = new Atomic.AnimationController();
  138. }
  139. }
  140. openAnimationSelectionBox(animationWidget: Atomic.UIEditField, animationSlot: Atomic.Animation) {
  141. EditorUI.getModelOps().showResourceSelection("Select Animation", "ModelImporter", "Animation", function (resource: Atomic.Animation, args: any) {
  142. var animation = resource;
  143. if (animation) {
  144. animationSlot = animation;
  145. animationWidget.text = animation.getAnimationName();
  146. }
  147. });
  148. }
  149. showAnimationWarning() {
  150. EditorUI.getModelOps().showError("Animation Preview Warning", "The animation cannot be played. Please make sure the animation you are trying to play exists in the AnimationController Component.");
  151. }
  152. //Animation Toolbar Widgets
  153. animationController: Atomic.AnimationController;
  154. animatedModel: Atomic.AnimatedModel;
  155. scene: Atomic.Scene = null;
  156. sceneEditor: Editor.SceneEditor3D;
  157. modelNode: Atomic.Node;
  158. leftAnimContainer: Atomic.UILayout;
  159. rightAnimContainer: Atomic.UILayout;
  160. blendFileContainer: Atomic.UILayout;
  161. leftAnimEditfield: Atomic.UIEditField;
  162. rightAnimEditfield: Atomic.UIEditField;
  163. leftAnim: Atomic.Animation;
  164. rightAnim: Atomic.Animation;
  165. asset: ToolCore.Asset;
  166. sceneAssetPath: string;
  167. stateDropDownList: string[];
  168. //Animation Properties Widgets
  169. animationPropertiesContainer: Atomic.UILayout;
  170. animationSpeed: Atomic.UIEditField;
  171. blendSpeed: Atomic.UIEditField;
  172. }
  173. export = AnimationToolbar;