Skip to content

Commit

Permalink
Merge pull request #125 from brunomikoski/feature/new-tweaks
Browse files Browse the repository at this point in the history
Feature/new tweaks
  • Loading branch information
brunomikoski authored Aug 22, 2023
2 parents 3b448af + 989b9c0 commit dc993ff
Show file tree
Hide file tree
Showing 60 changed files with 679 additions and 173 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.0.8]
### Changed
- Fixed a small typo in the package description.
- Since [2.0.3] the collection now uses the Editor for drawing the items on the collection, but this can cause some issues depending of how customized it is inside a ReorderableList, so you now can choose the item to use it or not by toggling the `Use Custom Editor` inside the advanced settings
- Upgraded the `CollectionCustomEditor` to use `BaseEditor`
- Updated `CollectionItemPicker<>` to use `IndirectReference<>` to store the items, since also contains the `LongGuid` reference to the Collection, allowing to work with multiple collections of the same time _(It should automatically upgrade to the new version automatically)_
- Added ability to compare `CollectionItemPicker<>` to a `IList<>` of the same type
- Other small fixes and improvements


## [2.0.7]
### Changed
Expand Down Expand Up @@ -459,6 +468,7 @@ public bool IsValidConsumable(Consumable consumable)
### Added
- First initial working version

[2.0.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.8
[2.0.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.7
[2.0.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.6
[2.0.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.5
Expand Down
32 changes: 30 additions & 2 deletions Scripts/Editor/Core/SOCSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public static SOCSettings Instance
[SerializeField]
private string generatedScriptsDefaultFilePath = @"Assets\Generated\Scripts";
public string GeneratedScriptsDefaultFilePath => generatedScriptsDefaultFilePath;


[SerializeField]
private List<LongGuid> useCustomEditorDrawer = new List<LongGuid>();


private static readonly GUIContent namespacePrefixGUIContent = new GUIContent(
"Prefix",
"When using the Create New Collection wizard," +
Expand Down Expand Up @@ -156,12 +159,37 @@ public void SetGeneratedScriptsDefaultFilePath(string assetPath)
generatedScriptsDefaultFilePath = assetPath;
Save();
}

public void Save()
{
string json = EditorJsonUtility.ToJson(this, prettyPrint: true);
File.WriteAllText(STORAGE_PATH, json);
}


public bool ShouldDrawUsingCustomEditor(ScriptableObjectCollection collection)
{
return useCustomEditorDrawer.Contains(collection.GUID);
}

public void SetUseCustomEditor(ScriptableObjectCollection collection, bool useCustomEditor)
{
if (useCustomEditor)
{
if (!useCustomEditorDrawer.Contains(collection.GUID))
{
useCustomEditorDrawer.Add(collection.GUID);
Save();
}
}
else
{
if (useCustomEditorDrawer.Contains(collection.GUID))
{
useCustomEditorDrawer.Remove(collection.GUID);
Save();
}
}
}
}
}
3 changes: 0 additions & 3 deletions Scripts/Editor/Core/Utility.meta

This file was deleted.

File renamed without changes.
129 changes: 129 additions & 0 deletions Scripts/Editor/CustomEditors/BaseEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using UnityEditor;

namespace BrunoMikoski.ScriptableObjectCollections
{
public abstract class BaseEditor<T> : Editor where T : class
{
protected T Target => target as T;

/// <summary>
/// Return the Serialized property for a field, and exclude it from being automatically
/// displayed in the inspector. Call this when you need to provide a custom UX for a field.
/// </summary>
/// <typeparam name="TValue">Magic experssion code</typeparam>
/// <param name="expr">Call format is FindAndExcludeProperty(x => x.myField)</param>
/// <returns>The serialized property for the field</returns>
protected SerializedProperty FindAndExcludeProperty<TValue>(Expression<Func<T, TValue>> expr)
{
SerializedProperty p = FindProperty(expr);
ExcludeProperty(p.name);
return p;
}

/// <summary>
/// Return the Serialized property for a field.
/// </summary>
/// <typeparam name="TValue">Magic experssion code</typeparam>
/// <param name="expr">Call format is FindProperty(x => x.myField)</param>
/// <returns>The serialized property for the field</returns>
protected SerializedProperty FindProperty<TValue>(Expression<Func<T, TValue>> expr)
{
return serializedObject.FindProperty(FieldPath(expr));
}

/// <summary>
/// Magic code to get the string name of a field. Will not build if the field name changes.
/// </summary>
/// <typeparam name="TValue">Magic experssion code</typeparam>
/// <param name="expr">Call format is FieldPath(x => x.myField)</param>
/// <returns>The string name of the field</returns>
protected string FieldPath<TValue>(Expression<Func<T, TValue>> expr)
{
return ReflectionUtility.GetFieldPath(expr);
}

/// <summary>Obsolete, do not use. Use the overload, which is more performant</summary>
/// <returns>List of property names to exclude</returns>
protected virtual List<string> GetExcludedPropertiesInInspector() { return mExcluded; }

/// <summary>Get the property names to exclude in the inspector.</summary>
/// <param name="excluded">Add the names to this list</param>
protected virtual void GetExcludedPropertiesInInspector(List<string> excluded)
{
excluded.Add("m_Script");
}

/// <summary>
/// Exclude a property from automatic inclusion in the inspector
/// when DrawRemainingPropertiesInInspector() is called
/// </summary>
/// <param name="propertyName">The property to exclude</param>
protected void ExcludeProperty(string propertyName)
{
mExcluded.Add(propertyName);
}

/// <summary>Check whenther a property is in the excluded list</summary>
/// <param name="propertyName">The property to check</param>
/// <returns>True if property is excluded from automatic inclusion in the inspector
/// when DrawRemainingPropertiesInInspector() is called</returns>
protected bool IsPropertyExcluded(string propertyName)
{
return mExcluded.Contains(propertyName);
}

/// <summary>
/// Draw the inspector
/// </summary>
public override void OnInspectorGUI()
{
BeginInspector();
DrawRemainingPropertiesInInspector();
}

List<string> mExcluded = new List<string>();

/// <summary>
/// Clients should call this at the start of OnInspectorGUI.
/// Updates the serialized object and Sets up for excluded properties.
/// </summary>
protected virtual void BeginInspector()
{
serializedObject.Update();
mExcluded.Clear();
GetExcludedPropertiesInInspector(mExcluded);
}

/// <summary>
/// Draw a property in the inspector, if it is not excluded.
/// Property is marked as drawn, so will not be drawn again
/// by DrawRemainingPropertiesInInspector()
/// </summary>
/// <param name="p">The property to draw</param>
protected virtual void DrawPropertyInInspector(SerializedProperty p)
{
if (!IsPropertyExcluded(p.name))
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(p);
if (EditorGUI.EndChangeCheck())
serializedObject.ApplyModifiedProperties();
ExcludeProperty(p.name);
}
}

/// <summary>
/// Draw all remaining unexcluded undrawn properties in the inspector.
/// </summary>
protected void DrawRemainingPropertiesInInspector()
{
EditorGUI.BeginChangeCheck();
DrawPropertiesExcluding(serializedObject, mExcluded.ToArray());
if (EditorGUI.EndChangeCheck())
serializedObject.ApplyModifiedProperties();
}
}
}
11 changes: 11 additions & 0 deletions Scripts/Editor/CustomEditors/BaseEditor.cs.meta

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

Loading

0 comments on commit dc993ff

Please sign in to comment.