public override void OnClose()
        {
            string oldUnknownType = default;
            var    oldType        = TraitGUIUtility.GetOperandValuePropertyType(m_Property, null, ref oldUnknownType);

            m_Property.FindPropertyRelative("m_Parameter").stringValue      = m_EditingOperand.Parameter;
            m_Property.FindPropertyRelative("m_Trait").objectReferenceValue = m_EditingOperand.Trait;
            m_Property.FindPropertyRelative("m_TraitPropertyId").intValue   = m_EditingOperand.TraitProperty.PropertyId;
            m_Property.FindPropertyRelative("m_Enum").objectReferenceValue  = m_EditingOperand.Enum;
            m_Property.FindPropertyRelative("m_Value").stringValue          = m_EditingOperand.Value;
            m_Property.serializedObject.ApplyModifiedProperties();

            string newUnknownType = default;
            var    newType        = TraitGUIUtility.GetOperandValuePropertyType(m_Property, null, ref newUnknownType);

            if (newType != oldType || newUnknownType != oldUnknownType)
            {
                m_OnExpectedTypeChanged?.Invoke(m_Property);
            }
        }
Esempio n. 2
0
        internal static void PropertyField(Rect rect, IList <ParameterDefinition> parameters, SerializedProperty precondition, Type[] types)
        {
            const int operatorSize = 65;
            const int spacer       = 5;

            var w          = rect.width;
            var buttonSize = (w - operatorSize - 3 * spacer) / 2;

            rect.x    += spacer;
            rect.y    += EditorGUIUtility.standardVerticalSpacing;
            rect.width = buttonSize;

            var @operator = precondition.FindPropertyRelative("m_Operator");

            switch (@operator.stringValue)
            {
            case nameof(Operation.SpecialOperators.Custom):
                rect.width = w - spacer;
                var customType = precondition.FindPropertyRelative("m_CustomOperatorType").stringValue;
                EditorStyleHelper.CustomMethodField(rect, customType, types);
                break;

            default:
            {
                var operandA = precondition.FindPropertyRelative("m_OperandA");
                var operandB = precondition.FindPropertyRelative("m_OperandB");

                TraitGUIUtility.DrawOperandSelectorField(rect, operandA, @operator, parameters, true, false, property =>
                    {
                        TraitGUIUtility.ClearOperandProperty(operandB);
                    });

                var validLeftOperand = !string.IsNullOrEmpty(operandA.FindPropertyRelative("m_Parameter").stringValue);

                rect.x    += buttonSize + spacer;
                rect.width = operatorSize;

                if (validLeftOperand)
                {
                    var operators = GetComparisonOperators(operandA);
                    var opIndex   = EditorGUI.Popup(rect, Array.IndexOf(operators, @operator.stringValue),
                                                    operators, EditorStyleHelper.listPopupStyleBold);

                    @operator.stringValue = operators[Math.Max(0, opIndex)];
                }
                else
                {
                    // No operand available
                    GUI.enabled = false;
                    GUI.Button(rect, string.Empty, EditorStyleHelper.listPopupStyle);
                    GUI.enabled = true;
                }

                rect.x    += operatorSize + spacer;
                rect.width = buttonSize;

                if (validLeftOperand)
                {
                    string unknownType = default;
                    TraitGUIUtility.DrawOperandSelectorField(rect, operandB, @operator, parameters,
                                                             TraitGUIUtility.GetOperandValuePropertyType(operandA, @operator, ref unknownType), unknownType);
                }
                else
                {
                    // No operand available
                    GUI.enabled = false;
                    GUI.Button(rect, string.Empty, EditorStyleHelper.listPopupStyle);
                    GUI.enabled = true;
                }
            }
            break;
            }
        }
        void DrawObjectModifierListElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            var actionDefinition = (target as ActionDefinition);

            if (actionDefinition == null)
            {
                return;
            }

            const int operatorSize = 50;
            const int spacer       = 5;

            var parameters        = actionDefinition.Parameters.ToList();
            var effectsParameters = actionDefinition.CreatedObjects.ToList();

            parameters.AddRange(effectsParameters);

            var w          = rect.width;
            var buttonSize = (w - operatorSize - 3 * spacer) / 2;

            rect.x    += spacer;
            rect.y    += EditorGUIUtility.standardVerticalSpacing;
            rect.width = buttonSize;

            var list   = m_ObjectModifierList.serializedProperty;
            var effect = list.GetArrayElementAtIndex(index);

            var @operator = effect.FindPropertyRelative("m_Operator");
            var operandA  = effect.FindPropertyRelative("m_OperandA");
            var operandB  = effect.FindPropertyRelative("m_OperandB");

            switch (@operator.stringValue)
            {
            case nameof(Operation.SpecialOperators.Add):
            case nameof(Operation.SpecialOperators.Remove):
            {
                TraitGUIUtility.DrawParameterSelectorField(rect, operandA.FindPropertyRelative("m_Parameter"), parameters);

                rect.x    += buttonSize + spacer;
                rect.width = operatorSize;

                EditorGUI.LabelField(rect, (@operator.stringValue == nameof(Operation.SpecialOperators.Add))?"Add":"Remove", EditorStyleHelper.listValueStyle);

                rect.x    += operatorSize + spacer;
                rect.width = buttonSize;

                var traitDefinitions = PlannerAssetDatabase.TraitDefinitions.ToArray();
                var traitNames       = traitDefinitions.Select(t => t?.name).ToArray();

                var traitIndex = Array.IndexOf(traitDefinitions, operandB.FindPropertyRelative("m_Trait").objectReferenceValue as TraitDefinition);
                EditorGUI.BeginChangeCheck();
                traitIndex = EditorGUI.Popup(rect, traitIndex, traitNames, EditorStyleHelper.listPopupStyle);

                if (EditorGUI.EndChangeCheck())
                {
                    TraitGUIUtility.ClearOperandProperty(operandB);
                    operandB.FindPropertyRelative("m_Trait").objectReferenceValue = traitDefinitions[traitIndex];
                }
            }
            break;

            case nameof(Operation.SpecialOperators.Custom):
                rect.width = w - spacer;
                var customType = effect.FindPropertyRelative("m_CustomOperatorType").stringValue;
                EditorStyleHelper.CustomMethodField(rect, customType, PlannerCustomTypeCache.ActionEffectTypes);
                break;

            default:
            {
                TraitGUIUtility.DrawOperandSelectorField(rect, operandA, null, parameters, modified =>
                    {
                        TraitGUIUtility.ClearOperandProperty(operandB);
                    });

                var validLeftOperand = !string.IsNullOrEmpty(operandA.FindPropertyRelative("m_Parameter").stringValue);

                rect.x    += buttonSize + spacer;
                rect.width = operatorSize;

                if (validLeftOperand)
                {
                    var operators = GetAssignmentOperators(operandA);
                    var opIndex   = EditorGUI.Popup(rect, Array.IndexOf(operators, @operator.stringValue),
                                                    operators, EditorStyleHelper.listPopupStyleBold);
                    var operatorSelected = operators[Math.Max(0, opIndex)];
                    if (@operator.stringValue != operatorSelected)
                    {
                        if (TraitGUIUtility.IsListOperand(operandA) && TraitGUIUtility.IsListUnaryOperator(@operator))
                        {
                            TraitGUIUtility.ClearOperandProperty(operandB);
                        }

                        @operator.stringValue = operatorSelected;
                    }
                }
                else
                {
                    // No operand available
                    GUI.enabled = false;
                    GUI.Button(rect, string.Empty, EditorStyleHelper.listPopupStyle);
                    GUI.enabled = true;
                }

                var showRightOperand = !TraitGUIUtility.IsListOperand(operandA) || !TraitGUIUtility.IsListUnaryOperator(@operator);

                rect.x    += operatorSize + spacer;
                rect.width = buttonSize;

                if (validLeftOperand && showRightOperand)
                {
                    string unknownType = default;
                    TraitGUIUtility.DrawOperandSelectorField(rect, operandB, @operator, parameters,
                                                             TraitGUIUtility.GetOperandValuePropertyType(operandA, @operator, ref unknownType), unknownType);
                }
                else
                {
                    // No operand available
                    GUI.enabled = false;
                    GUI.Button(rect, string.Empty, EditorStyleHelper.listPopupStyle);
                    GUI.enabled = true;
                }
            }
            break;
            }
        }