Ejemplo n.º 1
0
 /// <summary>
 ///     Sets the text element text to the specified text and data values.
 /// </summary>
 public void SetText()
 {
     try
     {
         TextElement.SetText(Text.Replace("{data}", Data.GetValueData().ToString()));
     }
     catch
     {
         // Ignore
     }
 }
        /// <summary>
        ///     Draws the data field.
        /// </summary>
        /// <param name="position"></param>
        /// <param name="property"></param>
        /// <param name="label"></param>
        protected void DrawDataField(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializedProperty useConstant = property.FindPropertyRelative("UseConstant");
            Event currentEvent             = Event.current;

            _mouseDragArea.x      = position.x;
            _mouseDragArea.y      = position.y;
            _mouseDragArea.width  = position.width;
            _mouseDragArea.height = EditorGUIUtility.singleLineHeight;

            if (_mouseDragArea.Contains(currentEvent.mousePosition))
            {
                if (currentEvent.type == EventType.DragUpdated)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    currentEvent.Use();
                }
                else if (currentEvent.type == EventType.DragPerform && DragAndDrop.objectReferences.Length > 0)
                {
                    Object             draggedObject     = DragAndDrop.objectReferences[0];
                    SerializedProperty referenceProperty = property.FindPropertyRelative("VariableType");
                    Type variableType = TypeExtensions.GetInstanceType(referenceProperty.type);

                    if (draggedObject.GetType().IsAssignableFrom(variableType))
                    {
                        referenceProperty.objectReferenceValue = draggedObject;
                        useConstant.boolValue = false;
                        property.serializedObject.ApplyModifiedProperties();
                    }

                    currentEvent.Use();

                    return;
                }
            }

            if (_labelContent == null)
            {
                _labelContent = new GUIContent(label.text, label.image, label.tooltip);
            }

            SerializedProperty displayValueChangedProperty = property.FindPropertyRelative("_displayValueChangedEvent");

            _displayValueChangedEvent = displayValueChangedProperty.boolValue;
            _isConstant             = useConstant.boolValue;
            label                   = EditorGUI.BeginProperty(position, GUIContent.none, property);
            _currentPosition        = EditorGUI.PrefixLabel(position, _labelContent);
            _positionWidth          = _currentPosition.width;
            _currentPosition.width  = _positionWidth * 0.2f;
            _currentPosition.height = EditorGUIUtility.singleLineHeight;

            EditorGUI.LabelField(position, label);

            _buttonsRect.x      = _currentPosition.x;
            _buttonsRect.y      = _currentPosition.y;
            _buttonsRect.width  = _buttonWidth;
            _buttonsRect.height = _currentPosition.height;

            if (GUI.Button(_buttonsRect, _rButtonContent))
            {
                _isConstant = false;
            }

            _buttonsRect.x += _buttonWidth;

            if (GUI.Button(_buttonsRect, _vButtonContent))
            {
                _isConstant = true;
            }

            if (_isConstant)
            {
                _buttonsRect.x     += _buttonWidth;
                _buttonsRect.width += 6f;

                // SerializedProperty persistentCalls =
                // property.FindPropertyRelative("OnConstantValueChanged._PersistentCalls");

                // Color originalColour = GUI.backgroundColor;

                // if (GUI.Button(_buttonsRect, $"E{persistentCalls.arraySize}"))
                // {
                // _displayValueChangedEvent = !_displayValueChangedEvent;
                // }

                // GUI.backgroundColor = originalColour;
            }

            _currentPosition.x    += _positionWidth * 0.28f;
            _currentPosition.width = _positionWidth * 0.72f;

            bool propertyIsArray = property.propertyPath.Contains("Array");

            if (_isConstant)
            {
                EditorGUI.BeginChangeCheck();

                EditorGUI.PropertyField(_currentPosition, property.FindPropertyRelative("ConstantValueType"),
                                        GUIContent.none);

                if (EditorGUI.EndChangeCheck())
                {
                    _invokeChangeEvent = true;
                }

                _lineHeight = propertyIsArray ? _lineHeight : 0f;
            }
            else
            {
                SerializedProperty referenceProperty = property.FindPropertyRelative("VariableType");

                bool  nullDetected  = false;
                Color currentColour = Color.white;

                if (referenceProperty.propertyType == SerializedPropertyType.ObjectReference)
                {
                    nullDetected = referenceProperty.objectReferenceValue == null;

                    if (nullDetected)
                    {
                        currentColour = GUI.color;
                    }
                }

                EditorGUI.PropertyField(_currentPosition, referenceProperty,
                                        GUIContent.none);

                if (nullDetected)
                {
                    GUI.color = currentColour;
                }

                if (!propertyIsArray)
                {
                    Type      dataType  = property.serializedObject.targetObject.GetType();
                    FieldInfo fieldData = dataType.GetField(property.propertyPath);

                    if (fieldData != null)
                    {
                        _dataValue = (DataField)fieldData.GetValue(property.serializedObject.targetObject);
                    }
                }

                PrimitiveData valueVariable = _dataValue?.GetVariable();
                object        valueData     = valueVariable != null?valueVariable.GetValueData() : null;

                if (valueData != null)
                {
                    _currentPosition.y     += EditorGUIUtility.singleLineHeight;
                    _lineHeight             = EditorGUIUtility.singleLineHeight;
                    _currentPosition.width -= 24f;

                    EditorGUI.LabelField(_currentPosition, valueData.ToString(),
                                         EditorStyles.toolbarButton);

                    _currentPosition.x    += _currentPosition.width + 2;
                    _currentPosition.width = 22f;

                    if (GUI.Button(_currentPosition, "→"))
                    {
                        Selection.activeObject = valueVariable;
                        EditorGUIUtility.PingObject(valueVariable);
                    }
                }
                else
                {
                    _lineHeight = propertyIsArray ? _lineHeight : 0f;
                }
            }

            if (_displayValueChangedEvent && _isConstant)
            {
                _currentPosition.x     = position.x;
                _currentPosition.y    += EditorGUIUtility.singleLineHeight;
                _currentPosition.width = position.width;
                EditorGUI.PropertyField(_currentPosition, property.FindPropertyRelative("OnConstantValueChanged"));
            }

            EditorGUI.EndProperty();

            if (GUI.changed)
            {
                useConstant.boolValue = _isConstant;
                displayValueChangedProperty.boolValue = _displayValueChangedEvent;
                property.serializedObject.ApplyModifiedProperties();

                if (_invokeChangeEvent)
                {
                    // object dataField = fieldInfo.GetValue(property.serializedObject.targetObject);
                    // object _event    = dataField.GetType().GetField("OnConstantValueChanged").GetValue(dataField);

                    // MethodInfo invokeMethod = _event.GetType().GetMethod("Invoke");
                    // invokeMethod.Invoke(_event, new[] {dataField.GetType().GetField("ConstantValueType").GetValue(dataField)});
                }
            }
        }