AnimationToolbar.ts 9.1 KB

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