示例#1
0
 private void HandleEditModeChange()
 {
     // Handles changes in EditMode while tool is expected to be in the same mode
     if (isPicking && !TilemapEditorTool.IsActive(typeof(PickingTool)))
     {
         m_MarqueeStart = null;
         m_MarqueeType  = MarqueeType.None;
         if (isHotControl)
         {
             GUI.changed           = true;
             GUIUtility.hotControl = 0;
         }
     }
     if (isBoxing && !TilemapEditorTool.IsActive(typeof(BoxTool)))
     {
         m_MarqueeStart = null;
         m_MarqueeType  = MarqueeType.None;
         if (isHotControl)
         {
             GUI.changed           = true;
             GUIUtility.hotControl = 0;
         }
     }
     if (!TilemapEditorTool.IsActive(typeof(SelectTool)) && !TilemapEditorTool.IsActive(typeof(MoveTool)))
     {
         ClearGridSelection();
     }
 }
示例#2
0
        private void HandleCustomTool()
        {
            Event evt = Event.current;

            if (evt.type == EventType.Layout || evt.type == EventType.Repaint)
            {
                return;
            }

            if (!TilemapEditorTool.IsCustomTilemapEditorToolActive())
            {
                return;
            }

            TilemapEditorTool activeTool = EditorToolManager.activeTool as TilemapEditorTool;
            var executed = CustomTool(isHotControl, activeTool, new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));

            if (executed != executing)
            {
                GUIUtility.hotControl = executed ? m_PermanentControlID : 0;
                executing             = executed;
                GUI.changed           = true;
                Event.current.Use();
            }
            else if (executing)
            {
                GUI.changed = true;
                Event.current.Use();
            }
        }
示例#3
0
 private bool IsPickingEvent(Event evt)
 {
     return(((evt.control && !TilemapEditorTool.IsActive(typeof(MoveTool))) ||
             TilemapEditorTool.IsActive(typeof(PickingTool)) ||
             !TilemapEditorTool.IsActive(typeof(SelectTool)) && PickingIsDefaultTool()) &&
            evt.button == 0 && !evt.alt);
 }
示例#4
0
 private bool IsErasingEvent(Event evt)
 {
     return(evt.button == 0 && !evt.control && !evt.alt &&
            ((evt.shift && !TilemapEditorTool.IsActive(typeof(BoxTool)) &&
              !TilemapEditorTool.IsActive(typeof(FillTool)) &&
              !TilemapEditorTool.IsActive(typeof(SelectTool)) &&
              !TilemapEditorTool.IsActive(typeof(MoveTool))) ||
             TilemapEditorTool.IsActive(typeof(EraseTool))));
 }
        internal static void UpdateBrushToolbar()
        {
            BrushToolsAttribute toolAttribute = null;

            if (instance.m_Brush != null)
            {
                toolAttribute = (BrushToolsAttribute)instance.m_Brush.GetType().GetCustomAttribute(typeof(BrushToolsAttribute), false);
            }
            TilemapEditorTool.UpdateEditorTools(toolAttribute);
        }
        protected override bool CustomTool(bool isHotControl, TilemapEditorTool tool, Vector3Int position)
        {
            var executed = false;

            if (grid != null)
            {
                executed = tool.HandleTool(isHotControl, grid, brushTarget, position);
            }
            return(executed);
        }
        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);
            }
        }
        protected override void PickBrush(BoundsInt position, Vector3Int pickingStart)
        {
            if (grid == null || gridBrush == null)
            {
                return;
            }

            gridBrush.Pick(grid, brushTarget, position, pickingStart);

            if (!InGridEditMode())
            {
                TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
            }

            m_ActivePick = new RectInt(position.min.x, position.min.y, position.size.x, position.size.y);
        }
示例#9
0
        private void HandleMoveTool()
        {
            Event evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt && TilemapEditorTool.IsActive(typeof(MoveTool)))
            {
                RegisterUndo();
                Vector3Int mouse3D = new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, GridSelection.position.zMin);
                if (GridSelection.active && GridSelection.position.Contains(mouse3D))
                {
                    GUIUtility.hotControl = m_PermanentControlID;
                    executing             = true;
                    m_MarqueeStart        = null;
                    m_MarqueeType         = MarqueeType.None;
                    m_PreviousMove        = mouseGridPosition;
                    MoveStart(GridSelection.position);
                    s_LastActivePaintableGrid = this;
                }
                Event.current.Use();
            }
            if (evt.type == EventType.MouseDrag && evt.button == 0 && TilemapEditorTool.IsActive(typeof(MoveTool)) && isHotControl)
            {
                if (m_MouseGridPositionChanged && m_PreviousMove.HasValue)
                {
                    executing = true;
                    BoundsInt previousRect   = GridSelection.position;
                    BoundsInt previousBounds = new BoundsInt(new Vector3Int(previousRect.xMin, previousRect.yMin, GridSelection.position.zMin), new Vector3Int(previousRect.size.x, previousRect.size.y, 1));

                    Vector2Int direction = mouseGridPosition - m_PreviousMove.Value;
                    BoundsInt  pos       = GridSelection.position;
                    pos.position           = new Vector3Int(pos.x + direction.x, pos.y + direction.y, pos.z);
                    GridSelection.position = pos;
                    Move(previousBounds, pos);
                    m_PreviousMove = mouseGridPosition;
                    Event.current.Use();
                }
            }
            if (evt.type == EventType.MouseUp && evt.button == 0 && m_PreviousMove.HasValue && TilemapEditorTool.IsActive(typeof(MoveTool)) && isHotControl)
            {
                m_PreviousMove = null;
                MoveEnd(GridSelection.position);
                executing             = false;
                GUIUtility.hotControl = 0;
                Event.current.Use();
            }
        }
示例#10
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;
            }
        }
示例#11
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();
            }
        }
示例#12
0
 private void HandleFloodFill()
 {
     if (TilemapEditorTool.IsActive(typeof(FillTool)) && GridPaintingState.gridBrush != null && ValidateFloodFillPosition(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, 0)))
     {
         Event evt = Event.current;
         if (evt.type == EventType.MouseDown && evt.button == 0 && !evt.alt)
         {
             GUIUtility.hotControl = m_PermanentControlID;
             GUI.changed           = true;
             executing             = true;
             Event.current.Use();
         }
         if (evt.type == EventType.MouseUp && evt.button == 0 && isHotControl)
         {
             RegisterUndo();
             FloodFill(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));
             executing   = false;
             GUI.changed = true;
             Event.current.Use();
             GUIUtility.hotControl = 0;
         }
     }
 }
示例#13
0
 protected abstract bool CustomTool(bool isHotControl, TilemapEditorTool tool, Vector3Int position);
示例#14
0
        private void HandleBrushPaintAndErase()
        {
            Event evt = Event.current;

            if (!IsPaintingEvent(evt) && !IsErasingEvent(evt))
            {
                return;
            }

            switch (evt.type)
            {
            case EventType.MouseDown:
                if (isNearestControl)
                {
                    RegisterUndo();
                    GUIUtility.hotControl = m_PermanentControlID;
                    executing             = true;
                    m_TypeBeforeExecution = EditorTools.ToolManager.activeToolType;
                    var position = new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition);
                    if (IsErasingEvent(evt))
                    {
                        if (!TilemapEditorTool.IsActive(typeof(EraseTool)))
                        {
                            TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
                        }
                        Erase(position);
                    }
                    else
                    {
                        if (!TilemapEditorTool.IsActive(typeof(PaintTool)))
                        {
                            TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                        }
                        Paint(position);
                    }
                    ResetPreviousMousePositionToCurrentPosition();
                    Event.current.Use();
                    GUI.changed = true;
                }
                break;

            case EventType.MouseDrag:
                executing = true;
                if (isHotControl && mouseGridPositionChanged)
                {
                    var points = GridEditorUtility.GetPointsOnLine(m_PreviousMouseGridPosition, mouseGridPosition);

                    if (!evt.shift && !TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                    }
                    else if (evt.shift && TilemapEditorTool.IsActive(typeof(PaintTool)))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
                    }

                    foreach (var point in points)
                    {
                        var position = new Vector3Int(point.x, point.y, zPosition);
                        if (IsErasingEvent(evt))
                        {
                            Erase(position);
                        }
                        else
                        {
                            Paint(position);
                        }
                    }
                    Event.current.Use();
                    GUI.changed = true;
                }
                break;

            case EventType.MouseUp:
                executing = false;
                if (isHotControl)
                {
                    if (!TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                    }

                    Event.current.Use();
                    GUI.changed           = true;
                    GUIUtility.hotControl = 0;
                }
                break;
            }
        }
示例#15
0
 private bool IsPaintingEvent(Event evt)
 {
     return(evt.button == 0 && !evt.control && !evt.alt && TilemapEditorTool.IsActive(typeof(PaintTool)));
 }
示例#16
0
        private void HandleBrushPaintAndErase()
        {
            Event evt = Event.current;

            if (!IsPaintingEvent(evt) && !IsErasingEvent(evt))
            {
                return;
            }

            switch (evt.type)
            {
            case EventType.MouseDown:
                RegisterUndo();
                GUIUtility.hotControl = m_PermanentControlID;
                executing             = true;
                m_TypeBeforeExecution = EditorTools.ToolManager.activeToolType;
                if (IsErasingEvent(evt))
                {
                    if (!TilemapEditorTool.IsActive(typeof(EraseTool)))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
                    }
                    Erase(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));
                }
                else
                {
                    if (!TilemapEditorTool.IsActive(typeof(PaintTool)))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                    }
                    Paint(new Vector3Int(mouseGridPosition.x, mouseGridPosition.y, zPosition));
                }
                Event.current.Use();
                GUI.changed = true;
                break;

            case EventType.MouseDrag:
                executing = true;
                if (isHotControl && mouseGridPositionChanged)
                {
                    List <Vector2Int> points = GridEditorUtility.GetPointsOnLine(m_PreviousMouseGridPosition, mouseGridPosition).ToList();
                    if (points[0] == mouseGridPosition)
                    {
                        points.Reverse();
                    }

                    if (!evt.shift && !TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                    }
                    else if (evt.shift && TilemapEditorTool.IsActive(typeof(PaintTool)))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(EraseTool));
                    }

                    for (int i = 1; i < points.Count; i++)
                    {
                        if (IsErasingEvent(evt))
                        {
                            Erase(new Vector3Int(points[i].x, points[i].y, zPosition));
                        }
                        else
                        {
                            Paint(new Vector3Int(points[i].x, points[i].y, zPosition));
                        }
                    }
                    Event.current.Use();
                    GUI.changed = true;
                }
                break;

            case EventType.MouseUp:
                executing = false;
                if (isHotControl)
                {
                    if (!TilemapEditorTool.IsActive(typeof(PaintTool)) && m_TypeBeforeExecution == typeof(PaintTool))
                    {
                        TilemapEditorTool.SetActiveEditorTool(typeof(PaintTool));
                    }

                    Event.current.Use();
                    GUI.changed           = true;
                    GUIUtility.hotControl = 0;
                }
                break;
            }
        }
示例#17
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();
            }
        }