Skip to content
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
9 changes: 9 additions & 0 deletions Assets/Editor Toolbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.8.13 [04.07.2021]

### Added:
- Begin/EndHorizontalGroupAttribute

### Changed:
- Fix overall layouting issues
- Fix IgnoreParentAttribute issues

## 0.8.11 [02.07.2021]

### Added:
Expand Down
8 changes: 8 additions & 0 deletions Assets/Editor Toolbox/Editor/Drawers/Storages.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

using UnityEngine;

namespace Toolbox.Editor.Drawers
{
public class ControlDataStorage<T> : DrawerDataStorage<int, Vector2, Vector2>
{
public ControlDataStorage(Func<int, Vector2, Vector2> createMethod) : base(createMethod)
{ }


protected override string GetKey(int context)
{
return context.ToString();
}


public int GetControlId()
{
return GUIUtility.GetControlID(FocusType.Passive);
}
}
}

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

102 changes: 102 additions & 0 deletions Assets/Editor Toolbox/Editor/Drawers/Storages/DrawerDataStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System;

namespace Toolbox.Editor.Drawers
{
/// <summary>
/// Internal system responsible for keeping and clearing data between <see cref="UnityEditor.Editor"/>s.
/// This small system works only for attribute-based drawers and should be defined as a static field.
/// </summary>
/// <typeparam name="T">Key-related object.</typeparam>
/// <typeparam name="T1">Data to store.</typeparam>
/// <typeparam name="T2">Any type needed for storage item creation. Pass <see cref="EventArgs.Empty"/> if no additional data is needed.</typeparam>
public abstract class DrawerDataStorage<T, T1, T2> : DrawerDataStorageBase
{
protected readonly Dictionary<string, T1> items = new Dictionary<string, T1>();

protected readonly Func<T, T2, T1> createMethod;
protected readonly Action<T1> removeMethod;


public DrawerDataStorage(Func<T, T2, T1> createMethod) : this(createMethod, null)
{ }

public DrawerDataStorage(Func<T, T2, T1> createMethod, Action<T1> removeMethod)
{
this.createMethod = createMethod;
this.removeMethod = removeMethod;
}


protected abstract string GetKey(T context);


/// <summary>
/// Returns and if needed creates new item.
/// </summary>
public virtual T1 ReturnItem(T context, T2 args)
{
var key = GetKey(context);
if (items.TryGetValue(key, out var item))
{
return item;
}
else
{
return items[key] = CreateItem(context, args);
}
}

public virtual T1 CreateItem(T context, T2 args)
{
return CreateItem(context, args, true);
}

public virtual T1 CreateItem(T context, T2 args, bool append)
{
var item = createMethod(context, args);
if (append)
{
AppendItem(context, item);
}

return item;
}

public virtual T1 AppendItem(T context, T1 item)
{
var key = GetKey(context);
return items[key] = item;
}

public virtual void ClearItem(T context)
{
var key = GetKey(context);
if (removeMethod != null)
{
if (items.TryGetValue(key, out var value))
{
removeMethod(value);
items.Remove(key);
}
}
else
{
items.Remove(key);
}
}

public override void ClearItems()
{
if (removeMethod != null)
{
foreach (var item in items.Values)
{
removeMethod(item);
}
}

items.Clear();
}
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;

namespace Toolbox.Editor.Drawers
{
public abstract class DrawerDataStorageBase
{
static DrawerDataStorageBase()
{
InspectorUtility.OnEditorReload += () =>
{
for (var i = 0; i < storages.Count; i++)
{
storages[i].ClearItems();
}
};
}


protected DrawerDataStorageBase()
{
storages.Add(this);
}

~DrawerDataStorageBase()
{
storages.Remove(this);
}


private static readonly List<DrawerDataStorageBase> storages = new List<DrawerDataStorageBase>();


/// <summary>
/// Called each time Editor is completely destroyed.
/// </summary>
public abstract void ClearItems();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;

using UnityEditor;

namespace Toolbox.Editor.Drawers
{
public class PropertyDataStorage<T, T1> : DrawerDataStorage<SerializedProperty, T, T1>
{
private readonly bool isPersistant;


public PropertyDataStorage(bool isPersistant, Func<SerializedProperty, T1, T> createMethod) : this(isPersistant, createMethod, null)
{ }

public PropertyDataStorage(bool isPersistant, Func<SerializedProperty, T1, T> createMethod, Action<T> removeMethod) : base (createMethod, removeMethod)
{
this.isPersistant = isPersistant;
}


protected override string GetKey(SerializedProperty property)
{
return isPersistant
? property.GetPropertyTypeKey()
: property.GetPropertyHashKey();
}


public override void ClearItems()
{
if (isPersistant)
{
return;
}
else
{
if (removeMethod != null)
{
foreach (var item in items.Values)
{
removeMethod(item);
}
}

items.Clear();
}
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class BeginHorizontalAttributeDrawer : ToolboxDecoratorDrawer<BeginHorizo
protected override void OnGuiBeginSafe(BeginHorizontalAttribute attribute)
{
var width = EditorGUIUtility.currentViewWidth;
//set a new label & field width for this layout
//set a new width value for label/field controls
EditorGUIUtility.labelWidth = width * attribute.LabelToWidthRatio;
EditorGUIUtility.fieldWidth = width * attribute.FieldToWidthRatio;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Drawers
{
public class BeginHorizontalGroupAttributeDrawer : ToolboxDecoratorDrawer<BeginHorizontalGroupAttribute>
{
static BeginHorizontalGroupAttributeDrawer()
{
storage = new ControlDataStorage<Vector2>((id, defaultValue) =>
{
return defaultValue;
});
}

/// <summary>
/// Storage used to cache scroll values depending on the given control ID.
/// </summary>
private static readonly ControlDataStorage<Vector2> storage;


private void HandleScrollView(float fixedHeight)
{
var controlId = storage.GetControlId();
var oldScroll = storage.ReturnItem(controlId, Vector2.zero);
var newScroll = fixedHeight > 0.0f
? EditorGUILayout.BeginScrollView(oldScroll, GUILayout.Height(fixedHeight))
: EditorGUILayout.BeginScrollView(oldScroll);
storage.AppendItem(controlId, newScroll);
}

private void ApplyIndentLevel()
{
GUILayout.Space(Style.extraIndentLevel);
}


protected override void OnGuiBeginSafe(BeginHorizontalGroupAttribute attribute)
{
var fixedWidth = EditorGUIUtility.currentViewWidth;
var fixedHeight = attribute.Height;
EditorGUIUtility.labelWidth = fixedWidth * attribute.LabelToWidthRatio;
EditorGUIUtility.fieldWidth = fixedWidth * attribute.FieldToWidthRatio;

ToolboxLayoutHelper.BeginVertical(Style.groupBackgroundStyle);
if (attribute.HasLabel)
{
GUILayout.Label(attribute.Label, EditorStyles.boldLabel);
}

HandleScrollView(fixedHeight);
ToolboxLayoutHelper.BeginHorizontal();
ApplyIndentLevel();
}


private static class Style
{
/// <summary>
/// Additional indent applied to keep foldout-based labels within the group.
/// </summary>
internal static readonly float extraIndentLevel = 8.0f;

internal static readonly GUIStyle groupBackgroundStyle;

static Style()
{
#if UNITY_2019_3_OR_NEWER
groupBackgroundStyle = new GUIStyle("helpBox")
#else
groupBackgroundStyle = new GUIStyle("box")
#endif
{
padding = new RectOffset(13, 12, 5, 5)
};
}
}
}
}

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

Loading