/src/com/pblabs/engine/core/NameManager.as

https://github.com/joelhooks/PushButtonEngine · ActionScript · 244 lines · 122 code · 33 blank · 89 comment · 43 complexity · 974d97cc6a1a0e01343ecc3a9a891a6f MD5 · raw file

  1. /*******************************************************************************
  2. * PushButton Engine
  3. * Copyright (C) 2009 PushButton Labs, LLC
  4. * For more information see http://www.pushbuttonengine.com
  5. *
  6. * This file is licensed under the terms of the MIT license, which is included
  7. * in the License.html file at the root directory of this SDK.
  8. ******************************************************************************/
  9. package com.pblabs.engine.core
  10. {
  11. import com.pblabs.engine.debug.Logger;
  12. import com.pblabs.engine.entity.IEntity;
  13. import com.pblabs.engine.entity.IEntityComponent;
  14. import flash.utils.Dictionary;
  15. /**
  16. * The name manager stores references to PBObjects that have been given
  17. * names. These PBObjects can be looked up by name.
  18. */
  19. public class NameManager
  20. {
  21. /**
  22. * The list of registered PBObjects.
  23. */
  24. public function get objectList():Dictionary
  25. {
  26. return _objects;
  27. }
  28. /**
  29. * Registers an entity under a specific name. If the name is in use, lookups will
  30. * return the last entity added under the name.
  31. *
  32. * @param entity The entity to add.
  33. * @param name The name to register the entity under.
  34. */
  35. public function add(object:IPBObject):void
  36. {
  37. var isNameValid:Boolean = (object.name != null && object.name != "");
  38. var isAliasValid:Boolean = (object.alias != null && object.alias != "");
  39. if (isNameValid && _objects[object.name])
  40. Logger.warn(this, "add", "A PBObject with the name " + object.name + " already exists. Future lookups by this name will return the newest object. Did you mean to make an entity a template?");
  41. if(TemplateManager.VERBOSE_LOGGING)
  42. Logger.print(this, "Registering PBObject '" + object.name + "', alias '" + object.alias + "'");
  43. if(isNameValid)
  44. _objects[object.name] = object;
  45. if(isAliasValid)
  46. _objects[object.alias] = object;
  47. }
  48. /**
  49. * Removes an object from the manager.
  50. *
  51. * @param entity The IPBObject to remove.
  52. */
  53. public function remove(object:IPBObject):void
  54. {
  55. // Unregister its alias, if it has one and the alias points to us.
  56. if (object.alias != null && object.alias != "" && _objects[object.alias] == object)
  57. {
  58. _objects[object.alias] = null;
  59. delete _objects[object.alias];
  60. }
  61. // And the normal name, if it is us.
  62. if(object.name != null && object.name != "" && _objects[object.name] == object )
  63. {
  64. _objects[object.name] = null;
  65. delete _objects[object.name];
  66. }
  67. unregisterAliases(object);
  68. }
  69. /**
  70. * Register an alias for an IPBObject
  71. * @param object The object to register the alias for
  72. * @param alias The alias to register
  73. **/
  74. public function registerAlias(object:IPBObject, alias:String):void
  75. {
  76. if(alias == null || alias == "")
  77. {
  78. Logger.warn(this, "registerAlias", "Attempt made to register invalid alias '"+alias+"'");
  79. return;
  80. }
  81. if(_objects[alias])
  82. {
  83. Logger.warn(this, "registerAlias","A PBObject with the name '" + alias + "' already exists ");
  84. return;
  85. }
  86. _objects[alias] = object;
  87. //Store the alias in the registeredAliases map
  88. if(!_registeredAliases[object])
  89. _registeredAliases[object] = [];
  90. _registeredAliases[object].push(alias);
  91. }
  92. /**
  93. * Unregister an alias for an IPBObject
  94. * @param object The object to register the alias for.
  95. * @param alias The alias to unregister.
  96. **/
  97. public function unregisterAlias(object:IPBObject, alias:String):void
  98. {
  99. // Check if the alias exists, points to the IPBObject and is an alias.
  100. if(_objects[alias] && _objects[alias] == object
  101. && _registeredAliases[object] && _registeredAliases[object].indexOf(alias) > -1)
  102. {
  103. _objects[alias] = null;
  104. delete _objects[alias];
  105. _registeredAliases[object].splice(_registeredAliases[object].indexOf(alias), 1);
  106. if(_registeredAliases[object].length < 1)
  107. delete _registeredAliases[object];
  108. }
  109. else
  110. {
  111. Logger.warn(this, "unregisterAlias", "Attempt made to unregister alias '"+alias+"' but the object registered is not the same or the alias is not a registered alias.");
  112. return;
  113. }
  114. }
  115. /**
  116. * Unregister all aliases for an IPBObject
  117. * @param object The IPBObject to unregister.
  118. **/
  119. public function unregisterAliases(object:IPBObject):void{
  120. if(!_registeredAliases[object])
  121. return;
  122. for each(var alias:String in _registeredAliases[object])
  123. {
  124. _objects[alias] = null;
  125. delete _objects[alias];
  126. }
  127. delete _registeredAliases[object];
  128. }
  129. /**
  130. * Looks up a PBObject with the specified name.
  131. *
  132. * @param name The name of the object to look up.
  133. *
  134. * @return The object with the specified name, or null if it wasn't found.
  135. */
  136. public function lookup(name:String):*
  137. {
  138. return _objects[name];
  139. }
  140. /**
  141. * Turns a potentially used name and returns a related unused name. The
  142. * given name will have a number appended, with the number continually
  143. * incremented until an unused name is found.
  144. *
  145. * @param name The name to validate.
  146. *
  147. * @return The validated name. This is guaranteed to not be in use.
  148. */
  149. public function validateName(name:String):String
  150. {
  151. if (!_objects[name])
  152. return name;
  153. var index:int = 1;
  154. var testName:String = name + index;
  155. while (_objects[testName])
  156. testName = name + index++;
  157. return testName;
  158. }
  159. /**
  160. * Looks up a component on an entity that has been registered. The same
  161. * conditions apply as with the lookupComponentByType method on IEntity.
  162. *
  163. * @param The name of the entity on which the component exists.
  164. * @param componentType The type of the component to lookup.
  165. *
  166. * @see com.pblabs.engine.entity.IEntity#lookupComponentByType()
  167. */
  168. public function lookupComponentByType(name:String, componentType:Class):IEntityComponent
  169. {
  170. var entity:IEntity = lookup(name);
  171. if (!entity)
  172. return null;
  173. return entity.lookupComponentByType(componentType);
  174. }
  175. /**
  176. * Looks up components on an entity that has been registered. The same
  177. * conditions apply as with the lookupComponentsByType method on IEntity.
  178. *
  179. * @param The name of the entity on which the component exists.
  180. * @param componentType The type of the components to lookup.
  181. *
  182. * @see com.pblabs.engine.entity.IEntity#lookupComponentByType()
  183. */
  184. public function lookupComponentsByType(name:String, componentType:Class):Array
  185. {
  186. var entity:IEntity = lookup(name);
  187. if (!entity)
  188. return null;
  189. return entity.lookupComponentsByType(componentType);
  190. }
  191. /**
  192. * Looks up a component on an entity that has been registered. The same
  193. * conditions apply as with the lookupComponentByName method on IEntity.
  194. *
  195. * @param The name of the entity on which the component exists.
  196. * @param componentName The name of the component to lookup.
  197. *
  198. * @see com.pblabs.engine.entity.IEntity#lookupComponentByName()
  199. */
  200. public function lookupComponentByName(name:String, componentName:String):IEntityComponent
  201. {
  202. var entity:IEntity = lookup(name);
  203. if (!entity)
  204. return null;
  205. return entity.lookupComponentByName(componentName);
  206. }
  207. private var _objects:Dictionary = new Dictionary();
  208. //Map from PBObject -> String[] that contains all registered aliases
  209. private var _registeredAliases:Dictionary = new Dictionary();
  210. }
  211. }