Skip to content

Commit

Permalink
Hide "default URL" in inspector field when play list system is contro…
Browse files Browse the repository at this point in the history
…lling core
  • Loading branch information
JLChnToZ committed Nov 25, 2023
1 parent 4c81b9b commit 91a88d1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 33 deletions.
109 changes: 77 additions & 32 deletions Packages/idv.jlchntoz.vvmw/Editor/VVMW/CoreEditor.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEditor;
using UdonSharp;
using UdonSharpEditor;
using VRC.SDK3.Video.Components;
using VRC.SDK3.Video.Components.AVPro;

using UnityObject = UnityEngine.Object;
using System.Reflection;

namespace JLChnToZ.VRC.VVMW.Editors {
[CustomEditor(typeof(Core))]
public class CoreEditor : VVMWEditorBase {
static readonly Dictionary<Type, FieldInfo> controllableTypes = new Dictionary<Type, FieldInfo>();
readonly Dictionary<Core, UdonSharpBehaviour> autoPlayControllers = new Dictionary<Core, UdonSharpBehaviour>();
static GUIContent tempContent;
static readonly string[] materialModeOptions = new [] { "Property Block", "Shared Material", "Cloned Materal" };
static GUIContent dropDownIcon;
Expand Down Expand Up @@ -42,6 +49,11 @@ public class CoreEditor : VVMWEditorBase {
List<bool> screenTargetVisibilityState;
bool showTrustUrlList;

static CoreEditor() {
AssemblyReloadEvents.afterAssemblyReload += GatherControlledTypes;
GatherControlledTypes();
}

protected override void OnEnable() {
base.OnEnable();
if (dropDownIcon == null) dropDownIcon = EditorGUIUtility.IconContent("icon dropdown");
Expand Down Expand Up @@ -78,40 +90,14 @@ protected override void OnEnable() {
for (int i = 0, count = screenTargetsProperty.arraySize; i < count; i++)
screenTargetVisibilityState.Add(false);
TrustedUrlUtils.CopyTrustedUrlsToStringArray(trustedUrlDomainsProperty, TrustedUrlTypes.AVProDesktop);
GetControlledTypesOnScene();
}

public override void OnInspectorGUI() {
base.OnInspectorGUI();
if (UdonSharpGUI.DrawDefaultUdonSharpBehaviourHeader(target, false, false)) return;
serializedObject.Update();
int autoPlayPlayerType = autoPlayPlayerTypeProperty.intValue - 1;
bool isAvPro = playerTypes != null && autoPlayPlayerType >= 0 && autoPlayPlayerType < playerTypes.Length && playerTypes[autoPlayPlayerType];
TrustedUrlUtils.DrawUrlField(defaultUrlProperty, isAvPro ? TrustedUrlTypes.AVProDesktop : TrustedUrlTypes.UnityVideo);
if (!string.IsNullOrEmpty(defaultUrlProperty.FindPropertyRelative("url").stringValue)) {
TrustedUrlUtils.DrawUrlField(defaultQuestUrlProperty, isAvPro ? TrustedUrlTypes.AVProAndroid : TrustedUrlTypes.UnityVideo);
if (playerNames == null || playerNames.Length != playerHandlersProperty.arraySize)
playerNames = new string[playerHandlersProperty.arraySize];
if (playerTypes == null || playerTypes.Length != playerHandlersProperty.arraySize)
playerTypes = new bool[playerHandlersProperty.arraySize];
for (int i = 0; i < playerNames.Length; i++) {
var playerHandler = playerHandlersProperty.GetArrayElementAtIndex(i).objectReferenceValue as VideoPlayerHandler;
if (playerHandler == null)
playerNames[i] = "null";
else {
playerNames[i] = string.IsNullOrEmpty(playerHandler.playerName) ? playerHandler.name : playerHandler.playerName;
playerTypes[i] = playerHandler.isAvPro;
}
}
var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
var content = GetTempContent(autoPlayPlayerTypeProperty);
using (new EditorGUI.PropertyScope(rect, content, autoPlayPlayerTypeProperty))
using (var changed = new EditorGUI.ChangeCheckScope()) {
rect = EditorGUI.PrefixLabel(rect, content);
autoPlayPlayerType = EditorGUI.Popup(rect, autoPlayPlayerType, playerNames);
if (changed.changed) autoPlayPlayerTypeProperty.intValue = autoPlayPlayerType + 1;
}
}
EditorGUILayout.PropertyField(loopProperty);
DrawAutoPlayField();
EditorGUILayout.PropertyField(totalRetryCountProperty);
EditorGUILayout.PropertyField(retryDelayProperty);
EditorGUILayout.Space();
Expand Down Expand Up @@ -157,6 +143,42 @@ public override void OnInspectorGUI() {
serializedObject.ApplyModifiedProperties();
}

void DrawAutoPlayField() {
if (autoPlayControllers.TryGetValue(target as Core, out var controller)) {
if (GUILayout.Button($"Edit URLs in {controller.name}"))
Selection.activeGameObject = controller.gameObject;
return;
}
int autoPlayPlayerType = autoPlayPlayerTypeProperty.intValue - 1;
bool isAvPro = playerTypes != null && autoPlayPlayerType >= 0 && autoPlayPlayerType < playerTypes.Length && playerTypes[autoPlayPlayerType];
TrustedUrlUtils.DrawUrlField(defaultUrlProperty, isAvPro ? TrustedUrlTypes.AVProDesktop : TrustedUrlTypes.UnityVideo);
if (!string.IsNullOrEmpty(defaultUrlProperty.FindPropertyRelative("url").stringValue)) {
TrustedUrlUtils.DrawUrlField(defaultQuestUrlProperty, isAvPro ? TrustedUrlTypes.AVProAndroid : TrustedUrlTypes.UnityVideo);
if (playerNames == null || playerNames.Length != playerHandlersProperty.arraySize)
playerNames = new string[playerHandlersProperty.arraySize];
if (playerTypes == null || playerTypes.Length != playerHandlersProperty.arraySize)
playerTypes = new bool[playerHandlersProperty.arraySize];
for (int i = 0; i < playerNames.Length; i++) {
var playerHandler = playerHandlersProperty.GetArrayElementAtIndex(i).objectReferenceValue as VideoPlayerHandler;
if (playerHandler == null)
playerNames[i] = "null";
else {
playerNames[i] = string.IsNullOrEmpty(playerHandler.playerName) ? playerHandler.name : playerHandler.playerName;
playerTypes[i] = playerHandler.isAvPro;
}
}
var rect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
var content = GetTempContent(autoPlayPlayerTypeProperty);
using (new EditorGUI.PropertyScope(rect, content, autoPlayPlayerTypeProperty))
using (var changed = new EditorGUI.ChangeCheckScope()) {
rect = EditorGUI.PrefixLabel(rect, content);
autoPlayPlayerType = EditorGUI.Popup(rect, autoPlayPlayerType, playerNames);
if (changed.changed) autoPlayPlayerTypeProperty.intValue = autoPlayPlayerType + 1;
}
}
EditorGUILayout.PropertyField(loopProperty);
}

void DrawPlayerHandlersListHeader(ref Rect rect) {
GetTempContent("Auto Find");
var miniButtonStyle = EditorStyles.miniButton;
Expand Down Expand Up @@ -353,7 +375,7 @@ void DrawScreenList() {
}
}
using (var changed = new EditorGUI.ChangeCheckScope()) {
var newTarget = EditorGUILayout.ObjectField(GetTempContent("Add Video Screen Target", "Drag renderers, materials, custom render textures, UI raw images here to receive video texture."), null, typeof(Object), true);
var newTarget = EditorGUILayout.ObjectField(GetTempContent("Add Video Screen Target", "Drag renderers, materials, custom render textures, UI raw images here to receive video texture."), null, typeof(UnityObject), true);
if (changed.changed && newTarget != null) {
if (AppendScreen(
newTarget,
Expand Down Expand Up @@ -397,7 +419,7 @@ static void DrawShaderPropertiesField(SerializedProperty property, GUIContent la
}
}

public static bool AddTarget(Core core, Object newTarget, bool recordUndo = true, bool copyToUdon = false) {
public static bool AddTarget(Core core, UnityObject newTarget, bool recordUndo = true, bool copyToUdon = false) {
using (var so = new SerializedObject(core)) {
if (newTarget is AudioSource) {
var audioSourcesProperty = so.FindProperty("audioSources");
Expand All @@ -422,7 +444,7 @@ public static bool AddTarget(Core core, Object newTarget, bool recordUndo = true
}

static bool AppendScreen(
Object newTarget,
UnityObject newTarget,
SerializedProperty screenTargetsProperty,
SerializedProperty screenTargetModesProperty,
SerializedProperty screenTargetIndecesProperty,
Expand Down Expand Up @@ -494,7 +516,7 @@ static GUIContent GetTempContent(string text, string tooltip = "") {
return tempContent;
}

static void AppendElement(SerializedProperty property, Object value) {
static void AppendElement(SerializedProperty property, UnityObject value) {
int size = property.arraySize;
property.arraySize++;
property.GetArrayElementAtIndex(size).objectReferenceValue = value;
Expand All @@ -518,5 +540,28 @@ static void UpdateTrustedUrlList() {
using (var so = new SerializedObject(cores.ToArray()))
TrustedUrlUtils.CopyTrustedUrlsToStringArray(so.FindProperty("trustedUrlDomains"), TrustedUrlTypes.AVProDesktop);
}

static void GatherControlledTypes() {
controllableTypes.Clear();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (var type in assembly.GetTypes()) {
if (type.IsAbstract || !type.IsSubclassOf(typeof(UdonSharpBehaviour))) continue;
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fields.Length == 0) continue;
foreach (var field in fields) {
if (field.FieldType == typeof(Core) && field.GetCustomAttribute<SingletonCoreControlAttribute>() != null) {
controllableTypes[type] = field;
break;
}
}
}
}

void GetControlledTypesOnScene() {
autoPlayControllers.Clear();
foreach (var controller in SceneManager.GetActiveScene().IterateAllComponents<UdonSharpBehaviour>())
if (controllableTypes.TryGetValue(controller.GetType(), out var field) && field.GetValue(controller) is Core coreComponent)
autoPlayControllers[coreComponent] = controller;
}
}
}
2 changes: 2 additions & 0 deletions Packages/idv.jlchntoz.vvmw/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("JLChnToZ.VVMW.Editor")]
11 changes: 11 additions & 0 deletions Packages/idv.jlchntoz.vvmw/Runtime/AssemblyInfo.cs.meta

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

2 changes: 1 addition & 1 deletion Packages/idv.jlchntoz.vvmw/Runtime/VVMW/FrontendHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace JLChnToZ.VRC.VVMW {
[DefaultExecutionOrder(1)]
public class FrontendHandler : UdonSharpEventSender {
protected const byte NONE = 0, REPEAT_ONE = 0x1, REPEAT_ALL = 0x2, SHUFFLE = 0x4;
[SerializeField, Locatable, BindUdonSharpEvent] public Core core;
[SerializeField, Locatable, BindUdonSharpEvent, SingletonCoreControl] public Core core;
[Tooltip("If enabled, while user want to play a video and it is playing other video, the video will be queued. Recommend as this is more polite to everyone.")]
[SerializeField] bool enableQueueList = true;
[Tooltip("Locks the player frontend by default, this option must be used with other scripts to control the player.")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System;

namespace JLChnToZ.VRC.VVMW {
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
internal class SingletonCoreControlAttribute : Attribute {}
}

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

0 comments on commit 91a88d1

Please sign in to comment.