/// <summary>
        /// Triggered when the user double-clicked on the entry.
        /// </summary>
        /// <param name="path">Project library path of the double-clicked entry.</param>
        private void OnEntryDoubleClicked(string path)
        {
            delayedSelect = false;

            LibraryEntry entry = ProjectLibrary.GetEntry(path);

            if (entry != null)
            {
                if (entry.Type == LibraryEntryType.Directory)
                {
                    owner.Window.EnterDirectory(path);
                }
                else
                {
                    ResourceMeta meta = ProjectLibrary.GetMeta(path);

                    FileEntry fileEntry = (FileEntry)entry;
                    if (meta.ResType == ResourceType.Prefab)
                    {
                        EditorApplication.LoadScene(fileEntry.Path);
                    }
                    else if (meta.ResType == ResourceType.ScriptCode)
                    {
                        ProgressBar.Show("Opening external code editor...", 1.0f);

                        delayedOpenCodeEditorFrame = Time.FrameIdx + 1;
                    }
                    else if (meta.ResType == ResourceType.PlainText || meta.ResType == ResourceType.Shader || meta.ResType == ResourceType.ShaderInclude)
                    {
                        string absPath = Path.Combine(ProjectLibrary.ResourceFolder, fileEntry.Path);
                        Process.Start(absPath);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Triggered by the runtime when a resource is dropped on the scene tree view.
        /// </summary>
        private void Internal_DoOnResourceDropped(SceneObject parent, string[] resourcePaths)
        {
            if (resourcePaths == null)
            {
                return;
            }

            List <SceneObject> addedObjects = new List <SceneObject>();

            for (int i = 0; i < resourcePaths.Length; i++)
            {
                ResourceMeta meta = ProjectLibrary.GetMeta(resourcePaths[i]);
                if (meta == null)
                {
                    continue;
                }

                if (meta.ResType == ResourceType.Mesh)
                {
                    if (!string.IsNullOrEmpty(resourcePaths[i]))
                    {
                        string meshName = Path.GetFileNameWithoutExtension(resourcePaths[i]);

                        Mesh mesh = ProjectLibrary.Load <Mesh>(resourcePaths[i]);
                        if (mesh == null)
                        {
                            continue;
                        }

                        SceneObject so = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                        so.Parent = parent;

                        Renderable renderable = so.AddComponent <Renderable>();
                        renderable.Mesh = mesh;

                        addedObjects.Add(so);
                    }
                }
                else if (meta.ResType == ResourceType.Prefab)
                {
                    if (!string.IsNullOrEmpty(resourcePaths[i]))
                    {
                        Prefab      prefab = ProjectLibrary.Load <Prefab>(resourcePaths[i]);
                        SceneObject so     = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
                        so.Parent = parent;

                        addedObjects.Add(so);
                    }
                }
            }

            if (addedObjects.Count > 0)
            {
                EditorApplication.SetSceneDirty();
            }

            Selection.SceneObjects = addedObjects.ToArray();
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new material parameter GUI.
        /// </summary>
        /// <param name="shaderParam">Shader parameter to create the GUI for. Must be of texture type.</param>
        /// <param name="material">Material the parameter is a part of.</param>
        /// <param name="layout">Layout to append the GUI elements to.</param>
        internal MaterialParamTextureGUI(ShaderParameter shaderParam, Material material, GUILayout layout)
            : base(shaderParam)
        {
            LocString title = new LocEdString(shaderParam.name);

            GUITextureFieldType type = shaderParam.type == ShaderParameterType.Texture2D ?
                                       GUITextureFieldType.TextureOrSpriteTexture :
                                       GUITextureFieldType.Texture;

            guiElem = new GUITextureField(type, title);

            switch (shaderParam.type)
            {
            case ShaderParameterType.Texture2D:
            case ShaderParameterType.Texture3D:
            case ShaderParameterType.TextureCube:
                guiElem.OnChanged += (x) =>
                {
                    string path = ProjectLibrary.GetPath(x.UUID);
                    if (!string.IsNullOrEmpty(path))
                    {
                        if (ProjectLibrary.GetEntry(path) is FileEntry fileEntry)
                        {
                            if (fileEntry.ResourceMetas.Length > 0)
                            {
                                ResourceMeta meta = fileEntry.ResourceMetas[0];
                                if (meta.ResType == ResourceType.SpriteTexture)
                                {
                                    material.SetSpriteTexture(shaderParam.name, x.As <SpriteTexture>());
                                }
                                else if (meta.ResType == ResourceType.Texture)
                                {
                                    material.SetTexture(shaderParam.name, x.As <Texture>());
                                }
                            }
                        }
                    }
                    else
                    {
                        material.SetTexture(shaderParam.name, null);
                    }

                    EditorApplication.SetDirty(material);
                };
                break;
            }

            layout.AddElement(guiElem);
        }
Beispiel #4
0
        private void OnEditorUpdate()
        {
            if (currentType == InspectorType.SceneObject)
            {
                Component[] allComponents   = activeSO.GetComponents();
                bool        requiresRebuild = allComponents.Length != inspectorComponents.Count;

                if (!requiresRebuild)
                {
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        if (inspectorComponents[i].instanceId != allComponents[i].InstanceId)
                        {
                            requiresRebuild = true;
                            break;
                        }
                    }
                }

                if (requiresRebuild)
                {
                    SceneObject so = activeSO;
                    Clear();
                    SetObjectToInspect(so);
                }
                else
                {
                    RefreshSceneObjectFields(false);

                    InspectableState componentModifyState = InspectableState.NotModified;
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        componentModifyState |= inspectorComponents[i].inspector.Refresh();
                    }

                    if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
                    {
                        EditorApplication.SetSceneDirty();
                    }

                    modifyState |= componentModifyState;
                }
            }
            else if (currentType == InspectorType.Resource)
            {
                inspectorResource.inspector.Refresh();
            }

            // Detect drag and drop
            bool isValidDrag = false;

            if (activeSO != null)
            {
                if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
                {
                    Vector2I windowPos     = ScreenToWindowPos(Input.PointerPosition);
                    Vector2I scrollPos     = windowPos;
                    Rect2I   contentBounds = inspectorLayout.Bounds;
                    scrollPos.x -= contentBounds.x;
                    scrollPos.y -= contentBounds.y;

                    bool   isInBounds = false;
                    Rect2I dropArea   = new Rect2I();
                    foreach (var bounds in dropAreas)
                    {
                        if (bounds.Contains(scrollPos))
                        {
                            isInBounds = true;
                            dropArea   = bounds;
                            break;
                        }
                    }

                    Type draggedComponentType = null;
                    if (isInBounds)
                    {
                        ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
                        if (dragData != null)
                        {
                            foreach (var resPath in dragData.Paths)
                            {
                                ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
                                if (meta != null)
                                {
                                    if (meta.ResType == ResourceType.ScriptCode)
                                    {
                                        ScriptCode scriptFile = ProjectLibrary.Load <ScriptCode>(resPath);

                                        if (scriptFile != null)
                                        {
                                            Type[] scriptTypes = scriptFile.Types;
                                            foreach (var type in scriptTypes)
                                            {
                                                if (type.IsSubclassOf(typeof(Component)))
                                                {
                                                    draggedComponentType = type;
                                                    isValidDrag          = true;
                                                    break;
                                                }
                                            }

                                            if (draggedComponentType != null)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (isValidDrag)
                    {
                        scrollAreaHighlight.Bounds = dropArea;

                        if (DragDrop.DropInProgress)
                        {
                            activeSO.AddComponent(draggedComponentType);

                            modifyState = InspectableState.Modified;
                            EditorApplication.SetSceneDirty();
                        }
                    }
                }
            }

            if (scrollAreaHighlight != null)
            {
                scrollAreaHighlight.Active = isValidDrag;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Sets a resource whose GUI is to be displayed in the inspector. Clears any previous contents of the window.
        /// </summary>
        /// <param name="resourcePath">Resource path relative to the project of the resource to inspect.</param>
        private void SetObjectToInspect(string resourcePath)
        {
            activeResourcePath = resourcePath;
            if (!ProjectLibrary.Exists(resourcePath))
            {
                return;
            }

            ResourceMeta meta = ProjectLibrary.GetMeta(resourcePath);

            if (meta == null)
            {
                return;
            }

            Type resourceType = meta.Type;

            currentType = InspectorType.Resource;

            inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow);
            GUI.AddElement(inspectorScrollArea);
            inspectorLayout = inspectorScrollArea.Layout;

            GUIPanel titlePanel = inspectorLayout.AddPanel();

            titlePanel.SetHeight(RESOURCE_TITLE_HEIGHT);

            GUILayoutY titleLayout = titlePanel.AddLayoutY();

            titleLayout.SetPosition(PADDING, PADDING);

            string name = Path.GetFileNameWithoutExtension(resourcePath);
            string type = resourceType.Name;

            LocString title      = new LocEdString(name + " (" + type + ")");
            GUILabel  titleLabel = new GUILabel(title);

            titleLayout.AddFlexibleSpace();
            GUILayoutX titleLabelLayout = titleLayout.AddLayoutX();

            titleLabelLayout.AddElement(titleLabel);
            titleLayout.AddFlexibleSpace();

            GUIPanel titleBgPanel = titlePanel.AddPanel(1);

            GUITexture titleBg = new GUITexture(null, EditorStylesInternal.InspectorTitleBg);

            titleBgPanel.AddElement(titleBg);

            inspectorLayout.AddSpace(COMPONENT_SPACING);

            inspectorResource           = new InspectorResource();
            inspectorResource.mainPanel = inspectorLayout.AddPanel();
            inspectorLayout.AddFlexibleSpace();
            inspectorResource.previewPanel = inspectorLayout.AddPanel();

            var persistentProperties = persistentData.GetProperties(meta.UUID.ToString());

            inspectorResource.inspector = InspectorUtility.GetInspector(resourceType);
            inspectorResource.inspector.Initialize(inspectorResource.mainPanel, inspectorResource.previewPanel,
                                                   activeResourcePath, persistentProperties);
        }
Beispiel #6
0
        private void OnEditorUpdate()
        {
            UpdateLoadingProgress();

            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
                    {
                        DuplicateSelection();
                    }
                    else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
                    {
                        DeleteSelection();
                    }
                    else if (VirtualInput.IsButtonDown(viewToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;
                    }
                    else if (VirtualInput.IsButtonDown(moveToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;
                    }
                    else if (VirtualInput.IsButtonDown(rotateToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
                    }
                    else if (VirtualInput.IsButtonDown(scaleToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
                    }
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGrid.Draw();

            ProjectionType currentProjType = camera.ProjectionType;

            if (sceneAxesLastProjectionType != currentProjType)
            {
                sceneAxesGUI.ProjectionType = currentProjType;
                sceneAxesLastProjectionType = currentProjType;
            }

            bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();

            Vector2I scenePos;
            bool     inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);

            bool clearSelection = false;

            if (AllowViewportInput)
            {
                if (Input.IsPointerButtonUp(PointerButton.Left))
                {
                    clearSelection = true;
                }
                else if (Input.IsPointerButtonDown(PointerButton.Left))
                {
                    mouseDownPosition = scenePos;
                }
            }
            else
            {
                clearSelection = true;
                inBounds       = false;
            }

            bool dragResult = false;

            if (clearSelection)
            {
                dragResult = EndDragSelection();
                if (sceneHandles.IsActive())
                {
                    sceneHandles.ClearSelection();
                }

                if (sceneAxesGUI.IsActive())
                {
                    sceneAxesGUI.ClearSelection();
                }
            }

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;

            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;
                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
                            if (meta != null)
                            {
                                if (meta.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load <Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent <Renderable>();
                                        renderable.Mesh = mesh;
                                        if (mesh != null)
                                        {
                                            draggedSOOffset = mesh.Bounds.Box.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                                else if (meta.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load <Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);

                                        if (draggedSO != null)
                                        {
                                            AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
                                            draggedSOOffset = draggedObjBounds.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        if (Input.IsButtonHeld(ButtonCode.Space))
                        {
                            SnapData snapData;
                            sceneSelection.Snap(scenePos, out snapData, new SceneObject[] { draggedSO });

                            Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
                            draggedSO.Position = snapData.position;
                            draggedSO.Rotation = q;
                        }
                        else
                        {
                            Ray worldRay = camera.ScreenPointToRay(scenePos);
                            draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
                        }
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if ((HasContentFocus || IsPointerHovering) && AllowViewportInput)
            {
                sceneCamera.EnableInput(true);

                if (inBounds && HasContentFocus)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
                                                               HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                        {
                            sceneAxesGUI.TrySelect(scenePos);
                        }
                        else
                        {
                            sceneHandles.TrySelect(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
                             draggedSO == null && scenePos != mouseDownPosition)
                    {
                        if (isDraggingSelection)
                        {
                            UpdateDragSelection(scenePos);
                        }
                        else
                        {
                            StartDragSelection(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive && !dragActive && !dragResult)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
                        }
                    }
                }
            }
            else
            {
                sceneCamera.EnableInput(false);
            }

            if (AllowViewportInput)
            {
                SceneHandles.BeginInput();
                sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
                sceneAxesGUI.UpdateInput(scenePos);
                SceneHandles.EndInput();
            }

            sceneHandles.Draw();
            sceneAxesGUI.Draw();

            // Must be done after handle input is processed, in order to reflect most recent transform
            sceneGizmos.Draw();
            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
            {
                sceneCamera.FrameSelected();
            }
        }
        /// <summary>
        /// Returns an icon that can be used for displaying a resource of the specified type.
        /// </summary>
        /// <param name="path">Path to the project library entry to display data for.</param>
        /// <param name="size">Size of the icon to retrieve, in pixels.</param>
        /// <returns>Icon to display for the specified entry.</returns>
        private static SpriteTexture GetIcon(string path, int size)
        {
            LibraryEntry entry = ProjectLibrary.GetEntry(path);

            if (entry.Type == LibraryEntryType.Directory)
            {
                return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Folder, size));
            }
            else
            {
                ResourceMeta         meta  = ProjectLibrary.GetMeta(path);
                ProjectResourceIcons icons = meta.Icons;

                RRef <Texture> icon;
                if (size <= 16)
                {
                    icon = icons.icon16;
                }
                else if (size <= 32)
                {
                    icon = icons.icon32;
                }
                else if (size <= 48)
                {
                    icon = icons.icon48;
                }
                else
                {
                    icon = icons.icon64;
                }

                if (icon.Value != null)
                {
                    return(new SpriteTexture(icon));
                }

                switch (meta.ResType)
                {
                case ResourceType.Font:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Font, size));

                case ResourceType.Mesh:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Mesh, size));

                case ResourceType.Texture:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Texture, size));

                case ResourceType.PlainText:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PlainText, size));

                case ResourceType.ScriptCode:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.ScriptCode, size));

                case ResourceType.SpriteTexture:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.SpriteTexture, size));

                case ResourceType.Shader:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Shader, size));

                case ResourceType.ShaderInclude:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Shader, size));

                case ResourceType.Material:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Material, size));

                case ResourceType.Prefab:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.Prefab, size));

                case ResourceType.GUISkin:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.GUISkin, size));

                case ResourceType.PhysicsMaterial:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PhysicsMaterial, size));

                case ResourceType.PhysicsMesh:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.PhysicsMesh, size));

                case ResourceType.AudioClip:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.AudioClip, size));

                case ResourceType.AnimationClip:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.AnimationClip, size));

                case ResourceType.VectorField:
                    return(EditorBuiltin.GetProjectLibraryIcon(ProjectLibraryIcon.VectorField, size));
                }
            }

            return(null);
        }