post processing
[WindSway-HDRP.git] / Library / PackageCache / com.unity.render-pipelines.high-definition@4.10.0-preview / Runtime / Core / Debugging / MousePositionDebug.cs
blob896d2f5a3b980dd839a56284ab16e6754746f2ea
1 using System;
2 using UnityEditor;
4 namespace UnityEngine.Experimental.Rendering
6 public class MousePositionDebug
8 // Singleton
9 private static MousePositionDebug s_Instance = null;
11 static public MousePositionDebug instance
13 get
15 if (s_Instance == null)
17 s_Instance = new MousePositionDebug();
20 return s_Instance;
24 public int debugStep
26 get
28 #if UNITY_EDITOR
29 return m_DebugStep;
30 #else
31 return 0;
32 #endif
36 #if UNITY_EDITOR
37 [ExecuteAlways]
38 class GameViewEventCatcher : MonoBehaviour
40 public static GameViewEventCatcher s_Instance = null;
41 public static void Cleanup()
43 if (s_Instance != null)
45 // Either we call DestroyImmediate or Destroy we get an error :(
46 // GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
47 //DestroyImmediate(s_Instance.gameObject);
48 //Destroy(s_Instance.gameObject);
52 public static void Build()
54 Cleanup();
55 var go = new GameObject("__GameViewEventCatcher");
56 go.hideFlags = HideFlags.HideAndDontSave;
57 s_Instance = go.AddComponent<GameViewEventCatcher>();
60 void Update()
62 if (Input.mousePosition.x < 0
63 || Input.mousePosition.y < 0
64 || Input.mousePosition.x > Screen.width
65 || Input.mousePosition.y > Screen.height)
66 return;
68 instance.m_mousePosition = Input.mousePosition;
69 instance.m_mousePosition.y = Screen.height - instance.m_mousePosition.y;
70 if (Input.GetMouseButton(1))
71 instance.m_MouseClickPosition = instance.m_mousePosition;
72 if (Input.GetKey(KeyCode.PageUp))
73 ++instance.m_DebugStep;
74 if (Input.GetKey(KeyCode.PageDown))
75 instance.m_DebugStep = Mathf.Max(0, instance.m_DebugStep - 1);
76 if (Input.GetKey(KeyCode.End))
77 instance.m_MouseClickPosition = instance.m_mousePosition;
81 private Vector2 m_mousePosition = Vector2.zero;
82 Vector2 m_MouseClickPosition = Vector2.zero;
83 int m_DebugStep = 0;
85 private void OnSceneGUI(UnityEditor.SceneView sceneview)
87 m_mousePosition = Event.current.mousePosition;
88 switch (Event.current.type)
90 case EventType.MouseDown:
91 m_MouseClickPosition = m_mousePosition;
92 break;
93 case EventType.KeyDown:
94 switch (Event.current.keyCode)
96 case KeyCode.PageUp:
97 ++m_DebugStep;
98 sceneview.Repaint();
99 break;
100 case KeyCode.PageDown:
101 m_DebugStep = Mathf.Max(0, m_DebugStep - 1);
102 sceneview.Repaint();
103 break;
104 case KeyCode.End:
105 // Usefull we you don't want to change the scene viewport but still update the mouse click position
106 m_MouseClickPosition = m_mousePosition;
107 sceneview.Repaint();
108 break;
110 break;
114 #endif
116 public void Build()
118 #if UNITY_EDITOR
119 #if UNITY_2019_1_OR_NEWER
120 UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;
121 UnityEditor.SceneView.duringSceneGui += OnSceneGUI;
122 #else
123 UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
124 UnityEditor.SceneView.onSceneGUIDelegate += OnSceneGUI;
125 #endif
126 // Disabled as it cause error: GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
127 //GameViewEventCatcher.Build();
128 #endif
131 public void Cleanup()
133 #if UNITY_EDITOR
134 #if UNITY_2019_1_OR_NEWER
135 UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;
136 #else
137 UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
138 #endif
139 // Disabled as it cause error: GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
140 //GameViewEventCatcher.Cleanup();
141 #endif
144 // This function can either return the mouse position in the scene view
145 // or in the game/game view.
146 public Vector2 GetMousePosition(float ScreenHeight, bool sceneView)
148 #if UNITY_EDITOR
149 if (sceneView)
151 // In play mode, m_mousePosition the one in the scene view
152 Vector2 mousePixelCoord = m_mousePosition;
153 mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
154 return mousePixelCoord;
156 else
158 // In play mode, Input.mousecoords matches the position in the game view
159 if (EditorApplication.isPlayingOrWillChangePlaymode)
161 return Input.mousePosition;
163 else
165 // In non-play mode, only m_mousePosition is valid.
166 // We force -1, -1 as a game view pixel pos to avoid
167 // rendering un-wanted effects
168 return new Vector2(-1.0f, -1.0f);
171 #else
172 // In app mode, we only use the Input.mousecoords
173 return Input.mousePosition;
174 #endif
177 public Vector2 GetMouseClickPosition(float ScreenHeight)
179 #if UNITY_EDITOR
180 Vector2 mousePixelCoord = m_MouseClickPosition;
181 mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
182 return mousePixelCoord;
183 #else
184 return Vector2.zero;
185 #endif