Source: extern/nla.js

  1. import register from "../util/register.js";
  2. import m_nla_fact from "../intern/nla.js";
  3. import m_time_fact from "../intern/time.js";
  4. import m_print_fact from "../intern/print.js";
  5. import * as m_util from "../intern/util.js";
  6. /**
  7. * API methods to control {@link https://www.blend4web.com/doc/en/animation.html#non-linear-animation|non linear animation}.
  8. * @module nla
  9. * @local NlaFinishCallback
  10. */
  11. function NLA(ns, exports) {
  12. var m_nla = m_nla_fact(ns);
  13. var m_time = m_time_fact(ns);
  14. var m_print = m_print_fact(ns);
  15. /**
  16. * Callback executed after the NLA animation has finished.
  17. * @callback NlaFinishCallback
  18. */
  19. /**
  20. * Set NLA animation frame for the active scene.
  21. * @method module:nla.set_frame
  22. * @param {number} frame NLA animation frame
  23. */
  24. exports.set_frame = function(frame) {
  25. frame = m_util.clamp(frame, m_nla.get_frame_start(),
  26. m_nla.get_frame_end());
  27. if (m_nla.check_logic_nodes()) {
  28. m_print.error("The active scene is using the Logic Editor.");
  29. return;
  30. }
  31. m_nla.set_frame(frame, m_time.get_timeline());
  32. }
  33. /**
  34. * Get NLA animation frame from the active scene
  35. * @method module:nla.get_frame
  36. * @returns {number} NLA animation current frame.
  37. */
  38. exports.get_frame = function() {
  39. return m_nla.get_frame(m_time.get_timeline());
  40. }
  41. /**
  42. * Stop NLA animation for the active scene.
  43. * @method module:nla.stop
  44. */
  45. exports.stop = function() {
  46. if (m_nla.check_logic_nodes())
  47. m_print.warn("The active scene is using the Logic Editor. It may conflicts with your JS-code.");
  48. m_nla.stop_nla();
  49. }
  50. /**
  51. * Play NLA animation for the active scene
  52. * @method module:nla.play
  53. * @param {?NlaFinishCallback} [callback=null] Nla finish callback.
  54. */
  55. exports.play = function(callback) {
  56. if (m_nla.check_logic_nodes())
  57. m_print.warn("The active scene is using the Logic Editor. It may conflicts with your JS-code.");
  58. m_nla.play_nla(callback);
  59. }
  60. /**
  61. * Check if NLA animation is being run for the active scene
  62. * @method module:nla.is_play
  63. * @returns {boolean} Check result.
  64. */
  65. exports.is_play = function() {
  66. return m_nla.is_play();
  67. }
  68. /**
  69. * Get NLA animation starting frame for the active scene
  70. * @method module:nla.get_frame_start
  71. * @returns {number} Start frame.
  72. */
  73. exports.get_frame_start = function() {
  74. return m_nla.get_frame_start();
  75. }
  76. /**
  77. * Get NLA animation ending frame for the active scene
  78. * @method module:nla.get_frame_end
  79. * @returns {number} End frame.
  80. */
  81. exports.get_frame_end = function() {
  82. return m_nla.get_frame_end();
  83. }
  84. /**
  85. * Check if the current scene is currently NLA animated.
  86. * @method module:nla.check_nla
  87. * @returns {boolean} Check result.
  88. */
  89. exports.check_nla = function() {
  90. return m_nla.check_nla();
  91. }
  92. /**
  93. * Check if the current scene has logic nodes
  94. * @method module:nla.check_logic_nodes
  95. * @returns {boolean} Check result.
  96. */
  97. exports.check_logic_nodes = function() {
  98. return m_nla.check_logic_nodes();
  99. }
  100. /**
  101. * Play NLA animation for the active scene from start frame to end frame.
  102. * @method module:nla.set_range
  103. * @param {number} start_frame Start NLA animation frame
  104. * @param {number} end_frame End NLA animation frame
  105. */
  106. exports.set_range = function(start_frame, end_frame) {
  107. if (m_nla.check_logic_nodes()) {
  108. m_print.error("The active scene is using the Logic Editor.");
  109. return;
  110. }
  111. end_frame = parseFloat(end_frame) || m_nla.get_frame_end();
  112. start_frame = parseFloat(start_frame) || m_nla.get_frame_end();
  113. end_frame = m_util.clamp(end_frame, m_nla.get_frame_start(),
  114. m_nla.get_frame_end());
  115. start_frame = m_util.clamp(start_frame, m_nla.get_frame_start(),
  116. end_frame);
  117. m_nla.set_range(start_frame, end_frame);
  118. }
  119. /**
  120. * Reset NLA animation playing range
  121. * @method module:nla.reset_range
  122. */
  123. exports.reset_range = function() {
  124. if (m_nla.check_logic_nodes()) {
  125. m_print.error("The active scene is using the Logic Editor.");
  126. return;
  127. }
  128. m_nla.reset_range();
  129. }
  130. /**
  131. * Set cyclic behaviour for the active scene
  132. * @method module:nla.set_cyclic
  133. * @param {boolean} is_cyclic Cyclic behavior.
  134. */
  135. exports.set_cyclic = function(is_cyclic) {
  136. if (m_nla.check_logic_nodes()) {
  137. m_print.error("The active scene is using the Logic Editor.");
  138. return;
  139. }
  140. m_nla.set_cyclic(is_cyclic);
  141. }
  142. /**
  143. * Clear callback executed after the NLA animation finished
  144. * @method module:nla.clear_callback
  145. */
  146. exports.clear_callback = function() {
  147. if (m_nla.check_logic_nodes()) {
  148. m_print.error("The active scene is using the Logic Editor.");
  149. return;
  150. }
  151. m_nla.clear_callback();
  152. }
  153. }
  154. var nla_factory = register("nla", NLA);
  155. export default nla_factory;