public static void DrawSettingValue(ICacheEntry setting, object value)
        {
            if (Event.current.isKey && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter))
            {
                _userHasHitReturn = true;
            }

            if (setting is MethodCacheEntry)
            {
                DrawUnknownField(value);
            }
            else
            {
                var canSetValue = setting.CanSetValue();
                if (!canSetValue)
                {
                    GUI.color = Color.gray;
                }

                var t = setting.Type();
                if (t.IsEnum)
                {
                    if (t.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
                    {
                        DrawFlagsField(setting, Enum.GetValues(t), value);
                    }
                    else
                    {
                        DrawComboboxField(setting, Enum.GetValues(t), value);
                    }
                }
                else if (setting.CanEnterValue() && SettingDrawHandlers.TryGetValue(setting.Type(), out var drawMethod))
                {
                    drawMethod(setting, value);
                }
                else
                {
                    if (canSetValue && ToStringConverter.CanEditValue(setting, value))
                    {
                        DrawGenericEditableValue(setting, value, GUILayout.ExpandWidth(true));
                    }
                    else
                    {
                        DrawUnknownField(value);
                    }
                }

                GUI.color = Color.white;
            }
        }
Exemplo n.º 2
0
        private void DrawSingleContentEntry(ICacheEntry entry)
        {
            GUILayout.BeginHorizontal((_inspectorRecordHeight));
            {
                GUILayout.Label(entry.TypeName(), (_inspectorTypeWidth));

                var value = entry.GetValue();

                if (entry.CanEnterValue() || value is Exception)
                {
                    DrawVariableNameEnterButton(entry);
                }
                else
                {
                    DrawVariableName(entry);
                }

                if (entry.CanSetValue() &&
                    CanCovert(EditorUtilities.ExtractText(value), entry.Type()))
                {
                    DrawEditableValue(entry, value, GUILayout.ExpandWidth(true));
                }
                else
                {
                    DrawValue(value, GUILayout.ExpandWidth(true));
                }

                if (DnSpyHelper.IsAvailable && GUILayout.Button("^", _dnSpyButtonOptions))
                {
                    DnSpyHelper.OpenInDnSpy(entry);
                }
            }
            GUILayout.EndHorizontal();
        }
Exemplo n.º 3
0
        private void DrawEditableValue(ICacheEntry field, object value, params GUILayoutOption[] layoutParams)
        {
            var isBeingEdited = _currentlyEditingTag == field;
            var text          = isBeingEdited ? _currentlyEditingText : EditorUtilities.ExtractText(value);
            var result        = GUILayout.TextField(text, layoutParams);

            if (!Equals(text, result) || isBeingEdited)
            {
                if (_userHasHitReturn)
                {
                    _currentlyEditingTag = null;
                    _userHasHitReturn    = false;
                    try
                    {
                        var converted = Convert.ChangeType(result, field.Type());
                        if (!Equals(converted, value))
                        {
                            field.SetValue(converted);
                        }
                    }
                    catch (Exception ex)
                    {
                        RuntimeUnityEditorCore.Logger.Log(LogLevel.Error, "[Inspector] Failed to set value - " + ex.Message);
                    }
                }
                else
                {
                    _currentlyEditingText = result;
                    _currentlyEditingTag  = field;
                }
            }
        }
Exemplo n.º 4
0
        public static string GetEditValue(ICacheEntry field, object value)
        {
            var valueType = field.Type();

            if (valueType == typeof(string))
            {
                return((string)value ?? "");
            }

            var isNull = value.IsNullOrDestroyed();

            if (isNull != null)
            {
                return(isNull);
            }

            var typeConverter = TomlTypeConverter.GetConverter(valueType);

            if (typeConverter != null)
            {
                return(typeConverter.ConvertToString(value, valueType));
            }

            return(ToStringConverter.ObjectToString(value));
        }
Exemplo n.º 5
0
        public static bool CanEditValue(ICacheEntry field, object value)
        {
            var valueType = field.Type();

            if (valueType == typeof(string))
            {
                return(true);
            }

            if (_canCovertCache.ContainsKey(valueType))
            {
                return(_canCovertCache[valueType]);
            }

            if (TomlTypeConverter.GetConverter(valueType) != null)
            {
                _canCovertCache[valueType] = true;
                return(true);
            }

            try
            {
                var converted = ToStringConverter.ObjectToString(value);
                var _         = Convert.ChangeType(converted, valueType);
                _canCovertCache[valueType] = true;
                return(true);
            }
            catch
            {
                _canCovertCache[valueType] = false;
                return(false);
            }
        }
        private static void DrawFlagsField(ICacheEntry setting, IList enumValues, object fieldValue)
        {
            var currentValue = Convert.ToInt64(fieldValue);
            var allValues    = enumValues.Cast <Enum>().Select(x => new { name = x.ToString(), val = Convert.ToInt64(x) }).ToArray();

            // Vertically stack Horizontal groups of the options to deal with the options taking more width than is available in the window
            GUILayout.BeginVertical(GUILayout.ExpandWidth(true));
            {
                for (var index = 0; index < allValues.Length;)
                {
                    GUILayout.BeginHorizontal();
                    {
                        var currentWidth = 0;
                        for (; index < allValues.Length; index++)
                        {
                            var value = allValues[index];

                            // Skip the 0 / none enum value, just uncheck everything to get 0
                            if (value.val != 0)
                            {
                                // Make sure this horizontal group doesn't extend over window width, if it does then start a new horiz group below
                                var textDimension = (int)GUI.skin.toggle.CalcSize(new GUIContent(value.name)).x;
                                currentWidth += textDimension;
                                if (currentWidth > 370)
                                {
                                    break;
                                }

                                GUI.changed = false;
                                var newVal = GUILayout.Toggle((currentValue & value.val) == value.val, value.name,
                                                              GUILayout.ExpandWidth(false));
                                if (GUI.changed)
                                {
                                    var newValue = newVal ? currentValue | value.val : currentValue & ~value.val;
                                    setting.SetValue(Enum.ToObject(setting.Type(), newValue));
                                }
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }

                GUI.changed = false;
            }
            GUILayout.EndVertical();
            // Make sure the reset button is properly spaced
            GUILayout.FlexibleSpace();
        }
Exemplo n.º 7
0
        public static void SetEditValue(ICacheEntry field, object value, string result)
        {
            var    valueType = field.Type();
            object converted;

            if (valueType == typeof(string))
            {
                converted = result;
            }
            else
            {
                var typeConverter = TomlTypeConverter.GetConverter(valueType);
                converted = typeConverter != null?typeConverter.ConvertToObject(result, valueType) : Convert.ChangeType(result, valueType);
            }

            if (!Equals(converted, value))
            {
                field.SetValue(converted);
            }
        }