private object GetFieldValue(FieldInfo field, WeatherMakerPropertyTransition t)
 {
     if (t.Value != null)
     {
         if (field.FieldType == t.Value.GetType())
         {
             return(t.Value);
         }
     }
     return(field.GetValue(t.Target));
 }
 private object GetMemberValue(MemberInfo member, WeatherMakerPropertyTransition t)
 {
     if (t.Value != null)
     {
         if (member.GetUnderlyingType() == t.Value.GetType())
         {
             return(t.Value);
         }
     }
     return(member.GetUnderlyingValue(t.Target));
 }
        private void RenderRangeOfInts(Rect rect, WeatherMakerPropertyTransition t, FieldInfo field, SingleLineClampAttribute clamp)
        {
            GUIContent      labelMin = new GUIContent("Min", "Minimum Value (Inclusive)");
            GUIContent      labelMax = new GUIContent("Max", "Maximum Value (Inclusive)");
            RangeOfIntegers r        = (RangeOfIntegers)GetFieldValue(field, t);
            float           w        = rect.width;

            rect.width *= 0.5f;
            rect.width -= 6.0f;
            r.Minimum   = EditorGUI.IntField(rect, labelMin, r.Minimum);
            rect.x     += rect.width + 6.0f;
            r.Maximum   = EditorGUI.IntField(rect, labelMax, r.Maximum);
            t.Value     = r;
        }
        private void RenderIntField(Rect rect, WeatherMakerPropertyTransition t, GUIContent label, FieldInfo field, RangeAttribute range, SingleLineClampAttribute clamp)
        {
            int val = (int)GetFieldValue(field, t);

            if (clamp != null)
            {
                val = Mathf.Clamp(val, (int)clamp.MinValue, (int)clamp.MaxValue);
            }
            if (range == null)
            {
                t.Value = EditorGUI.IntField(rect, label, val);
            }
            else
            {
                t.Value = EditorGUI.IntSlider(rect, label, val, (int)range.min, (int)range.max);
            }
        }
        private void RenderFloatField(Rect rect, WeatherMakerPropertyTransition t, GUIContent label, MemberInfo member, RangeAttribute range, SingleLineClampAttribute clamp)
        {
            float val = (float)GetMemberValue(member, t);

            if (clamp != null)
            {
                val = Mathf.Clamp(val, clamp.MinValue, clamp.MaxValue);
            }
            if (range == null)
            {
                t.Value = EditorGUI.FloatField(rect, label, val);
            }
            else
            {
                t.Value = EditorGUI.Slider(rect, label, val, range.min, range.max);
            }
        }
        private void RenderTransition(WeatherMakerTransitionGroupWeight w, Rect rect, int index)
        {
            if (index >= w.TransitionGroup.Transitions.Count)
            {
                return;
            }

            float textFieldWidth             = Mathf.Min(rect.width, 150.0f);
            float longWidth                  = Mathf.Min(rect.width, 300.0f);
            float fullWidth                  = rect.width - 10.0f;
            WeatherMakerPropertyTransition t = w.TransitionGroup.Transitions[index];

            EditorGUIUtility.labelWidth = labelWidth;
            float width = rect.width;
            float x     = rect.x;

            rect.height = individualHeight;
            rect.width  = fullWidth;
            MonoBehaviour newTarget = (MonoBehaviour)EditorGUI.ObjectField(rect, "Target", t.Target, typeof(MonoBehaviour), true);

            if (newTarget != t.Target)
            {
                t.Target         = newTarget;
                t.FieldNamePopup = null;
            }
            rect.y += individualHeightWithSpacing;
            if (t.Target != null)
            {
                List <FieldInfo> fields = GetPossibleFields(t.Target);
                if (fields.Count == 0)
                {
                    EditorGUI.LabelField(rect, "* No valid fields found *");
                }
                else
                {
                    if (t.FieldNamePopup == null)
                    {
                        t.FieldNamePopup = new PopupList();
                        t.FieldNamePopup.SelectedIndexChanged = (pu, ea) =>
                        {
                            if (t.FieldNamePopup.SelectedItemIndex < fields.Count)
                            {
                                t.FieldName = fields[t.FieldNamePopup.SelectedItemIndex].Name;
                            }
                            t.Curve = null;
                        };
                    }
                    rect.width = longWidth;
                    int i = 0;
                    foreach (FieldInfo f in fields)
                    {
                        if (f.Name == t.FieldName)
                        {
                            t.FieldNamePopup.SelectedItemIndex = i;
                            break;
                        }
                        i++;
                    }
                    EditorGUI.LabelField(rect, "Field:");
                    rect.x    += EditorGUIUtility.labelWidth;
                    rect.width = fullWidth - EditorGUIUtility.labelWidth;
                    GUIStyle buttonStyle = "button";
                    buttonStyle.alignment = TextAnchor.MiddleLeft;
                    if (GUI.Button(rect, fields[t.FieldNamePopup.SelectedItemIndex].Name + " (" + GetFieldDisplayName(fields[t.FieldNamePopup.SelectedItemIndex].FieldType.Name) + ")", buttonStyle))
                    {
                        List <GUIContent> options = new List <GUIContent>();
                        foreach (FieldInfo f in fields)
                        {
                            options.Add(new GUIContent(f.Name + " (" + GetFieldDisplayName(f.FieldType.Name) + ")"));
                        }
                        t.FieldNamePopup.ListStyle = comboBoxStyle;
                        t.FieldNamePopup.Items     = options.ToArray();
                        PopupWindow.Show(rect, t.FieldNamePopup);
                    }
                    string newName = fields[t.FieldNamePopup.SelectedItemIndex].Name;
                    if (newName != t.FieldName)
                    {
                        t.FieldName = newName;
                        GUI.changed = true;
                    }
                    rect.x  = x;
                    rect.y += individualHeightWithSpacing + 2.0f;
                    FieldInfo field = t.Target.GetType().GetField(t.FieldName, BindingFlags.GetField | BindingFlags.SetField | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    rect.width = fullWidth;
                    RenderField(rect, t, field, textFieldWidth);
                    rect.y += individualHeightWithSpacing;
                    AnimationCurve curve = t.Curve;
                    if (curve == null || curve.length == 0)
                    {
                        Type ft = fields[t.FieldNamePopup.SelectedItemIndex].FieldType;

                        // default to immediately setting the value
                        if (ft == typeof(bool) || ft == typeof(System.Enum) || ft == typeof(RangeOfFloats) || ft == typeof(RangeOfIntegers))
                        {
                            curve = AnimationCurve.Linear(0.0f, 1.0f, 0.0f, 1.0f);
                        }
                        else
                        {
                            // default to linear curve
                            curve = AnimationCurve.Linear(0.0f, 0.0f, 1.0f, 1.0f);
                        }
                    }
                    t.Curve     = EditorGUI.CurveField(rect, "Curve", curve);
                    rect.y     += individualHeightWithSpacing;
                    rect.height = 0.0f;
                    rect.width  = fullWidth;
                    EditorDrawLine.DrawLine(rect, Color.cyan, 1.0f);
                }
            }
        }
        private void RenderField(Rect rect, WeatherMakerPropertyTransition t, FieldInfo field, float textFieldWidth)
        {
            if (t == null || field == null)
            {
                return;
            }
            string                   tooltip = null;
            RangeAttribute           range   = null;
            SingleLineClampAttribute clamp   = null;

            object[] attributes = field.GetCustomAttributes(false);
            foreach (object obj in attributes)
            {
                if (obj is TooltipAttribute)
                {
                    tooltip = (obj as TooltipAttribute).tooltip;
                }
                else if (obj is SingleLineAttribute)
                {
                    tooltip = (obj as SingleLineAttribute).Tooltip;
                }
                else if (obj is RangeAttribute)
                {
                    range = obj as RangeAttribute;
                }
                else if (obj is SingleLineClampAttribute)
                {
                    clamp = obj as SingleLineClampAttribute;
                }
            }
            GUIContent label = new GUIContent("Value", tooltip);

            EditorGUIUtility.fieldWidth = 70.0f;
            if (field.FieldType == typeof(float))
            {
                RenderFloatField(rect, t, label, field, range, clamp);
            }
            else if (field.FieldType == typeof(int))
            {
                RenderIntField(rect, t, label, field, range, clamp);
            }
            else if (field.FieldType == typeof(bool))
            {
                t.Value = EditorGUI.Toggle(rect, label, (bool)GetFieldValue(field, t));
            }
            else if (field.FieldType == typeof(Color))
            {
                t.Value = EditorGUI.ColorField(rect, label, (Color)GetFieldValue(field, t));
            }
            else if (field.FieldType == typeof(Vector2))
            {
                t.Value = EditorGUI.Vector2Field(rect, label, (Vector2)GetFieldValue(field, t));
            }
            else if (field.FieldType == typeof(Vector3))
            {
                t.Value = EditorGUI.Vector3Field(rect, label, (Vector3)GetFieldValue(field, t));
            }
            else if (field.FieldType == typeof(Vector4))
            {
                t.Value = EditorGUI.Vector4Field(rect, label, (Vector4)GetFieldValue(field, t));
            }
            else if (field.FieldType == typeof(RangeOfFloats))
            {
                RenderRangeOfFloats(rect, t, field, clamp);
            }
            else if (field.FieldType == typeof(RangeOfIntegers))
            {
                RenderRangeOfInts(rect, t, field, clamp);
            }
            else if (field.FieldType.IsEnum)
            {
                t.Value = EditorGUI.EnumPopup(rect, label, (System.Enum)GetFieldValue(field, t));
            }
            else
            {
                EditorGUI.LabelField(rect, "Unsupported field type " + field.FieldType);
            }
        }