CinemachineFramingTransposerEditor.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using UnityEngine;
  2. using UnityEditor;
  3. using Cinemachine.Utility;
  4. using System.Collections.Generic;
  5. namespace Cinemachine.Editor
  6. {
  7. [CustomEditor(typeof(CinemachineFramingTransposer))]
  8. internal class CinemachineFramingTransposerEditor : BaseEditor<CinemachineFramingTransposer>
  9. {
  10. CinemachineScreenComposerGuides mScreenGuideEditor;
  11. GameViewEventCatcher mGameViewEventCatcher;
  12. /// <summary>Get the property names to exclude in the inspector.</summary>
  13. /// <param name="excluded">Add the names to this list</param>
  14. protected override void GetExcludedPropertiesInInspector(List<string> excluded)
  15. {
  16. base.GetExcludedPropertiesInInspector(excluded);
  17. if (Target.m_UnlimitedSoftZone)
  18. {
  19. excluded.Add(FieldPath(x => x.m_SoftZoneWidth));
  20. excluded.Add(FieldPath(x => x.m_SoftZoneHeight));
  21. excluded.Add(FieldPath(x => x.m_BiasX));
  22. excluded.Add(FieldPath(x => x.m_BiasY));
  23. }
  24. ICinemachineTargetGroup group = Target.AbstractFollowTargetGroup;
  25. if (group == null || Target.m_GroupFramingMode == CinemachineFramingTransposer.FramingMode.None)
  26. {
  27. excluded.Add(FieldPath(x => x.m_GroupFramingSize));
  28. excluded.Add(FieldPath(x => x.m_AdjustmentMode));
  29. excluded.Add(FieldPath(x => x.m_MaxDollyIn));
  30. excluded.Add(FieldPath(x => x.m_MaxDollyOut));
  31. excluded.Add(FieldPath(x => x.m_MinimumDistance));
  32. excluded.Add(FieldPath(x => x.m_MaximumDistance));
  33. excluded.Add(FieldPath(x => x.m_MinimumFOV));
  34. excluded.Add(FieldPath(x => x.m_MaximumFOV));
  35. excluded.Add(FieldPath(x => x.m_MinimumOrthoSize));
  36. excluded.Add(FieldPath(x => x.m_MaximumOrthoSize));
  37. if (group == null)
  38. excluded.Add(FieldPath(x => x.m_GroupFramingMode));
  39. }
  40. else
  41. {
  42. CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(Target.VirtualCamera);
  43. bool ortho = brain != null ? brain.OutputCamera.orthographic : false;
  44. if (ortho)
  45. {
  46. excluded.Add(FieldPath(x => x.m_AdjustmentMode));
  47. excluded.Add(FieldPath(x => x.m_MaxDollyIn));
  48. excluded.Add(FieldPath(x => x.m_MaxDollyOut));
  49. excluded.Add(FieldPath(x => x.m_MinimumDistance));
  50. excluded.Add(FieldPath(x => x.m_MaximumDistance));
  51. excluded.Add(FieldPath(x => x.m_MinimumFOV));
  52. excluded.Add(FieldPath(x => x.m_MaximumFOV));
  53. }
  54. else
  55. {
  56. excluded.Add(FieldPath(x => x.m_MinimumOrthoSize));
  57. excluded.Add(FieldPath(x => x.m_MaximumOrthoSize));
  58. switch (Target.m_AdjustmentMode)
  59. {
  60. case CinemachineFramingTransposer.AdjustmentMode.DollyOnly:
  61. excluded.Add(FieldPath(x => x.m_MinimumFOV));
  62. excluded.Add(FieldPath(x => x.m_MaximumFOV));
  63. break;
  64. case CinemachineFramingTransposer.AdjustmentMode.ZoomOnly:
  65. excluded.Add(FieldPath(x => x.m_MaxDollyIn));
  66. excluded.Add(FieldPath(x => x.m_MaxDollyOut));
  67. excluded.Add(FieldPath(x => x.m_MinimumDistance));
  68. excluded.Add(FieldPath(x => x.m_MaximumDistance));
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. protected virtual void OnEnable()
  77. {
  78. mScreenGuideEditor = new CinemachineScreenComposerGuides();
  79. mScreenGuideEditor.GetHardGuide = () => { return Target.HardGuideRect; };
  80. mScreenGuideEditor.GetSoftGuide = () => { return Target.SoftGuideRect; };
  81. mScreenGuideEditor.SetHardGuide = (Rect r) => { Target.HardGuideRect = r; };
  82. mScreenGuideEditor.SetSoftGuide = (Rect r) => { Target.SoftGuideRect = r; };
  83. mScreenGuideEditor.Target = () => { return serializedObject; };
  84. mGameViewEventCatcher = new GameViewEventCatcher();
  85. mGameViewEventCatcher.OnEnable();
  86. CinemachineDebug.OnGUIHandlers -= OnGUI;
  87. CinemachineDebug.OnGUIHandlers += OnGUI;
  88. if (CinemachineSettings.CinemachineCoreSettings.ShowInGameGuides)
  89. InspectorUtility.RepaintGameView();
  90. }
  91. protected virtual void OnDisable()
  92. {
  93. mGameViewEventCatcher.OnDisable();
  94. CinemachineDebug.OnGUIHandlers -= OnGUI;
  95. if (CinemachineSettings.CinemachineCoreSettings.ShowInGameGuides)
  96. InspectorUtility.RepaintGameView();
  97. }
  98. public override void OnInspectorGUI()
  99. {
  100. BeginInspector();
  101. if (Target.FollowTarget == null)
  102. EditorGUILayout.HelpBox(
  103. "Framing Transposer requires a Follow target. Change Body to Do Nothing if you don't want a Follow target.",
  104. MessageType.Warning);
  105. // First snapshot some settings
  106. Rect oldHard = Target.HardGuideRect;
  107. Rect oldSoft = Target.SoftGuideRect;
  108. // Draw the properties
  109. DrawRemainingPropertiesInInspector();
  110. mScreenGuideEditor.SetNewBounds(oldHard, oldSoft, Target.HardGuideRect, Target.SoftGuideRect);
  111. }
  112. /// Process a position drag from the user.
  113. /// Called "magically" by the vcam editor, so don't change the signature.
  114. public void OnVcamPositionDragged(Vector3 delta)
  115. {
  116. if (Target.FollowTarget != null)
  117. {
  118. Undo.RegisterCompleteObjectUndo(Target, "Camera drag");
  119. var fwd = Target.transform.forward;
  120. var zComponent = Vector3.Dot(fwd, delta);
  121. delta -= fwd * zComponent;
  122. Vector3 localOffset = Quaternion.Inverse(Target.FollowTarget.rotation) * delta;
  123. Target.m_TrackedObjectOffset += localOffset;
  124. Target.m_CameraDistance = Mathf.Max(0.01f, Target.m_CameraDistance - zComponent);
  125. }
  126. }
  127. protected virtual void OnGUI()
  128. {
  129. if (Target == null)
  130. return;
  131. // Draw the camera guides
  132. if (!Target.IsValid || !CinemachineSettings.CinemachineCoreSettings.ShowInGameGuides)
  133. return;
  134. CinemachineBrain brain = CinemachineCore.Instance.FindPotentialTargetBrain(Target.VirtualCamera);
  135. if (brain == null || (brain.OutputCamera.activeTexture != null && CinemachineCore.Instance.BrainCount > 1))
  136. return;
  137. bool isLive = CinemachineCore.Instance.IsLive(Target.VirtualCamera);
  138. // Screen guides
  139. mScreenGuideEditor.OnGUI_DrawGuides(isLive, brain.OutputCamera, Target.VcamState.Lens, !Target.m_UnlimitedSoftZone);
  140. // Draw an on-screen gizmo for the target
  141. if (Target.FollowTarget != null && isLive)
  142. {
  143. Vector3 targetScreenPosition = brain.OutputCamera.WorldToScreenPoint(Target.TrackedPoint);
  144. if (targetScreenPosition.z > 0)
  145. {
  146. targetScreenPosition.y = Screen.height - targetScreenPosition.y;
  147. GUI.color = CinemachineSettings.ComposerSettings.TargetColour;
  148. Rect r = new Rect(targetScreenPosition, Vector2.zero);
  149. float size = (CinemachineSettings.ComposerSettings.TargetSize
  150. + CinemachineScreenComposerGuides.kGuideBarWidthPx) / 2;
  151. GUI.DrawTexture(r.Inflated(new Vector2(size, size)), Texture2D.whiteTexture);
  152. size -= CinemachineScreenComposerGuides.kGuideBarWidthPx;
  153. if (size > 0)
  154. {
  155. Vector4 overlayOpacityScalar
  156. = new Vector4(1f, 1f, 1f, CinemachineSettings.ComposerSettings.OverlayOpacity);
  157. GUI.color = Color.black * overlayOpacityScalar;
  158. GUI.DrawTexture(r.Inflated(new Vector2(size, size)), Texture2D.whiteTexture);
  159. }
  160. }
  161. }
  162. }
  163. [DrawGizmo(GizmoType.Active | GizmoType.InSelectionHierarchy, typeof(CinemachineFramingTransposer))]
  164. private static void DrawGroupComposerGizmos(CinemachineFramingTransposer target, GizmoType selectionType)
  165. {
  166. // Show the group bounding box, as viewed from the camera position
  167. if (target.AbstractFollowTargetGroup != null
  168. && target.m_GroupFramingMode != CinemachineFramingTransposer.FramingMode.None)
  169. {
  170. Matrix4x4 m = Gizmos.matrix;
  171. Bounds b = target.LastBounds;
  172. Gizmos.matrix = target.LastBoundsMatrix;
  173. Gizmos.color = Color.yellow;
  174. if (target.VcamState.Lens.Orthographic)
  175. Gizmos.DrawWireCube(b.center, b.size);
  176. else
  177. {
  178. float z = b.center.z;
  179. Vector3 e = b.extents;
  180. Gizmos.DrawFrustum(
  181. Vector3.zero,
  182. Mathf.Atan2(e.y, z) * Mathf.Rad2Deg * 2,
  183. z + e.z, z - e.z, e.x / e.y);
  184. }
  185. Gizmos.matrix = m;
  186. }
  187. }
  188. }
  189. }