static void UpdateComponentQueryInternal(State previousState, ComponentQueryDeclarationModel queryDeclarationModel, IComponentQueryAction queryAction, int oldIndex, int newIndex) { UnityEngine.Assertions.Assert.IsTrue(queryAction is MoveComponentInQueryAction || (oldIndex == -1 && newIndex == -1)); s_CachedVariableList.Clear(); s_CachedVariableList.AddRange(((VSGraphModel)previousState.CurrentGraphModel).FindUsages(queryDeclarationModel)); foreach (var usage in s_CachedVariableList) { IPortModel output = usage.OutputPort; if (output != null) { s_CachedList.Clear(); s_CachedList.AddRange(queryDeclarationModel.GraphModel.GetConnections(output)); foreach (var connected in s_CachedList) { if (connected.NodeModel is IIteratorStackModel iteratorStackModel) { Assert.IsTrue(iteratorStackModel is IPrivateIteratorStackModel); ForAllEntitiesStackModel.UpdateComponentsVariables((IPrivateIteratorStackModel)iteratorStackModel, queryAction, oldIndex, newIndex); } connected.NodeModel.OnConnection(connected, output); } } previousState.CurrentGraphModel.LastChanges.ChangedElements.Add(usage); } }
internal static void UpdateComponentsVariables(IPrivateIteratorStackModel iteratorStackModel, IComponentQueryAction action, int movedOldIndex, int movedNewIndex) { void RemoveComponentVariable(IList <VariableDeclarationModel> list, ComponentDefinition componentDefinition) { VariableDeclarationModel toRemove = list.FirstOrDefault(x => x.DataType == componentDefinition.TypeHandle); if (toRemove) { ((VSGraphModel)iteratorStackModel.GraphModel).DeleteVariableDeclarations(Enumerable.Repeat(toRemove, 1), true); } else { Debug.LogWarning($"Failed to remove declaration of type {componentDefinition.TypeHandle} from stack {iteratorStackModel} not found"); } } IList <VariableDeclarationModel> variableDeclarationModels = iteratorStackModel.FunctionParameters; switch (action) { case RemoveComponentFromQueryAction removeAction: RemoveComponentVariable(variableDeclarationModels, removeAction.ComponentDefinition); break; case ChangeComponentUsageAction changeAction when changeAction.Subtract: // putting as subtractive is equivalent to removing the component RemoveComponentVariable(variableDeclarationModels, changeAction.ComponentDefinition); break; case ChangeComponentUsageAction changeAction when !changeAction.Subtract: // putting as subtractive is equivalent to removing the component // need to insert it var queryComponent = changeAction.ComponentQueryDeclarationModel.Query.Find(changeAction.ComponentDefinition, out _); if (queryComponent == null) { return; } queryComponent.ForceInsert = true; break; case MoveComponentInQueryAction _: Assert.AreNotEqual(-1, movedNewIndex); Assert.AreNotEqual(-1, movedOldIndex); variableDeclarationModels.Insert(movedNewIndex + k_NonComponentVariablesCount, variableDeclarationModels[movedOldIndex + k_NonComponentVariablesCount]); variableDeclarationModels.RemoveAt(movedOldIndex + k_NonComponentVariablesCount + (movedOldIndex > movedNewIndex ? 1 : 0)); break; case AddComponentToQueryAction _: // we always add at the end, so nothing to do case ChangeComponentTypeAction _: // nothing to do, the previous declaration will get reused break; } }
static void UpdateComponentQuery(State previousState, ComponentQueryDeclarationModel queryDeclarationModel, IComponentQueryAction queryAction) { Assert.False(queryAction is MoveComponentInQueryAction, $"Use {nameof(UpdateComponentQueryInternal)} instead when moving components"); UpdateComponentQueryInternal(previousState, queryDeclarationModel, queryAction, -1, -1); }