1+ using Newtonsoft . Json . Linq ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using UnityEditor ;
5+ using UnityEngine ;
6+
7+ namespace TarasK8 . SaveSystemEditor . JEditor
8+ {
9+ public class JArrayEditor
10+ {
11+ private JArray _array ;
12+ private Dictionary < JToken , JTokenEditor > _childTokens ;
13+ private Editor _editor ;
14+ private AddData _addData ;
15+ private int _displayElements = 15 ;
16+ private int _page = 0 ;
17+
18+ public JArrayEditor ( JArray array , Editor editor )
19+ {
20+ _array = array ;
21+ _addData = new AddData ( ) ;
22+ _childTokens = new Dictionary < JToken , JTokenEditor > ( ) ;
23+ _editor = editor ;
24+ }
25+
26+ public void Draw ( bool addMode )
27+ {
28+ if ( _array . Count == 0 )
29+ {
30+ JLayout . Empty ( ) ;
31+ if ( addMode )
32+ DrawAddButton ( ) ;
33+ }
34+ else
35+ {
36+ var arrList = _array . ToList ( ) ;
37+ int displayClamped = Mathf . Max ( _displayElements , 1 ) ;
38+ int maxPage = Mathf . Max ( arrList . Count / displayClamped , 0 ) ;
39+ if ( arrList . Count % displayClamped == 0 ) maxPage -- ;
40+ EditorGUILayout . BeginHorizontal ( "box" ) ;
41+ EditorGUILayout . LabelField ( $ "{ arrList [ 0 ] . Type } 's List", JLayout . WITH_80 , JLayout . MAX_WITH_2000 ) ;
42+ if ( arrList . Count > 15 )
43+ {
44+ EditorGUILayout . LabelField ( $ "{ _page + 1 } /{ maxPage + 1 } ", JLayout . WITH_50 ) ;
45+ if ( GUILayout . Button ( "F" , GUILayout . Width ( 25f ) ) ) _page = 0 ;
46+ if ( GUILayout . Button ( "L" , GUILayout . Width ( 25f ) ) ) _page = maxPage ;
47+ if ( GUILayout . Button ( "Prev" , JLayout . WITH_50 ) ) _page -- ;
48+ if ( GUILayout . Button ( "Next" , JLayout . WITH_50 ) ) _page ++ ;
49+ _displayElements = EditorGUILayout . IntField ( Mathf . Clamp ( _displayElements , 1 , 100 ) , GUILayout . Width ( 30f ) ) ;
50+ }
51+ EditorGUILayout . EndHorizontal ( ) ;
52+
53+ _page = Mathf . Clamp ( _page , 0 , maxPage ) ;
54+ int offset = _displayElements * _page ;
55+ int endIndex = offset + Mathf . Min ( arrList . Count - offset , _displayElements ) ;
56+ for ( int i = offset ; i < endIndex ; i ++ )
57+ {
58+ GetTokenEditor ( arrList [ i ] ) . Draw ( $ "Element { i } ", out var changedValue , addMode ) ;
59+ if ( changedValue != null )
60+ {
61+ arrList [ i ] . Replace ( changedValue ) ;
62+ }
63+ }
64+ if ( addMode )
65+ DrawAddElementButton ( ) ;
66+ }
67+ }
68+
69+ public void ShowAll ( )
70+ {
71+ foreach ( var item in _childTokens . Values )
72+ {
73+ item . Show ( ) ;
74+ }
75+ }
76+
77+ public void HideAll ( )
78+ {
79+ foreach ( var item in _childTokens . Values )
80+ {
81+ item . Hide ( ) ;
82+ }
83+ }
84+
85+ private void DrawAddElementButton ( )
86+ {
87+ EditorGUILayout . BeginHorizontal ( "box" ) ;
88+
89+ JToken lastElem = _array . Last ;
90+ if ( lastElem . Type == JTokenType . Array || lastElem . Type == JTokenType . Object )
91+ {
92+ if ( GUILayout . Button ( "Add (Clone Last)" ) )
93+ {
94+ _array . Add ( lastElem . DeepClone ( ) ) ;
95+ }
96+ }
97+ else
98+ {
99+ _addData . Value = JLayout . PropertyByType ( _addData . Value , lastElem . Type ) ;
100+ if ( GUILayout . Button ( "Add" ) )
101+ {
102+ _array . Add ( JToken . FromObject ( _addData . Value ) ) ;
103+ }
104+ }
105+
106+ EditorGUILayout . EndHorizontal ( ) ;
107+ }
108+
109+ private void DrawAddButton ( )
110+ {
111+ EditorGUILayout . BeginHorizontal ( "button" ) ;
112+
113+ /*
114+ switch (_addData.Type)
115+ {
116+ case SupportType.Integer:
117+ try { _addData.Value = EditorGUILayout.IntField(Convert.ToInt32(_addData.Value)); }
118+ catch { _addData.Value = 0; }
119+ break;
120+ case SupportType.Float:
121+ try { _addData.Value = EditorGUILayout.FloatField(Convert.ToSingle(_addData.Value)); }
122+ catch { _addData.Value = 0f; }
123+ break;
124+ case SupportType.String:
125+ try { _addData.Value = EditorGUILayout.TextField(Convert.ToString(_addData.Value)); }
126+ catch { _addData.Value = string.Empty; }
127+ break;
128+ case SupportType.Boolean:
129+ try { _addData.Value = EditorGUILayout.Toggle(Convert.ToBoolean(_addData.Value)); }
130+ catch { _addData.Value = false; }
131+ break;
132+ case SupportType.Object:
133+ EditorGUILayout.TextField("Structure object");
134+ //data.Value = EditorGUILayout.ObjectField(((Object)data.Value));
135+ break;
136+ case SupportType.Array:
137+ _addData.Value = new int[0];
138+ EditorGUILayout.TextField("Structure object");
139+ break;
140+ default:
141+ JLayout.Unsupported(_addData.Type.ToString());
142+ break;
143+ }
144+ */
145+
146+ /*
147+ UnityAction ifObjectAction = () =>
148+ {
149+ _addData.Value = new JObject();
150+ EditorGUILayout.TextField("Structure object");
151+ };
152+ UnityAction ifArrayAction = () =>
153+ {
154+ _addData.Value = new int[0];
155+ EditorGUILayout.TextField("Structure object");
156+ };
157+ _addData.Value = JLayout.PropertyByType(_addData.Value, JLayout.EnumConvert(_addData.Type), ifObject: ifObjectAction, ifArray: ifArrayAction) ?? _addData.Value;
158+ */
159+
160+ _addData . Value = JLayout . PropertyForAdd ( _addData . Value , _addData . Type ) ;
161+
162+
163+ _addData . Type = ( SupportType ) EditorGUILayout . EnumPopup ( _addData . Type , JLayout . WITH_80 ) ;
164+ if ( GUILayout . Button ( "Add" , GUILayout . MinWidth ( 50f ) ) )
165+ {
166+ var newObj = JToken . FromObject ( _addData . Value ) ;
167+ _array . Add ( newObj ) ;
168+ }
169+
170+ EditorGUILayout . EndHorizontal ( ) ;
171+ }
172+
173+ private JTokenEditor GetTokenEditor ( JToken token )
174+ {
175+ if ( _childTokens . TryGetValue ( token , out var propertyEditor ) )
176+ {
177+ return propertyEditor ;
178+ }
179+ else
180+ {
181+ var newProperty = new JTokenEditor ( token , _editor ) ;
182+ _childTokens . Add ( token , newProperty ) ;
183+ return newProperty ;
184+ }
185+ }
186+
187+ public class AddData
188+ {
189+ public object Value = 0 ;
190+ public SupportType Type = SupportType . Integer ;
191+ }
192+
193+ }
194+ }
0 commit comments