Exemplo n.º 1
0
        public void Erase(TileMap tileMap, Vector2 local_pos)
        {
            int minGridX = this.tileMap.min_grid_x;
            int minGridY = this.tileMap.min_grid_y;
            int maxGridX = this.tileMap.max_grid_x;
            int maxGridY = this.tileMap.max_grid_y;

            if (is_undo_enabled)
            {
#if UNITY_EDITOR
                Undo.RecordObject(tileMap, TileMapConst.Undo_Operation_Name + tileMap.name);
                Undo.RecordObjects(tileMap.GetComponentsInChildren <TileMapChunk>(),
                                   TileMapConst.Undo_Operation_Name + tileMap.name);
#endif
            }

            tileMap.is_undo_enabled = is_undo_enabled;
            int dstGy = TileSetBrushUtil.GetGridY(local_pos, tileMap.cell_size);
            for (int gridY = minGridY; gridY <= maxGridY; ++gridY, ++dstGy)
            {
                int dstGx = TileSetBrushUtil.GetGridY(local_pos, tileMap.cell_size);
                for (int gridX = minGridX; gridX <= maxGridX; ++gridX, ++dstGx)
                {
                    tileMap.SetTileData(dstGx, dstGy, TileSetConst.TileData_Empty);
                }
            }

            tileMap.UpdateMeshImmediate();
            tileMap.is_undo_enabled = false;
        }
Exemplo n.º 2
0
        public void ShrinkMapBoundsToVisibleArea()
        {
            Bounds  tileMap_bounds = new Bounds();
            Vector2 half_cell_size = cell_size / 2f; // used to avoid precission errors

            max_grid_x = max_grid_y = min_grid_x = min_grid_y = 0;
            var value_iterator = tileMapChunk_cache_dict.Values.GetEnumerator();

            while (value_iterator.MoveNext())
            {
                var tileMapChunk = value_iterator.Current;
                if (tileMapChunk)
                {
                    Bounds  tileMapChunk_bounds = tileMapChunk.GetBounds();
                    Vector2 min = transform.InverseTransformPoint(tileMapChunk.transform.TransformPoint(tileMapChunk_bounds.min));
                    Vector2 max = transform.InverseTransformPoint(tileMapChunk.transform.TransformPoint(tileMapChunk_bounds.max));
                    tileMap_bounds.Encapsulate(min + half_cell_size);
                    tileMap_bounds.Encapsulate(max - half_cell_size);
                }
            }
            min_grid_x = TileSetBrushUtil.GetGridX(tileMap_bounds.min, cell_size);
            min_grid_y = TileSetBrushUtil.GetGridY(tileMap_bounds.min, cell_size);
            max_grid_x = TileSetBrushUtil.GetGridX(tileMap_bounds.max, cell_size);
            max_grid_y = TileSetBrushUtil.GetGridY(tileMap_bounds.max, cell_size);
            RecalculateMapBounds();
        }
Exemplo n.º 3
0
        public uint GetTileData(Vector2 localPosition)
        {
            int grid_x = TileSetBrushUtil.GetGridX(localPosition, cell_size);
            int grid_y = TileSetBrushUtil.GetGridY(localPosition, cell_size);

            return(GetTileData(grid_x, grid_y));
        }
Exemplo n.º 4
0
        public static void FloodFill(TileMap tileMap, Vector2 vLocalPos, uint[,] tileData)
        {
            int gridX = TileSetBrushUtil.GetGridX(vLocalPos, tileMap.cell_size);
            int gridY = TileSetBrushUtil.GetGridY(vLocalPos, tileMap.cell_size);

            FloodFill(tileMap, gridX, gridY, tileData);
        }
Exemplo n.º 5
0
        public static void FloodFillPreview(TileMap tilemap, Vector2 vLocalPos, uint tileData, List <Vector2> outFilledPoints, uint maxPoints = uint.MaxValue)
        {
            int gridX = TileSetBrushUtil.GetGridX(vLocalPos, tilemap.cell_size);
            int gridY = TileSetBrushUtil.GetGridY(vLocalPos, tilemap.cell_size);

            FloodFillPreview(tilemap, gridX, gridY, tileData, outFilledPoints, maxPoints);
        }
Exemplo n.º 6
0
        public void Rot90(bool changeFlags = true)
        {
            int grid_x = TileSetBrushUtil.GetGridX(-offset, tileMap.cell_size);
            int grid_y = TileSetBrushUtil.GetGridY(-offset, tileMap.cell_size);

            offset = -new Vector2(grid_y * tileMap.cell_size.x,
                                  (tileMap.GridWidth - grid_x - 1) * tileMap.cell_size.y);

            tileMap.Rot90(changeFlags);
            tileMap.UpdateMeshImmediate();
        }
Exemplo n.º 7
0
        public void Rot90Back(bool changeFlags = true)
        {
            //NOTE: This is a fast way to rotate back 90º by rotating forward 3 times
            for (int i = 0; i < 3; ++i)
            {
                int gridX = TileSetBrushUtil.GetGridX(-offset, tileMap.cell_size);
                int gridY = TileSetBrushUtil.GetGridY(-offset, tileMap.cell_size);
                offset = -new Vector2(gridY * tileMap.cell_size.x,
                                      (tileMap.GridWidth - gridX - 1) * tileMap.cell_size.y);
                tileMap.Rot90(changeFlags);
            }

            tileMap.UpdateMeshImmediate();
        }
Exemplo n.º 8
0
        public void Paint(TileMap tileMap, Vector2 local_pos, bool skipEmptyTiles = false)
        {
            int min_grid_x = this.tileMap.min_grid_x;
            int min_grid_y = this.tileMap.min_grid_y;
            int max_grid_x = this.tileMap.max_grid_x;
            int max_grid_y = this.tileMap.max_grid_y;

            if (is_undo_enabled)
            {
#if UNITY_EDITOR
                Undo.RecordObject(tileMap, TileMapConst.Undo_Operation_Name + tileMap.name);
                Undo.RecordObjects(tileMap.GetComponentsInChildren <TileMapChunk>(),
                                   TileMapConst.Undo_Operation_Name + tileMap.name);
#endif
            }

            tileMap.is_undo_enabled = is_undo_enabled;
            int  target_grid_y     = TileSetBrushUtil.GetGridY(local_pos, tileMap.cell_size);
            bool is_do_paint_empty =
                this.tileMap.GridWidth == 1 && this.tileMap.GridHeight == 1 || // don't copy empty tiles
                brushPattern != null && brushPattern.GetLength(0) == 1 &&
                brushPattern.GetLength(1) == 1; // unless the brush size is one
            is_do_paint_empty &= !skipEmptyTiles;



            for (int grid_y = min_grid_y; grid_y <= max_grid_y; ++grid_y, ++target_grid_y)
            {
                int target_grid_x = TileSetBrushUtil.GetGridX(local_pos, tileMap.cell_size);
                for (int grid_x = min_grid_x; grid_x <= max_grid_x; ++grid_x, ++target_grid_x)
                {
                    uint tileData = this.tileMap.GetTileData(grid_x, grid_y);
                    if (
                        is_do_paint_empty ||
                        tileData != TileSetConst.TileData_Empty
                        )
                    {
                        tileMap.SetTileData(target_grid_x, target_grid_y, tileData);
                    }
                }
            }

            tileMap.UpdateMeshImmediate();
            tileMap.is_undo_enabled = false;
        }
Exemplo n.º 9
0
        public void DoPaintReleased(TileMap tileMap, Vector2 local_pos, EventModifiers modifiers = default(EventModifiers))
        {
            //Debug.Log("DoPaintReleased (" + TilemapUtils.GetGridX(tilemap, localPos) + "," + TilemapUtils.GetGridY(tilemap, localPos) + ")");
            if (paint_mode != TileSetBrushPaintMode.Pencil)
            {
                Vector2 pressedPos = TileSetBrushUtil.GetSnappedPosition(pressed_position, this.tileMap.cell_size) + this.tileMap.cell_size / 2f;
                Paint(tileMap, pressedPos + (Vector2)this.tileMap.tileMapBounds.min, true);
                pressed_position = local_pos;
                this.tileMap.ClearMap();
                for (int y = 0; y < brushPattern.GetLength(1); ++y)
                {
                    for (int x = 0; x < brushPattern.GetLength(0); ++x)
                    {
                        this.tileMap.SetTileData(x, y, brushPattern[x, y]);
                    }
                }

                this.tileMap.UpdateMesh();
                is_dragging = false;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Gets the grid Y position for a given tilemap and local position. To convert from world to local position use tilemap.transform.InverseTransformPoint(worldPosition).
 /// Avoid using positions multiple of cellSize like 0.32f if cellSize = 0.16f because due float imprecisions the return value could be wrong.
 /// </summary>
 /// <param name="tilemap"></param>
 /// <param name="locPosition"></param>
 /// <returns></returns>
 static public int GetGridY(TileMap tilemap, Vector2 locPosition)
 {
     return(TileSetBrushUtil.GetGridY(locPosition, tilemap.cell_size));
 }
        public void OnSceneGUI_DoPaint()
        {
            Event e = Event.current;

            DrawToolbar();
            if (DragAndDrop.objectReferences.Length > 0 || // hide brush when user is dragging a prefab into the scene
                EditorWindow.mouseOverWindow != SceneView.currentDrawingSceneView || // hide brush when it's not over the scene view
                (Tools.current != Tool.Rect && Tools.current != Tool.None))
            {
                is_tileSetBrush_visible = false;
                SceneView.RepaintAll();
                return;
            }

            int control_id = GUIUtility.GetControlID(FocusType.Passive);

            HandleUtility.AddDefaultControl(control_id);
            EventType current_event_type = Event.current.GetTypeForControl(control_id);
            bool      is_skip            = false;
            int       save_control       = GUIUtility.hotControl;

            try
            {
                if (current_event_type == EventType.Layout)
                {
                    is_skip = true;
                }
                else if (current_event_type == EventType.ScrollWheel)
                {
                    is_skip = true;
                }

                if (tileMap.tileSet == null)
                {
                    return;
                }

                if (!is_skip)
                {
                    if (e.type == EventType.KeyDown)
                    {
                        switch (e.keyCode)
                        {
                        case TileToolbarConst.Key_FlipH:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipH(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_FlipV:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipV(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_Rot90:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_Rot90Back:
                            TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90Back(!e.shift);
                            e.Use(); // Use key event
                            break;

                        case TileToolbarConst.Key_PencilTool:
                        case TileToolbarConst.Key_LineTool:
                        case TileToolbarConst.Key_RectTool:
                        case TileToolbarConst.Key_EllipseTool:
                            switch (e.keyCode)
                            {
                            case TileToolbarConst.Key_PencilTool:
                                TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Pencil;
                                break;

                            case TileToolbarConst.Key_LineTool:
                                TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Line;
                                break;

                            case TileToolbarConst.Key_RectTool:
                                TileSetBrushBehaviour.instance.paint_mode =
                                    TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Rect
                      ? TileSetBrushPaintMode.FilledRect
                      : TileSetBrushPaintMode.Rect;//在filledRect和Rect之间切换
                                break;

                            case TileToolbarConst.Key_EllipseTool:
                                TileSetBrushBehaviour.instance.paint_mode =
                                    TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Ellipse
                      ? TileSetBrushPaintMode.FilledEllipse
                      : TileSetBrushPaintMode.Ellipse;//在FilledEllipse和Ellipse之间切换
                                break;
                            }

                            tileSetBrush_mode = TileMapEditorBrushMode.Paint;
                            TileToolbar.instance.tileSetBrushToolbar.TriggerButton((int)tileSetBrush_mode);
                            TileToolbar.instance.tileSetBrushPaintToolbar.TriggerButton((int)TileSetBrushBehaviour.instance
                                                                                        .paint_mode);
                            e.Use();
                            break;
                        }
                    }

                    EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height),
                                                   MouseCursor.Arrow);
                    GUIUtility.hotControl = control_id;
                    {
                        Plane   tileMapChunk_plane = new Plane(tileMap.transform.forward, tileMap.transform.position);
                        Vector2 mouse_pos          = Event.current.mousePosition;
                        mouse_pos.y = Screen.height - mouse_pos.y;
                        Ray   ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                        float distance;
                        if (tileMapChunk_plane.Raycast(ray, out distance))
                        {
                            Rect tile_rect = new Rect(0, 0, tileMap.cell_size.x, tileMap.cell_size.y);
                            tile_rect.position = tileMap.transform.InverseTransformPoint(ray.GetPoint(distance));

                            Vector2 tile_pos = tile_rect.position;
                            if (tile_pos.x < 0)
                            {
                                tile_pos.x -= tileMap.cell_size.x;//因为负数是从-1开始的,正数是从0开始的
                            }
                            if (tile_pos.y < 0)
                            {
                                tile_pos.y -= tileMap.cell_size.y;                 //因为负数是从-1开始的,正数是从0开始的
                            }
                            tile_pos.x        -= tile_pos.x % tileMap.cell_size.x; //取整
                            tile_pos.y        -= tile_pos.y % tileMap.cell_size.y; //取整
                            tile_rect.position = tile_pos;


                            Vector2 start_pos = new Vector2(Mathf.Min(start_dragging_pos.x, end_dragging_pos.x),
                                                            Mathf.Min(start_dragging_pos.y, end_dragging_pos.y));
                            Vector2 end_pos = new Vector2(Mathf.Max(start_dragging_pos.x, end_dragging_pos.x),
                                                          Mathf.Max(start_dragging_pos.y, end_dragging_pos.y));
                            Vector2 selection_snapped_pos = TileSetBrushUtil.GetSnappedPosition(start_pos, tileMap.cell_size);
                            Vector2 selection_size        = TileSetBrushUtil.GetSnappedPosition(end_pos, tileMap.cell_size) - selection_snapped_pos + tileMap.cell_size;

                            TileSetBrushBehaviour tileSetBrushBehaviour = TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap);
                            // Update brush transform
                            local_tileSetBrushBehaviour_pos = (Vector2)tileMap.transform.InverseTransformPoint(ray.GetPoint(distance));//设置tileSetBrushBehaviour的position
                            Vector2 tileSetBrushBehaviour_snapped_pos = TileSetBrushUtil.GetSnappedPosition((tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos), tileMap.cell_size);
                            tileSetBrushBehaviour.transform.rotation   = tileMap.transform.rotation;
                            tileSetBrushBehaviour.transform.localScale = tileMap.transform.lossyScale;
                            if (!TileSetBrushBehaviour.instance.is_dragging)//不是拖动的时候
                            {
                                tileSetBrushBehaviour.transform.position =
                                    tileMap.transform.TransformPoint(new Vector3(tileSetBrushBehaviour_snapped_pos.x, tileSetBrushBehaviour_snapped_pos.y, -0.01f));
                            }

                            int pre_mouse_grid_x = mouse_grid_x;
                            int pre_mouse_grid_y = mouse_grid_y;
                            if (e.isMouse)
                            {
                                mouse_grid_x = TileSetBrushUtil.GetGridX(local_tileSetBrushBehaviour_pos, tileMap.cell_size);
                                mouse_grid_y = TileSetBrushUtil.GetGridY(local_tileSetBrushBehaviour_pos, tileMap.cell_size);
                            }

                            bool is_mouse_grid_changed = pre_mouse_grid_x != mouse_grid_x || pre_mouse_grid_y != mouse_grid_y;
                            //Update Fill Preview
                            if (GetTileSetBrushMode() == TileMapEditorBrushMode.Fill && is_mouse_grid_changed)
                            {
                                fill_preview_tile_pos_list.Clear();
                                TileMapDrawingUtil.FloodFillPreview(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos,
                                                                    tileSetBrushBehaviour.tileMap.GetTileData(0, 0), fill_preview_tile_pos_list);
                            }

                            bool is_modifiers_changed = false;
                            if (e.isKey)
                            {
                                EventModifiers filtered_modifiers = e.modifiers & (EventModifiers.Control | EventModifiers.Alt);
                                is_modifiers_changed = filtered_modifiers != m_prevEventModifiers;
                                m_prevEventModifiers = filtered_modifiers;
                            }

                            if (
                                (EditorWindow.focusedWindow ==
                                 EditorWindow.mouseOverWindow
                                ) && // fix painting tiles when closing another window popup over the SceneView like GameObject Selection window
                                (e.type == EventType.MouseDown || e.type == EventType.MouseDrag && is_mouse_grid_changed ||
                                 e.type == EventType.MouseUp || is_modifiers_changed)
                                )
                            {
                                if (e.button == 0)
                                {
                                    if (mouseDoubleClick.IsDoubleClick && tileSetBrushBehaviour.tileMap.GridWidth == 1 &&
                                        tileSetBrushBehaviour.tileMap.GridHeight == 1)
                                    {
                                        // Restore previous tiledata modified by Paint, because before the double click, a single click is done before
                                        tileMap.SetTileData(tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, flood_fill_restored_tileData);
                                        tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                    }
                                    // Do a brush paint action
                                    else
                                    {
                                        switch (GetTileSetBrushMode())
                                        {
                                        case TileMapEditorBrushMode.Paint:
                                            if (e.type == EventType.MouseDown)
                                            {
                                                flood_fill_restored_tileData = tileMap.GetTileData(mouse_grid_x, mouse_grid_y);
                                            }

                                            if (e.type == EventType.MouseDown)
                                            {
                                                tileSetBrushBehaviour.DoPaintPressed(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            else if (e.type == EventType.MouseDrag || is_modifiers_changed &&
                                                     TileSetBrushBehaviour.instance.paint_mode != TileSetBrushPaintMode.Pencil)
                                            {
                                                tileSetBrushBehaviour.DoPaintDragged(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            else if (e.type == EventType.MouseUp)
                                            {
                                                tileSetBrushBehaviour.DoPaintReleased(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers);
                                            }
                                            break;

                                        case TileMapEditorBrushMode.Erase:
                                            tileSetBrushBehaviour.Erase(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                            break;

                                        case TileMapEditorBrushMode.Fill:
                                            tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos);
                                            break;
                                        }
                                    }
                                }
                                else if (e.button == 1)
                                {
                                    if (e.type == EventType.MouseDown)
                                    {
                                        is_dragging = true;
                                        tileSetBrushBehaviour.tileMap.ClearMap();
                                        start_dragging_pos = end_dragging_pos = local_tileSetBrushBehaviour_pos;
                                    }
                                    else
                                    {
                                        end_dragging_pos = local_tileSetBrushBehaviour_pos;
                                    }
                                }
                            }

                            if (e.type == EventType.MouseUp)
                            {
                                if (e.button == 1) // right mouse button
                                {
                                    is_dragging = false;
                                    ResetTileSetBrushMode();
                                    // Copy one tile
                                    if (selection_size.x <= tileMap.cell_size.x && selection_size.y <= tileMap.cell_size.y)
                                    {
                                        uint tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos);
                                        //Select the first tile not null if any and select the tilemap
                                        if (e.control && tileMap.parent_tileMapGroup)
                                        {
                                            for (int i = tileMap.parent_tileMapGroup.tileMap_list.Count - 1; i >= 0; --i)
                                            {
                                                var tileMap = this.tileMap.parent_tileMapGroup.tileMap_list[i];
                                                tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos);
                                                if (tileData != TileSetConst.TileData_Empty)
                                                {
                                                    tileMap.parent_tileMapGroup.selected_tileMap = tileMap;
                                                    if (Selection.activeGameObject == this.tileMap.gameObject)
                                                    {
                                                        Selection.activeGameObject = tileMap.gameObject;
                                                    }
                                                    break;
                                                }
                                            }
                                        }

                                        if (tileData == TileSetConst.TileData_Empty)
                                        {
                                            tileMap.tileSet.selected_tileId = TileSetConst.TileId_Empty;
                                            tileSetBrushBehaviour.tileMap.SetTileData(0, 0, TileSetConst.TileData_Empty);
                                        }
                                        else
                                        {
                                            int brushId = TileSetUtil.GetTileSetBrushIdFromTileData(tileData);
                                            int tileId  = TileSetUtil.GetTileIdFromTileData(tileData);

                                            // Select the copied tile in the tileset
                                            if (brushId > 0 && !e.alt) //NOTE: if Alt is held, the tile is selected instead
                                            {
                                                tileMap.tileSet.selected_tileSetBrushId = brushId;
                                            }
                                            else
                                            {
                                                tileMap.tileSet.selected_tileId = tileId;
                                                tileSetBrushBehaviour.tileMap.SetTileData(0, 0,
                                                                                          tileData & ~TileSetConst.TileDataMask_TileSetBrushId); // keep tile flags
                                            }
                                        }

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size);
                                            int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size);
                                            tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, startGridX, startGridY);
                                        }

                                        tileSetBrushBehaviour.tileMap.UpdateMesh();
                                        tileSetBrushBehaviour.offset = Vector2.zero;
                                    }
                                    // copy a rect of tiles
                                    else
                                    {
                                        int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size);
                                        int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size);
                                        int endGridX   = TileSetBrushUtil.GetGridX(end_pos, tileMap.cell_size);
                                        int endGridY   = TileSetBrushUtil.GetGridY(end_pos, tileMap.cell_size);

                                        // Cut tile if key shift is pressed
                                        if (e.shift)
                                        {
                                            tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, endGridX, endGridY);
                                        }
                                        else
                                        {
                                            tileSetBrushBehaviour.CopyRect(tileMap, startGridX, startGridY, endGridX, endGridY);
                                        }

                                        tileSetBrushBehaviour.offset.x = end_dragging_pos.x > start_dragging_pos.x
                    ? -(endGridX - startGridX) * tileMap.cell_size.x
                    : 0f;
                                        tileSetBrushBehaviour.offset.y = end_dragging_pos.y > start_dragging_pos.y
                    ? -(endGridY - startGridY) * tileMap.cell_size.y
                    : 0f;
                                    }
                                }
                            }

                            if (is_dragging)
                            {
                                Rect rGizmo = new Rect(selection_snapped_pos, selection_size);
                                DrawUtil.HandlesDrawSolidRectangleWithOutline(rGizmo, new Color(), Color.white, tileMap.transform);
                            }
                            else // Draw brush border
                            {
                                Rect rBound = new Rect(tileSetBrushBehaviour.tileMap.tileMapBounds.min,
                                                       tileSetBrushBehaviour.tileMap.tileMapBounds.size);
                                Color fillColor;
                                switch (GetTileSetBrushMode())
                                {
                                case TileMapEditorBrushMode.Paint:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;

                                case TileMapEditorBrushMode.Erase:
                                    fillColor = new Color(1f, 0f, 0f, 0.1f);
                                    break;

                                case TileMapEditorBrushMode.Fill:
                                    fillColor = new Color(1f, 1f, 0f, 0.2f);
                                    break;

                                default:
                                    fillColor = new Color(0, 0, 0, 0);
                                    break;
                                }

                                DrawUtil.HandlesDrawSolidRectangleWithOutline(rBound, fillColor, new Color(1, 1, 1, 0.2f),
                                                                              tileSetBrushBehaviour.transform);
                            }
                        }
                    }

                    if (current_event_type == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button
                    {
                        // avoid dragging the map
                        Event.current.Use();
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }

            SceneView.RepaintAll();
            GUIUtility.hotControl = save_control;
        }
Exemplo n.º 12
0
        public void DoPaintDragged(TileMap tileMap, Vector2 local_pos, EventModifiers modifiers = default(EventModifiers))
        {
            //Debug.Log("DoPaintDragged (" + TilemapUtils.GetGridX(tilemap, localPos) + "," + TilemapUtils.GetGridY(tilemap, localPos) + ")");
            if (paint_mode == TileSetBrushPaintMode.Pencil)
            {
                Paint(tileMap, local_pos);
            }
            else
            {
                if (is_dragging)
                {
                    this.tileMap.ClearMap();
                    Vector2 brushLocPos = tileMap.transform.InverseTransformPoint(transform.position);
                    Vector2 startPos    = TileSetBrushUtil.GetSnappedPosition(pressed_position, this.tileMap.cell_size) + this.tileMap.cell_size / 2f -
                                          brushLocPos;
                    Vector2 endPos = TileSetBrushUtil.GetSnappedPosition(local_pos, this.tileMap.cell_size) + this.tileMap.cell_size / 2f -
                                     brushLocPos;
                    bool isCtrl  = (modifiers & EventModifiers.Control) != 0;
                    bool isShift = (modifiers & EventModifiers.Shift) != 0;
                    switch (paint_mode)
                    {
                    case TileSetBrushPaintMode.Line:
                        if (isCtrl)
                        {
                            TileMapDrawingUtil.DrawLineMirrored(this.tileMap, startPos, endPos, brushPattern);
                        }
                        else
                        {
                            TileMapDrawingUtil.DrawLine(this.tileMap, startPos, endPos, brushPattern);
                        }
                        break;

                    case TileSetBrushPaintMode.Rect:
                    case TileSetBrushPaintMode.FilledRect:
                    case TileSetBrushPaintMode.Ellipse:
                    case TileSetBrushPaintMode.FilledEllipse:
                        if (isShift)
                        {
                            Vector2 vTemp = endPos - startPos;
                            float   absX  = Mathf.Abs(vTemp.x);
                            float   absY  = Mathf.Abs(vTemp.y);
                            vTemp.x = (absX > absY) ? vTemp.x : Mathf.Sign(vTemp.x) * absY;
                            vTemp.y = Mathf.Sign(vTemp.y) * Mathf.Abs(vTemp.x);
                            endPos  = startPos + vTemp;
                        }

                        if (isCtrl)
                        {
                            startPos = 2f * startPos - endPos;
                        }
                        if (paint_mode == TileSetBrushPaintMode.Rect || paint_mode == TileSetBrushPaintMode.FilledRect)
                        {
                            TileMapDrawingUtil.DrawRect(this.tileMap, startPos, endPos, brushPattern,
                                                        paint_mode == TileSetBrushPaintMode.FilledRect, (modifiers & EventModifiers.Alt) != 0);
                        }
                        else if (paint_mode == TileSetBrushPaintMode.Ellipse || paint_mode == TileSetBrushPaintMode.FilledEllipse)
                        {
                            TileMapDrawingUtil.DrawEllipse(this.tileMap, startPos, endPos, brushPattern,
                                                           paint_mode == TileSetBrushPaintMode.FilledEllipse);
                        }
                        break;
                    }

                    this.tileMap.UpdateMeshImmediate();
                }
            }
        }