assets.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (C) 2014-2016 Triumph LLC
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. "use strict";
  18. /**
  19. * Low-level resource loader. In order to load exported scenes, use the {@link module:data|data} module instead.
  20. * @module assets
  21. * @local AssetCallback
  22. * @local ProgressCallback
  23. * @local PackCallback
  24. */
  25. b4w.module["assets"] = function(exports, require) {
  26. var m_assets = require("__assets");
  27. /**
  28. * Loading asset.
  29. * Asset has the following structure: [uri, type, filepath, optional_param],
  30. * where uri - asset identifier, type - asset type, filepath -
  31. * path to resource (URL), optional_param - any param passed to {@link module:assets~AssetCallback|AssetCallback}
  32. * @typedef {Array} Asset
  33. * @alias module:assets.Asset
  34. */
  35. /**
  36. * Callback executed after a single asset is loaded.
  37. * @callback AssetCallback
  38. * @param {Data} data Loaded data
  39. * @param {String} uri Data asset ID
  40. * @param {Number} type Data type
  41. * @param {String} filepath Data filepath
  42. * @param {*} [optional_param] Optional parameter
  43. */
  44. /**
  45. * Callback executed after the whole pack of assets is loaded.
  46. * @callback PackCallback
  47. */
  48. /**
  49. * Callback for the progress of loading.
  50. * @callback ProgressCallback
  51. * @param {Number} value Loading percentage
  52. */
  53. /**
  54. * Asset type: ArrayBuffer
  55. * @const module:assets.AT_ARRAYBUFFER
  56. */
  57. exports.AT_ARRAYBUFFER = m_assets.AT_ARRAYBUFFER;
  58. /**
  59. * Asset type: JSON
  60. * @const module:assets.AT_JSON
  61. */
  62. exports.AT_JSON = m_assets.AT_JSON;
  63. /**
  64. * Asset type: Text
  65. * @const module:assets.AT_TEXT
  66. */
  67. exports.AT_TEXT = m_assets.AT_TEXT;
  68. /**
  69. * Asset type: AudioBuffer
  70. * @const module:assets.AT_AUDIOBUFFER
  71. */
  72. exports.AT_AUDIOBUFFER = m_assets.AT_AUDIOBUFFER;
  73. /**
  74. * Asset type: HTMLImageElement
  75. * @const module:assets.AT_IMAGE_ELEMENT
  76. */
  77. exports.AT_IMAGE_ELEMENT = m_assets.AT_IMAGE_ELEMENT;
  78. /**
  79. * Asset type: HTMLAudioElement
  80. * @const module:assets.AT_AUDIO_ELEMENT
  81. */
  82. exports.AT_AUDIO_ELEMENT = m_assets.AT_AUDIO_ELEMENT;
  83. /**
  84. * Add the assets to the loading queue.
  85. * @method module:assets.enqueue
  86. * @param {Asset[]} assets_pack Array of the loading assets
  87. * @param {AssetCallback} [asset_cb] Callback executed after a single asset is loaded
  88. * @param {PackCallback} [pack_cb] Callback executed after the whole pack of assets is loaded
  89. * @param {ProgressCallback} [progress_cb] Callback for the progress of loading
  90. */
  91. exports.enqueue = function(assets_pack, asset_cb, pack_cb, progress_cb) {
  92. if (assets_pack.length)
  93. if (assets_pack["id"])
  94. m_assets.enqueue(assets_pack, asset_cb, pack_cb, progress_cb);
  95. else {
  96. var new_asset_pack = [];
  97. for (var i = 0; i < assets_pack.length; i++) {
  98. var pack_elem = assets_pack[i];
  99. new_asset_pack.push({
  100. id: pack_elem[0],
  101. type: pack_elem[1],
  102. url: pack_elem[2],
  103. request: pack_elem.request ? pack_elem.request : "GET",
  104. post_type: null,
  105. post_data: null,
  106. param: pack_elem[3]
  107. });
  108. }
  109. m_assets.enqueue(new_asset_pack, asset_cb, pack_cb, progress_cb);
  110. }
  111. }
  112. }