Initial Commit

This commit is contained in:
Sebastian Cabrera 2021-08-02 05:44:37 -04:00
parent 53eb92e9af
commit 270ab7d11f
15341 changed files with 700234 additions and 0 deletions

View file

@ -0,0 +1,172 @@
using System;
using Unity.Cloud.Collaborate.Assets;
using Unity.Cloud.Collaborate.UserInterface;
using Unity.Cloud.Collaborate.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Components.Menus
{
/// <summary>
/// Global element that is used to display dialogues throughout the window.
/// </summary>
internal class FloatingDialogue : VisualElement
{
/// <summary>
/// USS class name of elements of this type.
/// </summary>
public const string UssClassName = "unity-floating-dialogue";
/// <summary>
/// Location the uss file for this element is stored.
/// </summary>
static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(FloatingDialogue)}.uss";
/// <summary>
/// Backing field for the singleton.
/// </summary>
static FloatingDialogue s_Instance;
/// <summary>
/// Singleton for accessing the global FloatingDialogue
/// </summary>
public static FloatingDialogue Instance => s_Instance ?? (s_Instance = new FloatingDialogue());
/// <summary>
/// Constructor used by the singleton.
/// </summary>
FloatingDialogue()
{
AddToClassList(UssClassName);
AddToClassList(UiConstants.ussHidden);
styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
}
/// <summary>
/// Allows focus to be set when the window opens. Allows for the options to be next up for tab.
/// </summary>
public override bool canGrabFocus => true;
/// <summary>
/// Set the position of the dialogue.
/// </summary>
/// <param name="x">World x coordinate.</param>
/// <param name="y">World y coordinate.</param>
/// <param name="content">Content of the window.</param>
/// <param name="openDirection">Direction to open the dialogue towards.</param>
void SetPosition(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection)
{
// Correct for the top left corner of the element based on the direction it opens.
switch (openDirection)
{
case MenuUtilities.OpenDirection.UpLeft:
x -= content.worldBound.width;
y -= content.worldBound.height;
break;
case MenuUtilities.OpenDirection.UpRight:
y -= content.worldBound.height;
break;
case MenuUtilities.OpenDirection.DownLeft:
x -= content.worldBound.width;
break;
case MenuUtilities.OpenDirection.DownRight:
break;
default:
throw new ArgumentOutOfRangeException(nameof(openDirection), openDirection, null);
}
// Set the position.
style.top = y - parent.worldBound.yMin;
style.left = x - parent.worldBound.xMin;
style.right = new StyleLength(StyleKeyword.Auto);
style.bottom = new StyleLength(StyleKeyword.Auto);
}
/// <summary>
/// Open the dialogue.
/// </summary>
/// <param name="x">World x coordinate.</param>
/// <param name="y">World y coordinate.</param>
/// <param name="content">Content to display.</param>
/// <param name="openDirection">Direction to open the dialogue towards.</param>
internal void Open(float x, float y, VisualElement content, MenuUtilities.OpenDirection openDirection = MenuUtilities.OpenDirection.DownLeft)
{
// Set new content
Clear();
Add(content);
// Set visible and give focus
RemoveFromClassList(UiConstants.ussHidden);
Focus();
BringToFront();
// Catch scrolling to avoid menu becoming detached.
parent.RegisterCallback<WheelEvent>(OnScroll, TrickleDown.TrickleDown);
// Catch clicks outside of the dialogue and close it.
parent.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
// Catch window size changes so that the menu position can be fixed.
parent.RegisterCallback<GeometryChangedEvent>(OnGeometryChange, TrickleDown.TrickleDown);
content.RegisterCallback<GeometryChangedEvent>(SizeKnownCallback);
void SizeKnownCallback(GeometryChangedEvent _)
{
// Unregister now that we know it has a size.
content.UnregisterCallback<GeometryChangedEvent>(SizeKnownCallback);
// Set the position in the window
SetPosition(x, y, content, openDirection);
}
}
/// <summary>
/// Close the dialogue.
/// </summary>
internal void Close()
{
AddToClassList(UiConstants.ussHidden);
Clear();
// Avoid wasting extra cycles when closed.
parent.UnregisterCallback<WheelEvent>(OnScroll, TrickleDown.TrickleDown);
parent.UnregisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown);
}
/// <summary>
/// On scroll event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnScroll(WheelEvent evt)
{
// Close the window if the user scrolls outside the dialogue.
if (!worldBound.Contains(evt.mousePosition))
{
Close();
}
}
/// <summary>
/// Mouse down event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnMouseDown(MouseDownEvent evt)
{
// Close the window if the user clicks outside the dialogue.
if (!worldBound.Contains(evt.mousePosition))
{
Close();
}
}
/// <summary>
/// Geometry changed event.
/// </summary>
/// <param name="evt">Event data.</param>
void OnGeometryChange(GeometryChangedEvent evt)
{
// Close to avoid the dialogue getting detached.
Close();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b1607da2b0288db44acdeed2ddaf0962
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Unity.Cloud.Collaborate.UserInterface;
using Unity.Cloud.Collaborate.Utilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Components.Menus
{
[UsedImplicitly]
internal class FloatingMenu
{
public const string ussClassName = "unity-floating-menu";
// Fields used to display the option list.
const float k_ItemHeight = 28f;
readonly List<(string Text, Action Action, bool Enabled)> m_Items;
/// <summary>
/// Location the uss file for this element is stored.
/// </summary>
static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(FloatingMenu)}.uss";
/// <summary>
/// Container for the menu items.
/// </summary>
readonly VisualElement m_Content;
/// <summary>
/// Direction to open the menu.
/// </summary>
MenuUtilities.OpenDirection m_OpenDirection = MenuUtilities.OpenDirection.DownLeft;
/// <summary>
/// Create a new floating menu. Follows the builder pattern.
/// </summary>
public FloatingMenu()
{
m_Items = new List<(string Text, Action Action, bool Enabled)>();
m_Content = new VisualElement();
m_Content.AddToClassList(ussClassName);
m_Content.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
}
/// <summary>
/// Add a single option to the menu.
/// </summary>
/// <param name="text">Text in the option.</param>
/// <param name="action">Action to invoke on click.</param>
/// <param name="enabled">State of the entry.</param>
/// <returns>This.</returns>
public FloatingMenu AddEntry(string text, Action action, bool enabled)
{
m_Items.Add((text, action, enabled));
return this;
}
/// <summary>
/// Add a list of entries.
/// </summary>
/// <param name="items">Entries to add.</param>
/// <returns>This.</returns>
public FloatingMenu AddEntries(IEnumerable<(string Text, Action Action, bool Enabled)> items)
{
m_Items.AddRange(items);
return this;
}
/// <summary>
/// Sets the open direction of the menu.
/// </summary>
/// <param name="openDirection">Direction the menu opens towards.</param>
/// <returns>This.</returns>
public FloatingMenu SetOpenDirection(MenuUtilities.OpenDirection openDirection)
{
m_OpenDirection = openDirection;
return this;
}
/// <summary>
/// Opens the constructed menu.
/// </summary>
/// <param name="x">World x coordinate.</param>
/// <param name="y">World y coordinate.</param>
public void Open(float x, float y)
{
FloatingDialogue.Instance.Open(x, y, GenerateContent(), m_OpenDirection);
}
/// <summary>
/// Generate the visual element that displays the content of this menu.
/// </summary>
/// <returns>The constructed visual element.</returns>
VisualElement GenerateContent()
{
m_Content.Clear();
foreach (var item in m_Items)
{
// Ensure the dialogue closes once the option is selected
void Action()
{
FloatingDialogue.Instance.Close();
item.Action();
}
var elem = new FloatingMenuItem(item.Text, Action, item.Enabled, k_ItemHeight);
m_Content.Add(elem);
}
return m_Content;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3ec4bfe3e4b9e8479f84284679cb851
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,55 @@
using System;
using Unity.Cloud.Collaborate.UserInterface;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Components.Menus
{
internal class FloatingMenuItem : VisualElement
{
const string k_UssClassName = "unity-floating-menu-item";
/// <summary>
/// Location the uss file for this element is stored.
/// </summary>
static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(FloatingMenuItem)}.uss";
public override bool canGrabFocus { get; } = true;
readonly Action m_Action;
public FloatingMenuItem(string text, Action action, bool enabled, float height)
{
AddToClassList(k_UssClassName);
styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
m_Action = action;
focusable = true;
this.AddManipulator(new Clickable(OnExec));
SetEnabled(enabled);
Add(new Label(text));
style.height = height;
}
void OnExec()
{
m_Action();
}
/// <summary>
/// Catch the enter key event to allow for tab & enter UI navigation.
/// </summary>
/// <param name="evt">Event to check.</param>
protected override void ExecuteDefaultActionAtTarget(EventBase evt)
{
base.ExecuteDefaultActionAtTarget(evt);
// Catch enter key being pressed.
if (!(evt is KeyDownEvent downEvent)) return;
if (downEvent.keyCode != KeyCode.KeypadEnter && downEvent.keyCode != KeyCode.Return) return;
OnExec();
downEvent.StopPropagation();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1beb42c8af804da40a4ab5490b3d61d6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: