Ejemplo n.º 1
0
        void CallOnPaintSceneGUI()
        {
            bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;

            if (!hasSelection && GridPaintingState.activeGrid != this)
            {
                return;
            }

            RectInt rect = new RectInt(mouseGridPosition, new Vector2Int(1, 1));

            if (m_MarqueeStart.HasValue)
            {
                rect = GridEditorUtility.GetMarqueeRect(mouseGridPosition, m_MarqueeStart.Value);
            }
            else if (hasSelection)
            {
                rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
            }

            var       layoutGrid  = tilemap != null ? tilemap : grid as GridLayout;
            BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));

            if (GridPaintingState.activeBrushEditor != null)
            {
                GridPaintingState.activeBrushEditor.OnPaintSceneGUI(layoutGrid, brushTarget, brushBounds
                                                                    , EditTypeToBrushTool(EditorTools.EditorTools.activeToolType), m_MarqueeStart.HasValue || executing);
            }
            else // Fallback when user hasn't defined custom editor
            {
                GridBrushEditorBase.OnPaintSceneGUIInternal(layoutGrid, brushTarget, brushBounds
                                                            , EditTypeToBrushTool(EditorTools.EditorTools.activeToolType), m_MarqueeStart.HasValue || executing);
            }
        }
        internal static RectInt GetMinMaxRect(List <Vector2Int> positions)
        {
            if (positions == null || positions.Count == 0)
            {
                return(new RectInt());
            }

            return(GridEditorUtility.GetMarqueeRect(
                       new Vector2Int(positions.Min(p1 => p1.x), positions.Min(p1 => p1.y)),
                       new Vector2Int(positions.Max(p1 => p1.x), positions.Max(p1 => p1.y))
                       ));
        }
        private void CallOnPaintSceneGUI(Vector2Int position)
        {
            if (!unlocked && !TilemapEditorTool.IsActive(typeof(SelectTool)) && !TilemapEditorTool.IsActive(typeof(PickingTool)))
            {
                return;
            }

            bool hasSelection = GridSelection.active && GridSelection.target == brushTarget;

            if (!hasSelection && GridPaintingState.activeGrid != this)
            {
                return;
            }

            GridBrushBase brush = GridPaintingState.gridBrush;

            if (brush == null)
            {
                return;
            }

            var rect = new RectInt(position, new Vector2Int(1, 1));

            if (m_MarqueeStart.HasValue)
            {
                rect = GridEditorUtility.GetMarqueeRect(position, m_MarqueeStart.Value);
            }
            else if (hasSelection)
            {
                rect = new RectInt(GridSelection.position.xMin, GridSelection.position.yMin, GridSelection.position.size.x, GridSelection.position.size.y);
            }

            var       gridLayout  = tilemap != null ? tilemap.layoutGrid : grid as GridLayout;
            BoundsInt brushBounds = new BoundsInt(new Vector3Int(rect.x, rect.y, zPosition), new Vector3Int(rect.width, rect.height, 1));

            if (GridPaintingState.activeBrushEditor != null)
            {
                GridPaintingState.activeBrushEditor.OnPaintSceneGUI(gridLayout, brushTarget, brushBounds,
                                                                    EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType),
                                                                    m_MarqueeStart.HasValue || executing);
            }
            else // Fallback when user hasn't defined custom editor
            {
                GridBrushEditorBase.OnPaintSceneGUIInternal(gridLayout, Selection.activeGameObject, brushBounds,
                                                            EditTypeToBrushTool(UnityEditor.EditorTools.ToolManager.activeToolType),
                                                            m_MarqueeStart.HasValue || executing);
            }
        }
Ejemplo n.º 4
0
        private void HandleBoxTool()
        {
            Event evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && TilemapEditorTool.IsActive(typeof(BoxTool)))
            {
                m_MarqueeStart = mouseGridPosition;
                m_MarqueeType  = MarqueeType.Box;
                Event.current.Use();
                GUI.changed           = true;
                executing             = true;
                GUIUtility.hotControl = m_PermanentControlID;
            }
            if (evt.type == EventType.MouseDrag && evt.button == 0 && TilemapEditorTool.IsActive(typeof(BoxTool)))
            {
                if (isHotControl && m_MarqueeStart.HasValue)
                {
                    Event.current.Use();
                    executing   = true;
                    GUI.changed = true;
                }
            }
            if (evt.type == EventType.MouseUp && evt.button == 0 && TilemapEditorTool.IsActive(typeof(BoxTool)))
            {
                if (isHotControl && m_MarqueeStart.HasValue)
                {
                    RegisterUndo();
                    RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
                    if (evt.shift)
                    {
                        BoxErase(new BoundsInt(rect.x, rect.y, zPosition, rect.size.x, rect.size.y, 1));
                    }
                    else
                    {
                        BoxFill(new BoundsInt(rect.x, rect.y, zPosition, rect.size.x, rect.size.y, 1));
                    }
                    Event.current.Use();
                    executing             = false;
                    GUI.changed           = true;
                    GUIUtility.hotControl = 0;
                }
                m_MarqueeStart = null;
                m_MarqueeType  = MarqueeType.None;
            }
        }
Ejemplo n.º 5
0
        private void HandleSelectTool()
        {
            Event evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && (TilemapEditorTool.IsActive(typeof(SelectTool)) || (TilemapEditorTool.IsActive(typeof(MoveTool)) && evt.control)))
            {
                if (TilemapEditorTool.IsActive(typeof(MoveTool)) && evt.control)
                {
                    TilemapEditorTool.SetActiveEditorTool(typeof(SelectTool));
                }

                m_PreviousMove = null;
                m_MarqueeStart = mouseGridPosition;
                m_MarqueeType  = MarqueeType.Select;

                s_LastActivePaintableGrid = this;
                GUIUtility.hotControl     = m_PermanentControlID;
                Event.current.Use();
            }
            if (evt.rawType == EventType.MouseUp && evt.button == 0 && !evt.alt && m_MarqueeStart.HasValue && isHotControl && TilemapEditorTool.IsActive(typeof(SelectTool)))
            {
                // Check if event only occurred in the PaintableGrid window as evt.type will filter for this
                if (evt.type == EventType.MouseUp && m_MarqueeType == MarqueeType.Select)
                {
                    RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
                    Select(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)));
                    Event.current.Use();
                }
                if (evt.control)
                {
                    TilemapEditorTool.SetActiveEditorTool(typeof(MoveTool));
                }
                m_MarqueeStart = null;
                m_MarqueeType  = MarqueeType.None;
                InspectorWindow.RepaintAllInspectors();
                GUIUtility.hotControl = 0;
            }
            if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape && !m_MarqueeStart.HasValue && !m_PreviousMove.HasValue)
            {
                ClearGridSelection();
                Event.current.Use();
            }
        }
Ejemplo n.º 6
0
        internal static RectInt GetBounds(GameObject palette)
        {
            if (palette == null)
            {
                return(new RectInt());
            }

            Vector2Int min = new Vector2Int(int.MaxValue, int.MaxValue);
            Vector2Int max = new Vector2Int(int.MinValue, int.MinValue);

            foreach (var tilemap in palette.GetComponentsInChildren <Tilemap>())
            {
                Vector3Int p1         = tilemap.editorPreviewOrigin;
                Vector3Int p2         = p1 + tilemap.editorPreviewSize;
                Vector2Int tilemapMin = new Vector2Int(Mathf.Min(p1.x, p2.x), Mathf.Min(p1.y, p2.y));
                Vector2Int tilemapMax = new Vector2Int(Mathf.Max(p1.x, p2.x), Mathf.Max(p1.y, p2.y));
                min = new Vector2Int(Mathf.Min(min.x, tilemapMin.x), Mathf.Min(min.y, tilemapMin.y));
                max = new Vector2Int(Mathf.Max(max.x, tilemapMax.x), Mathf.Max(max.y, tilemapMax.y));
            }

            return(GridEditorUtility.GetMarqueeRect(min, max));
        }
Ejemplo n.º 7
0
        private void HandleBrushPicking()
        {
            Event evt = Event.current;

            if (evt.type == EventType.MouseDown && IsPickingEvent(evt) && !isHotControl)
            {
                m_TypeBeforeExecution = typeof(PaintTool);
                if (inEditMode && !TilemapEditorTool.IsActive(typeof(PickingTool)))
                {
                    m_TypeBeforeExecution = UnityEditor.EditorTools.ToolManager.activeToolType;
                    TilemapEditorTool.SetActiveEditorTool(typeof(PickingTool));
                }

                m_MarqueeStart            = mouseGridPosition;
                m_MarqueeType             = MarqueeType.Pick;
                s_LastActivePaintableGrid = this;
                Event.current.Use();
                GUI.changed           = true;
                GUIUtility.hotControl = m_PermanentControlID;
                OnBrushPickStarted();
            }
            if (evt.type == EventType.MouseDrag && isHotControl && m_MarqueeStart.HasValue && m_MarqueeType == MarqueeType.Pick && IsPickingEvent(evt))
            {
                RectInt rect = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
                OnBrushPickDragged(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)));
                Event.current.Use();
                GUI.changed = true;
            }
            if (evt.rawType == EventType.MouseUp && isHotControl && m_MarqueeStart.HasValue && m_MarqueeType == MarqueeType.Pick && IsPickingEvent(evt))
            {
                // Check if event only occurred in the PaintableGrid window as evt.type will filter for this
                if (evt.type == EventType.MouseUp && m_MarqueeType == MarqueeType.Pick)
                {
                    RectInt    rect  = GridEditorUtility.GetMarqueeRect(m_MarqueeStart.Value, mouseGridPosition);
                    Vector2Int pivot = GetMarqueePivot(m_MarqueeStart.Value, mouseGridPosition);
                    PickBrush(new BoundsInt(new Vector3Int(rect.xMin, rect.yMin, zPosition), new Vector3Int(rect.size.x, rect.size.y, 1)), new Vector3Int(pivot.x, pivot.y, 0));

                    if (inEditMode && UnityEditor.EditorTools.ToolManager.activeToolType != m_TypeBeforeExecution)
                    {
                        if (PickingIsDefaultTool() &&
                            (m_TypeBeforeExecution == typeof(EraseTool) ||
                             m_TypeBeforeExecution == typeof(MoveTool)))
                        {
                            // If Picking is default, change to a Paint Tool to facilitate editing if previous tool does not allow for painting
                            TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                        }
                        else
                        {
                            TilemapEditorTool.SetActiveEditorTool(m_TypeBeforeExecution);
                        }
                    }

                    GridPaletteBrushes.ActiveGridBrushAssetChanged();
                    s_LastActivePaintableGrid = this;
                    Event.current.Use();
                    GUI.changed = true;
                }
                else
                // Event occurred outside of PaintableGrid window, cancel the pick event
                {
                    OnBrushPickCancelled();
                }
                m_MarqueeType         = MarqueeType.None;
                m_MarqueeStart        = null;
                GUIUtility.hotControl = 0;
                InspectorWindow.RepaintAllInspectors();
            }
        }