SpherePointGUI.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace Funly.SkyStudio
  7. {
  8. public abstract class SpherePointGUI
  9. {
  10. // ID of the owner of the selection data.
  11. private static string m_ActiveReceiver;
  12. private static bool m_SelectionResultReady;
  13. private static SpherePoint m_SelectedSpherePoint;
  14. // Handles allowing user to make a selection in the scene view for an sphere point position.
  15. public static void RenderSpherePointSceneSelection()
  16. {
  17. if (m_ActiveReceiver == null || m_SelectionResultReady)
  18. {
  19. return;
  20. }
  21. Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  22. Vector3 worldPoint = ray.GetPoint(.01f);
  23. Handles.DrawWireDisc(worldPoint, ray.direction * -1.0f, HandleUtility.GetHandleSize(worldPoint));
  24. if (Event.current.keyCode == KeyCode.Escape)
  25. {
  26. CancelSpherePointSceneSelection();
  27. return;
  28. }
  29. // Ignore click if some special key is pressed for navigation reasons.
  30. if (Event.current.alt || Event.current.control || Event.current.command)
  31. {
  32. return;
  33. }
  34. if (Event.current.type != EventType.MouseDown)
  35. {
  36. return;
  37. }
  38. m_SelectedSpherePoint = new SpherePoint(ray.direction);
  39. m_SelectionResultReady = true;
  40. Event.current.Use();
  41. GUIUtility.hotControl = 0;
  42. }
  43. public static void CancelSpherePointSceneSelection()
  44. {
  45. m_ActiveReceiver = null;
  46. }
  47. // GUI for a sphere point selection.
  48. public static SpherePoint SpherePointField(SpherePoint spherePoint, bool sceneSelection, string controlId)
  49. {
  50. bool isActive = IsActiveToken(controlId);
  51. SpherePoint selectedSpherePoint = spherePoint;
  52. // Horizontal rotation layout.
  53. EditorGUILayout.BeginVertical();
  54. EditorGUILayout.BeginHorizontal();
  55. EditorGUI.BeginChangeCheck();
  56. float selectedHorizontalValue = EditorGUILayout.Slider("Horizontal", spherePoint.horizontalRotation,
  57. SpherePoint.MinHorizontalRotation, SpherePoint.MaxHorizontalRotation);
  58. if (EditorGUI.EndChangeCheck()) {
  59. selectedSpherePoint = new SpherePoint(selectedHorizontalValue, spherePoint.verticalRotation);
  60. GUI.changed = true;
  61. }
  62. EditorGUILayout.EndHorizontal();
  63. // Vertical rotation layout.
  64. EditorGUILayout.BeginHorizontal();
  65. EditorGUI.BeginChangeCheck();
  66. float selectedVerticalValue = EditorGUILayout.Slider("Vertical", spherePoint.verticalRotation,
  67. SpherePoint.MinVerticalRotation, SpherePoint.MaxVerticalRotation);
  68. if (EditorGUI.EndChangeCheck()) {
  69. selectedSpherePoint = new SpherePoint(spherePoint.horizontalRotation, selectedVerticalValue);
  70. GUI.changed = true;
  71. }
  72. EditorGUILayout.EndHorizontal();
  73. if (sceneSelection)
  74. {
  75. RenderSpherePointSelectionButton(controlId);
  76. }
  77. EditorGUILayout.EndVertical();
  78. // Check if a selection has completed for this control.
  79. if (isActive && m_SelectionResultReady)
  80. {
  81. selectedSpherePoint = m_SelectedSpherePoint;
  82. m_SelectionResultReady = false;
  83. m_ActiveReceiver = null;
  84. GUI.changed = true;
  85. }
  86. return selectedSpherePoint;
  87. }
  88. private static void RenderSpherePointSelectionButton(string controlId)
  89. {
  90. bool isActive = IsActiveToken(controlId);
  91. EditorGUI.BeginChangeCheck();
  92. string buttonTitle;
  93. GUIStyle btnStyle = new GUIStyle(GUI.skin.button);
  94. if (isActive)
  95. {
  96. buttonTitle = "Click in Scene View...";
  97. btnStyle.normal.textColor = Color.red;
  98. }
  99. else
  100. {
  101. buttonTitle = "Position With Cursor...";
  102. btnStyle.normal.textColor = GUI.skin.button.normal.textColor;
  103. }
  104. EditorGUILayout.BeginHorizontal();
  105. EditorGUILayout.PrefixLabel("Positional Visually");
  106. bool isClicked = GUILayout.Button(new GUIContent(buttonTitle), btnStyle);
  107. if (EditorGUI.EndChangeCheck()) {
  108. CancelSpherePointSceneSelection();
  109. if (!isActive && isClicked) {
  110. m_ActiveReceiver = controlId;
  111. }
  112. }
  113. EditorGUILayout.EndHorizontal();
  114. }
  115. private static bool IsActiveToken(string token)
  116. {
  117. return token != null && m_ActiveReceiver != null && token == m_ActiveReceiver;
  118. }
  119. }
  120. }