using System; using System.Collections.Generic; using System.Linq; using UnityExplorer.Helpers; using UnityExplorer.UI; using TMPro; using UnityEngine; using UnityEngine.UI; using UnityExplorer.Input; using UnityExplorer.Unstrip; namespace UnityExplorer.Inspectors.GameObjects { public class GameObjectControls { internal static GameObjectControls Instance; public GameObjectControls() { Instance = this; } private static TMP_InputField s_setParentInput; private static ControlEditor s_positionControl; private static ControlEditor s_localPosControl; private static ControlEditor s_rotationControl; private static ControlEditor s_scaleControl; // Transform Vector editors internal struct ControlEditor { public TMP_InputField fullValue; public Slider[] sliders; public TMP_InputField[] inputs; public Text[] values; } internal static bool s_sliderChangedWanted; private static Slider s_currentSlider; private static ControlType s_currentSliderType; private static VectorValue s_currentSliderValueType; private static float s_currentSliderValue; internal enum ControlType { position, localPosition, eulerAngles, localScale } internal enum VectorValue { x, y, z }; internal void RefreshControls() { var go = GameObjectInspector.ActiveInstance.TargetGO; s_positionControl.fullValue.text = go.transform.position.ToStringLong(); s_positionControl.values[0].text = go.transform.position.x.ToString("F3"); s_positionControl.values[1].text = go.transform.position.y.ToString("F3"); s_positionControl.values[2].text = go.transform.position.z.ToString("F3"); s_localPosControl.fullValue.text = go.transform.localPosition.ToStringLong(); s_localPosControl.values[0].text = go.transform.localPosition.x.ToString("F3"); s_localPosControl.values[1].text = go.transform.localPosition.y.ToString("F3"); s_localPosControl.values[2].text = go.transform.localPosition.z.ToString("F3"); s_rotationControl.fullValue.text = go.transform.eulerAngles.ToStringLong(); s_rotationControl.values[0].text = go.transform.eulerAngles.x.ToString("F3"); s_rotationControl.values[1].text = go.transform.eulerAngles.y.ToString("F3"); s_rotationControl.values[2].text = go.transform.eulerAngles.z.ToString("F3"); s_scaleControl.fullValue.text = go.transform.localScale.ToStringLong(); s_scaleControl.values[0].text = go.transform.localScale.x.ToString("F3"); s_scaleControl.values[1].text = go.transform.localScale.y.ToString("F3"); s_scaleControl.values[2].text = go.transform.localScale.z.ToString("F3"); } internal static void OnSetParentClicked() { var go = GameObjectInspector.ActiveInstance.TargetGO; if (!go) return; var input = s_setParentInput.text; if (string.IsNullOrEmpty(input)) { go.transform.parent = null; } else { if (GameObject.Find(input) is GameObject newParent) { go.transform.parent = newParent.transform; } else { ExplorerCore.Log($"Could not find any GameObject from name or path '{input}'! Note: The target must be enabled."); } } } internal static void OnSliderControlChanged(float value, Slider slider, ControlType controlType, VectorValue vectorValue) { if (value == 0) s_sliderChangedWanted = false; else { if (!s_sliderChangedWanted) { s_sliderChangedWanted = true; s_currentSlider = slider; s_currentSliderType = controlType; s_currentSliderValueType = vectorValue; } s_currentSliderValue = value; } } internal static void UpdateSliderControl() { if (!InputManager.GetMouseButton(0)) { s_sliderChangedWanted = false; s_currentSlider.value = 0; return; } if (GameObjectInspector.ActiveInstance == null) return; var transform = GameObjectInspector.ActiveInstance.TargetGO.transform; // get the current vector for the control type Vector3 vector = Vector2.zero; switch (s_currentSliderType) { case ControlType.position: vector = transform.position; break; case ControlType.localPosition: vector = transform.localPosition; break; case ControlType.eulerAngles: vector = transform.eulerAngles; break; case ControlType.localScale: vector = transform.localScale; break; } // apply vector value change switch (s_currentSliderValueType) { case VectorValue.x: vector.x += s_currentSliderValue; break; case VectorValue.y: vector.y += s_currentSliderValue; break; case VectorValue.z: vector.z += s_currentSliderValue; break; } // set vector to transform member switch (s_currentSliderType) { case ControlType.position: transform.position = vector; break; case ControlType.localPosition: transform.localPosition = vector; break; case ControlType.eulerAngles: transform.eulerAngles = vector; break; case ControlType.localScale: transform.localScale = vector; break; } } internal static void OnVectorControlInputApplied(ControlType controlType, VectorValue vectorValue) { if (!(InspectorManager.Instance.m_activeInspector is GameObjectInspector instance)) return; // get relevant input for controltype + value TMP_InputField[] inputs = null; switch (controlType) { case ControlType.position: inputs = s_positionControl.inputs; break; case ControlType.localPosition: inputs = s_localPosControl.inputs; break; case ControlType.eulerAngles: inputs = s_rotationControl.inputs; break; case ControlType.localScale: inputs = s_scaleControl.inputs; break; } TMP_InputField input = inputs[(int)vectorValue]; float val = float.Parse(input.text); // apply transform value Vector3 vector = Vector3.zero; var transform = instance.TargetGO.transform; switch (controlType) { case ControlType.position: vector = transform.position; break; case ControlType.localPosition: vector = transform.localPosition; break; case ControlType.eulerAngles: vector = transform.eulerAngles; break; case ControlType.localScale: vector = transform.localScale; break; } switch (vectorValue) { case VectorValue.x: vector.x = val; break; case VectorValue.y: vector.y = val; break; case VectorValue.z: vector.z = val; break; } // set back to transform switch (controlType) { case ControlType.position: transform.position = vector; break; case ControlType.localPosition: transform.localPosition = vector; break; case ControlType.eulerAngles: transform.eulerAngles = vector; break; case ControlType.localScale: transform.localScale = vector; break; } } #region UI CONSTRUCTION internal void ConstructControls(GameObject parent) { var controlsObj = UIFactory.CreateVerticalGroup(parent, new Color(0.07f, 0.07f, 0.07f)); var controlsGroup = controlsObj.GetComponent(); controlsGroup.childForceExpandWidth = false; controlsGroup.childControlWidth = true; controlsGroup.childForceExpandHeight = false; controlsGroup.spacing = 5; controlsGroup.padding.top = 4; controlsGroup.padding.left = 4; controlsGroup.padding.right = 4; controlsGroup.padding.bottom = 4; // ~~~~~~ Top row ~~~~~~ var topRow = UIFactory.CreateHorizontalGroup(controlsObj, new Color(1, 1, 1, 0)); var topRowGroup = topRow.GetComponent(); topRowGroup.childForceExpandWidth = false; topRowGroup.childControlWidth = true; topRowGroup.childForceExpandHeight = false; topRowGroup.childControlHeight = true; topRowGroup.spacing = 5; var hideButtonObj = UIFactory.CreateButton(topRow); var hideButton = hideButtonObj.GetComponent