Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using UnityEngine;
- #region MIT LICENSE
- /*
- License: The MIT License (MIT)
- Copyright (C) 2020 Shannon Rowe
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- documentation files (the "Software"), to deal in the Software without restriction, including without limitation
- the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
- and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of
- the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
- CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- DEALINGS IN THE SOFTWARE.
- */
- #endregion
- public class StreetViewWindow : EditorWindow
- {
- private const float DefaultImageSize = 400f;
- private const bool DefaultKeepImageSquare = true;
- private const float MinResizableImageSize = 400f;
- private const float MaxResizableImageSize = 2048f;
- private const float ImageMargin = 4f;
- private const float ImageOffsetFromTopForLabel = 40f;
- private static Texture _image;
- private static Texture _blankImage;
- private static StreetViewPlayerCamera.LoadChain _loadChain;
- private static Rect _imageRect;
- private static Vector2 _lastWindowSize;
- private static bool _isAdjustingWindowSize;
- private static bool _keepImageSquare;
- [MenuItem ("Tools/CityGen3D/StreetView Display", priority = 9999)]
- private static void InitializeFromMenuOption ()
- {
- Initialize (DefaultImageSize, false);
- }
- [InitializeOnEnterPlayMode]
- private static void InitializeFromPlayMode ()
- {
- Initialize (DefaultImageSize, true);
- }
- private static void Initialize (float imageSize, bool isEnteringPlayMode, bool keepImageSquare = DefaultKeepImageSquare)
- {
- _keepImageSquare = keepImageSquare;
- if (isEnteringPlayMode)
- {
- if (!HasOpenInstances<StreetViewWindow> ())
- {
- return;
- }
- }
- // Set expected image size and constrain window with same min/max so it defaults to a given size
- SetImageRectSize (imageSize, imageSize);
- ConstrainWindowRectSize (imageSize, imageSize);
- _blankImage = Texture2D.blackTexture;
- // Subscribe to event for setting the image render texture here once it has been loaded in the main script
- _loadChain = new StreetViewPlayerCamera.LoadChain ();
- StreetViewPlayerCamera.onImageLoaded += OnImageLoaded;
- // Ensure only one copy of the editor window is present
- if (HasOpenInstances<StreetViewWindow> ())
- {
- // Re-constrain window to let it be resizeable
- ConstrainWindowRectSize (MinResizableImageSize, MaxResizableImageSize);
- return;
- }
- StreetViewWindow window = GetWindowReference ();
- window.Show ();
- // Re-constrain window to let it be resizeable
- ConstrainWindowRectSize (MinResizableImageSize, MaxResizableImageSize);
- }
- private static StreetViewWindow GetWindowReference ()
- {
- // Get the window without changing the focus away from the current window
- return (StreetViewWindow) GetWindow (typeof (StreetViewWindow), false, "StreetView", false);
- }
- private static void SetImageRectSize (float imageWidth, float imageHeight)
- {
- _imageRect = new Rect (ImageMargin, ImageOffsetFromTopForLabel, imageWidth, imageHeight);
- }
- private static void ConstrainWindowRectSize (float imageMinSize, float imageMaxSize)
- {
- StreetViewWindow window = GetWindowReference ();
- // Padding to the same margin on all sides, plus a little extra at the top to make room for the label
- window.minSize = new Vector2 (
- imageMinSize + ImageMargin * 2,
- imageMinSize + ImageMargin * 2 + ImageOffsetFromTopForLabel);
- window.maxSize = new Vector2 (
- imageMaxSize + ImageMargin * 2,
- imageMaxSize + ImageMargin * 2 + ImageOffsetFromTopForLabel);
- }
- private static void OnImageLoaded (StreetViewPlayerCamera.LoadChain loadChain)
- {
- // Set the message as either a returned error or the latitude/longitude of a successful load
- _loadChain = loadChain;
- if (loadChain.success)
- {
- _image = !HasOpenInstances<StreetViewWindow> () ? null : loadChain.renderTexture;
- }
- else
- {
- _image = null;
- }
- }
- private void OnGUI ()
- {
- if (_loadChain == null)
- {
- return;
- }
- GUIStyle labelStyle = new GUIStyle (EditorStyles.label) {richText = true};
- if (!_loadChain.success)
- {
- GUILayout.Label ("<color=red>Failed to load panorama: " + _loadChain.message + "</color>", labelStyle);
- if (_blankImage != null)
- {
- EditorGUI.DrawPreviewTexture (_imageRect, _blankImage, null,
- _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
- }
- return;
- }
- if (_image != null && _loadChain != null)
- {
- GUILayout.Label ("Location: <b>" + _loadChain.message + "</b>", labelStyle);
- GUILayout.BeginHorizontal ();
- GUILayout.Label ("Date: <b>" + _loadChain.panorama.date + "</b>; " +
- "Copyright: <b>" + _loadChain.panorama.copyright + "</b>", labelStyle);
- GUILayout.FlexibleSpace ();
- if (GUILayout.Button ("Copy ID", GUILayout.Width (100)))
- {
- // These IDs can be loaded into https://istreetview.com/ for web verification
- EditorGUIUtility.systemCopyBuffer = _loadChain.panorama.pano_id;
- }
- GUILayout.EndHorizontal ();
- EditorGUI.DrawPreviewTexture (_imageRect, _image, null,
- _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
- }
- else if (_blankImage != null)
- {
- GUILayout.Label ("Awaiting panorama load...", labelStyle);
- GUILayout.FlexibleSpace ();
- EditorGUI.DrawPreviewTexture (_imageRect, _blankImage, null,
- _keepImageSquare ? ScaleMode.ScaleToFit : ScaleMode.StretchToFill);
- }
- }
- public void Update ()
- {
- StreetViewWindow window = GetWindowReference ();
- // Push the resize over to the next frame, to let it redraw properly after the resize
- if (_isAdjustingWindowSize)
- {
- // Derive the new max image size from the window size
- _lastWindowSize = window.position.size;
- float imageWidth = _lastWindowSize.x - ImageMargin * 2;
- float imageHeight = _lastWindowSize.y - ImageMargin * 2 - ImageOffsetFromTopForLabel;
- float maxSide = Mathf.Max (imageWidth, imageHeight);
- // Resize the image and containing window accordingly - if shift is held down, keep it square
- SetImageRectSize (imageWidth, imageHeight);
- Rect tempPosition = window.position;
- if (_keepImageSquare)
- {
- tempPosition.size = new Vector2 (
- maxSide + ImageMargin * 2,
- maxSide + ImageMargin * 2 + ImageOffsetFromTopForLabel);
- }
- else
- {
- tempPosition.size = new Vector2 (
- imageWidth + ImageMargin * 2,
- imageHeight + ImageMargin * 2 + ImageOffsetFromTopForLabel);
- }
- //window.position = tempPosition;
- _isAdjustingWindowSize = false;
- }
- else if (!window.position.size.Equals (_lastWindowSize))
- {
- _isAdjustingWindowSize = true;
- }
- Repaint ();
- }
- private void OnDestroy ()
- {
- // ReSharper disable once DelegateSubtraction
- StreetViewPlayerCamera.onImageLoaded -= OnImageLoaded;
- _loadChain = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement