using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
using UnityEngine.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
using System.Linq;
using System.IO;
// MultiCUARescaler v1.0 - hazmhox
//
// Allows you to rescale all CUAs at once
namespace MultiCUARescalerPlugin
{
public class MultiCUARescaler : MVRScript
{
// Statics
private static string PLUGIN_NAME = "Multi CUA Rescaler";
protected List<JSONStorableFloat> allCuaSliders = new List<JSONStorableFloat>();
protected List<UIDynamicSlider> allCuaSlidersUI = new List<UIDynamicSlider>();
protected bool _isSceneLoading { get { return SuperController.singleton.isLoading; } }
public bool isSceneFrozen { get { return SuperController.singleton.freezeAnimation; } }
public bool isInEditMode { get { return SuperController.singleton.gameMode == SuperController.GameMode.Edit; } }
public override void Init()
{
try
{
if (containingAtom.type != "SessionPluginManager")
{
SuperController.singleton.Alert("<b><color=green>"+PLUGIN_NAME+"</color></b>\nSession plugin.\nPlease add it in your session plugins.", () => {});
return;
}
CreateStaticDescriptionText("Title","<color=#000><size=35><b>" + PLUGIN_NAME + "</b>\nThe plugin can rescale all CUA, it's a session plugin and does not save data. It will simply update the CUA scale in the scene</size></color>",true,250,TextAnchor.MiddleLeft);
OnSceneLoaded();
}
catch(Exception e)
{
logError(PLUGIN_NAME + " - Exception caught: " + e);
}
}
void Start()
{
}
void ClearUI()
{
foreach (UIDynamicSlider uids in allCuaSlidersUI)
{
RemoveSlider(uids);
}
allCuaSlidersUI.Clear();
allCuaSliders.Clear();
}
private void OnEnable()
{
SuperController.singleton.onSceneLoadedHandlers += OnSceneLoaded;
}
private void OnDisable()
{
SuperController.singleton.onSceneLoadedHandlers -= OnSceneLoaded;
}
// **************************
// Callbacks
// **************************
private void OnSceneLoaded()
{
ClearUI();
UIDynamicSlider tmpSlider;
foreach (Atom at in SuperController.singleton.GetAtoms())
{
if (at.type == "CustomUnityAsset")
{
JSONStorable scaleVal = at.GetStorableByID("scale");
JSONStorableFloat cuaSizeElem = new JSONStorableFloat(at.name + "_resize", scaleVal.GetFloatParamValue("scale"), 0f, 10f){isStorable=false,isRestorable=false};
cuaSizeElem.setCallbackFunction += (val) => { OnChangeCUASize(at, val); };
tmpSlider = CreateSlider(cuaSizeElem, false);
tmpSlider.valueFormat = "F2";
tmpSlider.label = at.name + " Scale";
allCuaSliders.Add(cuaSizeElem);
allCuaSlidersUI.Add(tmpSlider);
}
}
JSONStorableFloat cuaSizeAll = new JSONStorableFloat("all_resize", 1f, 0f, 10f){isStorable=false,isRestorable=false};
cuaSizeAll.setCallbackFunction += (val) => { OnChangeCUAAllSizes(val); };
tmpSlider = CreateSlider(cuaSizeAll, true);
tmpSlider.valueFormat = "F2";
tmpSlider.label = "Change all CUA Scale";
allCuaSlidersUI.Add(tmpSlider);
}
private void OnChangeCUASize(Atom at, float val)
{
at.GetStorableByID("scale").SetFloatParamValue("scale", val);
}
private void OnChangeCUAAllSizes(float value)
{
foreach (JSONStorableFloat jsf in allCuaSliders)
{
jsf.val = value;
}
}
// **************************
// Time to cleanup !
// **************************
void OnDestroy() {
}
// **************************
// Local Tools
// **************************
private void logDebug( string debugText ) {
SuperController.LogMessage( debugText );
}
private void logError( string debugText ) {
SuperController.LogError( debugText );
}
public UIDynamicTextField CreateStaticDescriptionText(string DescTitle, string DescText, bool rightSide, int fieldHeight, TextAnchor textAlignment = TextAnchor.UpperLeft, bool disableBackground = true, bool disableScroll = true ) {
JSONStorableString staticDescString = new JSONStorableString(DescTitle,DescText) {isStorable=false,isRestorable=false};
staticDescString.hidden = true;
UIDynamicTextField staticDescStringField = CreateTextField(staticDescString, rightSide);
if( disableBackground ) staticDescStringField.backgroundColor = new Color(1f, 1f, 1f, 0f);
staticDescStringField.UItext.alignment = textAlignment;
LayoutElement sdsfLayout = staticDescStringField.GetComponent<LayoutElement>();
sdsfLayout.preferredHeight = sdsfLayout.minHeight = fieldHeight;
staticDescStringField.height = fieldHeight;
if( disableScroll ) DisableScrollOnText(staticDescStringField);
return staticDescStringField;
}
public static void DisableScrollOnText(UIDynamicTextField target) {
// THIS is important, it's a "useless" temporary canvas group that will prevent the event from the mousewheel
CanvasGroup tmpCG = target.UItext.transform.parent.transform.parent.transform.parent.gameObject.AddComponent<CanvasGroup>();
tmpCG.blocksRaycasts = false;
ScrollRect targetSR = target.UItext.transform.parent.transform.parent.transform.parent.GetComponent<ScrollRect>();
if( targetSR != null ) {
targetSR.horizontal = false;
targetSR.vertical = false;
}
}
}
}