public BuilderViewport(BuilderPaneWindow paneWindow, BuilderSelection selection, BuilderElementContextMenu contextMenuManipulator)
        {
            m_PaneWindow             = paneWindow;
            m_Selection              = selection;
            m_ContextMenuManipulator = contextMenuManipulator;

            AddToClassList("unity-builder-viewport");

            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(BuilderConstants.UIBuilderPackagePath + "/BuilderViewport.uxml");

            template.CloneTree(this);

            m_Toolbar             = this.Q("toolbar");
            m_ViewportWrapper     = this.Q("viewport-wrapper");
            m_Viewport            = this.Q("viewport");
            m_Surface             = this.Q("viewport-surface");
            m_Surface.pickingMode = PickingMode.Ignore;
            m_Canvas          = this.Q <BuilderCanvas>("canvas");
            m_Canvas.document = paneWindow.document;
            m_SharedStylesAndDocumentElement = this.Q("shared-styles-and-document");
            m_StyleSelectorElementContainer  = this.Q(BuilderConstants.StyleSelectorElementContainerName);
            m_DocumentElement        = this.Q("document");
            m_Canvas.documentElement = m_DocumentElement;
            m_PickOverlay            = this.Q("pick-overlay");
            m_HighlightOverlay       = this.Q("highlight-overlay");
            m_BuilderParentTracker   = this.Q <BuilderParentTracker>("parent-tracker");
            m_BuilderResizer         = this.Q <BuilderResizer>("resizer");
            m_BuilderMover           = this.Q <BuilderMover>("mover");
            m_BuilderAnchorer        = this.Q <BuilderAnchorer>("anchorer");
            m_BuilderZoomer          = new BuilderZoomer(this);
            m_BuilderPanner          = new BuilderPanner(this);

            m_BuilderMover.parentTracker = m_BuilderParentTracker;

            m_PickOverlay.RegisterCallback <MouseDownEvent>(OnPick);
            m_PickOverlay.RegisterCallback <MouseMoveEvent>(OnHover);
            m_PickOverlay.RegisterCallback <MouseLeaveEvent>(OnMouseLeave);
            m_Viewport.RegisterCallback <MouseDownEvent>(OnMissPick);
            m_Viewport.RegisterCallback <GeometryChangedEvent>(OnGeometryChanged);

            m_ContextMenuManipulator?.RegisterCallbacksOnTarget(m_Viewport);

            // Make sure this gets focus when the pane gets focused.
            primaryFocusable = this;
            focusable        = true;

            // Restore the zoom scale
            zoomScale     = paneWindow.document.viewportZoomScale;
            contentOffset = paneWindow.document.viewportContentOffset;
        }
        public virtual void Deactivate()
        {
            if (m_Target == null)
            {
                return;
            }

            m_Target.UnregisterCallback <GeometryChangedEvent>(OnExternalTargetResize);
            m_Target.UnregisterCallback <DetachFromPanelEvent>(OnTargetDeletion);
            m_Canvas?.UnregisterCallback <GeometryChangedEvent>(OnCanvasResize);

            m_Target = null;
            m_Canvas = null;

            RemoveFromClassList(s_ActiveClassName);
        }
Beispiel #3
0
        public BuilderViewport(BuilderPaneWindow paneWindow, BuilderSelection selection)
        {
            m_PaneWindow = paneWindow;
            m_Selection  = selection;

            AddToClassList("unity-builder-viewport");

            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(BuilderConstants.UIBuilderPackagePath + "/BuilderViewport.uxml");

            template.CloneTree(this);

            m_Toolbar         = this.Q("toolbar");
            m_ViewportWrapper = this.Q("viewport-wrapper");
            m_Viewport        = this.Q("viewport");
            m_Canvas          = this.Q <BuilderCanvas>("canvas");
            m_Canvas.document = paneWindow.document;
            m_SharedStylesAndDocumentElement = this.Q("shared-styles-and-document");
            m_StyleSelectorElementContainer  = this.Q(BuilderConstants.StyleSelectorElementContainerName);
            m_DocumentElement      = this.Q("document");
            m_PickOverlay          = this.Q("pick-overlay");
            m_HighlightOverlay     = this.Q("highlight-overlay");
            m_BuilderParentTracker = this.Q <BuilderParentTracker>("parent-tracker");
            m_BuilderResizer       = this.Q <BuilderResizer>("resizer");
            m_BuilderMover         = this.Q <BuilderMover>("mover");
            m_BuilderAnchorer      = this.Q <BuilderAnchorer>("anchorer");
            m_FitCanvasButton      = this.Q <Button>("fit-canvas-button");

            m_FitCanvasButton.clickable.clicked += FitCanvas;
            m_Canvas.RegisterCallback <GeometryChangedEvent>(VerifyCanvasStillFitsViewport);
            m_Viewport.RegisterCallback <GeometryChangedEvent>(VerifyCanvasStillFitsViewport);

            m_BuilderMover.parentTracker = m_BuilderParentTracker;

            m_PickOverlay.RegisterCallback <MouseDownEvent>(OnPick);
            m_PickOverlay.RegisterCallback <MouseMoveEvent>(OnHover);
            m_PickOverlay.RegisterCallback <MouseLeaveEvent>(OnMouseLeave);
            m_Viewport.RegisterCallback <MouseDownEvent>(OnMissPick);

            // Make sure this gets focus when the pane gets focused.
            primaryFocusable = this;
            this.focusable   = true;
        }
        public virtual void Activate(VisualElement target)
        {
            if (m_Target == target)
            {
                return;
            }

            if (m_Target != null)
            {
                Deactivate();
            }

            if (target == null)
            {
                return;
            }

            m_Target = target;

            AddToClassList(s_ActiveClassName);

            m_Target.RegisterCallback <GeometryChangedEvent>(OnExternalTargetResize);
            m_Target.RegisterCallback <DetachFromPanelEvent>(OnTargetDeletion);

            m_Canvas = m_Target.GetFirstAncestorOfType <BuilderCanvas>();
            m_Canvas.RegisterCallback <GeometryChangedEvent>(OnCanvasResize);

            if (float.IsNaN(m_Target.layout.width))
            {
                m_Target.RegisterCallback <GeometryChangedEvent>(OnInitialStylesResolved);
            }
            else
            {
                SetStylesFromTargetStyles();
                ResizeSelfFromTarget(m_Target.layout);
            }
        }
        public BuilderViewport(BuilderPaneWindow paneWindow, BuilderSelection selection, BuilderElementContextMenu contextMenuManipulator)
        {
            m_PaneWindow             = paneWindow;
            m_Selection              = selection;
            m_ContextMenuManipulator = contextMenuManipulator;

            AddToClassList("unity-builder-viewport");

            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(BuilderConstants.UIBuilderPackagePath + "/BuilderViewport.uxml");

            template.CloneTree(this);

            m_Toolbar             = this.Q("toolbar");
            m_ViewportWrapper     = this.Q("viewport-wrapper");
            m_Viewport            = this.Q("viewport");
            m_Surface             = this.Q("viewport-surface");
            m_Surface.pickingMode = PickingMode.Ignore;
            m_Canvas          = this.Q <BuilderCanvas>("canvas");
            m_Canvas.document = paneWindow.document;
            m_SharedStylesAndDocumentElement = this.Q("shared-styles-and-document");
            m_SharedStylesAndDocumentElement.pseudoStates |= PseudoStates.Root; // To apply variables of the active theme that are defined in the :root selector
            m_StyleSelectorElementContainer = this.Q(BuilderConstants.StyleSelectorElementContainerName);
            m_DocumentElement        = this.Q("document");
            m_Canvas.documentElement = m_DocumentElement;
            m_EditorLayer            = this.Q("__unity-editor-layer");
            m_EditorLayer.AddToClassList(BuilderConstants.HiddenStyleClassName);
            m_TextEditor                = this.Q <TextField>("__unity-text-editor");
            m_Canvas.editorLayer        = m_EditorLayer;
            m_PickOverlay               = this.Q("pick-overlay");
            m_HighlightOverlay          = this.Q("highlight-overlay");
            m_BuilderParentTracker      = this.Q <BuilderParentTracker>("parent-tracker");
            m_BuilderSelectionIndicator = this.Q <BuilderSelectionIndicator>("selection-indicator");
            m_BuilderResizer            = this.Q <BuilderResizer>("resizer");
            m_BuilderMover              = this.Q <BuilderMover>("mover");
            m_BuilderAnchorer           = this.Q <BuilderAnchorer>("anchorer");
            m_BuilderZoomer             = new BuilderZoomer(this);
            m_BuilderPanner             = new BuilderPanner(this);

            m_BuilderMover.parentTracker = m_BuilderParentTracker;

            m_PickOverlay.RegisterCallback <MouseDownEvent>(OnPick);
            m_PickOverlay.RegisterCallback <MouseMoveEvent>(OnHover);
            m_PickOverlay.RegisterCallback <MouseLeaveEvent>(OnMouseLeave);
            m_Viewport.RegisterCallback <MouseDownEvent>(OnMissPick);
            m_Viewport.RegisterCallback <GeometryChangedEvent>(OnGeometryChanged);

            m_Canvas.header.AddManipulator(new Clickable(OnCanvasHeaderClick));
            m_ContextMenuManipulator?.RegisterCallbacksOnTarget(m_Viewport);

            // Make sure this gets focus when the pane gets focused.
            primaryFocusable = this;
            focusable        = true;

            // Restore the zoom scale
            zoomScale     = paneWindow.document.viewportZoomScale;
            contentOffset = paneWindow.document.viewportContentOffset;

            // Repaint bug workaround.
            m_CheckerboardBackground = this.Q <CheckerboardBackground>();
            RegisterCallback <BlurEvent>(e => { m_CheckerboardBackground.MarkDirtyRepaint(); });
            RegisterCallback <FocusEvent>(e => { m_CheckerboardBackground.MarkDirtyRepaint(); });
        }
        public BuilderInspectorCanvas(BuilderInspector inspector)
        {
            m_Inspector       = inspector;
            m_Document        = inspector.document;
            m_CanvasInspector = m_Inspector.Q(ContainerName);

            var builderWindow = inspector.paneWindow as Builder;

            if (builderWindow == null)
            {
                return;
            }

            m_Canvas = builderWindow.canvas;

            m_CameraModeEnabled = false;

            // Size Fields
            m_CanvasWidth = root.Q <IntegerField>("canvas-width");
            m_CanvasWidth.RegisterValueChangedCallback(OnWidthChange);
            m_CanvasHeight = root.Q <IntegerField>("canvas-height");
            m_CanvasHeight.RegisterValueChangedCallback(OnHeightChange);
            m_Canvas.RegisterCallback <GeometryChangedEvent>(OnCanvasSizeChange);

            // This allows user to temporarily type values below the minimum canvas size
            SetDelayOnSizeFieldsEnabled(true);

            // To update the canvas size as user mouse drags the width or height labels
            DisableDelayOnActiveLabelMouseDraggers();

            m_MatchGameViewToggle = root.Q <Toggle>("match-game-view");
            m_MatchGameViewToggle.RegisterValueChangedCallback(OnMatchGameViewModeChanged);
            m_MatchGameViewHelpBox = root.Q <HelpBox>("match-game-view-hint");

            // Background Opacity
            m_ColorOpacityField = root.Q <PercentSlider>("background-color-opacity-field");
            m_ColorOpacityField.RegisterValueChangedCallback(e =>
            {
                settings.ColorModeBackgroundOpacity = e.newValue;
                OnBackgroundOpacityChange(e.newValue);
            });

            m_ImageOpacityField = root.Q <PercentSlider>("background-image-opacity-field");
            m_ImageOpacityField.RegisterValueChangedCallback(e =>
            {
                settings.ImageModeCanvasBackgroundOpacity = e.newValue;
                OnBackgroundOpacityChange(e.newValue);
            });

            m_CameraOpacityField = root.Q <PercentSlider>("background-camera-opacity-field");
            m_CameraOpacityField.RegisterValueChangedCallback(e =>
            {
                settings.CameraModeCanvasBackgroundOpacity = e.newValue;
                OnBackgroundOpacityChange(e.newValue);
            });

            // Setup Background State
            m_BackgroundOptionsFoldout = root.Q <FoldoutWithCheckbox>("canvas-background-foldout");
            m_BackgroundOptionsFoldout.RegisterCheckboxValueChangedCallback(e =>
            {
                settings.EnableCanvasBackground = e.newValue;
                PostSettingsChange();
                ApplyBackgroundOptions();
            });

            // Setup Background Mode
            var backgroundModeType   = typeof(BuilderCanvasBackgroundMode);
            var backgroundModeValues = Enum.GetValues(backgroundModeType)
                                       .OfType <BuilderCanvasBackgroundMode>().Select((v) => v.ToString()).ToList();

            m_BackgroundMode          = root.Q <ToggleButtonStrip>("background-mode-field");
            m_BackgroundMode.enumType = backgroundModeType;
            m_BackgroundMode.choices  = backgroundModeValues;
            m_BackgroundMode.RegisterValueChangedCallback(OnBackgroundModeChange);

            // Color field.
            m_ColorField = root.Q <ColorField>("background-color-field");
            m_ColorField.RegisterValueChangedCallback(OnBackgroundColorChange);

            // Set Image field.
            m_ImageField            = root.Q <ObjectField>("background-image-field");
            m_ImageField.objectType = typeof(Texture2D);
            m_ImageField.RegisterValueChangedCallback(OnBackgroundImageChange);
            m_ImageScaleModeField          = root.Q <ToggleButtonStrip>("background-image-scale-mode-field");
            m_ImageScaleModeField.enumType = typeof(ScaleMode);
            var backgroundScaleModeValues = Enum.GetValues(typeof(ScaleMode))
                                            .OfType <ScaleMode>().Select((v) => BuilderNameUtilities.ConvertCamelToDash(v.ToString())).ToList();

            m_ImageScaleModeField.choices = backgroundScaleModeValues;
            m_ImageScaleModeField.RegisterValueChangedCallback(OnBackgroundImageScaleModeChange);
            m_FitCanvasToImageButton = root.Q <Button>("background-image-fit-canvas-button");
            m_FitCanvasToImageButton.clickable.clicked += FitCanvasToImage;

            // Set Camera field.
            m_CameraField            = root.Q <ObjectField>("background-camera-field");
            m_CameraField.objectType = typeof(Camera);
            m_CameraField.RegisterValueChangedCallback(OnBackgroundCameraChange);

#if !UNITY_2019_4
            SetupEditorExtensionsModeToggle();
#else
            RemoveDocumentSettings();
#endif

            // Control Containers
            m_BackgroundColorModeControls  = root.Q("canvas-background-color-mode-controls");
            m_BackgroundImageModeControls  = root.Q("canvas-background-image-mode-controls");
            m_BackgroundCameraModeControls = root.Q("canvas-background-camera-mode-controls");

            EditorApplication.playModeStateChanged += PlayModeStateChange;
        }
        public BuilderInspectorCanvas(BuilderInspector inspector)
        {
            m_Inspector       = inspector;
            m_Document        = inspector.document;
            m_CanvasInspector = m_Inspector.Q("canvas-inspector");

            var builderWindow = inspector.paneWindow as Builder;

            if (builderWindow == null)
            {
                return;
            }

            m_Canvas = builderWindow.canvas;

            m_CameraModeEnabled = false;

            // Size Fields
            m_CanvasWidth           = root.Q <IntegerField>("canvas-width");
            m_CanvasWidth.isDelayed = true;
            m_CanvasWidth.RegisterValueChangedCallback(OnWidthChange);
            m_CanvasHeight           = root.Q <IntegerField>("canvas-height");
            m_CanvasHeight.isDelayed = true;
            m_CanvasHeight.RegisterValueChangedCallback(OnHeightChange);
            m_Canvas.RegisterCallback <GeometryChangedEvent>(OnCanvasSizeChange);

            // Background Opacity
            m_OpacityField = root.Q <PercentSlider>("background-opacity-field");
            m_OpacityField.RegisterValueChangedCallback(OnBackgroundOpacityChange);

            // Setup Background Mode
            var backgroundModeType   = typeof(BuilderCanvasBackgroundMode);
            var backgroundModeValues = Enum.GetValues(backgroundModeType)
                                       .OfType <BuilderCanvasBackgroundMode>().Select((v) => v.ToString()).ToList();
            var backgroundModeNames = Enum.GetNames(backgroundModeType);

            m_BackgroundMode          = root.Q <ToggleButtonStrip>("background-mode-field");
            m_BackgroundMode.enumType = backgroundModeType;
            m_BackgroundMode.labels   = backgroundModeNames;
            m_BackgroundMode.choices  = backgroundModeValues;
            m_BackgroundMode.RegisterValueChangedCallback(OnBackgroundModeChange);

            // Color field.
            m_ColorField = root.Q <ColorField>("background-color-field");
            m_ColorField.RegisterValueChangedCallback(OnBackgroundColorChange);

            // Set Image field.
            m_ImageField            = root.Q <ObjectField>("background-image-field");
            m_ImageField.objectType = typeof(Texture2D);
            m_ImageField.RegisterValueChangedCallback(OnBackgroundImageChange);
            m_ImageScaleModeField          = root.Q <ToggleButtonStrip>("background-image-scale-mode-field");
            m_ImageScaleModeField.enumType = typeof(ScaleMode);
            var backgroundScaleModeValues = Enum.GetValues(typeof(ScaleMode))
                                            .OfType <ScaleMode>().Select((v) => BuilderNameUtilities.ConvertCamelToDash(v.ToString())).ToList();

            m_ImageScaleModeField.choices = backgroundScaleModeValues;
            m_ImageScaleModeField.RegisterValueChangedCallback(OnBackgroundImageScaleModeChange);
            m_FitCanvasToImageButton = root.Q <Button>("background-image-fit-canvas-button");
            m_FitCanvasToImageButton.clickable.clicked += FitCanvasToImage;

            // Set Camera field.
            m_CameraField            = root.Q <ObjectField>("background-camera-field");
            m_CameraField.objectType = typeof(Camera);
            m_CameraField.RegisterValueChangedCallback(OnBackgroundCameraChange);

            // Control Containers
            m_BackgroundColorModeControls  = root.Q("canvas-background-color-mode-controls");
            m_BackgroundImageModeControls  = root.Q("canvas-background-image-mode-controls");
            m_BackgroundCameraModeControls = root.Q("canvas-background-camera-mode-controls");

            EditorApplication.playModeStateChanged += PlayModeStateChange;
        }