Advertisement
chmodseven

StreetViewWindow.cs

Jul 12th, 2020
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.58 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. #region MIT LICENSE
  5.  
  6. /*
  7.     License: The MIT License (MIT)
  8.     Copyright (C) 2020 Shannon Rowe
  9.    
  10.     Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  11.     documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  12.     the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  13.     and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  14.  
  15.     The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  16.     the Software.
  17.    
  18.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  19.     TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20.     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  21.     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.     DEALINGS IN THE SOFTWARE.
  23. */
  24.  
  25. #endregion
  26.  
  27. public class StreetViewWindow : EditorWindow
  28. {
  29.     private const float DefaultImageSize = 400f;
  30.     private const bool DefaultKeepImageSquare = true;
  31.     private const float MinResizableImageSize = 400f;
  32.     private const float MaxResizableImageSize = 2048f;
  33.     private const float ImageMargin = 4f;
  34.     private const float ImageOffsetFromTopForLabel = 40f;
  35.  
  36.     private static Texture _image;
  37.     private static Texture _blankImage;
  38.     private static StreetViewPlayerCamera.LoadChain _loadChain;
  39.     private static Rect _imageRect;
  40.     private static Vector2 _lastWindowSize;
  41.     private static bool _isAdjustingWindowSize;
  42.     private static bool _keepImageSquare;
  43.  
  44.     [MenuItem ("Tools/CityGen3D/StreetView Display", priority = 9999)]
  45.     private static void InitializeFromMenuOption ()
  46.     {
  47.         Initialize (DefaultImageSize, false);
  48.     }
  49.  
  50.     [InitializeOnEnterPlayMode]
  51.     private static void InitializeFromPlayMode ()
  52.     {
  53.         Initialize (DefaultImageSize, true);
  54.     }
  55.  
  56.     private static void Initialize (float imageSize, bool isEnteringPlayMode, bool keepImageSquare = DefaultKeepImageSquare)
  57.     {
  58.         _keepImageSquare = keepImageSquare;
  59.  
  60.         if (isEnteringPlayMode)
  61.         {
  62.             if (!HasOpenInstances<StreetViewWindow> ())
  63.             {
  64.                 return;
  65.             }
  66.         }
  67.  
  68.         // Set expected image size and constrain window with same min/max so it defaults to a given size
  69.         SetImageRectSize (imageSize, imageSize);
  70.         ConstrainWindowRectSize (imageSize, imageSize);
  71.         _blankImage = Texture2D.blackTexture;
  72.  
  73.         // Subscribe to event for setting the image render texture here once it has been loaded in the main script
  74.         _loadChain = new StreetViewPlayerCamera.LoadChain ();
  75.         StreetViewPlayerCamera.onImageLoaded += OnImageLoaded;
  76.  
  77.         // Ensure only one copy of the editor window is present
  78.         if (HasOpenInstances<StreetViewWindow> ())
  79.         {
  80.             // Re-constrain window to let it be resizeable
  81.             ConstrainWindowRectSize (MinResizableImageSize, MaxResizableImageSize);
  82.             return;
  83.         }
  84.  
  85.         StreetViewWindow window = GetWindowReference ();
  86.         window.Show ();
  87.  
  88.         // Re-constrain window to let it be resizeable
  89.         ConstrainWindowRectSize (MinResizableImageSize, MaxResizableImageSize);
  90.     }
  91.  
  92.     private static StreetViewWindow GetWindowReference ()
  93.     {
  94.         // Get the window without changing the focus away from the current window
  95.         return (StreetViewWindow) GetWindow (typeof (StreetViewWindow), false, "StreetView", false);
  96.     }
  97.  
  98.     private static void SetImageRectSize (float imageWidth, float imageHeight)
  99.     {
  100.         _imageRect = new Rect (ImageMargin, ImageOffsetFromTopForLabel, imageWidth, imageHeight);
  101.     }
  102.  
  103.     private static void ConstrainWindowRectSize (float imageMinSize, float imageMaxSize)
  104.     {
  105.         StreetViewWindow window = GetWindowReference ();
  106.  
  107.         // Padding to the same margin on all sides, plus a little extra at the top to make room for the label
  108.         window.minSize = new Vector2 (
  109.             imageMinSize + ImageMargin * 2,
  110.             imageMinSize + ImageMargin * 2 + ImageOffsetFromTopForLabel);
  111.         window.maxSize = new Vector2 (
  112.             imageMaxSize + ImageMargin * 2,
  113.             imageMaxSize + ImageMargin * 2 + ImageOffsetFromTopForLabel);
  114.     }
  115.  
  116.     private static void OnImageLoaded (StreetViewPlayerCamera.LoadChain loadChain)
  117.     {
  118.         // Set the message as either a returned error or the latitude/longitude of a successful load
  119.         _loadChain = loadChain;
  120.         if (loadChain.success)
  121.         {
  122.             _image = !HasOpenInstances<StreetViewWindow> () ? null : loadChain.renderTexture;
  123.         }
  124.         else
  125.         {
  126.             _image = null;
  127.         }
  128.     }
  129.  
  130.     private void OnGUI ()
  131.     {
  132.         if (_loadChain == null)
  133.         {
  134.             return;
  135.         }
  136.  
  137.         GUIStyle labelStyle = new GUIStyle (EditorStyles.label) {richText = true};
  138.  
  139.         if (!_loadChain.success)
  140.         {
  141.             GUILayout.Label ("<color=red>Failed to load panorama: " + _loadChain.message + "</color>", labelStyle);
  142.             if (_blankImage != null)
  143.             {
  144.                 EditorGUI.DrawPreviewTexture (_imageRect, _blankImage, null,
  145.                     _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
  146.             }
  147.  
  148.             return;
  149.         }
  150.  
  151.         if (_image != null && _loadChain != null)
  152.         {
  153.             GUILayout.Label ("Location: <b>" + _loadChain.message + "</b>", labelStyle);
  154.             GUILayout.BeginHorizontal ();
  155.             GUILayout.Label ("Date: <b>" + _loadChain.panorama.date + "</b>; " +
  156.                              "Copyright: <b>" + _loadChain.panorama.copyright + "</b>", labelStyle);
  157.             GUILayout.FlexibleSpace ();
  158.             if (GUILayout.Button ("Copy ID", GUILayout.Width (100)))
  159.             {
  160.                 // These IDs can be loaded into https://istreetview.com/ for web verification
  161.                 EditorGUIUtility.systemCopyBuffer = _loadChain.panorama.pano_id;
  162.             }
  163.  
  164.             GUILayout.EndHorizontal ();
  165.             EditorGUI.DrawPreviewTexture (_imageRect, _image, null,
  166.                 _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
  167.         }
  168.         else if (_blankImage != null)
  169.         {
  170.             GUILayout.Label ("Awaiting panorama load...", labelStyle);
  171.             GUILayout.FlexibleSpace ();
  172.             EditorGUI.DrawPreviewTexture (_imageRect, _blankImage, null,
  173.                 _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
  174.         }
  175.     }
  176.  
  177.     public void Update ()
  178.     {
  179.         StreetViewWindow window = GetWindowReference ();
  180.  
  181.         // Push the resize over to the next frame, to let it redraw properly after the resize
  182.         if (_isAdjustingWindowSize)
  183.         {
  184.             // Derive the new max image size from the window size
  185.             _lastWindowSize = window.position.size;
  186.             float imageWidth = _lastWindowSize.x - ImageMargin * 2;
  187.             float imageHeight = _lastWindowSize.y - ImageMargin * 2 - ImageOffsetFromTopForLabel;
  188.             float maxSide = Mathf.Max (imageWidth, imageHeight);
  189.  
  190.             // Resize the image and containing window accordingly - if shift is held down, keep it square
  191.             SetImageRectSize (imageWidth, imageHeight);
  192.             Rect tempPosition = window.position;
  193.             if (_keepImageSquare)
  194.             {
  195.                 tempPosition.size = new Vector2 (
  196.                     maxSide + ImageMargin * 2,
  197.                     maxSide + ImageMargin * 2 + ImageOffsetFromTopForLabel);
  198.             }
  199.             else
  200.             {
  201.                 tempPosition.size = new Vector2 (
  202.                     imageWidth + ImageMargin * 2,
  203.                     imageHeight + ImageMargin * 2 + ImageOffsetFromTopForLabel);
  204.             }
  205.  
  206.             //window.position = tempPosition;
  207.             _isAdjustingWindowSize = false;
  208.         }
  209.         else if (!window.position.size.Equals (_lastWindowSize))
  210.         {
  211.             _isAdjustingWindowSize = true;
  212.         }
  213.  
  214.         Repaint ();
  215.     }
  216.  
  217.     private void OnDestroy ()
  218.     {
  219.         // ReSharper disable once DelegateSubtraction
  220.         StreetViewPlayerCamera.onImageLoaded -= OnImageLoaded;
  221.         _loadChain = null;
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement