Guest User

Untitled

a guest
Apr 29th, 2022
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.Callbacks;
  6. using UnityEditor.Compilation;
  7. using UnityEngine;
  8.  
  9. public static class FlyoutPopup
  10. {
  11.     private const float _sizeRatio = 0.4f;
  12.  
  13.     private const double _animDuration = 0.25f;
  14.     private static WindowData data = new WindowData();
  15.     private static Dictionary<SceneView, WindowData> _windows = new Dictionary<SceneView, WindowData>();
  16.     private static Type _projectBrowserType = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow)).GetTypes().First(t => t.Name == "ProjectBrowser");
  17.     private static Func<Event, bool> _triggerEvent = e => {
  18. #if UNITY_EDITOR_WIN
  19.         return e.alt && e.keyCode == KeyCode.I;
  20. #else
  21.         return e.control && e.shift && e.keyCode == KeyCode.P;
  22. #endif
  23.     };
  24.     [InitializeOnLoadMethod]
  25.     private static void Init() {
  26.         SceneView.duringSceneGui += OnSceneGUI;
  27.         SceneView.duringSceneGui += OnButtonSceneGUI;
  28.         EditorApplication.update += Update;
  29.         CompilationPipeline.assemblyCompilationStarted += CloseAllWindows;
  30.     }
  31.     private class WindowData {
  32.         public EditorWindow Browser;
  33.         public bool Showing;
  34.         public bool EventActive;
  35.         public double AnimStartTime;
  36.         public Rect Target;
  37.         public Rect StartSize;
  38.  
  39.     }
  40.     private static SceneView currentSceneView;
  41.     private static Event e;
  42.     private static Rect buttonRect;
  43.     private static float lerpValue = 0;
  44.     private static float time = 0;
  45.     [DidReloadScripts]
  46.     private static void ScriptsCompiling() {
  47.         CloseAllWindows(null);
  48.        
  49.     }
  50.     private static void CloseAllWindows(string _) {
  51.         foreach (var window in _windows)
  52.             if (window.Value.Browser != null)
  53.                 window.Value.Browser.Close();
  54.         _windows.Clear();
  55.     }
  56.     private static void Update() {
  57.         if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive) return;
  58.         if (currentSceneView == null) return;        
  59.         if (e == null) return;
  60.  
  61.  
  62.         if (_triggerEvent(e)) {
  63.             if (data?.EventActive ?? false) return;
  64.             GetProjectBrowser(currentSceneView);
  65.  
  66.             switch(e.type) {
  67.             case EventType.KeyUp: {                
  68.                         ShowHidePanel();                      
  69.  
  70.                         break;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         if (data.Browser == null) {
  76.             GetProjectBrowser(currentSceneView);
  77.             return;
  78.         }
  79.  
  80.         var padding = 10;
  81.         var targetY = 242;
  82.         //  FixSize();
  83.         if (time <= 1) {
  84.             time = (float)(EditorApplication.timeSinceStartup - data.AnimStartTime);
  85.             lerpValue = OutBackEase(time, (float)_animDuration);
  86.            
  87.             if (data.Showing) {
  88.                 data.Browser.position = new Rect(data.Target.x + padding, Mathf.Lerp(data.Target.y, data.Target.y - targetY, lerpValue), data.Target.width - padding*2, Mathf.Lerp(20, 250, lerpValue));
  89.             }
  90.             else {
  91.                 data.Browser.minSize = Vector2.zero;
  92.                 data.Browser.position = new Rect(data.Target.x + 0, Mathf.Lerp(data.Target.y - targetY, data.Target.y, lerpValue), data.Target.width - 0*2, Mathf.Lerp(250, 20, lerpValue));
  93.             }
  94.             Debug.Log("lerp");
  95.         }
  96.         else {
  97.             if (data.Showing) {
  98.                 data.Browser.position = new Rect(data.Target.x + padding, data.Target.y - targetY, data.Target.width - padding*2, 250);
  99.             }
  100.             else {
  101.                 data.Browser.minSize = Vector2.zero;
  102.                 data.Browser.position = new Rect(data.Target.x + 0, data.Target.y, data.Target.width - 0*2, 20);
  103.                
  104.             }
  105.             //Debug.Log("no lerp : x: " + data.Target.x + " - width: " + data.Target.width + " - y: " + data.Target.y + " - height: " + data.Target.height);
  106.  
  107.         }
  108.        
  109.  
  110.         data.Browser.Repaint();
  111.     }
  112.     private static float OutBackEase(float time, float duration, float overshoot = 0.25f) {
  113.         return ((time = time / duration - 1) * time * ((overshoot + 1) * time + overshoot) + 1);
  114.     }
  115.     private static void ShowHidePanel() {
  116.         e.Use();
  117.         lerpValue = 0;
  118.         time = 0;
  119.         data.AnimStartTime = EditorApplication.timeSinceStartup;
  120.         data.Showing = !data.Showing;
  121.     }
  122.  
  123.     private static void FixSize() {
  124.         //TODO: get rid of the flicker when changing layout size
  125.         var scenePosition = SceneView.lastActiveSceneView.position;
  126.         scenePosition.y += scenePosition.height;
  127.        // Debug.Log("scene pos y: " + scenePosition.y);
  128.        // Debug.Log("scene pos: " + scenePosition);
  129.         scenePosition.height = 0;
  130.         data.Target = scenePosition;
  131.     }
  132.     private static void OnSceneGUI(SceneView sceneView) {
  133.         if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive) return;
  134.         currentSceneView = sceneView;
  135.         e = Event.current;
  136.         if (e == null) return;
  137.         if (data.Browser == null) return;
  138.         FixSize();
  139.  
  140.        
  141.  
  142.     }
  143.     private static void OnButtonSceneGUI(SceneView sceneView) {
  144.         Handles.BeginGUI();
  145.  
  146.         var scenePosition = sceneView.position;
  147.         var padding = 10;
  148.         var up = 290;
  149.         var down = 45;
  150.         var size = 30;
  151.  
  152.         var heightfix = sceneView.camera.pixelRect.max.y / 3;
  153.         var ypos = sceneView.camera.pixelRect.max.y - heightfix;
  154.  
  155.         if (time <= 1) {          
  156.            
  157.  
  158.             if (data.Showing) {
  159.                
  160.                 buttonRect = new Rect(padding, Mathf.Lerp(ypos - down, ypos - up, lerpValue), size, size);
  161.             }
  162.             else {
  163.                 buttonRect = new Rect(0, Mathf.Lerp(ypos - up, ypos - down, lerpValue), size, size);
  164.             }
  165.            
  166.         }
  167.         else {
  168.             if (data.Showing) {
  169.                
  170.                 buttonRect = new Rect(padding, ypos - up, size, size);
  171.             }
  172.             else {
  173.                
  174.                 //   buttonRect = new Rect(0, down, size, size);
  175.                // Rect rect = new Rect(Screen.width - width, Screen.height - height, width, height);
  176.                 buttonRect = new Rect(0, ypos - down, size, size);
  177.  
  178.             }
  179.  
  180.         }
  181.         GUIStyle style = new GUIStyle(GUI.skin.button);
  182.         style.alignment = TextAnchor.MiddleCenter;
  183.         GUI.skin.button.normal.background = EditorGUIUtility.FindTexture("d_winbtn_mac_close");
  184.         if (data.Showing) {
  185.             GUI.Box(buttonRect, EditorGUIUtility.IconContent("d_scrolldown_uielements@2x"), style);
  186.         }
  187.         else {
  188.             GUI.Box(buttonRect, EditorGUIUtility.IconContent("d_scrollup_uielements@2x"), style);
  189.         }
  190.        
  191.         if (e.type == EventType.MouseDown && buttonRect.Contains(e.mousePosition)) {
  192.             ShowHidePanel();
  193.             Debug.Log("work whore!!");
  194.         }
  195.  
  196.         Handles.EndGUI();
  197.     }
  198.  
  199.     private static WindowData GetProjectBrowser(SceneView sceneView) {
  200.         try {
  201.             if (_windows.Count == 0) {
  202.                 if (data.Browser == null) {
  203.                     data.Browser = ScriptableObject.CreateInstance(_projectBrowserType) as EditorWindow;
  204.                     var scenePosition = SceneView.lastActiveSceneView.position;
  205.                     scenePosition.y += scenePosition.height;
  206.                     scenePosition.height = 0;
  207.                     data.Target = scenePosition;
  208.                     _windows.Add(sceneView, data);
  209.                     data.Browser.ShowPopup();
  210.                     data.Browser.Repaint();
  211.                     data.Showing = false;                    
  212.  
  213.                     Action<SceneView> validate = null;
  214.                     validate = _ => {
  215.                         if (!ValidateBrowser(sceneView, data.Browser))
  216.                             SceneView.beforeSceneGui -= validate;
  217.                     };
  218.                     SceneView.beforeSceneGui += validate;
  219.                     EditorApplication.CallbackFunction validateEditor = null;
  220.                     validateEditor = () => {
  221.                         if (!ValidateBrowser(sceneView, data.Browser))
  222.                             EditorApplication.update -= validateEditor;
  223.                     };
  224.                     EditorApplication.update += validateEditor;
  225.  
  226.                 }
  227.                 return data;
  228.             }
  229.            
  230.         }
  231.         catch (Exception ex) {
  232.             Debug.LogException(ex);
  233.         }
  234.         return null;
  235.     }
  236.     private static bool ValidateBrowser(SceneView sceneView, EditorWindow browser) {
  237.         if (sceneView == null) {
  238.             _windows.Remove(sceneView);
  239.             if (browser != null) {
  240.                 browser.Close();
  241.             }
  242.             return false;
  243.         }
  244.         if (!_windows.ContainsKey(sceneView) && browser != null) {
  245.             browser.Close();
  246.             return false;
  247.         }
  248.         if (browser == null)
  249.             return false;
  250.         return true;
  251.     }
  252. }
  253.  
Add Comment
Please, Sign In to add comment