/// <summary> /// This method calls the Activate method on a chip /// if it exists in the manager. This method can be used to register /// chips as well as register them with unique IDs. Chips /// registered without class names will not be restored correctly /// through the serialization API. /// </summary> /// <param name="id"> /// The name of the chip. This should be the fully qualified class name /// if you want to automatically create the instance if one doesn't /// exist. The supplied chip will be registered to /// this id. /// </param> /// <param name="chip"> /// Instance to the chip that needs to be activated. /// </param> public void ActivateChip(string id, AbstractChip chip, bool autoActivate = true) { if (HasChip(id)) { //TODO do we need to disable the old chip first chips[id] = chip; } else { //TODO fixed bug here but need to make sure we don't need to do this above chips.Add(id, chip); if (chip is IUpdate) { updateChips.Add(chip as IUpdate); } if (chip is IDraw) { drawChips.Add(chip as IDraw); } } if (autoActivate) { chip.Activate(engine); } }
/// <summary> /// Gets a reference to a chip in the manager. This method will attempt /// to create the chip automatically. You can optionally chose to /// activate it or not when a new instance is created. Use this method /// for creating new <see cref="chips" /> at run time. /// </summary> /// <param name="id"> /// The name of the chip. This should be the fully qualified class name /// if you want to automatically create the instance if one doesn't /// exist. /// </param> /// <param name="activeOnCreate"> /// Optional value to have method automatically activate any /// chips it creates. This is set to /// true by default. /// </param> /// <returns name="AbstractChip"> /// Returns a reference to any chips associated with the supplied ID /// </returns> public AbstractChip GetChip(string id, bool activeOnCreate = true) { //Debug.Log("Chip Manager: Get Chip " + id); if (HasChip(id)) { var chip = chips[id]; if (activeOnCreate) { ActivateChip(id, chip); } return(chip); } // TODO create a chip var type = Type.GetType(id); AbstractChip chipInstance = null; try { chipInstance = Activator.CreateInstance(type) as AbstractChip; ActivateChip(id, chipInstance); } catch (Exception) { //throw new Exception("Chip '" + id + "' could not be created."); } return(chipInstance); }
/// <summary> /// This deactivates a chip and safely removed it from the manager. /// </summary> /// <param name="id">Name of the chip.</param> /// <param name="chip">Reference to the chip.</param> public void DeactivateChip(string id, AbstractChip chip) { chip.Deactivate(); if (chip is IUpdate) { updateChips.Remove(chip as IUpdate); } if (chip is IDraw) { drawChips.Remove(chip as IDraw); } chips.Remove(id); }