MythicalWar/Assets/Invector-3rdPersonController/Basic Locomotion/Scripts/CharacterController/Editor/vGenericInputDrawer.cs

197 lines
9.6 KiB
C#

using System;
using System.Collections;
using UnityEditor;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace Invector.vCharacterController
{
[CustomPropertyDrawer(typeof(GenericInput))]
public class vGenericInputDrawer : PropertyDrawer
{
public static GUISkin skin;
public static GUIContent axisContent;
public static GUIContent invertAxisContent;
public static GUIContent unityInputContent;
public static float heightOpen => ((EditorGUIUtility.singleLineHeight) * 8) + 10;
private static float GetInputSystemHeight(SerializedProperty property)
{
var axisInput = property.FindPropertyRelative("inputSystemAxis");
var lineCount = axisInput != null && axisInput.boolValue ? 7 : 4;
return (EditorGUIUtility.singleLineHeight * lineCount) + 10;
}
const string axisButtonTootip = "IsTriggerAxis?\nConvert Input Axis to Trigger Axis \nThis is usefull if you want to use a axis input like a trigger button.\n \n***Ps. This work only if input is used for GetButton or GetButton(down,up), not if is used for GetAxis";
const string invertAxisButtonTootip = "InvertTriggerAxis?\nIf the Input is an TriggerAxis button you can invert the valid input.\nIf enable the valid axis input is -1 else valid axis input is 1";
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Color color = new Color(1f, 0.549f, 0f );
GUI.color = property.isExpanded ? color : Color.white;
if (axisContent == null)
{
axisContent = new GUIContent(EditorGUIUtility.IconContent("d_MoveTool On@2x"));
axisContent.tooltip = axisButtonTootip;
}
if (invertAxisContent == null)
{
invertAxisContent = new GUIContent(EditorGUIUtility.IconContent("Mirror"));
invertAxisContent.tooltip = invertAxisButtonTootip;
}
if (unityInputContent == null)
{
unityInputContent = new GUIContent();
}
var rect1 = new Rect(position.x, position.y, EditorGUIUtility.singleLineHeight * 2f, EditorGUIUtility.singleLineHeight);
var rect2 = new Rect(position.x + rect1.width, position.y, position.width - rect1.width, EditorGUIUtility.singleLineHeight);
var useInput = property.FindPropertyRelative("useInput");
var inputSource = property.FindPropertyRelative("inputSource");
if (property.isExpanded)
{
var bgRect = position;
bgRect.height = inputSource.enumValueIndex == (int)GenericInputSource.InputSystem ? GetInputSystemHeight(property) : heightOpen;
GUI.Box(bgRect, "", EditorStyles.helpBox);
}
if (useInput != null)
{
useInput.boolValue = GUI.Toggle(rect1, useInput.boolValue, "USE", EditorStyles.miniButtonMid);
if (useInput.boolValue == false)
{
property.isExpanded = false;
}
GUI.enabled = useInput.boolValue;
}
property.isExpanded = GUI.Toggle(rect2, property.isExpanded, property.displayName, EditorStyles.miniButtonMid);
GUI.color = Color.white;
if (property.isExpanded)
{
var keyboard = property.FindPropertyRelative("keyboard");
var joystick = property.FindPropertyRelative("joystick");
var mobile = property.FindPropertyRelative("mobile");
var keyboardAxis = property.FindPropertyRelative("keyboardAxis");
var joystickAxis = property.FindPropertyRelative("joystickAxis");
var mobileAxis = property.FindPropertyRelative("mobileAxis");
var joystickAxisInvert = property.FindPropertyRelative("joystickAxisInvert");
var keyboardAxisInvert = property.FindPropertyRelative("keyboardAxisInvert");
var mobileAxisInvert = property.FindPropertyRelative("mobileAxisInvert");
var isUnityInput = property.FindPropertyRelative("isUnityInput");
EditorGUI.indentLevel++;
var totalRect = position;
totalRect.height = EditorGUIUtility.singleLineHeight;
totalRect.width -= 10;
totalRect.x += 5;
totalRect.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(totalRect, inputSource, new GUIContent("Input Source"));
if (inputSource.enumValueIndex == (int)GenericInputSource.InputSystem)
{
#if ENABLE_INPUT_SYSTEM
DrawProperty(ref totalRect, property.FindPropertyRelative("inputAction"), "Input Action");
DrawProperty(ref totalRect, property.FindPropertyRelative("inputSystemAxis"), "Axis Input");
if (property.FindPropertyRelative("inputSystemAxis").boolValue)
{
DrawProperty(ref totalRect, property.FindPropertyRelative("inputSystemValueComponent"), "Value Component");
DrawProperty(ref totalRect, property.FindPropertyRelative("inputSystemAxisInvert"), "Invert");
DrawProperty(ref totalRect, property.FindPropertyRelative("inputSystemRawThreshold"), "Raw Threshold");
}
#else
totalRect.y += EditorGUIUtility.singleLineHeight;
EditorGUI.HelpBox(totalRect, "Enable the Input System package to assign an action.", MessageType.Warning);
#endif
EditorGUI.indentLevel--;
GUI.enabled = true;
return;
}
DrawInput(ref totalRect, "Mouse Keyboard Input", keyboard, keyboardAxis, keyboardAxisInvert, isUnityInput, true);
DrawInput(ref totalRect, "Joystick Input", joystick, joystickAxis, joystickAxisInvert, null, false);
DrawInput(ref totalRect,"Mobile Input", mobile, mobileAxis, mobileAxisInvert, null, false);
}
GUI.enabled = true;
}
private static void DrawProperty(ref Rect rect, SerializedProperty property, string label)
{
rect.y += EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rect, property, new GUIContent(label));
}
void DrawInput(ref Rect totalRect,string tooltip, SerializedProperty input, SerializedProperty axis, SerializedProperty invert, SerializedProperty isUnityInput = null, bool withKeys = false)
{
totalRect.y += EditorGUIUtility.singleLineHeight ;
GUI.Label(totalRect, tooltip,EditorStyles.miniLabel);
totalRect.y += EditorGUIUtility.singleLineHeight;
var width1 = EditorGUIUtility.singleLineHeight * 1.5f;
var width2 = totalRect.width - (width1 * 3);
var rectA = new Rect(totalRect.x, totalRect.y, width1, totalRect.height);
var rectB = new Rect(totalRect.x + width1, totalRect.y, width2, totalRect.height);
var rectC = new Rect(totalRect.x + width1 + width2, totalRect.y, width1, totalRect.height);
var rectD = new Rect(totalRect.x + (width1) * 2 + width2, totalRect.y, width1, totalRect.height);
var content = unityInputContent;
content.tooltip = (isUnityInput == null || isUnityInput.boolValue) ? "Input is a UnityInput" : "Input is a KeyCode";
content.image = (isUnityInput == null || isUnityInput.boolValue) ? EditorGUIUtility.IconContent("UnityLogo").image : EditorGUIUtility.IconContent("Font Icon").image;
GUI.Box(rectA, content, EditorStyles.miniButton);
DrawInputEnum(input, isUnityInput, rectB, withKeys);
GUI.color = axis.boolValue ? Color.grey : Color.white;
axis.boolValue = GUI.Toggle(rectC, axis.boolValue, axisContent, EditorStyles.miniButton);
GUI.color = invert.boolValue ? Color.grey : Color.white;
GUI.enabled = axis.boolValue;
invert.boolValue = GUI.Toggle(rectD, invert.boolValue, invertAxisContent, EditorStyles.miniButton) && axis.boolValue;
GUI.enabled = true;
GUI.color = Color.white;
}
void DrawInputEnum(SerializedProperty input, SerializedProperty isUnityInput, Rect rect, bool withKeys = false)
{
if (GUI.Button(rect, new GUIContent(input.stringValue), EditorStyles.miniPullDown))
{
PopupWindow.Show(rect, new vGenericInputSelector
("Input for " + input.displayName, input.stringValue, withKeys, isUnityInput == null || isUnityInput.boolValue, (string newInput, bool isKey) =>
{
input.stringValue = newInput;
if (isUnityInput != null)
{
isUnityInput.boolValue = !isKey;
}
input.serializedObject.ApplyModifiedProperties();
input.serializedObject.Update();
}));
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (!property.isExpanded) return EditorGUIUtility.singleLineHeight;
var inputSource = property.FindPropertyRelative("inputSource");
return inputSource.enumValueIndex == (int)GenericInputSource.InputSystem ? GetInputSystemHeight(property) : heightOpen;
}
}
}