AnimationToolbar.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. updateDelta: number = 0.0;
  28. updateYaw: number = 0.0;
  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, Atomic.UIWidgetEvent((ev) => this.handleWidgetEvent(ev)));
  36. this.subscribeToEvent(Editor.EditorActiveSceneEditorChangeEvent((data) => this.handleActiveSceneEditorChanged(data)));
  37. this.subscribeToEvent(Editor.EditorSceneClosedEvent((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. var attrLayout = new Atomic.UILayout();
  56. attrLayout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  57. attrLayout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT_RIGHT;
  58. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  59. var nameField = new Atomic.UITextField();
  60. nameField.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_RIGHT;
  61. nameField.skinBg = "InspectorTextAttrName";
  62. nameField.text = "Spin Speed:";
  63. attrLayout.addChild(nameField);
  64. this.rotateModel = new Atomic.UISlider();
  65. this.rotateModel.setLimits(0, 10);
  66. this.rotateModel.setValue(0);
  67. attrLayout.addChild(this.rotateModel);
  68. this.animationPropertiesContainer.addChild( attrLayout );
  69. //Set default values
  70. this.animationSpeed.setText("1");
  71. this.blendSpeed.setText("0");
  72. properties.addChild(this.animationPropertiesContainer);
  73. this.subscribeToEvent(Atomic.UpdateEvent((ev) => this.handleUpdate(ev))); // if we want the model to rotate
  74. }
  75. handleUpdate(ev) {
  76. var rotspeed = this.rotateModel.value;
  77. if (rotspeed > 0) { // only do work if the spinning is turned on
  78. this.updateDelta += ev.timeStep; // add some time to the clock
  79. if (this.updateDelta > (0.134 * (10 - rotspeed))) { //see if we have reached our limit
  80. this.updateDelta = 0.0; // reset the limit
  81. this.updateYaw += 2.1; // increase the yaw
  82. if (this.updateYaw > 26.3) // clamp the yaw
  83. this.updateYaw = 0;
  84. this.modelNode.yaw(this.updateYaw); // and rotate the model.
  85. }
  86. }
  87. }
  88. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  89. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  90. if (this.animationController != null) {
  91. if (ev.target.id == "play_left") {
  92. if (this.animationController.playExclusive(this.leftAnimEditfield.text, 0, true))
  93. this.animationController.setSpeed(this.leftAnimEditfield.text, Number(this.animationSpeed.text));
  94. else
  95. this.showAnimationWarning();
  96. return true;
  97. }
  98. if (ev.target.id == "play_right") {
  99. if (this.animationController.playExclusive(this.rightAnimEditfield.text, 0, true))
  100. this.animationController.setSpeed(this.rightAnimEditfield.text, Number(this.animationSpeed.text));
  101. else
  102. this.showAnimationWarning();
  103. return true;
  104. }
  105. if (ev.target.id == "blend_left") {
  106. if (this.animationController.playExclusive(this.leftAnimEditfield.text, 0, true, Number(this.blendSpeed.text)))
  107. this.animationController.setSpeed(this.leftAnimEditfield.text, Number(this.animationSpeed.text));
  108. else
  109. this.showAnimationWarning();
  110. return true;
  111. }
  112. if (ev.target.id == "blend_right") {
  113. if (this.animationController.playExclusive(this.rightAnimEditfield.text, 0, true, Number(this.blendSpeed.text)))
  114. this.animationController.setSpeed(this.rightAnimEditfield.text, Number(this.animationSpeed.text));
  115. else
  116. 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.");
  117. return true;
  118. }
  119. if (ev.target.id == "stop") {
  120. this.animationController.stopAll();
  121. return true;
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. handleSceneClosed(ev: Editor.EditorSceneClosedEvent) {
  128. if (ev.scene == this.scene) {
  129. Atomic.fileSystem.delete(this.sceneAssetPath);
  130. if (this.animationPropertiesContainer)
  131. this.animationPropertiesContainer.remove();
  132. this.remove();
  133. }
  134. }
  135. closeViewer() {
  136. Atomic.fileSystem.delete(this.sceneAssetPath);
  137. this.sceneEditor.close();
  138. if (this.animationPropertiesContainer)
  139. this.animationPropertiesContainer.remove();
  140. this.remove();
  141. }
  142. handleActiveSceneEditorChanged(event: Editor.EditorActiveSceneEditorChangeEvent) {
  143. if (!event.sceneEditor)
  144. return;
  145. this.sceneEditor = event.sceneEditor;
  146. this.scene = event.sceneEditor.scene;
  147. if (this.scene) {
  148. this.unsubscribeFromEvents(this.scene);
  149. return;
  150. }
  151. if (!event.sceneEditor)
  152. return;
  153. }
  154. populateScene() {
  155. this.scene.setUpdateEnabled(true);
  156. var modelNode = this.asset.instantiateNode(this.scene, this.asset.name);
  157. this.modelNode = modelNode;
  158. this.sceneEditor.selection.addNode(modelNode, true);
  159. this.sceneEditor.sceneView3D.frameSelection();
  160. this.animatedModel = <Atomic.AnimatedModel>modelNode.getComponent("AnimatedModel");
  161. this.animationController = <Atomic.AnimationController>modelNode.getComponent("AnimationController");
  162. if ( this.animatedModel != null && this.animationController != null ) {
  163. var model = this.animatedModel.model;
  164. this.animatedModel.setBoneCreationOverride(true);
  165. this.animatedModel.setModel(model, true);
  166. var animComp = new Atomic.AnimatedModel();
  167. var animContComp = new Atomic.AnimationController();
  168. }
  169. }
  170. openAnimationSelectionBox(animationWidget: Atomic.UIEditField, animationSlot: Atomic.Animation) {
  171. EditorUI.getModelOps().showResourceSelection("Select Animation", "ModelImporter", "Animation", function (resource: Atomic.Animation, args: any) {
  172. var animation = resource;
  173. if (animation) {
  174. animationSlot = animation;
  175. animationWidget.text = animation.getAnimationName();
  176. }
  177. });
  178. }
  179. showAnimationWarning() {
  180. 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.");
  181. }
  182. //Animation Toolbar Widgets
  183. animationController: Atomic.AnimationController;
  184. animatedModel: Atomic.AnimatedModel;
  185. scene: Atomic.Scene = null;
  186. sceneEditor: Editor.SceneEditor3D;
  187. modelNode: Atomic.Node;
  188. leftAnimContainer: Atomic.UILayout;
  189. rightAnimContainer: Atomic.UILayout;
  190. blendFileContainer: Atomic.UILayout;
  191. leftAnimEditfield: Atomic.UIEditField;
  192. rightAnimEditfield: Atomic.UIEditField;
  193. leftAnim: Atomic.Animation;
  194. rightAnim: Atomic.Animation;
  195. asset: ToolCore.Asset;
  196. sceneAssetPath: string;
  197. stateDropDownList: string[];
  198. //Animation Properties Widgets
  199. animationPropertiesContainer: Atomic.UILayout;
  200. animationSpeed: Atomic.UIEditField;
  201. blendSpeed: Atomic.UIEditField;
  202. rotateModel: Atomic.UISlider;
  203. }
  204. export = AnimationToolbar;