|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Collections.Generic; |
| 4 | +using Mapzen; |
| 5 | +using Mapzen.Unity; |
| 6 | +using Mapzen.VectorData.Filters; |
| 7 | +using UnityEditor; |
| 8 | +using UnityEngine; |
| 9 | + |
| 10 | +[Serializable] |
| 11 | +public class MatcherEditor : EditorBase |
| 12 | +{ |
| 13 | + [SerializeField] |
| 14 | + private FeatureStyle.Matcher.Type selectedMatcherType; |
| 15 | + |
| 16 | + [SerializeField] |
| 17 | + private FeatureStyle.Matcher matcher; |
| 18 | + |
| 19 | + [SerializeField] |
| 20 | + private List<MatcherEditor> matcherEditors; |
| 21 | + |
| 22 | + public FeatureStyle.Matcher Matcher |
| 23 | + { |
| 24 | + get { return matcher; } |
| 25 | + } |
| 26 | + |
| 27 | + public MatcherEditor(FeatureStyle.Matcher matcher) |
| 28 | + : base() |
| 29 | + { |
| 30 | + this.matcher = matcher; |
| 31 | + this.matcherEditors = new List<MatcherEditor>(); |
| 32 | + |
| 33 | + foreach (var matcherChild in matcher.Matchers) |
| 34 | + { |
| 35 | + this.matcherEditors.Add(new MatcherEditor(matcherChild)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private MatcherEditor AddMatcherLayout() |
| 40 | + { |
| 41 | + MatcherEditor editor = null; |
| 42 | + |
| 43 | + EditorGUILayout.BeginHorizontal(); |
| 44 | + { |
| 45 | + var matcherTypeList = Enum.GetValues(typeof(FeatureStyle.Matcher.Type)).Cast<FeatureStyle.Matcher.Type>(); |
| 46 | + var matcherTypeStringList = matcherTypeList.Select(type => type.ToString()); |
| 47 | + |
| 48 | + selectedMatcherType = (FeatureStyle.Matcher.Type)EditorGUILayout.Popup("Matcher:", |
| 49 | + (int)selectedMatcherType, matcherTypeStringList.ToArray()); |
| 50 | + |
| 51 | + EditorConfig.SetColor(EditorConfig.AddButtonColor); |
| 52 | + if (GUILayout.Button(EditorConfig.AddButtonContent, EditorConfig.SmallButtonWidth) |
| 53 | + && selectedMatcherType != FeatureStyle.Matcher.Type.None) |
| 54 | + { |
| 55 | + var matcherType = (FeatureStyle.Matcher.Type)selectedMatcherType; |
| 56 | + var newMatcher = new FeatureStyle.Matcher(matcherType); |
| 57 | + |
| 58 | + editor = new MatcherEditor(newMatcher); |
| 59 | + matcher.Matchers.Add(newMatcher); |
| 60 | + } |
| 61 | + EditorConfig.ResetColor(); |
| 62 | + } |
| 63 | + EditorGUILayout.EndHorizontal(); |
| 64 | + |
| 65 | + return editor; |
| 66 | + } |
| 67 | + |
| 68 | + public void OnInspectorGUI() |
| 69 | + { |
| 70 | + EditorGUI.indentLevel++; |
| 71 | + |
| 72 | + if (matcher.IsCompound()) |
| 73 | + { |
| 74 | + var editor = AddMatcherLayout(); |
| 75 | + if (editor != null) |
| 76 | + { |
| 77 | + matcherEditors.Add(editor); |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + switch (matcher.MatcherType) |
| 83 | + { |
| 84 | + case FeatureStyle.Matcher.Type.Property: |
| 85 | + matcher.HasProperty = EditorGUILayout.TextField("Has property:", matcher.HasProperty); |
| 86 | + break; |
| 87 | + |
| 88 | + case FeatureStyle.Matcher.Type.PropertyRange: |
| 89 | + matcher.HasProperty = EditorGUILayout.TextField("Property:", matcher.HasProperty); |
| 90 | + EditorGUILayout.BeginHorizontal(); |
| 91 | + matcher.MinRange = EditorGUILayout.FloatField("min:", matcher.MinRange); |
| 92 | + matcher.MinRangeEnabled = EditorGUILayout.Toggle(matcher.MinRangeEnabled); |
| 93 | + EditorGUILayout.EndHorizontal(); |
| 94 | + EditorGUILayout.BeginHorizontal(); |
| 95 | + matcher.MaxRange = EditorGUILayout.FloatField("max:", matcher.MaxRange); |
| 96 | + matcher.MaxRangeEnabled = EditorGUILayout.Toggle(matcher.MaxRangeEnabled); |
| 97 | + EditorGUILayout.EndHorizontal(); |
| 98 | + break; |
| 99 | + |
| 100 | + case FeatureStyle.Matcher.Type.PropertyValue: |
| 101 | + matcher.HasProperty = EditorGUILayout.TextField("Property:", matcher.HasProperty); |
| 102 | + matcher.PropertyValue = EditorGUILayout.TextField("Property value:", matcher.PropertyValue); |
| 103 | + break; |
| 104 | + |
| 105 | + case FeatureStyle.Matcher.Type.PropertyRegex: |
| 106 | + matcher.HasProperty = EditorGUILayout.TextField("Property:", matcher.HasProperty); |
| 107 | + matcher.RegexPattern = EditorGUILayout.TextField("Regex:", matcher.RegexPattern); |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for (int i = matcherEditors.Count - 1; i >= 0; i--) |
| 113 | + { |
| 114 | + var editor = matcherEditors[i]; |
| 115 | + |
| 116 | + var state = FoldoutEditor.OnInspectorGUI(editor.GUID.ToString(), editor.Matcher.MatcherType.ToString()); |
| 117 | + |
| 118 | + if (state.show) |
| 119 | + { |
| 120 | + editor.OnInspectorGUI(); |
| 121 | + } |
| 122 | + |
| 123 | + if (state.markedForDeletion) |
| 124 | + { |
| 125 | + matcher.Matchers.Remove(editor.Matcher); |
| 126 | + matcherEditors.RemoveAt(i); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + EditorGUI.indentLevel--; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | + |
0 commit comments