|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using System; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace UnityEditor |
| 9 | +{ |
| 10 | + class SceneViewCameraWindow : PopupWindowContent |
| 11 | + { |
| 12 | + static class Styles |
| 13 | + { |
| 14 | + static bool s_Initialized; |
| 15 | + public static GUIStyle settingsArea; |
| 16 | + |
| 17 | + public static void Init() |
| 18 | + { |
| 19 | + if (s_Initialized) |
| 20 | + return; |
| 21 | + |
| 22 | + s_Initialized = true; |
| 23 | + |
| 24 | + settingsArea = new GUIStyle() |
| 25 | + { |
| 26 | + padding = new RectOffset(4, 4, 4, 4), |
| 27 | + }; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + readonly SceneView m_SceneView; |
| 32 | + |
| 33 | + GUIContent m_CameraSpeedSliderContent; |
| 34 | + GUIContent[] m_CameraSpeedMinMax; |
| 35 | + GUIContent m_FieldOfView; |
| 36 | + |
| 37 | + const int k_FieldCount = 8; |
| 38 | + const int k_WindowWidth = 280; |
| 39 | + const int k_WindowHeight = ((int)EditorGUI.kSingleLineHeight) * k_FieldCount + kFrameWidth * 2; |
| 40 | + const int kFrameWidth = 10; |
| 41 | + const float k_PrefixLabelWidth = 100f; |
| 42 | + const float kMinMaxSpeedLabelWidth = 26f; |
| 43 | + const float k_NearClipMin = .01f; |
| 44 | + |
| 45 | + float[] m_Vector2Floats = { 0, 0 }; |
| 46 | + |
| 47 | + public override Vector2 GetWindowSize() |
| 48 | + { |
| 49 | + return new Vector2(k_WindowWidth, k_WindowHeight); |
| 50 | + } |
| 51 | + |
| 52 | + public SceneViewCameraWindow(SceneView sceneView) |
| 53 | + { |
| 54 | + m_SceneView = sceneView; |
| 55 | + |
| 56 | + m_CameraSpeedSliderContent = EditorGUIUtility.TrTextContent("Speed", "The current speed of the camera in the Scene view."); |
| 57 | + m_CameraSpeedMinMax = new GUIContent[] |
| 58 | + { |
| 59 | + EditorGUIUtility.TrTextContent("Min", "The minimum speed of the camera in the Scene view. Valid values are between [0.01, 98]."), |
| 60 | + EditorGUIUtility.TrTextContent("Max", "The maximum speed of the camera in the Scene view. Valid values are between [0.02, 99].") |
| 61 | + }; |
| 62 | + m_FieldOfView = EditorGUIUtility.TrTextContent("Field of View", "The height of the Camera's view angle. Measured in degrees vertically, or along the local Y axis."); |
| 63 | + } |
| 64 | + |
| 65 | + public override void OnGUI(Rect rect) |
| 66 | + { |
| 67 | + if (m_SceneView == null || m_SceneView.sceneViewState == null) |
| 68 | + return; |
| 69 | + |
| 70 | + Draw(rect); |
| 71 | + |
| 72 | + // Escape closes the window |
| 73 | + if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape) |
| 74 | + { |
| 75 | + editorWindow.Close(); |
| 76 | + GUIUtility.ExitGUI(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void Draw(Rect rect) |
| 81 | + { |
| 82 | + var settings = m_SceneView.sceneViewCameraSettings; |
| 83 | + Styles.Init(); |
| 84 | + |
| 85 | + const int k_SettingsIconPad = 2; |
| 86 | + Vector2 settingsSize = EditorStyles.iconButton.CalcSize(EditorGUI.GUIContents.titleSettingsIcon); |
| 87 | + Rect settingsRect = new Rect(rect.xMax - Styles.settingsArea.padding.right - k_SettingsIconPad - settingsSize.x, Styles.settingsArea.padding.top + k_SettingsIconPad, settingsSize.x, settingsSize.y); |
| 88 | + |
| 89 | + if (GUI.Button(settingsRect, EditorGUI.GUIContents.titleSettingsIcon, EditorStyles.iconButton)) |
| 90 | + ShowContextMenu(); |
| 91 | + |
| 92 | + GUILayout.BeginArea(rect, Styles.settingsArea); |
| 93 | + |
| 94 | + EditorGUI.BeginChangeCheck(); |
| 95 | + |
| 96 | + EditorGUIUtility.labelWidth = k_PrefixLabelWidth; |
| 97 | + |
| 98 | + GUILayout.Label("Camera", EditorStyles.boldLabel); |
| 99 | + |
| 100 | + // fov isn't applicable in orthographic mode, and orthographic size is controlled by the user zoom |
| 101 | + using (new EditorGUI.DisabledScope(m_SceneView.orthographic)) |
| 102 | + { |
| 103 | + settings.fieldOfView = EditorGUILayout.Slider(m_FieldOfView, settings.fieldOfView, 4f, 179f); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + float near = settings.nearClip, far = settings.farClip; |
| 108 | + ClipPlanesField(EditorGUI.s_ClipingPlanesLabel, ref near, ref far, EditorGUI.kNearFarLabelsWidth); |
| 109 | + settings.nearClip = near; |
| 110 | + settings.farClip = far; |
| 111 | + |
| 112 | + settings.nearClip = Mathf.Max(k_NearClipMin, settings.nearClip); |
| 113 | + |
| 114 | + if (settings.nearClip > settings.farClip) |
| 115 | + settings.farClip = settings.nearClip + k_NearClipMin; |
| 116 | + |
| 117 | + if (EditorGUI.EndChangeCheck()) |
| 118 | + m_SceneView.Repaint(); |
| 119 | + |
| 120 | + GUILayout.Label("Navigation", EditorStyles.boldLabel); |
| 121 | + |
| 122 | + settings.speed = EditorGUILayout.Slider(m_CameraSpeedSliderContent, settings.speed, settings.speedMin, settings.speedMax); |
| 123 | + |
| 124 | + EditorGUI.BeginChangeCheck(); |
| 125 | + |
| 126 | + m_Vector2Floats[0] = settings.speedMin; |
| 127 | + m_Vector2Floats[1] = settings.speedMax; |
| 128 | + |
| 129 | + GUILayout.BeginHorizontal(); |
| 130 | + GUILayout.Space(EditorGUIUtility.labelWidth); |
| 131 | + Rect r = EditorGUILayout.GetControlRect(false, EditorGUI.kSingleLineHeight, EditorStyles.numberField); |
| 132 | + EditorGUI.MultiFloatField(r, m_CameraSpeedMinMax, m_Vector2Floats, kMinMaxSpeedLabelWidth); |
| 133 | + GUILayout.EndHorizontal(); |
| 134 | + |
| 135 | + if (EditorGUI.EndChangeCheck()) |
| 136 | + settings.SetSpeedMinMax(m_Vector2Floats); |
| 137 | + |
| 138 | + EditorGUIUtility.labelWidth = 0f; |
| 139 | + |
| 140 | + GUILayout.EndArea(); |
| 141 | + } |
| 142 | + |
| 143 | + internal static void ClipPlanesField(GUIContent label, ref float near, ref float far, float propertyLabelsWidth, params GUILayoutOption[] options) |
| 144 | + { |
| 145 | + bool hasLabel = EditorGUI.LabelHasContent(label); |
| 146 | + const float height = EditorGUI.kSingleLineHeight * 2 + EditorGUI.kVerticalSpacingMultiField; |
| 147 | + Rect r = EditorGUILayout.GetControlRect(hasLabel, height, EditorStyles.numberField, options); |
| 148 | + |
| 149 | + Rect fieldPosition = EditorGUI.PrefixLabel(r, label); |
| 150 | + fieldPosition.height = EditorGUI.kSingleLineHeight; |
| 151 | + |
| 152 | + float oldLabelWidth = EditorGUIUtility.labelWidth; |
| 153 | + int oldIndentLevel = EditorGUI.indentLevel; |
| 154 | + |
| 155 | + EditorGUIUtility.labelWidth = propertyLabelsWidth; |
| 156 | + EditorGUI.indentLevel = 0; |
| 157 | + |
| 158 | + near = EditorGUI.FloatField(fieldPosition, EditorGUI.s_NearAndFarLabels[0], near); |
| 159 | + fieldPosition.y += EditorGUI.kSingleLineHeight + EditorGUI.kVerticalSpacingMultiField; |
| 160 | + far = EditorGUI.FloatField(fieldPosition, EditorGUI.s_NearAndFarLabels[1], far); |
| 161 | + |
| 162 | + EditorGUI.indentLevel = oldIndentLevel; |
| 163 | + EditorGUIUtility.labelWidth = oldLabelWidth; |
| 164 | + } |
| 165 | + |
| 166 | + void ShowContextMenu() |
| 167 | + { |
| 168 | + var menu = new GenericMenu(); |
| 169 | + menu.AddItem(GUIContent.Temp("Reset"), false, Reset); |
| 170 | + menu.ShowAsContext(); |
| 171 | + } |
| 172 | + |
| 173 | + void Reset() |
| 174 | + { |
| 175 | + m_SceneView.ResetSceneViewCameraSettings(); |
| 176 | + m_SceneView.Repaint(); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments