diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ContentEditorFrame.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ContentEditorFrame.cs index ba9ed0d0..e57d8a84 100644 --- a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ContentEditorFrame.cs +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ContentEditorFrame.cs @@ -94,10 +94,18 @@ public override void OnGUI() for (int i = 0; i < CurrentResourceTabs.Count; i++) { var tab = CurrentResourceTabs[i]; + + var originalColor = GUI.color; + GUI.color = i == CurrentResourceTabIndex + ? GUI.color + : GUI.color * 0.725f; + if (GUILayout.Button(tab.Title, EditorStyles.toolbarButton)) { CurrentResourceTabIndex = i; } + + GUI.color = originalColor; } EditorGUILayout.EndHorizontal(); diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/JsonTextWindowFrame.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/JsonTextWindowFrame.cs index e609f4f5..fe2bded6 100644 --- a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/JsonTextWindowFrame.cs +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/JsonTextWindowFrame.cs @@ -1,59 +1,39 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using RPGCore.Packages; -using System; using System.IO; using System.Text; -using UnityEditor; -using UnityEngine; namespace RPGCore.Unity.Editors { - public class JsonTextWindowFrame : WindowFrame + public class JsonTextWindowFrame : RawTextWindowFrame { private const string KeyColor = "#0451a5"; private const string StringValueColor = "#a31515"; private const string BoolValueColor = "#0000ff"; private const string NumericValueColor = "#098658"; - private readonly string[] lines; - private Vector2 offset; - - private GUIStyle textStyle; - - public JsonTextWindowFrame(IResource resource) + protected override string[] BuildLines(IResource resource) { - try - { - var serializer = new JsonSerializer(); - JObject jobject; - - using (var stream = resource.LoadStream()) - using (var reader = new StreamReader(stream)) - using (var jsonReader = new JsonTextReader(reader)) - { - jobject = serializer.Deserialize(jsonReader); - } + var serializer = new JsonSerializer(); + JObject jobject; - string allLines = jobject.ToString(Formatting.Indented); - - lines = allLines.Split(new char[] { '\n' }, System.StringSplitOptions.None); - } - catch (Exception exception) + using (var stream = resource.LoadStream()) + using (var reader = new StreamReader(stream)) + using (var jsonReader = new JsonTextReader(reader)) { - Debug.LogError(exception); + jobject = serializer.Deserialize(jsonReader); } - if (lines == null) - { - return; - } + string allLines = jobject.ToString(Formatting.Indented); + + string[] builtLines = allLines.Split(new char[] { '\n' }, System.StringSplitOptions.None); var sb = new StringBuilder(); - for (int i = 0; i < lines.Length; i++) + for (int i = 0; i < builtLines.Length; i++) { - string oldLine = lines[i]; + string oldLine = builtLines[i]; int quoteIndex = 0; bool isValue = false; @@ -111,56 +91,15 @@ public JsonTextWindowFrame(IResource resource) sb.Append(""); } - lines[i] = sb.ToString(); + builtLines[i] = sb.ToString(); } - } - - public override void OnEnable() - { + return builtLines; } - public override void OnGUI() + public JsonTextWindowFrame(IResource resource) + : base(resource) { - if (textStyle == null) - { - var courierNewFont = Font.CreateDynamicFontFromOSFont("Courier New", 24); - textStyle = new GUIStyle(EditorStyles.label) - { - richText = true, - font = courierNewFont, - padding = new RectOffset() - { - top = 4, - bottom = 4, - left = 8 - } - }; - } - - var rect = GUILayoutUtility.GetRect(0, 480, GUILayout.ExpandWidth(true)); - EditorGUI.DrawRect(rect, new Color(0.95f, 0.95f, 0.95f)); - foreach (var element in new UniformScrollController(rect, EditorGUIUtility.singleLineHeight, ref offset, lines.Length)) - { - var lineNumberRect = new Rect( - element.Position.x, - element.Position.y, - 25, - element.Position.height - ); - var lineRect = new Rect( - element.Position.x + 25, - element.Position.y, - element.Position.width - 25, - element.Position.height - ); - - if (element.Index < lines.Length) - { - EditorGUI.LabelField(lineNumberRect, (element.Index + 1).ToString(), textStyle); - EditorGUI.LabelField(lineRect, lines[element.Index], textStyle); - } - } } } } diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/RawTextWindowFrame.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/RawTextWindowFrame.cs index 7de74f96..e95e9cc3 100644 --- a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/RawTextWindowFrame.cs +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/RawTextWindowFrame.cs @@ -1,4 +1,5 @@ using RPGCore.Packages; +using System; using System.Collections.Generic; using System.IO; using UnityEditor; @@ -11,31 +12,38 @@ public class RawTextWindowFrame : WindowFrame private readonly string[] lines; private Vector2 offset; + private GUIStyle lineNumberStyle; private GUIStyle textStyle; public RawTextWindowFrame(IResource resource) { try { - var linesList = new List(); - using (var stream = resource.LoadStream()) - using (var reader = new StreamReader(stream)) - { - while (!reader.EndOfStream) - { - linesList.Add(reader.ReadLine()); - } - } - lines = linesList.ToArray(); + lines = BuildLines(resource); } - catch + catch (Exception exception) + { + Debug.LogError(exception); + } + } + + protected virtual string[] BuildLines(IResource resource) + { + var linesList = new List(); + using (var stream = resource.LoadStream()) + using (var reader = new StreamReader(stream)) { + while (!reader.EndOfStream) + { + linesList.Add(reader.ReadLine()); + } } + return linesList.ToArray(); } public override void OnEnable() { - + } public override void OnGUI() @@ -52,30 +60,62 @@ public override void OnGUI() top = 4, bottom = 4, left = 8 - } + }, + alignment = TextAnchor.UpperLeft + }; + lineNumberStyle = new GUIStyle(textStyle) + { + padding = new RectOffset() + { + top = 4, + bottom = 4, + left = 8, + right = 6 + }, + alignment = TextAnchor.UpperRight }; } + if (lines == null) + { + return; + } + + string guideString = new string('0', lines.Length.ToString().Length); + + lineNumberStyle.CalcMinMaxWidth( + new GUIContent(guideString), + out _, out float maxWidth); + var rect = GUILayoutUtility.GetRect(0, 480, GUILayout.ExpandWidth(true)); + var lineNumberBackgroundRect = new Rect( + rect.x, + rect.y, + maxWidth, + rect.height + ); + EditorGUI.DrawRect(rect, new Color(0.95f, 0.95f, 0.95f)); + EditorGUI.DrawRect(lineNumberBackgroundRect, new Color(0.90f, 0.90f, 0.90f)); + foreach (var element in new UniformScrollController(rect, EditorGUIUtility.singleLineHeight, ref offset, lines.Length)) { var lineNumberRect = new Rect( element.Position.x, element.Position.y, - 25, + maxWidth, element.Position.height ); var lineRect = new Rect( - element.Position.x + 25, + element.Position.x + maxWidth, element.Position.y, - element.Position.width - 25, + element.Position.width - maxWidth, element.Position.height ); if (element.Index < lines.Length) { - EditorGUI.LabelField(lineNumberRect, (element.Index + 1).ToString(), textStyle); + EditorGUI.LabelField(lineNumberRect, (element.Index + 1).ToString(), lineNumberStyle); EditorGUI.LabelField(lineRect, lines[element.Index], textStyle); } } diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs new file mode 100644 index 00000000..d285e4cb --- /dev/null +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs @@ -0,0 +1,29 @@ +using RPGCore.Packages; +using UnityEngine; + +namespace RPGCore.Unity.Editors +{ + public class TextureWindowFrame : WindowFrame + { + private readonly Texture2D texture; + + public TextureWindowFrame(IResource resource) + { + texture = new Texture2D(2, 2); + texture.LoadImage(resource.LoadData()); + } + + public override void OnEnable() + { + + } + + public override void OnGUI() + { + int aspect = texture.width / texture.height; + var rect = GUILayoutUtility.GetRect(480, 480 / aspect, GUILayout.ExpandWidth(false)); + + GUI.DrawTexture(rect, texture); + } + } +} diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs.meta b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs.meta new file mode 100644 index 00000000..3eb82989 --- /dev/null +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/Frames/TextureWindowFrame.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c2f18edbbc000864fbeba50080455a71 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ResourceInformationFrame.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ResourceInformationFrame.cs index dcdf21af..e7fc5713 100644 --- a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ResourceInformationFrame.cs +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/ResourceInformationFrame.cs @@ -27,6 +27,12 @@ public ResourceInformationFrame(IResource resource) Title = new GUIContent("Json"), Frame = new JsonTextWindowFrame(resource) }); + + VisualiserTabs.Add(new FrameTab() + { + Title = new GUIContent("Image"), + Frame = new TextureWindowFrame(resource) + }); } public override void OnEnable() @@ -55,13 +61,22 @@ public override void OnGUI() for (int i = 0; i < VisualiserTabs.Count; i++) { var tab = VisualiserTabs[i]; + + var originalColor = GUI.color; + GUI.color = i == VisualiserTabsIndex + ? GUI.color + : GUI.color * 0.725f; + if (GUILayout.Button(tab.Title, EditorStyles.toolbarButton)) { VisualiserTabsIndex = i; } + + GUI.color = originalColor; } EditorGUILayout.EndHorizontal(); + if (VisualiserTabsIndex >= 0 && VisualiserTabsIndex < VisualiserTabs.Count) { var currentTab = VisualiserTabs[VisualiserTabsIndex]; diff --git a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/UniformScrollController.cs b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/UniformScrollController.cs index f13058c9..73616434 100644 --- a/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/UniformScrollController.cs +++ b/src/RPGCoreUnity/Assets/RPGCore/Scripts/Editors/Packages/UniformScrollController.cs @@ -19,7 +19,7 @@ public struct UniformScrollController : IEnumerable, IEnumerator< public UniformScrollController(Rect area, float elementHeight, ref Vector2 offset, int elements) { float maxOverflow = area.height % elementHeight; - int maxScrollMinIndex = Mathf.CeilToInt((area.height + maxOverflow) / elementHeight) - 1; + int maxScrollMinIndex = Mathf.CeilToInt((area.height + maxOverflow) / elementHeight) - 2; float scrollHeight = Mathf.Max(0.0f, ((elements - maxScrollMinIndex) * elementHeight) + (1 - maxOverflow));