Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored user/project settings #96

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void OnInspectorGUI()
{
DrawRaiseButton();

if (!SOArchitecture_Settings.Instance.EnableDebug)
if (!SOArchitecturePreferences.IsDebugEnabled)
EditorGUILayout.HelpBox("Debug mode disabled\nStack traces will not be filed on raise!", MessageType.Warning);

_stackTrace.Draw();
Expand Down
4 changes: 2 additions & 2 deletions Assets/SO Architecture/Events/Game Events/GameEventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public abstract class GameEventBase : SOArchitectureBaseObject, IGameEvent, ISta
public void AddStackTrace()
{
#if UNITY_EDITOR
if (SOArchitecture_Settings.Instance.EnableDebug)
if (SOArchitecturePreferences.IsDebugEnabled)
_stackTraces.Insert(0, StackTraceEntry.Create());
#endif
}
public void AddStackTrace(object value)
{
#if UNITY_EDITOR
if(SOArchitecture_Settings.Instance.EnableDebug)
if(SOArchitecturePreferences.IsDebugEnabled)
_stackTraces.Insert(0, StackTraceEntry.Create(value));
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private void DrawPoint(Vector3 position, Vector3 direction)
}
private bool EnableGizmoDebuggin()
{
if (!SOArchitecture_Settings.Instance.DrawEventGizmos)
if (!SOArchitecturePreferences.AreGizmosEnabled)
return false;

return _enableGizmoDebugging;
Expand Down
187 changes: 187 additions & 0 deletions Assets/SO Architecture/SOArchitecturePreferences.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace ScriptableObjectArchitecture
{
/// <summary>
/// An editor class for managing project and user preferences for the SOArchitecture library. This is kept
/// in the runtime assembly for the purpose of enabling editor-only additional features when playing such as
/// gizmos and debugging.
/// </summary>
public static class SOArchitecturePreferences
{
/// <summary>
/// Returns true if debug features should be enabled, otherwise false.
/// </summary>
public static bool IsDebugEnabled
{
get { return GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT); }
}

/// <summary>
/// Returns true if Gizmos should be enabled, otherwise false.
/// </summary>
public static bool AreGizmosEnabled
{
get { return GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT); }
}

// UI
private const string PREFERENCES_TITLE_PATH = "Preferences/SOArchitecture";
private const string PROJECT_TITLE_PATH = "Project/SOArchitecture";

private const string USER_PREFERENCES_HEADER = "User Preferences";
private const string PROJECT_REFERENCES_HEADER = "Project Preferences";

private const string CODE_GEN_DIRECTORY_LABEL = "Code Generation Output Directory";
private const string CODE_GEN_DIRECTORY_DESCRIPTION
= "The directory where the output of code generation will write to.";

private const string ALLOW_OVERWRITE_LABEL = "Allow Code Generation to Overwrite";
private const string ALLOW_OVERWRITE_DESCRIPTION =
"Allow newly generated code files to overwrite existing ones.";

private const string ASSET_MENU_ORDER_LABEL = "Create Asset Menu Order";
private const string ASSET_MENU_ORDER_DESCRIPTION =
"This determines the order in which the CreateAsset Context Menu will be placed into.";

private static readonly GUILayoutOption MAX_WIDTH;

// Searchable Fields
private static readonly string[] KEYWORDS =
{
"Scriptable",
"Architecture"
};

// User Editor Preferences
private const string DRAW_EVENT_GIZMOS_PREF = "SOArchitecture.DrawEventGizmoos";
private const string ENABLE_DEBUG_PREF = "SOArchitecture.EnableDebug";

private const bool DRAW_EVENT_GIZMOS_DEFAULT = true;
private const bool ENABLE_DEBUG_DEFAULT = true;

static SOArchitecturePreferences()
{
MAX_WIDTH = GUILayout.MaxWidth(200f);
}

[SettingsProvider]
private static SettingsProvider CreateProjectPreferenceSettingsProvider()
{
return new SettingsProvider(PROJECT_TITLE_PATH, SettingsScope.Project)
{
guiHandler = DrawProjectGUI,
keywords = KEYWORDS
};
}

[SettingsProvider]
private static SettingsProvider CreatePersonalPreferenceSettingsProvider()
{
return new SettingsProvider(PREFERENCES_TITLE_PATH, SettingsScope.User)
{
guiHandler = DrawPersonalPrefsGUI,
keywords = KEYWORDS
};
}

#if !UNITY_2019_1_OR_NEWER
[PreferenceItem(PREFERENCES_TITLE)]
#endif
private static void DrawAllGUI()
{
DrawProjectGUI();
DrawPersonalPrefsGUI();
}

private static void DrawProjectGUI(string value = "")
{
EditorGUILayout.LabelField(PROJECT_REFERENCES_HEADER, EditorStyles.boldLabel);

var settings = SOArchitecture_Settings.Instance;

GUI.changed = false;

// Code Generation Target Directory
EditorGUILayout.HelpBox(CODE_GEN_DIRECTORY_DESCRIPTION, MessageType.Info);
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField(new GUIContent(CODE_GEN_DIRECTORY_LABEL), MAX_WIDTH);
var directory = EditorGUILayout.TextField(settings.CodeGenerationTargetDirectory);
settings.CodeGenerationTargetDirectory = directory;
}

// Code Generation Allow Overwrite
EditorGUILayout.HelpBox(ALLOW_OVERWRITE_DESCRIPTION, MessageType.Info);
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField(new GUIContent(ALLOW_OVERWRITE_LABEL), MAX_WIDTH);
var newOverwrite = EditorGUILayout.Toggle(settings.CodeGenerationAllowOverwrite);
settings.CodeGenerationAllowOverwrite = newOverwrite;
}

// Default Create Asset Menu Order
EditorGUILayout.HelpBox(ASSET_MENU_ORDER_DESCRIPTION, MessageType.Info);
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField(ASSET_MENU_ORDER_LABEL, MAX_WIDTH);
var newMenuOrder = EditorGUILayout.IntField(settings.DefaultCreateAssetMenuOrder);
settings.DefaultCreateAssetMenuOrder = newMenuOrder;
}

if (GUI.changed)
{
EditorUtility.SetDirty(settings);
}
}

private static void DrawPersonalPrefsGUI(string value = "")
{
EditorGUILayout.LabelField(USER_PREFERENCES_HEADER, EditorStyles.boldLabel);

// Draw Event Gizmo
var drawEventPref = GetBoolPref(DRAW_EVENT_GIZMOS_PREF, DRAW_EVENT_GIZMOS_DEFAULT);

GUI.changed = false;
drawEventPref = EditorGUILayout.Toggle("Draw Event Gizmo", drawEventPref);
if (GUI.changed)
{
EditorPrefs.SetBool(DRAW_EVENT_GIZMOS_PREF, drawEventPref);
}

// Enable Debug
EditorGUILayout.HelpBox("This will enable debug features for troubleshooting purposes such as " +
"game events collecting stack traces. This will decrease performance " +
"in-editor.", MessageType.Info);
var enableDebugPref = GetBoolPref(ENABLE_DEBUG_PREF, ENABLE_DEBUG_DEFAULT);

GUI.changed = false;
enableDebugPref = EditorGUILayout.Toggle("Enable Debug", enableDebugPref);
if (GUI.changed)
{
EditorPrefs.SetBool(ENABLE_DEBUG_PREF, enableDebugPref);
}
}

/// <summary>
/// Returns the current bool preference; if none exists, the default is set and returned.
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
private static bool GetBoolPref(string key, bool defaultValue)
{
if (!EditorPrefs.HasKey(key))
{
EditorPrefs.SetBool(key, defaultValue);
}

return EditorPrefs.GetBool(key);
}
}
}

#endif
3 changes: 3 additions & 0 deletions Assets/SO Architecture/SOArchitecturePreferences.cs.meta

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

28 changes: 19 additions & 9 deletions Assets/SO Architecture/Utility/SOArchitecture_Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,31 @@ private static SOArchitecture_Settings CreateInstance()
}
#endregion

public bool DrawEventGizmos { get { return _drawEventGizmos; } }
public string CodeGenerationTargetDirectory { get { return _codeGenerationTargetDirectory; } }
public bool CodeGenerationAllowOverwrite { get { return _codeGenerationAllowOverwrite; } }
public int DefaultCreateAssetMenuOrder { get { return _defualtCreateAssetMenuOrder; } }
public bool EnableDebug { get { return _enableDebug; } }
public string CodeGenerationTargetDirectory
{
get { return _codeGenerationTargetDirectory; }
set { _codeGenerationTargetDirectory = value; }
}

public bool CodeGenerationAllowOverwrite
{
get { return _codeGenerationAllowOverwrite; }
set { _codeGenerationAllowOverwrite = value; }
}

public int DefaultCreateAssetMenuOrder
{
get { return _defualtCreateAssetMenuOrder; }
set { _defualtCreateAssetMenuOrder = value; }
}

[SerializeField]
private bool _drawEventGizmos = true;
[SerializeField]
private string _codeGenerationTargetDirectory = "CODE_GENERATION";

[SerializeField, Tooltip("Allow newly generated code files to overwrite existing ones")]
private bool _codeGenerationAllowOverwrite = false;

[SerializeField]
private int _defualtCreateAssetMenuOrder = 120;
[SerializeField, Tooltip("Will enable features such as game events collecting stack traces.\nWill decrease performance in-editor.")]
private bool _enableDebug = true;
}
}