1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Net . Mime ;
5+ using UnityEditor ;
6+ using UnityEngine ;
7+ using UtilitySystem . Runtime ;
8+
9+ namespace UtilitySystem . Editor
10+ {
11+ [ CustomEditor ( typeof ( UtilitySystemData ) ) ]
12+ public class UtilitySystemDataEditor : UnityEditor . Editor
13+ {
14+ private static UtilitySystemData _utilitySystemData ;
15+ private static SerializedProperty InputsProperty ;
16+ private static SerializedProperty OutputsProperty ;
17+
18+ private void OnEnable ( )
19+ {
20+ _utilitySystemData = target as UtilitySystemData ;
21+ InputsProperty = serializedObject . FindProperty ( "inputs" ) ;
22+ OutputsProperty = serializedObject . FindProperty ( "utilities" ) ;
23+ }
24+
25+ public override void OnInspectorGUI ( )
26+ {
27+ // base.OnInspectorGUI();
28+
29+ serializedObject . Update ( ) ;
30+
31+ EditorGUILayout . PropertyField ( InputsProperty ) ;
32+
33+ ShowOutputs ( ) ;
34+
35+ serializedObject . ApplyModifiedProperties ( ) ;
36+ }
37+
38+ private void ShowOutputs ( )
39+ {
40+ EditorGUILayout . BeginHorizontal ( ) ;
41+ OutputsProperty . isExpanded =
42+ EditorGUILayout . Foldout ( OutputsProperty . isExpanded , OutputsProperty . displayName ) ;
43+
44+ // TODO: Use default constructor
45+ if ( FitButton ( "Add Utility" ) )
46+ {
47+ OutputsProperty . InsertArrayElementAtIndex ( OutputsProperty . arraySize ) ;
48+ OutputsProperty . GetArrayElementAtIndex ( OutputsProperty . arraySize - 1 ) . isExpanded = true ;
49+ }
50+
51+ EditorGUILayout . EndHorizontal ( ) ;
52+ if ( OutputsProperty . isExpanded )
53+ {
54+ EditorGUI . indentLevel += 1 ;
55+ for ( int i = 0 ; i < OutputsProperty . arraySize ; i ++ )
56+ {
57+ SerializedProperty outputProperty = OutputsProperty . GetArrayElementAtIndex ( i ) ;
58+ SerializedProperty statProperties = outputProperty . FindPropertyRelative ( "statImportances" ) ;
59+
60+ EditorGUILayout . BeginHorizontal ( ) ;
61+
62+ outputProperty . isExpanded =
63+ EditorGUILayout . Foldout ( outputProperty . isExpanded , outputProperty . displayName ) ;
64+ if ( FitButton ( "Remove Utility" ) )
65+ {
66+ OutputsProperty . DeleteArrayElementAtIndex ( i ) ;
67+ break ;
68+ }
69+
70+ // TODO: Use default constructor
71+ if ( statProperties . arraySize < InputsProperty . arraySize && FitButton ( "Add Stat Importance" ) )
72+ {
73+ outputProperty . isExpanded = true ;
74+ statProperties . InsertArrayElementAtIndex ( statProperties . arraySize ) ;
75+ statProperties . isExpanded = true ;
76+ statProperties . GetArrayElementAtIndex ( statProperties . arraySize - 1 ) . isExpanded = true ;
77+ }
78+
79+ EditorGUILayout . EndHorizontal ( ) ;
80+
81+ if ( outputProperty . isExpanded )
82+ {
83+ EditorGUI . indentLevel += 1 ;
84+
85+ EditorGUILayout . PropertyField ( outputProperty . FindPropertyRelative ( "Name" ) ) ;
86+
87+ EditorGUILayout . BeginHorizontal ( ) ;
88+
89+ statProperties . isExpanded =
90+ EditorGUILayout . Foldout ( statProperties . isExpanded , statProperties . displayName ) ;
91+
92+ EditorGUILayout . EndHorizontal ( ) ;
93+
94+ if ( statProperties . isExpanded )
95+ {
96+ EditorGUI . indentLevel += 1 ;
97+
98+ for ( int j = 0 ; j < statProperties . arraySize ; j ++ )
99+ {
100+ SerializedProperty statProperty = statProperties . GetArrayElementAtIndex ( j ) ;
101+
102+ EditorGUILayout . BeginHorizontal ( ) ;
103+
104+ statProperty . isExpanded =
105+ EditorGUILayout . Foldout ( statProperty . isExpanded , statProperty . displayName ) ;
106+
107+ if ( FitButton ( "Remove Stat Importance" ) )
108+ {
109+ statProperties . DeleteArrayElementAtIndex ( j ) ;
110+ break ;
111+ }
112+
113+ EditorGUILayout . EndHorizontal ( ) ;
114+
115+ if ( statProperty . isExpanded )
116+ {
117+ SerializedProperty statName = statProperty . FindPropertyRelative ( "name" ) ;
118+
119+ List < string > selectable = _utilitySystemData . inputs . Where ( s =>
120+ {
121+ for ( int k = 0 ; k < statProperties . arraySize ; k ++ )
122+ {
123+ if ( statProperties . GetArrayElementAtIndex ( k )
124+ . FindPropertyRelative ( "name" ) . stringValue == s && k != j )
125+ {
126+ return false ;
127+ }
128+ }
129+
130+ return true ;
131+ } ) . ToList ( ) ;
132+
133+ int current = selectable . FindIndex ( value =>
134+ statName . stringValue == value ) ;
135+
136+ int selected = EditorGUILayout . Popup ( current >= 0 ? current : 0 , selectable . ToArray ( ) ) ;
137+
138+ if ( current != selected )
139+ {
140+ statProperty . FindPropertyRelative ( "name" ) . stringValue =
141+ selected >= 0 ? selectable [ selected ] : "" ;
142+ }
143+
144+ EditorGUILayout . PropertyField ( statProperty . FindPropertyRelative ( "curve" ) ,
145+ new GUIContent ( "Importance Curve" ) ) ;
146+
147+ SerializedProperty statWeight = statProperty . FindPropertyRelative ( "weight" ) ;
148+
149+ statWeight . intValue = EditorGUILayout . IntSlider ( statWeight . displayName , statWeight . intValue , 0 , 10 ) ;
150+ }
151+ }
152+
153+ EditorGUI . indentLevel -= 1 ;
154+ }
155+
156+ EditorGUI . indentLevel -= 1 ;
157+ }
158+ }
159+
160+ EditorGUI . indentLevel -= 1 ;
161+ }
162+ }
163+
164+ private static Vector2 ComputeTextWidth ( String text )
165+ {
166+ return GUI . skin . button . CalcSize ( new GUIContent ( text ) ) ;
167+ }
168+
169+ private static bool FitButton ( string text )
170+ {
171+ return GUILayout . Button ( text , EditorStyles . miniButton , GUILayout . Width ( ComputeTextWidth ( text ) . x ) ) ;
172+ }
173+ }
174+ }
0 commit comments