Source: extern/data.js

  1. import register from "../util/register.js";
  2. import m_data_fact from "../intern/data.js";
  3. import m_loader_fact from "../intern/loader.js";
  4. /**
  5. * Data API. Used to load/unload exported JSON data files.
  6. * @module data
  7. * @local StageloadCallback
  8. * @local LoadedCallback
  9. */
  10. function Data(ns, exports) {
  11. /**
  12. * Data loaded callback.
  13. * Executed when the data loading process has been completed.
  14. * @callback LoadedCallback
  15. * @param {number} data_id Data ID
  16. * @param {boolean} success Load success
  17. */
  18. /**
  19. * Loading stage callback.
  20. * Used to implement loading progress indicators (preloaders).
  21. * @callback StageloadCallback
  22. * @param {number} percentage Loading progress (0-100).
  23. * @param {number} load_time Loading time in ms.
  24. * @param {number} data_id Data ID
  25. */
  26. var m_data = m_data_fact(ns);
  27. var m_loader = m_loader_fact(ns);
  28. /**
  29. * Load data from the json file exported from Blender.
  30. * @method module:data.load
  31. * @param {string} path Path to JSON file
  32. * @param {LoadedCallback} [loaded_cb=null] Callback to be executed right after load
  33. * @param {StageloadCallback} [stageload_cb=null] Callback to report about the loading progress
  34. * @param {boolean} [wait_complete_loading=false] Wait until all resources are loaded
  35. * @param {boolean} [load_hidden=false] Hide loaded and disable physics objects
  36. * @returns {number} ID of loaded data.
  37. */
  38. exports.load = m_data.load;
  39. /**
  40. * Unload the previously loaded data.
  41. * @method module:data.unload
  42. * @param {number} [data_id=0] ID of unloaded data. Unload all data if data_id is zero.
  43. */
  44. exports.unload = function(data_id) {
  45. data_id = data_id | 0;
  46. m_data.unload(data_id);
  47. }
  48. /**
  49. * Set the root which contains the resources, for debug purposes.
  50. * Enables the checking of loading paths, so if the resources are not loaded from
  51. * the app root, there will be a warning in m_print.
  52. * @method module:data.set_debug_resources_root
  53. * @param {string} debug_resources_root App root directory.
  54. */
  55. exports.set_debug_resources_root = m_data.set_debug_resources_root;
  56. /**
  57. * Check if the engine primary data (main scene) is loaded (detect the last loading stage).
  58. * @method module:data.is_primary_loaded
  59. * @returns {boolean} Check result
  60. */
  61. exports.is_primary_loaded = m_data.is_primary_loaded;
  62. /**
  63. * Check if the engine has finished all of the scheduled loading actions.
  64. * @method module:data.is_idle
  65. * @returns {boolean} Check result
  66. */
  67. exports.is_idle = m_loader.is_finished;
  68. exports.load_and_add_new = m_data.load;
  69. exports.cleanup = exports.unload;
  70. /**
  71. * Activate media data context.
  72. * Activation of audio/video contexts is required for mobile platforms which
  73. * disable media playback without explicit user interaction. This method
  74. * should be executed inside some input event listener.
  75. * @method module:data.activate_media
  76. */
  77. exports.activate_media = m_data.activate_media;
  78. /**
  79. * Preload scene's resources and put them into cache.
  80. * @param {string} path Path to JSON file
  81. * @param {LoadedCallback} [loaded_cb=null] Callback to be executed right after load
  82. * @param {StageloadCallback} [stageload_cb=null] Callback to report about the loading progress
  83. * @method module:data.prefetch
  84. */
  85. exports.prefetch = m_data.prefetch;
  86. /**
  87. * Clear loading cache.
  88. * @method module:data.unfetch
  89. */
  90. exports.unfetch = m_data.unfetch;
  91. }
  92. var data_factory = register("data", Data);
  93. export default data_factory;