-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Garume/v1.0.0
Version up to 1.0.0
- Loading branch information
Showing
38 changed files
with
1,121 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
109 changes: 109 additions & 0 deletions
109
Assets/ContextCircleMenu/Editor/Core/CircleMenus/CircleMenu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace ContextCircleMenu.Editor | ||
{ | ||
/// <summary> | ||
/// Represents a menu item in the circle menu. | ||
/// </summary> | ||
public abstract class CircleMenu | ||
{ | ||
protected internal readonly CircleMenu Parent; | ||
|
||
private bool _alreadyInitialized; | ||
private IButtonFactory _buttonFactory; | ||
|
||
protected CircleButton[] ButtonElements; | ||
protected VisualElement[] UtilityElements; | ||
|
||
protected CircleMenu(string path, GUIContent icon, Action onSelected, CircleMenu parent, IButtonFactory factory, | ||
bool shouldCloseMenuAfterSelection = true) | ||
{ | ||
Path = path; | ||
Icon = icon; | ||
OnSelected = onSelected; | ||
Parent = parent; | ||
ShouldCloseMenuAfterSelection = shouldCloseMenuAfterSelection; | ||
_buttonFactory = factory; | ||
} | ||
|
||
public List<CircleMenu> Children { get; } = new(8); | ||
public GUIContent Icon { get; } | ||
public string Path { get; } | ||
public bool ShouldCloseMenuAfterSelection { get; } | ||
public Action OnSelected { get; protected set; } | ||
|
||
internal ReadOnlySpan<VisualElement> BuildElements(ref ContextCircleMenuOption menuOption) | ||
{ | ||
if (!_alreadyInitialized) | ||
{ | ||
_buttonFactory ??= new ButtonFactory(); | ||
var buttons = CreateButtons(_buttonFactory, ref menuOption); | ||
ButtonElements = ButtonElements == null ? buttons : ButtonElements.Concat(buttons).ToArray(); | ||
UtilityElements = CreateUtilityElements(ref menuOption); | ||
|
||
OnInitialized(ref menuOption); | ||
_alreadyInitialized = true; | ||
} | ||
|
||
OnBuild(); | ||
|
||
var pool = ArrayPool<VisualElement>.Shared; | ||
var buffer = pool.Rent(ButtonElements.Length + UtilityElements.Length); | ||
ButtonElements.CopyTo(buffer, 0); | ||
UtilityElements.CopyTo(buffer, ButtonElements.Length); | ||
var combinedSpan = new Span<VisualElement>(buffer, 0, ButtonElements.Length + UtilityElements.Length); | ||
pool.Return(buffer); | ||
return combinedSpan; | ||
} | ||
|
||
|
||
internal void PrepareButton(CircleButton button) | ||
{ | ||
if (ButtonElements == null) | ||
{ | ||
ButtonElements = new[] { button }; | ||
} | ||
else | ||
{ | ||
Array.Resize(ref ButtonElements, ButtonElements.Length + 1); | ||
ButtonElements[^1] = button; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates the buttons for the menu. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected abstract CircleButton[] | ||
CreateButtons(IButtonFactory factory, ref ContextCircleMenuOption menuOption); | ||
|
||
/// <summary> | ||
/// Creates the utility elements for the menu. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected virtual VisualElement[] CreateUtilityElements(ref ContextCircleMenuOption menuOption) | ||
{ | ||
return Array.Empty<VisualElement>(); | ||
} | ||
|
||
/// <summary> | ||
/// Called when the menu is initialized. | ||
/// </summary> | ||
/// <param name="menuOption"></param> | ||
protected virtual void OnInitialized(ref ContextCircleMenuOption menuOption) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Called when the menu is built. | ||
/// </summary> | ||
protected virtual void OnBuild() | ||
{ | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
99 changes: 99 additions & 0 deletions
99
Assets/ContextCircleMenu/Editor/Core/CircleMenus/FolderCircleMenu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace ContextCircleMenu.Editor | ||
{ | ||
/// <inheritdoc /> | ||
public class FolderCircleMenu : CircleMenu | ||
{ | ||
private Vector3[] _buttonPositions; | ||
|
||
public FolderCircleMenu(string path, IMenuControllable menu, | ||
GUIContent icon, | ||
CircleMenu parent, | ||
IButtonFactory factory) : | ||
base(path, icon, null, parent, factory, false) | ||
{ | ||
OnSelected = () => menu.Open(this); | ||
} | ||
|
||
internal FolderCircleMenu(string path, IMenuControllable menu, | ||
CircleMenu parent, | ||
IButtonFactory factory) : | ||
this(path, menu, EditorGUIUtility.IconContent(EditorIcons.FolderIcon), parent, factory) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override CircleButton[] CreateButtons(IButtonFactory factory, ref ContextCircleMenuOption menuOption) | ||
{ | ||
var buttons = new CircleButton[Children.Count]; | ||
for (var index = 0; index < buttons.Length; index++) | ||
{ | ||
var item = Children[index]; | ||
var button = factory.Create( | ||
item.Path, | ||
item.Icon, | ||
item.OnSelected, | ||
Children.Count - index); | ||
button.ShouldCloseMenuAfterSelection = item.ShouldCloseMenuAfterSelection; | ||
buttons[index] = button; | ||
} | ||
|
||
return buttons; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override VisualElement[] CreateUtilityElements(ref ContextCircleMenuOption menuOption) | ||
{ | ||
var label = new Label(Path) | ||
{ | ||
style = | ||
{ | ||
marginBottom = menuOption.Height * 0.5f + 5.0f, | ||
fontSize = 10, | ||
unityTextAlign = TextAnchor.MiddleCenter, | ||
color = Color.white, | ||
textShadow = new TextShadow | ||
{ | ||
offset = new Vector2(0.2f, 0.2f), | ||
blurRadius = 0, | ||
color = Color.black | ||
} | ||
} | ||
}; | ||
return new VisualElement[] { label }; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnInitialized(ref ContextCircleMenuOption menuOption) | ||
{ | ||
var buttonCount = ButtonElements.Length; | ||
_buttonPositions = new Vector3[buttonCount]; | ||
for (var i = 0; i < buttonCount; i++) | ||
_buttonPositions[i] = GetPositionForIndex(i, buttonCount, menuOption.Radius); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnBuild() | ||
{ | ||
for (var i = 0; i < ButtonElements.Length; i++) | ||
{ | ||
var button = ButtonElements[i]; | ||
button.transform.position = Vector3.zero; | ||
var to = _buttonPositions[i]; | ||
button.experimental.animation.Position(to, 100); | ||
} | ||
} | ||
|
||
private Vector3 GetPositionForIndex(float index, float totalCount, float radius) | ||
{ | ||
var angle = index / totalCount * 360f; | ||
return new Vector2( | ||
Mathf.Sin(angle * Mathf.Deg2Rad) * radius, | ||
Mathf.Cos(angle * Mathf.Deg2Rad) * radius | ||
); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
Assets/ContextCircleMenu/Editor/Core/CircleMenus/LeafCircleMenu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace ContextCircleMenu.Editor | ||
{ | ||
/// <inheritdoc /> | ||
public sealed class LeafCircleMenu : CircleMenu | ||
{ | ||
public LeafCircleMenu(string path, GUIContent icon, Action onSelected, IButtonFactory factory, | ||
CircleMenu parent = null) : base(path, icon, onSelected, parent, factory) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override CircleButton[] CreateButtons(IButtonFactory factory, ref ContextCircleMenuOption menuOption) | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.