Skip to content

Commit 6d38261

Browse files
authored
Merge pull request #2 from TarasK8/feature/file-preview
Added Json Editor
2 parents 6bf34fe + 4b152ad commit 6d38261

14 files changed

+690
-61
lines changed

Editor/JsonEditor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/JsonEditor/JArrayEditor.cs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
}

Editor/JsonEditor/JArrayEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/JsonEditor/JLayout.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using UnityEngine.Events;
6+
7+
namespace TarasK8.SaveSystemEditor.JEditor
8+
{
9+
public static class JLayout
10+
{
11+
public static readonly GUILayoutOption WITH_50 = GUILayout.Width(50f);
12+
public static readonly GUILayoutOption WITH_80 = GUILayout.Width(80f);
13+
public static readonly GUILayoutOption WITH_120 = GUILayout.Width(120f);
14+
public static readonly GUILayoutOption MAX_WITH_2000 = GUILayout.MaxWidth(2000f);
15+
16+
public static void DeleteButton(JToken token)
17+
{
18+
if (GUILayout.Button("Delete", WITH_50))
19+
{
20+
token.Remove();
21+
}
22+
}
23+
24+
public static void RenameField(JProperty property)
25+
{
26+
string newName = EditorGUILayout.TextField(property.Name, WITH_80);
27+
if (newName != property.Name)
28+
{
29+
var newProp = new JProperty(newName, property.Value);
30+
property.Replace(newProp);
31+
//Debug.Log($"Name Changed\nOld Token:\n{token}\nNew Token:\n{newProp}");
32+
}
33+
}
34+
35+
public static void Empty()
36+
{
37+
EditorGUILayout.BeginVertical("box");
38+
var style = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter };
39+
EditorGUILayout.LabelField("Empty", style, GUILayout.ExpandWidth(true));
40+
EditorGUILayout.EndVertical();
41+
}
42+
43+
public static void Unsupported(string typeName)
44+
{
45+
EditorGUILayout.LabelField($"{typeName} type is not supported.", WITH_80, MAX_WITH_2000);
46+
}
47+
48+
public static object PropertyByType(object obj, JTokenType type, string label = "", UnityAction ifObject = null, UnityAction ifArray = null)
49+
{
50+
switch (type)
51+
{
52+
case JTokenType.Integer:
53+
try { return EditorGUILayout.IntField(label, Convert.ToInt32(obj), WITH_80, MAX_WITH_2000); }
54+
catch { return 0; }
55+
case JTokenType.Float:
56+
try { return EditorGUILayout.FloatField(label, Convert.ToSingle(obj), WITH_80, MAX_WITH_2000); }
57+
catch { return 0f; }
58+
case JTokenType.String:
59+
try { return EditorGUILayout.TextField(label, Convert.ToString(obj), WITH_80, MAX_WITH_2000); }
60+
catch { return string.Empty; }
61+
case JTokenType.Boolean:
62+
try { return EditorGUILayout.Toggle(label, Convert.ToBoolean(obj), WITH_80, MAX_WITH_2000); }
63+
catch { return false; }
64+
case JTokenType.Object:
65+
ifObject?.Invoke();
66+
return null;
67+
case JTokenType.Array:
68+
ifArray?.Invoke();
69+
return null;
70+
default:
71+
Unsupported(type.ToString());
72+
return null;
73+
}
74+
}
75+
76+
public static object PropertyForAdd(object obj, SupportType type)
77+
{
78+
UnityAction ifObjectAction = () =>
79+
{
80+
obj = new JObject();
81+
EditorGUILayout.TextField("Structure object", WITH_80, MAX_WITH_2000);
82+
};
83+
UnityAction ifArrayAction = () =>
84+
{
85+
obj = new int[0];
86+
EditorGUILayout.TextField("Structure object", WITH_80, MAX_WITH_2000);
87+
};
88+
return PropertyByType(obj, EnumConvert(type), ifObject: ifObjectAction, ifArray: ifArrayAction) ?? obj;
89+
}
90+
91+
public static JTokenType EnumConvert(SupportType type)
92+
{
93+
switch (type)
94+
{
95+
case SupportType.Object: return JTokenType.Object;
96+
case SupportType.Integer: return JTokenType.Integer;
97+
case SupportType.Float: return JTokenType.Float;
98+
case SupportType.String: return JTokenType.String;
99+
case SupportType.Boolean: return JTokenType.Boolean;
100+
case SupportType.Array: return JTokenType.Array;
101+
default: return JTokenType.None;
102+
}
103+
}
104+
}
105+
106+
public enum SupportType
107+
{
108+
Object,
109+
Integer,
110+
Float,
111+
String,
112+
Boolean,
113+
Array,
114+
}
115+
}

Editor/JsonEditor/JLayout.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/JsonEditor/JPropertyEditor.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Newtonsoft.Json.Linq;
2+
using UnityEditor;
3+
4+
namespace TarasK8.SaveSystemEditor.JEditor
5+
{
6+
public class JPropertyEditor : JTokenEditor
7+
{
8+
protected JProperty _property;
9+
10+
public JPropertyEditor(JProperty property, Editor editor) : base(property.Value, editor)
11+
{
12+
_property = property;
13+
}
14+
15+
public override void Draw(string label, out JToken changedValue, bool addMode)
16+
{
17+
EditorGUILayout.BeginHorizontal();
18+
JLayout.RenameField(_property);
19+
DrawValueField(_property.Value, label, out changedValue);
20+
JLayout.DeleteButton(_property);
21+
EditorGUILayout.EndHorizontal();
22+
base.DrawObject(addMode);
23+
base.DrawArray(addMode);
24+
}
25+
}
26+
}

Editor/JsonEditor/JPropertyEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)