123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- namespace Funly.SkyStudio
- {
- public abstract class SpherePointGUI
- {
- // ID of the owner of the selection data.
- private static string m_ActiveReceiver;
- private static bool m_SelectionResultReady;
- private static SpherePoint m_SelectedSpherePoint;
- // Handles allowing user to make a selection in the scene view for an sphere point position.
- public static void RenderSpherePointSceneSelection()
- {
- if (m_ActiveReceiver == null || m_SelectionResultReady)
- {
- return;
- }
- Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
- Vector3 worldPoint = ray.GetPoint(.01f);
- Handles.DrawWireDisc(worldPoint, ray.direction * -1.0f, HandleUtility.GetHandleSize(worldPoint));
-
- if (Event.current.keyCode == KeyCode.Escape)
- {
- CancelSpherePointSceneSelection();
- return;
- }
- // Ignore click if some special key is pressed for navigation reasons.
- if (Event.current.alt || Event.current.control || Event.current.command)
- {
- return;
- }
- if (Event.current.type != EventType.MouseDown)
- {
- return;
- }
- m_SelectedSpherePoint = new SpherePoint(ray.direction);
- m_SelectionResultReady = true;
- Event.current.Use();
- GUIUtility.hotControl = 0;
- }
- public static void CancelSpherePointSceneSelection()
- {
- m_ActiveReceiver = null;
- }
- // GUI for a sphere point selection.
- public static SpherePoint SpherePointField(SpherePoint spherePoint, bool sceneSelection, string controlId)
- {
- bool isActive = IsActiveToken(controlId);
- SpherePoint selectedSpherePoint = spherePoint;
- // Horizontal rotation layout.
- EditorGUILayout.BeginVertical();
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginChangeCheck();
- float selectedHorizontalValue = EditorGUILayout.Slider("Horizontal", spherePoint.horizontalRotation,
- SpherePoint.MinHorizontalRotation, SpherePoint.MaxHorizontalRotation);
- if (EditorGUI.EndChangeCheck()) {
- selectedSpherePoint = new SpherePoint(selectedHorizontalValue, spherePoint.verticalRotation);
- GUI.changed = true;
- }
- EditorGUILayout.EndHorizontal();
- // Vertical rotation layout.
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginChangeCheck();
- float selectedVerticalValue = EditorGUILayout.Slider("Vertical", spherePoint.verticalRotation,
- SpherePoint.MinVerticalRotation, SpherePoint.MaxVerticalRotation);
- if (EditorGUI.EndChangeCheck()) {
- selectedSpherePoint = new SpherePoint(spherePoint.horizontalRotation, selectedVerticalValue);
- GUI.changed = true;
- }
- EditorGUILayout.EndHorizontal();
- if (sceneSelection)
- {
- RenderSpherePointSelectionButton(controlId);
- }
- EditorGUILayout.EndVertical();
- // Check if a selection has completed for this control.
- if (isActive && m_SelectionResultReady)
- {
- selectedSpherePoint = m_SelectedSpherePoint;
- m_SelectionResultReady = false;
- m_ActiveReceiver = null;
- GUI.changed = true;
- }
- return selectedSpherePoint;
- }
- private static void RenderSpherePointSelectionButton(string controlId)
- {
- bool isActive = IsActiveToken(controlId);
- EditorGUI.BeginChangeCheck();
- string buttonTitle;
- GUIStyle btnStyle = new GUIStyle(GUI.skin.button);
- if (isActive)
- {
- buttonTitle = "Click in Scene View...";
- btnStyle.normal.textColor = Color.red;
- }
- else
- {
- buttonTitle = "Position With Cursor...";
- btnStyle.normal.textColor = GUI.skin.button.normal.textColor;
- }
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PrefixLabel("Positional Visually");
- bool isClicked = GUILayout.Button(new GUIContent(buttonTitle), btnStyle);
- if (EditorGUI.EndChangeCheck()) {
- CancelSpherePointSceneSelection();
- if (!isActive && isClicked) {
- m_ActiveReceiver = controlId;
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- private static bool IsActiveToken(string token)
- {
- return token != null && m_ActiveReceiver != null && token == m_ActiveReceiver;
- }
- }
- }
|