public void Initialize(IQueryableEntity entity) { _entity = entity; Visualizer.Instance.Add(this); OnInitialize(); }
/// <summary> /// Constructs a container GameObject for the given entity under the given root object. /// </summary> /// <param name="entity">The entity to create a GameObject for</param> /// <param name="root">The root object to create the GameObject under</param> /// <returns>The created GameObject.</returns> protected static GameObject CreateGameObject(IQueryableEntity entity, GameObject root) { GameObject created = null; // There is special logic for instantiating an object If the entity contains prefab data // that specifies it should be instantiated from a specific resource, then we load that // resource and clone it instead of creating a new object if (entity.ContainsData<PrefabData>()) { string resourcePath = entity.Current<PrefabData>().PrefabResourcePath; GameObject prefab = (GameObject)Resources.Load(resourcePath); if (prefab == null) { created = new GameObject(""); Debug.LogError("Unable to find prefab resource \"" + resourcePath + "\"", created); } else { created = (GameObject)Instantiate(prefab); } } else { created = new GameObject(""); } created.transform.parent = root.transform; return created; }
public void Initialize(IQueryableEntity entity) { Entity = entity; DataRendererManager.Instance.Add(this); OnInitialize(); UpdateVisualization(1.0f); }
private static DataReference <TData> CreateDataReference <TData>(IQueryableEntity entity) where TData : Data.IData { var dataReference = new DataReference <TData>(); ((IDataReference)dataReference).Provider = entity; return(dataReference); }
public void DrawAddData(IQueryableEntity entity) { GUILayout.Space(5); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); Rect rect = GUILayoutUtility.GetRect(new GUIContent("Add Data"), new GUIStyle("Button"), GUILayout.Width(250)); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); _lastAddDataRect = GUIToScreenRect(rect); if (GUI.Button(rect, "Add Data")) { AddDataMenuItem(); } }
public void DrawDataInspector(IQueryableEntity entity, Type dataType, GameObject context) { DataEditState editState = GetEditState(entity, dataType); bool removedData = false; EditorGUILayout.BeginHorizontal(); { SetHidden(entity, dataType, ForgeEditorUtils.DrawFoldout(IsHidden(entity, dataType))); EditorGUILayout.LabelField(GetDataHeader(dataType, entity, editState), ForgeEditorUtils.HeaderStyle); if (CanEditPrevious(entity, dataType)) { bool editingCurrent = ForgeEditorUtils.DrawPrettyToggle( editState == DataEditState.Current, "Switch to Previous", "Switch to Current", GUILayout.ExpandWidth(false)); SetEditState(entity, dataType, editingCurrent ? DataEditState.Current : DataEditState.Previous); } if (GUILayout.Button("X", GUILayout.ExpandWidth(false))) { RemoveData(entity, dataType); removedData = true; } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); // It is possible that we removed the data instance from the entity as above. In that // case, we don't want to actually edit it. if (removedData) { return; } // edit the data if (IsHidden(entity, dataType) == false) { IDataInspector inspector = DataInspector.Get(dataType); Data.IData data = GetEditedData(entity, editState, dataType); try { inspector.Edit(data, context); } catch (ExitGUIException) { throw; } catch (Exception e) { Debug.LogError("While running inspector caught exception " + e); } } }
/// <summary> /// Called when we have an entity to inspect. /// </summary> private void DrawInspector(IQueryableEntity entity, GameObject context) { _inspectorScroll = EditorGUILayout.BeginScrollView(_inspectorScroll); EditorGUILayout.LabelField("Metadata", ForgeEditorUtils.HeaderStyle); entity.PrettyName = EditorGUILayout.TextField("Pretty Name", entity.PrettyName); if (GUI.changed) { BaseContainer container = context.GetComponent<BaseContainer>(); container.UpdateName(); } ForgeEditorUtils.DrawSeperator(); foreach (DataAccessor accessor in entity.SelectData(/*includeRemoved:*/ true)) { Type dataType = entity.Current(accessor).GetType(); _dataInspectorView.DrawDataInspector(entity, dataType, context); ForgeEditorUtils.DrawSeperator(); } DrawAddData(entity); EditorGUILayout.EndScrollView(); }
internal PreviousRequiresVersionedDataException(IQueryableEntity context, DataAccessor accessor) : base(string.Format("Retrieving previous data requires that the data extends " + "Data.IVersioned, but type={0} in context={1} does not", accessor.DataType, context)) { }
public static T Previous <T>(this IQueryableEntity entity) where T : Data.IVersioned { return((T)entity.Previous(DataMap <T> .Accessor)); }
/// <summary> /// Sets the editing state for the given data type in the given entity to the given value. /// </summary> private void SetEditState(IQueryableEntity entity, Type dataType, DataEditState value) { int entityId = ((IEntity)entity).UniqueId; HashSet<int> hashSet; if (_previous.TryGetValue(entityId, out hashSet) == false) { hashSet = new HashSet<int>(); _previous[entityId] = hashSet; } int dataId = new DataAccessor(dataType).Id; switch (value) { case DataEditState.Current: hashSet.Remove(dataId); break; case DataEditState.Previous: hashSet.Add(dataId); break; } }
public void Add(IQueryableEntity entity) { Entities.Add(entity); }
/// <summary> /// Removes the given data type from the entity. /// </summary> private static void RemoveData(IQueryableEntity entity, Type dataType) { var accessor = new DataAccessor(dataType); if (entity is IEntity) { ((IEntity)entity).RemoveData(accessor); } else if (entity is ITemplate) { ((ITemplate)entity).RemoveDefaultData(accessor); } }
public override void CopyFrom(DataQueryableEntity source) { QueryableEntity = source.QueryableEntity; }
/// <summary> /// Creates the exception with the given context and data type. /// </summary> /// <param name="context">The entity that triggered the exception.</param> /// <param name="accessor">The data type that the entity lacks.</param> internal NoSuchDataException(IQueryableEntity context, DataAccessor accessor) : base(string.Format("No such data for type={0} in context={1}", accessor.DataType, context)) { }
/// <summary> /// Returns the data header that should be shown above the data type. /// </summary> private static string GetDataHeader(Type dataType, IQueryableEntity queryableEntity, DataEditState editState) { StringBuilder result = new StringBuilder(); result.Append(dataType.Name); if (queryableEntity is IEntity) { result.Append(editState == DataEditState.Current ? " (current)" : " (previous)"); IEntity entity = (IEntity)queryableEntity; DataAccessor accessor = new DataAccessor(dataType); if (entity.WasAdded(accessor)) { result.Append(" (added)"); } if (entity.WasModified(accessor)) { result.Append(" (modified)"); } if (entity.WasRemoved(accessor)) { result.Append(" (removed)"); } } return result.ToString(); }
/// <summary> /// Returns the current editing state for the given data type in the given entity. /// </summary> private DataEditState GetEditState(IQueryableEntity entity, Type dataType) { if (CanEditPrevious(entity, dataType)) { HashSet<int> value; if (_previous.TryGetValue(((IEntity)entity).UniqueId, out value)) { int id = new DataAccessor(dataType).Id; return value.Contains(id) ? DataEditState.Previous : DataEditState.Current; } } // ITemplate / Data.NonVersioned don't support previous return DataEditState.Current; }
/// <summary> /// Returns the data instance that should be edited based on the given edit state and data /// type. /// </summary> private static Data.IData GetEditedData(IQueryableEntity entity, DataEditState editState, Type dataType) { var accessor = new DataAccessor(dataType); switch (editState) { case DataEditState.Current: return entity.Current(accessor); case DataEditState.Previous: return entity.Previous(accessor); } throw new InvalidOperationException("Unknown edit state"); }
public QueryConditions(IQueryableEntity entity) { Entities.Add(entity); }
/// <summary> /// Returns true if the given data type has a previous instance that can be edited for the /// given entity. /// </summary> private bool CanEditPrevious(IQueryableEntity entity, Type dataType) { return entity is IEntity && typeof(Data.IVersioned).IsAssignableFrom(dataType); }
/// <summary> /// Initializes renderers for any data that is currently contained within the given entity. /// </summary> /// <param name="containingObject">The GameObject that contains the BaseContainer for the /// entity.</param> /// <param name="entity">The entity to select data from.</param> protected static void InitializeRenderers(GameObject containingObject, IQueryableEntity entity) { ICollection<DataAccessor> entityData = entity.SelectData(); foreach (DataAccessor accessor in entityData) { DataRegistry.TryAddRenderer(accessor, containingObject, entity); } }
private bool IsHidden(IQueryableEntity entity, Type dataType) { int id; if (entity is ITemplate) { id = ((ITemplate)entity).TemplateId; } else { id = -(((IEntity)entity).UniqueId + 1); } HashSet<int> hiddenData; if (_hidden.TryGetValue(id, out hiddenData) == false) { return false; } return hiddenData.Contains(new DataAccessor(dataType).Id); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { IQueryableEntity entity = ((IDataReference)value).Provider; serializer.Serialize(writer, entity); }
private void SetHidden(IQueryableEntity entity, Type dataType, bool value) { int id; if (entity is ITemplate) { id = ((ITemplate)entity).TemplateId; } else { id = -(((IEntity)entity).UniqueId + 1); } if (value == true) { HashSet<int> hiddenData; if (_hidden.TryGetValue(id, out hiddenData) == false) { hiddenData = new HashSet<int>(); _hidden[id] = hiddenData; } hiddenData.Add(new DataAccessor(dataType).Id); } else { HashSet<int> hiddenData; if (_hidden.TryGetValue(id, out hiddenData)) { hiddenData.Remove(new DataAccessor(dataType).Id); } } }
private void ComputeDataTypes(IEnumerable<Type> requiredTypes, IQueryableEntity entity, out List<Type> missing, out List<Type> contained) { missing = new List<Type>(); contained = new List<Type>(); foreach (var type in requiredTypes) { DataAccessor accessor = new DataAccessor(type); if (entity.ContainsData(accessor)) { contained.Add(type); } else { missing.Add(type); } } }
public static T Current <T>(this IQueryableEntity entity) where T : Data.IData { return((T)entity.Current(DataMap <T> .Accessor)); }
private void ComputeSystems(GameObject selected, IQueryableEntity entity, out List<SystemSuccess> success, out List<SystemPartial> partial, out List<SystemFail> failed, out List<ISystem> generic) { success = new List<SystemSuccess>(); partial = new List<SystemPartial>(); failed = new List<SystemFail>(); generic = new List<ISystem>(); foreach (ISystem system in GetSystems(selected)) { if (system is ITriggerFilterProvider) { if (entity == null) { failed.Add(new SystemFail() { System = system }); } else { ITriggerFilterProvider filter = (ITriggerFilterProvider)system; List<Type> missing, contained; ComputeDataTypes(filter.RequiredDataTypes, entity, out missing, out contained); if (contained.Count == 0) { failed.Add(new SystemFail() { System = filter }); } else if (contained.Count > 0) { if (missing.Count == 0) { success.Add(new SystemSuccess() { System = system, ContainedTypes = contained }); } else { partial.Add(new SystemPartial() { System = system, ContainedTypes = contained, MissingTypes = missing }); } } } } else { generic.Add(system); } } }
public static bool ContainsData <T>(this IQueryableEntity entity) where T : Data.IData { return(entity.ContainsData(DataMap <T> .Accessor)); }