AnimationToolbar.ts 9.0 KB

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