Skip to content

Commit

Permalink
Improved USS, templates, creating new scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
App24 committed May 28, 2023
1 parent c39b4e5 commit b38c65f
Show file tree
Hide file tree
Showing 23 changed files with 245 additions and 176 deletions.
4 changes: 4 additions & 0 deletions Assets/Editor Default Resources/RicUtils/Common.uss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.hidden{
height: 0px;
visibility: hidden;
}
11 changes: 11 additions & 0 deletions Assets/Editor Default Resources/RicUtils/Common.uss.meta

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

5 changes: 0 additions & 5 deletions Assets/Editor Default Resources/RicUtils/TabBar.uss
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
.hidden{
height: 0px;
visibility: hidden;
}

.tabBar{
flex-direction: row;
background-color: #1e1e1e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using UnityEditor;
using UnityEngine;

namespace RicUtils.Editor.CustomEditor
namespace RicUtils.Editor.CustomEditors
{
[UnityEditor.CustomEditor(typeof(AvailableScriptableObject<>), true)]
[CanEditMultipleObjects]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UnityEditor;
using UnityEngine;

namespace RicUtils.Editor.CustomEditor
namespace RicUtils.Editor.CustomEditors
{
[UnityEditor.CustomEditor(typeof(GenericScriptableObject), true)]
[CanEditMultipleObjects]
Expand Down
2 changes: 1 addition & 1 deletion Assets/RicUtils/Editor/Elements.meta

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 @@ -7,6 +7,6 @@ using RicUtils.ScriptableObjects;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : AvailableScriptableObject<#SCRIPTABLEOBJECT#>
{

#NOTRIM#
}
#ROOTNAMESPACEEND#
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public class #SCRIPTNAME# : GenericEditorWindow<#SCRIPTABLEOBJECT#, #AVAILABLESC

protected override void DrawGUI()
{

#NOTRIM#
}

protected override void LoadScriptableObject(#SCRIPTABLEOBJECT# so, bool isNull)
{

#NOTRIM#
}

protected override void CreateAsset(ref #SCRIPTABLEOBJECT# asset)
{

#NOTRIM#
}
}
#ROOTNAMESPACEEND#
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ using RicUtils.ScriptableObjects;
#ROOTNAMESPACEBEGIN#
public class #SCRIPTNAME# : GenericScriptableObject
{

#NOTRIM#
}
#ROOTNAMESPACEEND#
8 changes: 8 additions & 0 deletions Assets/RicUtils/Editor/UIElements.meta

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 @@ -4,7 +4,7 @@
using UnityEngine.Scripting;
using UnityEngine.UIElements;

namespace RicUtils.Editor
namespace RicUtils.Editor.UIElements
{
// web* src: https://gist.github.com/andrew-raphael-lukasik/69c7858e39e22f197ca51b318b218cc7
[Preserve]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine.UIElements;

namespace RicUtils.Editor.Elements
namespace RicUtils.Editor.UIElements
{
public class TabBar
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine.UIElements;

namespace RicUtils.Editor.Elements
namespace RicUtils.Editor.UIElements
{
public struct TabData
{
Expand Down
108 changes: 1 addition & 107 deletions Assets/RicUtils/Editor/Utilities/DoCreateScriptAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,119 +13,13 @@ public class DoCreateScriptAsset : EndNameEditAction
{
public override void Action(int instanceId, string pathName, string resourceFile)
{
Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
Object o = FileUtilities.CreateScriptAssetFromTemplate(pathName, resourceFile, CustomReplaces);
ProjectWindowUtil.ShowCreatedAsset(o);
}

private Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
{
string content = File.ReadAllText(resourceFile);
var method = typeof(ProjectWindowUtil).GetMethod("CreateScriptAssetWithContent", BindingFlags.Static | BindingFlags.NonPublic);
return (Object)method.Invoke(null, new object[] { pathName, PreprocessScriptAssetTemplate(pathName, content) });
}

// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs
private string PreprocessScriptAssetTemplate(string pathName, string resourceContent)
{
string rootNamespace = null;

if (Path.GetExtension(pathName) == ".cs")
{
rootNamespace = CompilationPipeline.GetAssemblyRootNamespaceFromScriptPath(pathName);
}

string content = resourceContent;

// #NOTRIM# is a special marker that is used to mark the end of a line where we want to leave whitespace. prevent editors auto-stripping it by accident.
content = content.Replace("#NOTRIM#", "");

// macro replacement
string baseFile = Path.GetFileNameWithoutExtension(pathName);

content = content.Replace("#NAME#", baseFile);
string baseFileNoSpaces = baseFile.Replace(" ", "");
content = content.Replace("#SCRIPTNAME#", baseFileNoSpaces);

content = CustomReplaces(content);

content = RemoveOrInsertNamespace(content, rootNamespace);

// if the script name begins with an uppercase character we support a lowercase substitution variant
if (char.IsUpper(baseFileNoSpaces, 0))
{
baseFileNoSpaces = char.ToLower(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1);
content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces);
}
else
{
// still allow the variant, but change the first character to upper and prefix with "my"
baseFileNoSpaces = "my" + char.ToUpper(baseFileNoSpaces[0]) + baseFileNoSpaces.Substring(1);
content = content.Replace("#SCRIPTNAME_LOWER#", baseFileNoSpaces);
}

return content;
}

protected virtual string CustomReplaces(string content)
{
return content;
}

private string RemoveOrInsertNamespace(string content, string rootNamespace)
{
var rootNamespaceBeginTag = "#ROOTNAMESPACEBEGIN#";
var rootNamespaceEndTag = "#ROOTNAMESPACEEND#";

if (!content.Contains(rootNamespaceBeginTag) || !content.Contains(rootNamespaceEndTag))
return content;

if (string.IsNullOrEmpty(rootNamespace))
{
content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceBeginTag}[ \\t]*", string.Empty);
content = Regex.Replace(content, $"((\\r\\n)|\\n)[ \\t]*{rootNamespaceEndTag}[ \\t]*", string.Empty);

return content;
}

// Use first found newline character as newline for entire file after replace.
var newline = content.Contains("\r\n") ? "\r\n" : "\n";
var contentLines = new List<string>(content.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None));

int i = 0;

for (; i < contentLines.Count; ++i)
{
if (contentLines[i].Contains(rootNamespaceBeginTag))
break;
}

var beginTagLine = contentLines[i];

// Use the whitespace between beginning of line and #ROOTNAMESPACEBEGIN# as identation.
var indentationString = beginTagLine.Substring(0, beginTagLine.IndexOf("#"));

contentLines[i] = $"namespace {rootNamespace}";
contentLines.Insert(i + 1, "{");

i += 2;

for (; i < contentLines.Count; ++i)
{
var line = contentLines[i];

if (string.IsNullOrEmpty(line) || line.Trim().Length == 0)
continue;

if (line.Contains(rootNamespaceEndTag))
{
contentLines[i] = "}";
break;
}

contentLines[i] = $"{indentationString}{line}";
}

return string.Join(newline, contentLines.ToArray());
}
}
}
7 changes: 6 additions & 1 deletion Assets/RicUtils/Editor/Utilities/EditorGUIHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using RicUtils.Editor.Elements;
using RicUtils.Editor.UIElements;
using System.Collections;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -407,5 +407,10 @@ public static TabBar AddTabBar(this VisualElement root)

return tabBar;
}

public static void AddCommonStylesheet(this VisualElement root)
{
root.AddStylesheet("RicUtils/Common.uss");
}
}
}
Loading

0 comments on commit b38c65f

Please sign in to comment.