Skip to content

Commit

Permalink
Finish controller code generation in Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Mar 6, 2019
1 parent 6c0f589 commit 131b085
Show file tree
Hide file tree
Showing 31 changed files with 679 additions and 31 deletions.
8 changes: 8 additions & 0 deletions Assets/Assets.meta

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

8 changes: 8 additions & 0 deletions Assets/Assets/Example.meta

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

8 changes: 8 additions & 0 deletions Assets/Assets/Example/Scripts.meta

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

6 changes: 6 additions & 0 deletions Assets/Assets/Example/Scripts/ViewController.cs
@@ -0,0 +1,6 @@
namespace Nova
{
public sealed class ViewController : UIViewController
{
}
}
11 changes: 11 additions & 0 deletions Assets/Assets/Example/Scripts/ViewController.cs.meta

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

3 changes: 3 additions & 0 deletions Assets/Scripts/Nova/Editor/CustomEditor.meta

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

3 changes: 3 additions & 0 deletions Assets/Scripts/Nova/Editor/EditorWindow.meta

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

@@ -0,0 +1,80 @@
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Nova.Editor
{
internal class CreateNavigationControllerEditorWindow : EditorWindow
{
private string m_navigationControllerInput = "NavigationController";

[MenuItem( "Assets/Create/Nova/NavigationController" )]
private static void CreateUINavigationController( MenuCommand menuCommand )
{
CreateNavigationControllerEditorWindow window = CreateInstance<CreateNavigationControllerEditorWindow>();
window.position = new Rect( Screen.width / 2f, Screen.height / 2f, 300f, 100f );
window.titleContent = new GUIContent( "Create a NavigationController" );
window.ShowUtility();
}

private void OnGUI()
{
m_navigationControllerInput = EditorGUILayout.TextField( "Class Name: ", m_navigationControllerInput );

GUILayout.Space( 15f );
GUILayout.BeginHorizontal();

if ( GUILayout.Button( "Create" ) || Event.current.keyCode == KeyCode.Return )
{
MakeViewController( m_navigationControllerInput );
Close();
}

if ( GUILayout.Button( "Cancel" ) )
{
Close();
}

GUILayout.EndHorizontal();
Repaint();
}

private void MakeViewController( string className )
{
string selectedPath = AssetDatabase.GUIDToAssetPath( Selection.assetGUIDs[0] );

// generate code text
StringBuilder stringBuilder = new StringBuilder();
TextIndentHelper indent = TextIndentHelper.StandardSpacesHelper;

stringBuilder.Append( "using Nova;\n" );
stringBuilder.Append( "\n" );
stringBuilder.Append( $"namespace {ScriptingPreferences.NamespaceName}\n" );
stringBuilder.Append( "{\n" );
indent.IndentLevel++;

stringBuilder.Append( indent.ApplyIndent( $"public sealed class {className} : UINavigationController\n" ) );
stringBuilder.Append( indent.ApplyIndent( "{\n" ) );
indent.IndentLevel++;

stringBuilder.Append( indent.ApplyIndent( "\n" ) );
indent.IndentLevel--;

stringBuilder.Append( indent.ApplyIndent( "}\n" ) );

indent.IndentLevel--;
stringBuilder.Append( indent.ApplyIndent( "}\n" ) );

string fullPath = $"{selectedPath}/{className}.cs";

// save to file
using ( StreamWriter writer = new StreamWriter( fullPath, false ) )
{
writer.Write( stringBuilder.ToString() );
}

AssetDatabase.ImportAsset( fullPath, ImportAssetOptions.ForceUpdate );
}
}
}

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

@@ -0,0 +1,80 @@
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Nova.Editor
{
internal class CreateViewControllerEditorWindow : EditorWindow
{
private string m_viewControllerInput = "ViewController";

[MenuItem( "Assets/Create/Nova/ViewController" )]
private static void CreateUIViewController( MenuCommand menuCommand )
{
CreateViewControllerEditorWindow window = CreateInstance<CreateViewControllerEditorWindow>();
window.position = new Rect( Screen.width / 2f, Screen.height / 2f, 300f, 100f );
window.titleContent = new GUIContent( "Create a ViewController" );
window.ShowUtility();
}

private void OnGUI()
{
m_viewControllerInput = EditorGUILayout.TextField( "Class Name: ", m_viewControllerInput );

GUILayout.Space( 15f );
GUILayout.BeginHorizontal();

if ( GUILayout.Button( "Create" ) || Event.current.keyCode == KeyCode.Return )
{
MakeViewController( m_viewControllerInput );
Close();
}

if ( GUILayout.Button( "Cancel" ) )
{
Close();
}

GUILayout.EndHorizontal();
Repaint();
}

private void MakeViewController( string className )
{
string selectedPath = AssetDatabase.GUIDToAssetPath( Selection.assetGUIDs[0] );

// generate code text
StringBuilder stringBuilder = new StringBuilder();
TextIndentHelper indent = TextIndentHelper.StandardSpacesHelper;

stringBuilder.Append( "using Nova;\n" );
stringBuilder.Append( "\n" );
stringBuilder.Append( $"namespace {ScriptingPreferences.NamespaceName}\n" );
stringBuilder.Append( "{\n" );
indent.IndentLevel++;

stringBuilder.Append( indent.ApplyIndent( $"public sealed class {className} : UIViewController\n" ) );
stringBuilder.Append( indent.ApplyIndent( "{\n" ) );
indent.IndentLevel++;

stringBuilder.Append( indent.ApplyIndent( "\n" ) );
indent.IndentLevel--;

stringBuilder.Append( indent.ApplyIndent( "}\n" ) );

indent.IndentLevel--;
stringBuilder.Append( indent.ApplyIndent( "}\n" ) );

string fullPath = $"{selectedPath}/{className}.cs";

// save to file
using ( StreamWriter writer = new StreamWriter( fullPath, false ) )
{
writer.Write( stringBuilder.ToString() );
}

AssetDatabase.ImportAsset( fullPath, ImportAssetOptions.ForceUpdate );
}
}
}

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

17 changes: 0 additions & 17 deletions Assets/Scripts/Nova/Editor/NovaEditorSettings.cs

This file was deleted.

17 changes: 3 additions & 14 deletions Assets/Scripts/Nova/Editor/NovaMenuItems.cs
@@ -1,5 +1,8 @@
using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -22,20 +25,6 @@ private static void CreateUIView( MenuCommand menuCommand )
MakeUIView( menuCommand );
}

[MenuItem( "Assets/Create/Nova/ViewController" )]
private static void CreateUIViewController( MenuCommand menuCommand )
{
CreateScriptAsset( $"{NovaEditorSettings.NovaPath}/Stubs/ViewController.cs.stub",
"ViewController.cs" );
}

[MenuItem( "Assets/Create/Nova/NavigationController" )]
private static void CreateUINavigationController( MenuCommand menuCommand )
{
CreateScriptAsset( $"{NovaEditorSettings.NovaPath}/Stubs/NavigationController.cs.stub",
"NavigationController.cs" );
}

private static GameObject MakeUIView( MenuCommand menuCommand, string name = "UIView" )
{
GameObject gameObject = CreateGameObject( menuCommand, name, typeof( Canvas ), typeof( CanvasScaler ),
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Nova/Editor/Preferences.meta

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

3 changes: 3 additions & 0 deletions Assets/Scripts/Nova/Editor/Preferences/Items.meta

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

10 changes: 10 additions & 0 deletions Assets/Scripts/Nova/Editor/Preferences/Items/IPreferenceItem.cs
@@ -0,0 +1,10 @@
namespace Nova.Editor
{
internal interface IPreferenceItem
{
string Name { get; }

void OnInitialize();
void OnGUI();
}
}

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

@@ -0,0 +1,78 @@
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Text.RegularExpressions;

namespace Nova.Editor
{
internal sealed class ScriptingPreferences : IPreferenceItem
{
public static string NamespaceName => NovaPreferences.Get( c_namespacePrefKey, c_defaultNamespace );
public static string NovaPath => NovaPreferences.Get( c_novaPathPrefKey, $"{Environment.CurrentDirectory}/Packages/Nova" );

#region IPreferenceItem

public string Name => "Scripting";

public void OnInitialize()
{
m_namespaceName = NamespaceName;
m_novaPath = NovaPath;
}

public void OnGUI()
{
// namespace
EditorGUI.BeginChangeCheck();
GUIContent namespaceContent = new GUIContent( "Namespace", "Namespace to use when generating code" );
m_namespaceName = EditorGUILayout.TextField( namespaceContent, m_namespaceName );
if ( EditorGUI.EndChangeCheck() )
{
m_namespaceName = ValidateCodeName( m_namespaceName );
if ( string.IsNullOrEmpty( m_namespaceName ) )
{
m_namespaceName = c_defaultNamespace;
}

NovaPreferences.Set( c_namespacePrefKey, m_namespaceName );
}

// nova path
EditorGUI.BeginChangeCheck();
GUIContent novaPathContent = new GUIContent( "Nova Package Path", "Path to the Nova package folder" );
m_novaPath = EditorGUILayout.TextField( novaPathContent, m_novaPath );
if ( EditorGUI.EndChangeCheck() )
{
m_novaPath = ValidatePath( m_novaPath );
if ( m_novaPath.EndsWith( "/" ) == false )
{
m_novaPath += "/";
}

NovaPreferences.Set( c_novaPathPrefKey, m_novaPath );
}
}

#endregion

private const string c_defaultNamespace = "Nova";
private const string c_namespacePrefKey = "Scripting_Namespace";
private const string c_novaPathPrefKey = "Scripting_NovaPath";

private string m_namespaceName;
private string m_novaPath;

private static string ValidateCodeName( string str )
{
return Regex.Replace( str, @"^\s*[0-9]+|\s+|\W", string.Empty );
}

private string ValidatePath( string str )
{
string format =
$"[{Regex.Escape( new string( Path.GetInvalidPathChars() ) )}]|[{Regex.Escape( ( ":*?|\"<>|" ) )}]";
return Regex.Replace( str, format, string.Empty );
}
}
}

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

0 comments on commit 131b085

Please sign in to comment.