|
| 1 | +using System.Collections.Generic; |
| 2 | +using UnityEditor.Experimental.EditorVR.Core; |
| 3 | +using UnityEditor.Experimental.EditorVR.Modules; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace UnityEditor.Experimental.EditorVR.UI |
| 7 | +{ |
| 8 | + sealed class HapticPulseEditor : EditorWindow |
| 9 | + { |
| 10 | + class Pulse |
| 11 | + { |
| 12 | + public HapticPulse pulse; |
| 13 | + public SerializedObject serializedObject; |
| 14 | + public SerializedProperty duration; |
| 15 | + public SerializedProperty intensity; |
| 16 | + } |
| 17 | + |
| 18 | + readonly List<Pulse> m_HapticPulses = new List<Pulse>(); |
| 19 | + |
| 20 | + Vector2 m_Scroll; |
| 21 | + float m_Multiplier = 1; |
| 22 | + |
| 23 | + [MenuItem("Edit/Project Settings/EditorVR/Haptic Pulses")] |
| 24 | + static void Init() |
| 25 | + { |
| 26 | + GetWindow<HapticPulseEditor>("Haptic Pulse Editor").Show(); |
| 27 | + } |
| 28 | + |
| 29 | + void OnEnable() |
| 30 | + { |
| 31 | + Reset(); |
| 32 | + |
| 33 | + Undo.undoRedoPerformed += OnUndoRedo; |
| 34 | + } |
| 35 | + |
| 36 | + void Reset() |
| 37 | + { |
| 38 | + m_HapticPulses.Clear(); |
| 39 | + var pulses = AssetDatabase.FindAssets("t:HapticPulse"); |
| 40 | + foreach (var guid in pulses) |
| 41 | + { |
| 42 | + var pulse = AssetDatabase.LoadAssetAtPath<HapticPulse>(AssetDatabase.GUIDToAssetPath(guid)); |
| 43 | + if (pulse) |
| 44 | + { |
| 45 | + var serializedObject = new SerializedObject(pulse); |
| 46 | + m_HapticPulses.Add(new Pulse |
| 47 | + { |
| 48 | + pulse = pulse, |
| 49 | + serializedObject = serializedObject, |
| 50 | + duration = serializedObject.FindProperty("m_Duration"), |
| 51 | + intensity = serializedObject.FindProperty("m_Intensity") |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + void OnDisable() |
| 58 | + { |
| 59 | + Undo.undoRedoPerformed -= OnUndoRedo; |
| 60 | + } |
| 61 | + |
| 62 | + void OnGUI() |
| 63 | + { |
| 64 | + if (Event.current.Equals(Event.KeyboardEvent("^w"))) |
| 65 | + { |
| 66 | + Close(); |
| 67 | + GUIUtility.ExitGUI(); |
| 68 | + } |
| 69 | + |
| 70 | + const float nameColumnWidth = 250f; |
| 71 | + const float floatFieldColumnWidth = 60f; |
| 72 | + |
| 73 | + m_Scroll = GUILayout.BeginScrollView(m_Scroll); |
| 74 | + |
| 75 | + GUILayout.BeginHorizontal(); |
| 76 | + m_Multiplier = EditorGUILayout.FloatField("Multiplier", m_Multiplier); |
| 77 | + if (GUILayout.Button("Multiply Intensity")) |
| 78 | + { |
| 79 | + foreach (var pulse in m_HapticPulses) |
| 80 | + { |
| 81 | + pulse.intensity.floatValue *= m_Multiplier; |
| 82 | + pulse.serializedObject.ApplyModifiedProperties(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (GUILayout.Button("Multiply Duration")) |
| 87 | + { |
| 88 | + foreach (var pulse in m_HapticPulses) |
| 89 | + { |
| 90 | + pulse.duration.floatValue *= m_Multiplier; |
| 91 | + pulse.serializedObject.ApplyModifiedProperties(); |
| 92 | + } |
| 93 | + } |
| 94 | + GUILayout.EndHorizontal(); |
| 95 | + |
| 96 | + GUILayout.BeginHorizontal(); |
| 97 | + GUILayout.Label("Haptic Pulses", EditorStyles.boldLabel, GUILayout.Width(nameColumnWidth)); |
| 98 | + GUILayout.Label("Duration"); |
| 99 | + GUILayout.Label("Intensity"); |
| 100 | + GUILayout.EndHorizontal(); |
| 101 | + |
| 102 | + foreach (var pulse in m_HapticPulses) |
| 103 | + { |
| 104 | + GUILayout.BeginHorizontal(); |
| 105 | + EditorGUILayout.ObjectField(pulse.pulse, typeof(HapticPulse), false, GUILayout.Width(nameColumnWidth)); |
| 106 | + EditorGUI.BeginChangeCheck(); |
| 107 | + var durationProperty = pulse.duration; |
| 108 | + durationProperty.floatValue = GUILayout.HorizontalSlider(durationProperty.floatValue, 0, HapticsModule.MaxDuration); |
| 109 | + durationProperty.floatValue = EditorGUILayout.FloatField(durationProperty.floatValue, GUILayout.Width(floatFieldColumnWidth)); |
| 110 | + durationProperty.floatValue = Mathf.Clamp(durationProperty.floatValue, 0, HapticsModule.MaxDuration); |
| 111 | + var intensityProperty = pulse.intensity; |
| 112 | + intensityProperty.floatValue = GUILayout.HorizontalSlider(intensityProperty.floatValue, 0, 1); |
| 113 | + intensityProperty.floatValue = EditorGUILayout.FloatField(intensityProperty.floatValue, GUILayout.Width(floatFieldColumnWidth)); |
| 114 | + intensityProperty.floatValue = Mathf.Clamp01(intensityProperty.floatValue); |
| 115 | + if (EditorGUI.EndChangeCheck()) |
| 116 | + pulse.serializedObject.ApplyModifiedProperties(); |
| 117 | + |
| 118 | + GUILayout.EndHorizontal(); |
| 119 | + } |
| 120 | + |
| 121 | + GUILayout.EndScrollView(); |
| 122 | + } |
| 123 | + |
| 124 | + void OnUndoRedo() |
| 125 | + { |
| 126 | + Reset(); |
| 127 | + Repaint(); |
| 128 | + } |
| 129 | + |
| 130 | + void OnProjectChange() |
| 131 | + { |
| 132 | + Reset(); |
| 133 | + Repaint(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments