AssetHandler.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /************************************************************************
  2. * AssetHandler.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. const AssetHandler = {
  22. cachedTextures: [],
  23. cachedAudio: [],
  24. cachedFonts: [],
  25. loadTexture: function(name, link)
  26. {
  27. let textRes = new TextureRes(name, null);
  28. this.cachedTextures.push(textRes);
  29. loadImage(link, img =>
  30. {
  31. textRes.P5Image = img;
  32. });
  33. },
  34. getTextureByName: function(name)
  35. {
  36. for (let i = 0; i < this.cachedTextures.length; i++)
  37. {
  38. if (this.cachedTextures[i].name == name)
  39. {
  40. return this.cachedTextures[i];
  41. }
  42. }
  43. return null;
  44. },
  45. getP5ImageByName: function(name)
  46. {
  47. for (let i = 0; i < this.cachedTextures.length; i++)
  48. {
  49. if (this.cachedTextures[i].name == name)
  50. {
  51. return this.cachedTextures[i].P5Image;
  52. }
  53. }
  54. return null;
  55. },
  56. loadAudio: function(name, link)
  57. {
  58. let audio = createAudio(link);
  59. this.cachedAudio.push(new AudioRes(name, audio));
  60. },
  61. getAudioByName: function(name)
  62. {
  63. for (let i = 0; i < this.cachedAudio.length; i++)
  64. {
  65. if (this.cachedAudio[i].name == name)
  66. {
  67. return this.cachedAudio[i];
  68. }
  69. }
  70. return null;
  71. },
  72. getP5AudioByName: function(name)
  73. {
  74. for (let i = 0; i < this.cachedAudio.length; i++)
  75. {
  76. if (this.cachedAudio[i].name == name)
  77. {
  78. return this.cachedAudio[i].P5Audio;
  79. }
  80. }
  81. return null;
  82. },
  83. loadFont: function(name, link)
  84. {
  85. let fontRes = new FontRes(name, null);
  86. this.cachedFonts.push(fontRes);
  87. loadFont(link, font =>
  88. {
  89. fontRes.P5Font = font;
  90. })
  91. },
  92. getFontByName: function(name)
  93. {
  94. for (let i = 0; i < this.cachedFonts.length; i++)
  95. {
  96. if (this.cachedFonts[i].name == name)
  97. {
  98. return this.cachedFonts[i];
  99. }
  100. }
  101. return null;
  102. },
  103. getP5FontByName: function(name)
  104. {
  105. for (let i = 0; i < this.cachedFonts.length; i++)
  106. {
  107. if (this.cachedFonts[i].name == name)
  108. {
  109. return this.cachedFonts[i].P5Font;
  110. }
  111. }
  112. return null;
  113. }
  114. };