Skip to content

Commit

Permalink
🚧 Improved resource content rendering #20
Browse files Browse the repository at this point in the history
Abstracted text document rendering.
Automatically increase the line number column width.
Improved tab controls in the Content Editor.
Fixed UniformScrollController not rendering the last line of the document.
Added a content renderer that attempts to render images (png and jpeg).
  • Loading branch information
Fydar committed Mar 2, 2020
1 parent 0dd50db commit 2abcc7c
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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<JObject>(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<JObject>(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;
Expand Down Expand Up @@ -111,56 +91,15 @@ public JsonTextWindowFrame(IResource resource)
sb.Append("</color>");
}

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);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RPGCore.Packages;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
Expand All @@ -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<string>();
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<string>();
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()
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct UniformScrollController : IEnumerable<ElementDrawer>, 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));

Expand Down

0 comments on commit 2abcc7c

Please sign in to comment.