Skip to content

Commit

Permalink
feat: add InheritClass and AccessModifier field in `CodeGenerateS…
Browse files Browse the repository at this point in the history
…ettingEditorWindow`.
  • Loading branch information
Shaun-Fong committed Mar 2, 2023
1 parent 86b5d0b commit 73799b3
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 96 deletions.
8 changes: 8 additions & 0 deletions Assets/Examples.meta

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

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ GameObject:
m_Component:
- component: {fileID: 1913365000}
- component: {fileID: 1913364999}
- component: {fileID: 1913365001}
m_Layer: 0
m_Name: GameObject
m_TagString: Untagged
Expand Down Expand Up @@ -349,3 +350,15 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1913365001
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1913364998}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e6e84114291c0384485210997429a585, type: 3}
m_Name:
m_EditorClassIdentifier:
File renamed without changes.
8 changes: 8 additions & 0 deletions Assets/Examples/Scripts.meta

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

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public partial class BindTest : MonoBehaviour
public UnityEngine.UIElements.Label Title { get; private set; }
public UnityEngine.UIElements.Button Button1 { get; private set; }
public UnityEngine.UIElements.TextField TextField { get; private set; }

public void Bind()
{
VisualElement root = gameObject.GetComponent<UIDocument>().rootVisualElement;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,129 +3,163 @@
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using com.shaunfong.UIToolkitFieldBinding.editor;
using UnityEditor.UIElements;
using UnityEditor.PackageManager.UI;
using System;
using System.IO;
using System.Net.Http;

public class CodeGenerateSettingEditorWindow : EditorWindow
namespace com.shaunfong.UIToolkitFieldBinding.editor
{
public enum AccessModifiers
{
PUBLIC = 1 << 0,
PRIVATE = 1 << 1,
PROTECTED = 1 << 2,
INTERNAL = 1 << 3,
PROTECTED_INTERNAL = 1 << 4,
PRIVATE_PROTECTED = 1 << 5
}

public string m_NameSpace;
public class CodeGenerateSettingEditorWindow : EditorWindow
{

public string m_ScriptPath;
public string m_NameSpace;
public string m_InheritClass;
public AccessModifiers m_AccessModifier;

public string m_ScriptContent;
public string m_ScriptPath;

private FieldBindingData m_Data;
public string m_ScriptContent;

public static CodeGenerateSettingEditorWindow ShowWindow(FieldBindingData data)
{
CodeGenerateSettingEditorWindow window = CreateInstance<CodeGenerateSettingEditorWindow>();
window.titleContent = new GUIContent("Code Generation");
window.Init(data);
window.position = new Rect(
(Screen.currentResolution.width - 350) / 2,
(Screen.currentResolution.height - 550) / 2,
350,
550
);
window.ShowModalUtility();
return window;
}
private FieldBindingData m_Data;

private void CreateGUI()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
public static CodeGenerateSettingEditorWindow ShowWindow(FieldBindingData data)
{
CodeGenerateSettingEditorWindow window = CreateInstance<CodeGenerateSettingEditorWindow>();
window.titleContent = new GUIContent("Code Generation");
window.Init(data);
window.position = new Rect(
(Screen.currentResolution.width - 350) / 2,
(Screen.currentResolution.height - 550) / 2,
350,
550
);
window.ShowModalUtility();
return window;
}

// Import UXML
string vtaPath = PathUtility.GetUXMLRes("CodeGenerationSettingDocument");
var m_InspectorVisualTreeAsset = (AssetDatabase.LoadAssetAtPath(vtaPath, typeof(VisualTreeAsset)) as VisualTreeAsset);
root.Add(m_InspectorVisualTreeAsset.Instantiate());
private void CreateGUI()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;

// Read NameSpace In Prefs.
m_NameSpace = SettingUtility.GetNameSpaceValue();
// Import UXML
string vtaPath = PathUtility.GetUXMLRes("CodeGenerationSettingDocument");
var m_InspectorVisualTreeAsset = (AssetDatabase.LoadAssetAtPath(vtaPath, typeof(VisualTreeAsset)) as VisualTreeAsset);
root.Add(m_InspectorVisualTreeAsset.Instantiate());

// Register Callbacks
TextField namespaceField = root.Q<TextField>("namespace");
namespaceField.RegisterValueChangedCallback<string>(OnNamespaceValueChange);
// Read NameSpace In Prefs.
m_NameSpace = SettingUtility.GetNameSpaceValue();

Button changeLocation = root.Q<Button>("chaneglocation");
changeLocation.clicked += OnChangeScriptLocation;
m_InheritClass = SettingUtility.GetInheritClassValue();

Button generate = root.Q<Button>("generate");
generate.clicked += GenerateScript;
m_AccessModifier = SettingUtility.GetAccessModifierValue();

root.Bind(new SerializedObject(this));
// Register Callbacks
TextField namespaceField = root.Q<TextField>("namespace");
namespaceField.RegisterValueChangedCallback<string>(OnNamespaceValueChange);
TextField inheritclassField = root.Q<TextField>("inheritclass");
inheritclassField.RegisterValueChangedCallback<string>(OnInheritClassValueChange);
EnumField accessmodifierField = root.Q<EnumField>("accessmodifier");
accessmodifierField.RegisterValueChangedCallback<Enum>(OnAccessModifierValueChange);

// Refresh Preview.
RefreshScriptContent();
}
Button changeLocation = root.Q<Button>("chaneglocation");
changeLocation.clicked += OnChangeScriptLocation;

private void OnNamespaceValueChange(ChangeEvent<string> evt)
{
SettingUtility.SetNameSpaceValue(evt.newValue);
RefreshScriptContent();
}
Button generate = root.Q<Button>("generate");
generate.clicked += GenerateScript;

private void OnChangeScriptLocation()
{
var path = OpenSaveFilePanel();
root.Bind(new SerializedObject(this));

// Refresh Preview.
RefreshScriptContent();
}

if (string.IsNullOrEmpty(path))
private void OnNamespaceValueChange(ChangeEvent<string> evt)
{
return;
SettingUtility.SetNameSpaceValue(evt.newValue);
RefreshScriptContent();
}

m_ScriptPath = path.GetRelativePath();
m_Data.ScriptPath = m_ScriptPath;
private void OnInheritClassValueChange(ChangeEvent<string> evt)
{
SettingUtility.SetInheritClassValue(evt.newValue);
RefreshScriptContent();
}

RefreshScriptContent();
private void OnAccessModifierValueChange(ChangeEvent<Enum> evt)
{
SettingUtility.SetAccessModifierValue((AccessModifiers)evt.newValue);
RefreshScriptContent();
}

AssetDatabase.SaveAssets();
}
private void OnChangeScriptLocation()
{
var path = OpenSaveFilePanel();

private string OpenSaveFilePanel()
{
var path = EditorUtility.SaveFilePanel("Script Path", Application.dataPath, "Script", "cs");
if (string.IsNullOrEmpty(path))
{
return;
}

if (string.IsNullOrEmpty(path))
{
return null;
m_ScriptPath = path.GetRelativePath();
m_Data.ScriptPath = m_ScriptPath;

RefreshScriptContent();

AssetDatabase.SaveAssets();
}

return path;
}
private string OpenSaveFilePanel()
{
var path = EditorUtility.SaveFilePanel("Script Path", Application.dataPath, "Script", "cs");

private void RefreshScriptContent()
{
m_ScriptContent = ScriptGenerator.GetScriptPreview(m_Data, m_NameSpace, Path.GetFileNameWithoutExtension(m_ScriptPath));
}
if (string.IsNullOrEmpty(path))
{
return null;
}

public void Init(FieldBindingData data)
{
m_Data = data;
m_ScriptPath = data.ScriptPath;
return path;
}

while (string.IsNullOrEmpty(data.ScriptPath))
private void RefreshScriptContent()
{
OnChangeScriptLocation();
m_ScriptContent = ScriptGenerator.GetScriptPreview(m_Data, m_NameSpace, m_InheritClass, m_AccessModifier, Path.GetFileNameWithoutExtension(m_ScriptPath));
}
}

public void GenerateScript()
{
RefreshScriptContent();
public void Init(FieldBindingData data)
{
m_Data = data;
m_ScriptPath = data.ScriptPath;

string fileName = Path.GetFileNameWithoutExtension(m_ScriptPath.GetGlobalPath());
string path = Path.Combine(Path.GetDirectoryName(m_ScriptPath.GetGlobalPath()), fileName + "_BindFields.cs");
while (string.IsNullOrEmpty(data.ScriptPath))
{
OnChangeScriptLocation();
}
}

ScriptGenerator.GenerateScript(m_ScriptContent, path);
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
Close();
public void GenerateScript()
{
RefreshScriptContent();

string fileName = Path.GetFileNameWithoutExtension(m_ScriptPath.GetGlobalPath());
string path = Path.Combine(Path.GetDirectoryName(m_ScriptPath.GetGlobalPath()), fileName + "_BindFields.cs");

ScriptGenerator.GenerateScript(m_ScriptContent, path);
AssetDatabase.Refresh();
AssetDatabase.SaveAssets();
Close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ internal void Generate(FieldBinding fieldBinding)
string fileName = Path.GetFileNameWithoutExtension(data.ScriptPath);
string path = Path.Combine(Path.GetDirectoryName(data.ScriptPath.GetGlobalPath()), fileName + "_BindFields.cs");

string content = ScriptGenerator.GetScriptPreview(data, SettingUtility.GetNameSpaceValue(), fileName);
string content = ScriptGenerator.GetScriptPreview(data, SettingUtility.GetNameSpaceValue(), SettingUtility.GetInheritClassValue(), SettingUtility.GetAccessModifierValue(), fileName);

ScriptGenerator.GenerateScript(content, path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static void GenerateScript(string content, string path)
Debug.Log($"Generate Script At '{path}'.");
}

internal static string GetScriptPreview(FieldBindingData m_Data, string classNameSpace, string className)
internal static string GetScriptPreview(FieldBindingData m_Data, string classNameSpace, string inheritClass, AccessModifiers accessModifier, string className)
{
string result =
"using UnityEngine;\r\n" +
Expand All @@ -35,7 +35,7 @@ internal static string GetScriptPreview(FieldBindingData m_Data, string classNam

result +=
"[RequireComponent(typeof(UnityEngine.UIElements.UIDocument))]\r\n" +
"public partial class {CLASSNAME} : MonoBehaviour\r\n" +
"{ACCESSMODIFIERS} partial class {CLASSNAME} : {INHERITCLASS}\r\n" +
"{\r\n" +
"{CONTENT}\r\n" +
" public void Bind()\r\n" +
Expand All @@ -52,7 +52,7 @@ internal static string GetScriptPreview(FieldBindingData m_Data, string classNam
"namespace {NAMESPACE}\r\n" +
"{\r\n" +
" [RequireComponent(typeof(UnityEngine.UIElements.UIDocument))]\r\n" +
" public partial class {CLASSNAME} : MonoBehaviour\r\n" +
" {ACCESSMODIFIERS} partial class {CLASSNAME} : {INHERITCLASS}\r\n" +
" {\r\n" +
"{CONTENT}\r\n" +
" public void Bind()\r\n" +
Expand Down Expand Up @@ -84,9 +84,38 @@ internal static string GetScriptPreview(FieldBindingData m_Data, string classNam
$"{m_Data.FieldDatas[i].FieldName} = root.Q<{m_Data.FieldDatas[i].FieldType}>(\"{m_Data.FieldDatas[i].FieldName}\");\r\n";
}

string accessModifierStr;

switch (accessModifier)
{
case AccessModifiers.PUBLIC:
accessModifierStr = "public";
break;
case AccessModifiers.PRIVATE:
accessModifierStr = "private";
break;
case AccessModifiers.PROTECTED:
accessModifierStr = "protected";
break;
case AccessModifiers.INTERNAL:
accessModifierStr = "internal";
break;
case AccessModifiers.PROTECTED_INTERNAL:
accessModifierStr = "protected internal";
break;
case AccessModifiers.PRIVATE_PROTECTED:
accessModifierStr = "private protected";
break;
default:
accessModifierStr = "public";
break;
}

result = result.
Replace("{NAMESPACE}", classNameSpace).
Replace("{ACCESSMODIFIERS}", accessModifierStr).
Replace("{CLASSNAME}", className).
Replace("{INHERITCLASS}", inheritClass).
Replace("{CONTENT}", content).
Replace("{BINDING}", binding);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<ui:Label text="NameSpace" display-tooltip-when-elided="true" style="width: 85px; -unity-text-align: middle-center;" />
<ui:TextField picking-mode="Ignore" name="namespace" binding-path="m_NameSpace" style="flex-grow: 1; height: 100%;" />
</ui:VisualElement>
<ui:VisualElement style="flex-direction: row; height: 25px; margin-bottom: 5px; margin-top: 5px; flex-shrink: 0;">
<ui:Label text="InheritClass" display-tooltip-when-elided="true" style="width: 85px; -unity-text-align: middle-center;" />
<ui:TextField picking-mode="Ignore" name="inheritclass" binding-path="m_InheritClass" style="flex-grow: 1; height: 100%;" />
</ui:VisualElement>
<ui:VisualElement style="flex-direction: row; height: 25px; margin-bottom: 5px; margin-top: 5px; flex-shrink: 0;">
<ui:Label text="AccessModifier" display-tooltip-when-elided="true" style="width: 85px; -unity-text-align: middle-center;" />
<uie:EnumField value="PUBLIC" type="com.shaunfong.UIToolkitFieldBinding.editor.AccessModifiers, com.shaunfong.uitoolkit-fieldbinding.editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" binding-path="m_AccessModifier" name="accessmodifier" style="flex-grow: 1; height: 100%;" />
</ui:VisualElement>
<ui:Button text="Change Script Location" display-tooltip-when-elided="true" name="chaneglocation" style="height: 25px;" />
<ui:Button text="Generate" display-tooltip-when-elided="true" name="generate" style="height: 25px;" />
<ui:Label display-tooltip-when-elided="true" name="path" binding-path="m_ScriptPath" style="white-space: normal; padding-left: 2px; padding-right: 2px; padding-top: 2px; padding-bottom: 2px;" />
Expand Down
Loading

0 comments on commit 73799b3

Please sign in to comment.