Esempio n. 1
0
        public void Initialize(IQueryableEntity entity)
        {
            _entity = entity;
            Visualizer.Instance.Add(this);

            OnInitialize();
        }
Esempio n. 2
0
        /// <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;
        }
Esempio n. 3
0
        public void Initialize(IQueryableEntity entity)
        {
            Entity = entity;
            DataRendererManager.Instance.Add(this);

            OnInitialize();
            UpdateVisualization(1.0f);
        }
Esempio n. 4
0
        private static DataReference <TData> CreateDataReference <TData>(IQueryableEntity entity)
            where TData : Data.IData
        {
            var dataReference = new DataReference <TData>();

            ((IDataReference)dataReference).Provider = entity;
            return(dataReference);
        }
Esempio n. 5
0
        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();
            }
        }
Esempio n. 6
0
        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);
                }
            }
        }
Esempio n. 7
0
        /// <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();
        }
Esempio n. 8
0
 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)) {
 }
Esempio n. 9
0
 public static T Previous <T>(this IQueryableEntity entity) where T : Data.IVersioned
 {
     return((T)entity.Previous(DataMap <T> .Accessor));
 }
Esempio n. 10
0
        /// <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);
 }
Esempio n. 12
0
        /// <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);
            }
        }
Esempio n. 13
0
 public override void CopyFrom(DataQueryableEntity source)
 {
     QueryableEntity = source.QueryableEntity;
 }
Esempio n. 14
0
 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))
 {
 }
Esempio n. 15
0
 /// <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))
 {
 }
Esempio n. 16
0
        public void Initialize(IQueryableEntity entity) {
            _entity = entity;
            Visualizer.Instance.Add(this);

            OnInitialize();
        }
Esempio n. 17
0
        /// <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();
        }
Esempio n. 18
0
        /// <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;
        }
Esempio n. 19
0
        /// <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);
 }
Esempio n. 21
0
 /// <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);
 }
Esempio n. 22
0
 /// <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);
     }
 }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            IQueryableEntity entity = ((IDataReference)value).Provider;

            serializer.Serialize(writer, entity);
        }
Esempio n. 25
0
        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);
                }
            }
        }
Esempio n. 26
0
            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);
                    }
                }
            }
Esempio n. 27
0
 public static T Current <T>(this IQueryableEntity entity) where T : Data.IData
 {
     return((T)entity.Current(DataMap <T> .Accessor));
 }
Esempio n. 28
0
            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);
                    }
                }
            }
Esempio n. 29
0
 public static bool ContainsData <T>(this IQueryableEntity entity) where T : Data.IData
 {
     return(entity.ContainsData(DataMap <T> .Accessor));
 }
Esempio n. 30
0
 /// <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)) {
 }