Beispiel #1
0
        private void AssignTable(RSRuleTableData inTable)
        {
            if (m_Table == inTable)
            {
                return;
            }

            if (m_Table != null)
            {
                m_Entity.Manager?.DeregisterTriggers(m_Entity, m_Table.UniqueTriggers);
            }

            m_Table = inTable;
            StopAll();

            if (m_Table != null)
            {
                int length = m_Table.Rules != null ? m_Table.Rules.Length : 0;
                Array.Resize(ref m_States, length);
                Array.Resize(ref m_Routines, length);

                for (int i = 0; i < length; ++i)
                {
                    RSRuleData rule = m_Table.Rules[i];
                    m_States[i] = rule.Enabled ? 0 : RuleState.Disabled;
                }

                m_Entity.Manager?.RegisterTriggers(m_Entity, m_Table.UniqueTriggers);
            }
            else
            {
                m_States   = null;
                m_Routines = null;
            }
        }
        static private void ValidateRule(RSRuleData inRule, RSValidationState ioState, RSValidationContext inContext)
        {
            if (inRule == null)
            {
                ioState.Error("Null rule");
                return;
            }

            ioState.PushContext("Trigger");
            RSTriggerInfo triggerInfo = ValidateTriggerId(inRule.TriggerId, RSValidationFlags.None, ioState, inContext);

            inContext = inContext.WithTrigger(triggerInfo);
            ioState.PopContext();

            if (inRule.Conditions != null && inRule.Conditions.Length > 0)
            {
                for (int i = 0; i < inRule.Conditions.Length; ++i)
                {
                    ioState.PushContext("Condition {0}", i);
                    ValidateCondition(inRule.Conditions[i], ioState, inContext);
                    ioState.PopContext();
                }
            }

            if (inRule.Actions != null && inRule.Actions.Length > 0)
            {
                for (int i = 0; i < inRule.Actions.Length; ++i)
                {
                    ioState.PushContext("Action {0}", i);
                    ValidateAction(inRule.Actions[i], ioState, inContext);
                    ioState.PopContext();
                }
            }
        }
Beispiel #3
0
        static public void CopyRule(RSRuleData inRuleData)
        {
            Clear();

            s_CurrentTarget = Target.Rule;
            s_CurrentRule   = inRuleData.Clone();
        }
Beispiel #4
0
 static public void Clear()
 {
     s_CurrentTarget    = Target.None;
     s_CurrentRule      = null;
     s_CurrentCondition = null;
     s_CurrentAction    = null;
 }
Beispiel #5
0
        static public void PasteRule(RSRuleData ioTarget)
        {
            if (!HasRule())
            {
                Debug.LogError("No rule copied");
                return;
            }

            ioTarget.CopyFrom(s_CurrentRule);
        }
        private void OnAddNewRule(ReorderableList list)
        {
            RSRuleData ruleData = new RSRuleData(true);
            int        index    = list.index;

            if (index >= 0)
            {
                ++index;
            }
            InsertRule(ruleData, index);
        }
Beispiel #7
0
        /// <summary>
        /// Resets all rules.
        /// </summary>
        public void ResetAll()
        {
            StopAll();

            if (m_Table != null)
            {
                int length = m_Table.Rules != null ? m_Table.Rules.Length : 0;
                for (int i = 0; i < length; ++i)
                {
                    RSRuleData rule = m_Table.Rules[i];
                    m_States[i] = rule.Enabled ? 0 : RuleState.Disabled;
                }
            }
        }
        private void ConfigureActionList(RSRuleData inRule, ref RSReorderableList <RSActionData> ioList)
        {
            ioList = new RSReorderableList <RSActionData>(inRule.Actions);
            ioList.drawElementCallback     = RenderActionListElement;
            ioList.drawNoneElementCallback = RenderNoActionsElement;
            ioList.drawHeaderCallback      = RenderActionsHeaderElement;
            ioList.onAddCallback           = OnAddNewAction;
            ioList.onRemoveCallback        = OnRemoveAction;
            ioList.onSelectCallback        = OnSelectAction;
            ioList.onWillReorderCallback   = OnWillReorder;
            ioList.onReorderCallback       = OnActionReorder;
            ioList.index = m_SelectionState.ActionIndex;

            SyncAllowedListOperations(ioList);
        }
        private void InsertRule(RSRuleData inRule, int inIndex = -1)
        {
            m_TargetState.UndoTarget.MarkDirty("Added Rule", true);
            if (inIndex < 0 || inIndex >= m_SelectionState.Table.Rules.Length)
            {
                ArrayUtility.Add(ref m_SelectionState.Table.Rules, inRule);
                inIndex = m_SelectionState.Table.Rules.Length - 1;
            }
            else
            {
                ArrayUtility.Insert(ref m_SelectionState.Table.Rules, inIndex, inRule);
            }

            TableUtils.UpdateUniqueRuleTriggers(m_SelectionState.Table);
            SelectRule(inIndex);
        }
Beispiel #10
0
        /// <summary>
        /// Enables all rules with the given name.
        /// </summary>
        public void EnableRule(string inRuleName)
        {
            RSRuleData[] rules = m_Table?.Rules;
            if (rules == null || rules.Length <= 0)
            {
                return;
            }

            for (int i = 0; i < rules.Length; ++i)
            {
                RSRuleData rule = rules[i];
                if (ScriptUtils.StringMatch(rule.Name, inRuleName))
                {
                    m_States[i] &= ~RuleState.Disabled;
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// Stops execution of all rules with the given group.
        /// </summary>
        public void StopRuleGroup(string inGroupName)
        {
            RSRuleData[] rules = m_Table?.Rules;
            if (rules == null || rules.Length <= 0)
            {
                return;
            }

            for (int i = 0; i < rules.Length; ++i)
            {
                RSRuleData rule = rules[i];
                if (ScriptUtils.StringMatch(rule.RoutineGroup, inGroupName))
                {
                    m_Routines[i].Stop();
                }
            }
        }
        private void ShowRuleElementContextMenu(RSRuleData inRuleData, int inIndex)
        {
            GenericMenu menu = new GenericMenu();

            menu.AddItem(s_ContextMenuCopyLabel, false, () => RSEditorClipboard.CopyRule(inRuleData));
            if (RSEditorClipboard.HasRule())
            {
                menu.AddItem(s_ContextMenuPasteOverwriteLabel, false, () =>
                {
                    m_TargetState.UndoTarget.MarkDirty("Paste rule (overwrite)");
                    RSEditorClipboard.PasteRule(inRuleData);
                });
                if (EditorApplication.isPlaying)
                {
                    menu.AddDisabledItem(s_ContextMenuPasteInsertLabel, false);
                }
                else
                {
                    menu.AddItem(s_ContextMenuPasteInsertLabel, false, () =>
                    {
                        RSRuleData clone = RSEditorClipboard.PasteRule();
                        InsertRule(clone, inIndex + 1);
                    });
                }
            }
            else
            {
                menu.AddDisabledItem(s_ContextMenuPasteOverwriteLabel, false);
                menu.AddDisabledItem(s_ContextMenuPasteInsertLabel, false);
            }

            if (EditorApplication.isPlaying)
            {
                menu.AddDisabledItem(s_ContextMenuDeleteLabel, false);
            }
            else
            {
                menu.AddItem(s_ContextMenuDeleteLabel, false, () => DeleteRule(inIndex));
            }

            menu.ShowAsContext();
        }
        private void ShowRuleHeaderContextMenu()
        {
            GenericMenu menu = new GenericMenu();

            if (EditorApplication.isPlaying)
            {
                menu.AddDisabledItem(s_ContextMenuPasteAddToEndLabel, false);
                menu.AddDisabledItem(s_ContextMenuDeleteAllLabel, false);
            }
            else
            {
                if (RSEditorClipboard.HasRule())
                {
                    menu.AddItem(s_ContextMenuPasteAddToEndLabel, false, () =>
                    {
                        RSRuleData clone = RSEditorClipboard.PasteRule();
                        InsertRule(clone, -1);
                    });
                }
                else
                {
                    menu.AddDisabledItem(s_ContextMenuPasteAddToEndLabel, false);
                }

                if (m_SelectionState.Table.Rules.Length > 0)
                {
                    menu.AddItem(s_ContextMenuDeleteAllLabel, false, () =>
                    {
                        SelectRule(-1);
                        m_TargetState.UndoTarget.MarkDirty("Removed all Rules", true);
                        m_SelectionState.Table.Rules = new RSRuleData[0];
                    });
                }
                else
                {
                    menu.AddDisabledItem(s_ContextMenuDeleteAllLabel, false);
                }
            }

            menu.ShowAsContext();
        }
        private void RenderRuleListElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            RSRuleData rule = m_SelectionState.Table.Rules[index];

            Rect labelRect = rect;

            labelRect.width -= CLONE_BUTTON_WIDTH + CLONE_BUTTON_SPACING;

            string labelText = rule.GetPreviewString(null, m_Context.Library);

            using (new RSGUI.ColorScope(rule.Enabled ? Color.white : Color.gray))
            {
                EditorGUI.LabelField(labelRect, labelText);
            }

            Rect cloneRect = rect;

            cloneRect.width   = CLONE_BUTTON_WIDTH;
            cloneRect.height -= 4;
            cloneRect.x       = labelRect.xMax + CLONE_BUTTON_SPACING;

            using (new EditorGUI.DisabledScope(EditorApplication.isPlaying))
            {
                if (GUI.Button(cloneRect, "Clone"))
                {
                    RSRuleData clone = rule.Clone();
                    clone.Name += " (Clone)";
                    InsertRule(clone, index + 1);
                }
            }

            if (DetectContextClick(rect))
            {
                ShowRuleElementContextMenu(rule, index);
            }
        }
Beispiel #15
0
        internal void EvaluateTrigger(RSTriggerId inTriggerId, ExecutionScope inScope)
        {
            if (!m_Entity.IsAlive())
            {
                return;
            }

            if (OnTrigger != null)
            {
                object arg = RSInterop.ToObject(inScope.Argument, inScope);
                OnTrigger.Invoke(inTriggerId, arg);
            }

            RSRuleData[] rules = m_Table?.Rules;
            int          ruleCount;

            if (rules == null || (ruleCount = rules.Length) <= 0)
            {
                return;
            }

            using (PooledSet <string> triggeredGroups = PooledSet <string> .Alloc())
            {
                for (int i = 0; i < ruleCount; ++i)
                {
                    RSRuleData rule = rules[i];
                    if (rule.TriggerId != inTriggerId)
                    {
                        continue;
                    }

                    if (m_States[i].HasFlag(RuleState.Disabled))
                    {
                        continue;
                    }

                    if (rule.DontInterrupt && m_Routines[i])
                    {
                        continue;
                    }

                    if (!inScope.EvaluateConditions(rule.Conditions, rule.ConditionSubset))
                    {
                        continue;
                    }

                    if (!string.IsNullOrEmpty(rule.RoutineGroup))
                    {
                        if (!triggeredGroups.Add(rule.RoutineGroup))
                        {
                            continue;
                        }

                        StopRuleGroup(rule.RoutineGroup);
                    }

                    if (rule.OnlyOnce)
                    {
                        m_States[i] |= RuleState.Disabled;
                    }

                    if (rule.Actions != null)
                    {
                        ExecutionScope scope = inScope;
                        scope.m_Environment.CloneScopeIfNecessary(scope, rule.Flags, out scope);

                        m_Routines[i].Replace(m_Entity.ProxyObject, scope.PerformActions(rule.Actions))
                        .ExecuteWhileDisabled().SetPhase(m_Entity.ExecutionPhase)
                        .TryManuallyUpdate(0);
                    }
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// Renders editor for rule info. Does not include conditions or actions.
        /// </summary>
        static public void RuleData(UndoTarget inUndo, RSRuleData ioRule, RSRuleTableData ioTable, RSValidationFlags inFlags, RSValidationContext inContext)
        {
            string preview = ioRule.GetPreviewString(null, inContext.Library);

            GUILayout.Label(preview, RSGUIStyles.RuleHeaderStyle);

            EditorGUILayout.Space();

            string newName = EditorGUILayout.TextField(Content.RuleNameLabel, ioRule.Name);

            if (newName != ioRule.Name)
            {
                inUndo.MarkDirty("Changed Rule Name");
                ioRule.Name = newName;
            }

            RSTriggerId newTriggerId;

            if (inFlags.Has(RSValidationFlags.FilterSelection) && inContext.Entity != null)
            {
                newTriggerId = LibraryGUILayout.TriggerSelector(Content.RuleTriggerLabel, ioRule.TriggerId, inContext.Entity, inContext.Library);
            }
            else
            {
                newTriggerId = LibraryGUILayout.TriggerSelector(Content.RuleTriggerLabel, ioRule.TriggerId, inContext.Library);
            }

            if (newTriggerId != ioRule.TriggerId)
            {
                inUndo.MarkDirty("Changed Rule Trigger", true);
                ioRule.TriggerId = newTriggerId;

                TableUtils.UpdateUniqueRuleTriggers(ioTable);
            }

            if (newTriggerId != RSTriggerId.Null)
            {
                RSTriggerInfo info = inContext.Library.GetTrigger(newTriggerId);
                if (info != null && info.ParameterType != null)
                {
                    using (new EditorGUI.IndentLevelScope())
                    {
                        EditorGUILayout.LabelField(Content.RuleParameterLabel, EditorGUIUtility.TrTextContent(info.ParameterType.ToStringWithoutDefault(), info.ParameterType.Tooltip));
                    }
                }
            }

            EditorGUILayout.Space();

            // Enabled
            bool bEnabled = EditorGUILayout.Toggle(Content.RuleEnabledLabel, ioRule.Enabled);

            if (bEnabled != ioRule.Enabled)
            {
                inUndo.MarkDirty("Changed Rule Enabled");
                ioRule.Enabled = bEnabled;
            }

            bool bOnlyOnce = EditorGUILayout.Toggle(Content.RuleOnlyOnceLabel, ioRule.OnlyOnce);

            if (bOnlyOnce != ioRule.OnlyOnce)
            {
                inUndo.MarkDirty("Changed Rule OnlyOnce");
                ioRule.OnlyOnce = bOnlyOnce;
            }

            bool bDontInterrupt = EditorGUILayout.Toggle(Content.RuleDontInterruptLabel, ioRule.DontInterrupt);

            if (bDontInterrupt != ioRule.DontInterrupt)
            {
                inUndo.MarkDirty("Changed Rule DontInterrupt");
                ioRule.DontInterrupt = bDontInterrupt;
            }

            EditorGUILayout.Space();

            string newGroup = EditorGUILayout.TextField(Content.RuleGroupLabel, ioRule.RoutineGroup);

            if (newGroup != ioRule.RoutineGroup)
            {
                inUndo.MarkDirty("Changed Rule Group");
                ioRule.RoutineGroup = newGroup;
            }
        }