using System.Runtime.CompilerServices;
using Rive.Components;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[assembly: InternalsVisibleTo("Rive.Tests.Editor")]
namespace Rive.EditorTools
{
///
/// Custom menu items for creating Rive components.
///
//Make internals visible to test assembly
internal class MenuItems
{
internal enum PanelContext
{
Standalone = 0,
Canvas = 1
}
[MenuItem("GameObject/Rive/Rive Panel", false, 10)]
static void CreateRivePanel(MenuCommand menuCommand) =>
CreateRivePanelInternal(menuCommand, PanelContext.Standalone);
[MenuItem("GameObject/Rive/Rive Panel (Canvas)", false, 11)]
static void CreateRivePanelWithCanvas(MenuCommand menuCommand) =>
CreateRivePanelInternal(menuCommand, PanelContext.Canvas);
[MenuItem("GameObject/Rive/Widgets/Rive Widget", false, 12)]
static void CreateRiveWidget(MenuCommand menuCommand)
{
GameObject widgetObj = new GameObject("Rive Widget", typeof(RiveWidget));
// If we have a context (selected object), try to parent to it
GameObject parent = menuCommand.context as GameObject;
if (parent != null)
{
GameObjectUtility.SetParentAndAlign(widgetObj, parent);
ConfigureRectTransformToFill(widgetObj.GetComponent());
}
Undo.RegisterCreatedObjectUndo(widgetObj, "Create Rive Widget");
Selection.activeObject = widgetObj;
}
[MenuItem("GameObject/Rive/Widgets/Procedural Rive Widget", false, 13)]
static void CreateProceduralRiveWidget(MenuCommand menuCommand)
{
GameObject widgetObj = new GameObject("Procedural Rive Widget", typeof(ProceduralRiveWidget));
// If we have a context (selected object), try to parent to it
GameObject parent = menuCommand.context as GameObject;
if (parent != null)
{
GameObjectUtility.SetParentAndAlign(widgetObj, parent);
ConfigureRectTransformToFill(widgetObj.GetComponent());
}
Undo.RegisterCreatedObjectUndo(widgetObj, "Create Procedural Rive Widget");
Selection.activeObject = widgetObj;
}
[MenuItem("GameObject/Rive/Render Target Strategies/Atlas Render Target Strategy", false, 21)]
static void CreateAtlasRenderTargetStrategy(MenuCommand menuCommand) =>
CreateRenderTargetStrategy(menuCommand);
[MenuItem("GameObject/Rive/Render Target Strategies/Pooled Render Target Strategy", false, 22)]
static void CreatePooledRenderTargetStrategy(MenuCommand menuCommand) =>
CreateRenderTargetStrategy(menuCommand);
private static void CreateRenderTargetStrategy(MenuCommand menuCommand) where T : RenderTargetStrategy
{
string typeName = typeof(T).Name;
string objectName = ObjectNames.NicifyVariableName(typeName);
GameObject obj = new GameObject(objectName, typeof(T));
GameObjectUtility.SetParentAndAlign(obj, menuCommand.context as GameObject);
Undo.RegisterCreatedObjectUndo(obj, $"Create {objectName}");
Selection.activeObject = obj;
}
internal static RivePanel CreateRivePanelInternal(MenuCommand menuCommand, PanelContext context)
{
GameObject rootObject;
GameObject panelObj;
if (context == PanelContext.Canvas)
{
// Check if we already have a canvas parent
Canvas parentCanvas = null;
GameObject contextObj = menuCommand.context as GameObject;
if (contextObj != null)
{
parentCanvas = contextObj.GetComponentInParent