/home/lindsay/xeolabs/xeogl-next/xeogl/src/core.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/src/core.js

  1. import {Map} from './utils/map.js';
  2. import {stats} from './stats.js';
  3. import {utils} from './utils.js';
  4. import {Scene} from "./scene/scene.js";
  5. import {componentClasses} from "./componentClasses.js";
  6.  
  7. const scenesRenderInfo = {}; // Used for throttling FPS for each Scene
  8. const sceneIDMap = new Map(); // Ensures unique scene IDs
  9. let defaultScene = null;// Default singleton Scene, lazy-initialized in getter
  10.  
  11. const core = {
  12.  
  13. /**
  14. Semantic version number. The value for this is set by an expression that's concatenated to
  15. the end of the built binary by the xeogl build script.
  16. @property version
  17. @namespace xeogl
  18. @type {String}
  19. */
  20. version: null,
  21.  
  22. /**
  23. Existing {{#crossLink "Scene"}}Scene{{/crossLink}}s , mapped to their IDs
  24. @property scenes
  25. @namespace xeogl
  26. @type {{String:xeogl.Scene}}
  27. */
  28. scenes: {},
  29.  
  30. _superTypes: {}, // For each component type, a list of its supertypes, ordered upwards in the hierarchy.
  31.  
  32. /**
  33. Returns the current default {{#crossLink "Scene"}}{{/crossLink}}.
  34.  
  35. If no Scenes exist yet, or no Scene has been made default yet with a previous call to
  36. {{#crossLink "xeogl/setDefaultScene:function"}}{{/crossLink}}, then this method will create the default
  37. Scene on-the-fly.
  38.  
  39. Components created without specifying their Scene will be created within this Scene.
  40.  
  41. @method getDefaultScene
  42. @returns {Scene} The current default scene
  43. */
  44. getDefaultScene() {
  45. if (!defaultScene) {
  46. defaultScene = new Scene({id: "default.scene"});
  47. }
  48. return defaultScene;
  49. },
  50.  
  51. /**
  52. Sets the current default {{#crossLink "Scene"}}{{/crossLink}}.
  53.  
  54. A subsequent call to {{#crossLink "xeogl/getDefaultScene:function"}}{{/crossLink}} will return this Scene.
  55.  
  56. Components created without specifying their Scene will be created within this Scene.
  57.  
  58. @method setDefaultScene
  59. @param {Scene} scene The new current default scene
  60. @returns {Scene} The new current default scene
  61. */
  62. setDefaultScene(scene) {
  63. defaultScene = scene;
  64. return defaultScene;
  65. },
  66.  
  67. /**
  68. Registers a scene on xeogl.
  69. This is called within the xeogl.Scene constructor.
  70.  
  71. @method _addScene
  72. @param {Scene} scene The scene
  73. @private
  74. */
  75. _addScene(scene) {
  76. if (scene.id) { // User-supplied ID
  77. if (core.scenes[scene.id]) {
  78. console.error(`[ERROR] Scene ${utils.inQuotes(scene.id)} already exists`);
  79. return;
  80. }
  81. } else { // Auto-generated ID
  82. scene.id = sceneIDMap.addItem({});
  83. }
  84. core.scenes[scene.id] = scene;
  85. const ticksPerRender = scene.ticksPerRender;
  86. scenesRenderInfo[scene.id] = {
  87. ticksPerRender,
  88. renderCountdown: ticksPerRender
  89. };
  90. stats.components.scenes++;
  91. scene.on("destroyed", () => { // Unregister destroyed scenes
  92. sceneIDMap.removeItem(scene.id);
  93. delete core.scenes[scene.id];
  94. delete scenesRenderInfo[scene.id];
  95. stats.components.scenes--;
  96. });
  97. },
  98.  
  99. /**
  100. Destroys all user-created {{#crossLink "Scene"}}Scenes{{/crossLink}} and
  101. clears the default {{#crossLink "Scene"}}Scene{{/crossLink}}.
  102.  
  103. @method clear
  104. @demo foo
  105. */
  106. clear() {
  107. let scene;
  108. for (const id in core.scenes) {
  109. if (core.scenes.hasOwnProperty(id)) {
  110. scene = core.scenes[id];
  111. // Only clear the default Scene
  112. // but destroy all the others
  113. if (id === "default.scene") {
  114. scene.clear();
  115. } else {
  116. scene.destroy();
  117. delete core.scenes[scene.id];
  118. }
  119. }
  120. }
  121. },
  122.  
  123. //////////////////////////////////////////////////////////////////////////
  124. /////////// Fix me
  125. //////////////////////////////////////////////////////////////////////////
  126.  
  127. /**
  128. Tests if the given component type is a subtype of another component supertype.
  129. @param {String} type
  130. @param {String} [superType="xeogl.Component"]
  131. @returns {boolean}
  132. @private
  133. */
  134. isComponentType: function (type, superType = "xeogl.Component") {
  135. if (type === superType) {
  136. return true;
  137. }
  138. var clas = componentClasses[type];
  139. if (!clas) {
  140. return false;
  141. }
  142. var superClas = componentClasses[superType];
  143. if (!superClas) {
  144. return false;
  145. }
  146. let result = subclasses(clas, superClas);
  147. return result;
  148. }
  149. };
  150.  
  151. function subclasses(ChildClass, ParentClass) {
  152. var c = ChildClass.prototype;
  153. while (c !== null) {
  154. if (c === ParentClass.prototype) {
  155. return true;
  156. }
  157. c = c.__proto__;
  158. }
  159. return false;
  160. }
  161.  
  162. export {core};