Skip to content

Commit

Permalink
Merge pull request #77 from brunomikoski/feature/keyboard-navigation
Browse files Browse the repository at this point in the history
Feature/keyboard navigation
  • Loading branch information
brunomikoski committed Apr 28, 2021
2 parents a24fd44 + 6ab4e7b commit c59cbf5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
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).

## [1.6.1]
### Added
- Added arrow navigation on the Collection Editor, now you can navigate between items and expand/collapse it by <kbd>left</kbd> <kbd>right</kbd>
- You can now expand/collapse all items by holding <kbd>alt</kbd> while clicking on the foldout arrow, like default unity controls

## [1.6.0]
### Changed
- Refactored the `ScriptableObjectCollection` `Custom Editor` to use `ReorderableList` instead of the previous custom list drawer
Expand Down
51 changes: 48 additions & 3 deletions Scripts/Editor/Core/CollectionCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,24 @@ private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool
rect.width -= 20;

Rect foldoutArrowRect = rect;
bool wasExpanded = collectionItemSerializedProperty.isExpanded;
collectionItemSerializedProperty.isExpanded = EditorGUI.Foldout(
foldoutArrowRect,
collectionItemSerializedProperty.isExpanded,
GUIContent.none
);

if (!wasExpanded && collectionItemSerializedProperty.isExpanded)
{
if (Event.current.alt)
SetAllExpanded(true);
}
else if (wasExpanded && !collectionItemSerializedProperty.isExpanded)
{
if (Event.current.alt)
SetAllExpanded(false);
}

using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
GUI.SetNextControlName(collectionItemSerializedProperty.objectReferenceValue.name);
Expand Down Expand Up @@ -203,6 +215,15 @@ private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool
heights[index] = rect.y - originY;
}

private void SetAllExpanded(bool expanded)
{
for (int i = 0; i < reorderableList.count; i++)
{
SerializedProperty property = reorderableList.serializedProperty.GetArrayElementAtIndex(i);
property.isExpanded = expanded;
}
}

private void CheckForContextInputOnItem(SerializedProperty collectionItemSerializedProperty, int index, float originY, Rect rect)
{
Event current = Event.current;
Expand Down Expand Up @@ -323,7 +344,33 @@ public override void OnInspectorGUI()
DrawBottomMenu();
}
DrawSettings();
}
CheckForKeyboardShortcuts();
}

private void CheckForKeyboardShortcuts()
{
if (reorderableList.index == -1)
return;

if (!reorderableList.HasKeyboardControl())
return;

if (Event.current.type == EventType.Layout || Event.current.type == EventType.Repaint)
return;

SerializedProperty element = reorderableList.serializedProperty.GetArrayElementAtIndex(reorderableList.index);

if (Event.current.keyCode == KeyCode.RightArrow)
{
element.isExpanded = true;
Event.current.Use();
}
else if (Event.current.keyCode == KeyCode.LeftArrow)
{
element.isExpanded = false;
Event.current.Use();
}
}

private void RemoveNullReferences()
{
Expand Down Expand Up @@ -505,7 +552,6 @@ private void DrawSearchField()

private void DrawSettings()
{
Profiler.BeginSample("Collection Editor");
using (new GUILayout.VerticalScope("Box"))
{
EditorGUI.indentLevel++;
Expand All @@ -525,7 +571,6 @@ private void DrawSettings()
EditorGUI.indentLevel--;
}
}
Profiler.EndSample();
}

private void DrawGeneratedFileName()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.scriptableobjectcollection",
"displayName": "Scriptable Object Collection",
"version": "1.6.0",
"version": "1.6.1",
"unity": "2018.4",
"description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping then into a collection and exposing then by code or nice inspectors!",
"keywords": [
Expand Down

0 comments on commit c59cbf5

Please sign in to comment.