Beispiel #1
0
        public static void DrawdTerrainTypeRect(AStarMonoBehaviour astarMonoBehaviour, int gridX, int gridY,
                                                int terrainType)
        {
            Rect cellRect = new Rect(0, 0, astarMonoBehaviour.astarConfigData.cellSize.x,
                                     astarMonoBehaviour.astarConfigData.cellSize.y);

            cellRect.position = astarMonoBehaviour.astarConfigData.GetPosition(gridX, gridY);
            DrawUtil.HandlesDrawSolidRectangleWithOutline(cellRect, default(Color), Color.green,
                                                          astarMonoBehaviour.transform);
            if (terrainType != 0)
            {
                DrawUtil.HandlesDrawString(astarMonoBehaviour.transform.TransformPoint(cellRect.center),
                                           terrainType.ToString());
            }
        }
 void DrawBounds()
 {
     for (int gridY = _target.astarConfigData.minGridY; gridY <= _target.astarConfigData.maxGridY; gridY++)
     {
         for (int gridX = _target.astarConfigData.minGridX;
              gridX <= _target.astarConfigData.maxGridX;
              gridX++)
         {
             Rect cellRect = new Rect(0, 0, _target.astarConfigData.cellSize.x,
                                      _target.astarConfigData.cellSize.y);
             cellRect.position = _target.astarConfigData.GetPosition(gridX, gridY);
             DrawUtil.HandlesDrawSolidRectangleWithOutline(cellRect, default(Color), Color.white,
                                                           _target.transform);
         }
     }
 }
        void DrawInfo()
        {
            string     info           = "<b>Grid:(" + mouseGridX + "," + mouseGridY + ") </b>";
            GUIContent infoGUIContent = info.ToGUIContent();
            Rect       infoRect       = new Rect(new Vector2(4f, 4f), GUIStyleConst.ToolbarBoxStyle.CalcSize(infoGUIContent));

            using (new HandlesBeginGUIScope())
            {
                using (new GUILayoutBeginAreaScope(infoRect))
                {
                    DrawUtil.HandlesDrawSolidRectangleWithOutline(new Rect(Vector2.zero, infoRect.size),
                                                                  new Color(0, 0, 1, 0.2f), Color.black);
                    GUILayout.Label(info, GUIStyleConst.ToolbarBoxStyle);
                }
            }
        }
        void DrawTips()
        {
            GUIContent infoGUIContent = "按住Ctrl进行绘制".ToGUIContent();
            Rect       infoRect       = new Rect(new Vector2(230, 6), GUIStyleConst.ToolbarBoxStyle.CalcSize(infoGUIContent));

            using (new HandlesBeginGUIScope())
            {
                using (new GUILayoutBeginAreaScope(infoRect))
                {
                    DrawUtil.HandlesDrawSolidRectangleWithOutline(new Rect(Vector2.zero, infoRect.size),
                                                                  new Color(1, 1, 0, 1f), Color.black);
                    using (new GUIColorScope(Color.black))
                    {
                        GUILayout.Label(infoGUIContent, GUIStyleConst.ToolbarBoxStyle);
                    }
                }
            }
        }
        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;
        }
Beispiel #6
0
        void DrawToolbar()
        {
            GUIContent tileSetBrush_coords_guiContent        = new GUIContent(string.Format("<b> Brush Pos: ({0},{1})</b>", mouse_grid_x, mouse_grid_y));
            GUIContent selected_tileId_or_brushId_guiContent = null;

            if (tileMap.tileSet.selected_tileId != TileSetConst.TileId_Empty)
            {
                selected_tileId_or_brushId_guiContent =
                    new GUIContent(string.Format("<b> Selected Tile Id: {0}</b>", tileMap.tileSet.selected_tileId));
            }
            else if (tileMap.tileSet.selected_tileSetBrushId != TileSetConst.TileSetBrushId_Default)
            {
                selected_tileId_or_brushId_guiContent =
                    new GUIContent(string.Format("<b> Selected Brush Id: {0}</b>", tileMap.tileSet.selected_tileSetBrushId));
            }
            else
            {
                selected_tileId_or_brushId_guiContent = new GUIContent("<b> Empty tile selected</b>");
            }

            Rect toolbar_rect = new Rect(4f, 4f,
                                         Mathf.Max(
                                             Mathf.Max(GUIStyleConst.Toolbar_Box_Style.CalcSize(tileSetBrush_coords_guiContent).x,
                                                       GUIStyleConst.Toolbar_Box_Style.CalcSize(selected_tileId_or_brushId_guiContent).x) + 4f, 180f), 54f);

            using (new HandlesBeginGUIScope())
            {
                using (new GUILayoutBeginAreaScope(toolbar_rect))
                {
                    DrawUtil.HandlesDrawSolidRectangleWithOutline(new Rect(Vector2.zero, toolbar_rect.size), toolbar_box_background_color,
                                                                  toolbar_box_outline_color);

                    GUILayout.Space(2f);
                    GUILayout.Label(tileSetBrush_coords_guiContent, GUIStyleConst.Toolbar_Box_Style);
                    if (selected_tileId_or_brushId_guiContent != null)
                    {
                        GUILayout.Label(selected_tileId_or_brushId_guiContent, GUIStyleConst.Toolbar_Box_Style);
                    }
                    GUILayout.Label("<b> F1 - 显示帮助</b>", GUIStyleConst.Toolbar_Box_Style);
                    GUILayout.Label("<b> F5 - 刷新tileMap</b>", GUIStyleConst.Toolbar_Box_Style);
                }

                // 显示Toolbar
                Vector2 toolbar_pos         = new Vector2(toolbar_rect.xMax + 4f, toolbar_rect.y);
                Vector2 toolbar_button_size = new Vector2(32f, 32f);
                TileToolbar.instance.tileSetBrushToolbar.SetHighlight((int)TileToolbarEditMode.Erase,
                                                                      GetTileSetBrushMode() == TileMapEditorBrushMode.Erase);
                TileToolbar.instance.tileSetBrushToolbar.DrawGUI(toolbar_pos, toolbar_button_size, toolbar_box_background_color,
                                                                 toolbar_box_outline_color);

                if (Tools.current != Tool.None && Tools.current != Tool.Rect)
                {
                    Rect warn_rect = new Rect(toolbar_pos.x, toolbar_pos.y + toolbar_button_size.y, 370f, 22f);
                    using (new GUILayoutBeginAreaScope(warn_rect))
                    {
                        EditorGUI.HelpBox(new Rect(Vector2.zero, warn_rect.size),
                                          "选择一个tool_button开始绘画", MessageType.Warning);
                    }
                }
                else if (tileMapEditorBrushMode == TileMapEditorBrushMode.Paint)
                {
                    toolbar_pos.y += toolbar_button_size.y;
                    TileToolbar.instance.tileSetBrushPaintToolbar.DrawGUI(toolbar_pos, toolbar_button_size,
                                                                          toolbar_box_background_color * 1.4f, toolbar_box_outline_color * 1.4f);
                }
            }


            if (is_display_help_box)
            {
                DisplayHelpBox();
            }

            if (Event.current.type == EventType.KeyDown)
            {
                if (Event.current.keyCode == KeyCode.F1)
                {
                    is_display_help_box = !is_display_help_box;
                }
                else if (Event.current.keyCode == KeyCode.F5)
                {
                    tileMap.Refresh(true, true, true, true);
                }
            }
        }
        public void DisplayTileSetBrush()
        {
            var  e = Event.current;
            bool is_mouse_inside_tileSetBrush_palette_scroll_area =
                e.isMouse && tileSetBrush_palette_scroll_area_rect.Contains(e.mousePosition);

            using (new EditorGUILayoutBeginHorizontalScope())
            {
                string tileSetBrushId_label = tileSet.selected_tileSetBrushId > 0
        ? string.Format("(id:{0})", tileSet.selected_tileSetBrushId)
        : "";
                EditorGUILayout.LabelField("TileSetBrush Palette" + tileSetBrushId_label, EditorStyles.boldLabel);
                is_display_tileSetBrush_reordableList =
                    GUILayoutUtil.ToggleButton("Display List", is_display_tileSetBrush_reordableList);
            }

//    tileSet.tileSetBrush_type_mask = EditorGUILayout.MaskField("TileSetBrush Mask", tileSet.tileSetBrush_type_mask,
//      tileSet.GetTileSetBrushTypeArray());

            int column_count = (int)(tile_scroll_size_rect.width / (tileSet.visual_tile_size.x + visual_tile_padding));

            if (column_count <= 0)
            {
                column_count = 1;
            }
            float tileSetBrushes_scroll_max_height = Screen.height / 4;
            //commented because m_rTileScrollSize.width.height was changed to Screen.height;  fBrushesScrollMaxHeight -= fBrushesScrollMaxHeight % 2; // sometimes because size of tile scroll affects size of brush scroll, the height is dancing between +-1, so this is always taking the pair value
            float tileSetBrush_palette_scroll_area_height = Mathf.Min(tileSetBrushes_scroll_max_height,
                                                                      4 + (tileSet.visual_tile_size.y + visual_tile_padding) * (1 + (tileSet.tileSetBrush_list.Count / column_count)));

            using (new EditorGUILayoutBeginVerticalScope(GUILayout.MinHeight(tileSetBrush_palette_scroll_area_height)))
            {
                if (is_display_tileSetBrush_reordableList)
                {
                    DisplayTileSetBrushReorderableList();
                }
                else
                {
                    bool    is_refresh_tileSetBrushList = false;
                    Vector2 tileSetBrushes_scroll_pos   = sharedTileSetData.tileSetBrushes_scroll_pos;
                    using (new EditorGUILayoutBeginScrollViewScope(ref tileSetBrushes_scroll_pos, GUIStyleConst.Scroll_Style))
                    {
                        if (is_update_scroll_pos)
                        {
                            sharedTileSetData.tileSetBrushes_scroll_pos = tileSetBrushes_scroll_pos;
                        }

                        Rect scroll_view_rect = new Rect(0, 0, tile_scroll_size_rect.width, 0);
                        column_count =
                            Mathf.Clamp((int)scroll_view_rect.width / (int)(tileSet.visual_tile_size.x + visual_tile_padding), 1,
                                        column_count);
                        if (tileSet.tileSetBrush_list != null)
                        {
                            GUILayout.Space((tileSet.visual_tile_size.y + visual_tile_padding) *
                                            (1 + (tileSet.tileSetBrush_list.Count - 1) / column_count));
                            for (int i = 0, index = 0; i < tileSet.tileSetBrush_list.Count; ++i, ++index)
                            {
                                var tileSetBrushContainer = tileSet.tileSetBrush_list[i];
                                if (tileSetBrushContainer.tileSetBrush == null || tileSetBrushContainer.tileSetBrush.tileSet != tileSet)
                                {
                                    is_refresh_tileSetBrushList = true;
                                    continue;
                                }

                                if (!tileSetBrushContainer.tileSetBrush.is_show_in_palette ||
                                    !tileSet.IsTileSetBrushVisibleByTypeMask(tileSetBrushContainer.tileSetBrush))
                                {
                                    --index;
                                    continue;
                                }

                                int  tile_x           = index % column_count;
                                int  tile_y           = index / column_count;
                                Rect visual_tile_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding),
                                                                 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), tileSet.visual_tile_size.x,
                                                                 tileSet.visual_tile_size.y);
                                //Fix Missing Tileset reference
                                if (tileSetBrushContainer.tileSetBrush.tileSet == null)
                                {
                                    LogCat.warn(string.Format("Fixed missing tileset reference in brush {0}  Id({1})",
                                                              tileSetBrushContainer.tileSetBrush.name, tileSetBrushContainer.id));
                                    tileSetBrushContainer.tileSetBrush.tileSet = tileSet;
                                }

                                uint tileData = TileSetConst.TileData_Empty;
                                if (tileSetBrushContainer.tileSetBrush.IsAnimated())
                                {
                                    tileData = tileSetBrushContainer.tileSetBrush.GetAnimTileData();
                                }
                                else
                                {
                                    tileData = tileSetBrushContainer.tileSetBrush.PreviewTileData();
                                }
                                TileSetEditor.DoGUIDrawTileFromTileData(visual_tile_rect, tileData, tileSet,
                                                                        tileSetBrushContainer.tileSetBrush.GetAnimUV());
                                if ((is_left_mouse_released || is_right_mouse_released || mouseDoubleClick.IsDoubleClick) && //左键放开,右键放开,双击鼠标
                                    is_mouse_inside_tileSetBrush_palette_scroll_area && visual_tile_rect.Contains(Event.current.mousePosition))//点钟该区域
                                {
                                    tileSet.selected_tileSetBrushId = tileSetBrushContainer.id;
                                    RemoveTileSelection();
                                    if (mouseDoubleClick.IsDoubleClick)
                                    {
                                        EditorGUIUtility.PingObject(tileSetBrushContainer.tileSetBrush);
                                        selected_tileSetBrush_in_inspector = tileSetBrushContainer.tileSetBrush;
                                    }

                                    if (is_right_mouse_released)
                                    {
                                        TilePropertiesEditor.Show(tileSet);
                                    }
                                }
                                else if (tileSet.selected_tileSetBrushId == tileSetBrushContainer.id)
                                {
                                    Rect selection_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding),
                                                                   2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding),
                                                                   (tileSet.visual_tile_size.x + visual_tile_padding),
                                                                   (tileSet.visual_tile_size.y + visual_tile_padding));
                                    DrawUtil.HandlesDrawSolidRectangleWithOutline(selection_rect, new Color(0f, 0f, 0f, 0.1f),
                                                                                  new Color(1f, 1f, 0f, 1f));
                                }
                            }


                            if (is_refresh_tileSetBrushList)
                            {
                                tileSet.RemoveInvalidTileSetBrushList();
                                tileSet.UpdateTileSetBrushTypeArray();
                            }
                        }
                    }

                    if (e.type == EventType.Repaint)
                    {
                        tileSetBrush_palette_scroll_area_rect = GUILayoutUtility.GetLastRect();
                    }
                }
            }

            if (GUILayout.Button("Import all tileSetBrush_list found in the project"))
            {
                AddAllTileSetBrushesFoundInTheProject(tileSet);
                EditorUtility.SetDirty(tileSet);
            }
        }
Beispiel #8
0
        private Vector2 last_tile_palette_scroll_mouse_pos;//上次在tile_palette中scroll_mouse_pos

        public void DrawTilePalette()
        {
            Event e = Event.current;

            is_left_mouse_released  = e.type == EventType.MouseUp && e.button == 0;                                          //鼠标左键放开
            is_right_mouse_released = e.type == EventType.MouseUp && e.button == 1;                                          //鼠标右键放开
            is_mouse_inside_tile_palette_scroll_area = e.isMouse && tile_palette_scroll_area_rect.Contains(e.mousePosition); //鼠标是否在tile_palette中


            //header
            string tileId_label = tileSet.selected_tileId != TileSetConst.TileId_Empty
     ? string.Format("(id:{0})", tileSet.selected_tileId)
     : "";

            EditorGUILayout.LabelField(string.Format("Tile Palette {0}", tileId_label), EditorStyles.boldLabel);


            sharedTileSetData.tileView_column_count_in_tile_palette = Mathf.Max(1, sharedTileSetData.tileView_column_count_in_tile_palette);
            float tile_area_width  = sharedTileSetData.tileView_column_count_in_tile_palette * (tileSet.visual_tile_size.x + visual_tile_padding) + 4f;
            float tile_area_height = (tileSet.visual_tile_size.y + visual_tile_padding) * (1 + (visible_tile_count - 1) / sharedTileSetData.tileView_column_count_in_tile_palette) + 4f;

            sharedTileSetData.tileView_column_count_in_tile_palette = tileView != null ? tileView.tileSelection.column_count : tileSet.column_tile_count_in_palette;
            Vector2 tile_palette_scroll_pos = sharedTileSetData.tile_palette_scroll_pos;

            using (new EditorGUILayoutBeginScrollViewScope(ref tile_palette_scroll_pos, GUIStyleConst.Scroll_Style))
            {
                //更新新tile_palette中的scroll_pos
                if (is_update_scroll_pos)
                {
                    sharedTileSetData.tile_palette_scroll_pos = tile_palette_scroll_pos;
                }
                if (e.type == EventType.MouseDrag && (e.button == 1 || e.button == 2))//鼠标中键或者右键拖动中
                {
                    sharedTileSetData.tile_palette_scroll_pos = sharedTileSetData.tile_palette_scroll_pos - e.delta;
                }
                else
                {
                    DoAutoScroll();
                }

                //记录上次在tile_palette中scroll_mouse_pos
                if (e.isMouse)
                {
                    last_tile_palette_scroll_mouse_pos = e.mousePosition;
                }

                if (tileSet.tile_list != null)
                {
                    GUILayoutUtility.GetRect(tile_area_width, tile_area_height);
                    visible_tile_count = 0;
                    List <uint> visible_tile_list     = new List <uint>();
                    int         tileView_column_count = tileView != null ? tileView.tileSelection.column_count : tileSet.column_tile_count;
                    int         tileView_row_count    = tileView != null ? ((tileView.tileSelection.selection_tileData_list.Count - 1) / tileView.tileSelection.column_count) + 1 : tileSet.row_tile_count;
                    //在tile_palette中全部的tile个数(可能比实际的tile_count多,因为可能出现空的tile来填满行或者列的tile)
                    int total_tile_count_in_tile_palette = ((((tileView_column_count - 1) / sharedTileSetData.tileView_column_count_in_tile_palette) + 1) * sharedTileSetData.tileView_column_count_in_tile_palette) * tileView_row_count;
                    int tileId_offset = 0;

                    for (int i = 0; i < total_tile_count_in_tile_palette; ++i)
                    {
                        //i是在tile_palette中的index(在column_tile_count_in_palette下)
                        int  tileId   = GetTileIdFromIndex(i, sharedTileSetData.tileView_column_count_in_tile_palette, tileView_column_count, tileView_row_count) + tileId_offset;
                        uint tileData = (uint)tileId;
                        if (tileView != null && tileId != TileSetConst.TileId_Empty)
                        {
                            //如果是tileVeiw则,上面的tileId是在tileView中的index
                            tileData = tileView.tileSelection.selection_tileData_list[tileId - tileId_offset];
                            tileId   = TileSetUtil.GetTileIdFromTileData(tileData);
                        }
                        Tile tile = tileSet.GetTile(tileId);
                        while (tile != null && tile.uv == default(Rect)) // 忽略无效的tiles(tile.uv == default(Rect),普通的tile是有uv的)
                        {
                            tile          = tileSet.GetTile(++tileId);
                            tileData      = (uint)tileId;
                            tileId_offset = tileId;
                        }
                        visible_tile_list.Add(tileData);

                        int tile_x = visible_tile_count % sharedTileSetData.tileView_column_count_in_tile_palette; //在tile_palette中的x
                        int tile_y = visible_tile_count / sharedTileSetData.tileView_column_count_in_tile_palette; //在tile_palette中的y
                        //当前tile的rect
                        Rect visual_tile_rect       = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), tileSet.visual_tile_size.x, tileSet.visual_tile_size.y);
                        Rect local_visual_tile_rect = visual_tile_rect;
                        local_visual_tile_rect.position -= sharedTileSetData.tile_palette_scroll_pos;//相对于当前的scroll_rect区域矩形位置
                        if (local_visual_tile_rect.Overlaps(tile_scroll_size_rect))
                        {
                            // 画tile (Draw Tile)
                            if (tile == null)
                            {
                                DrawUtil.HandlesDrawSolidRectangleWithOutline(visual_tile_rect, new Color(0f, 0f, 0f, 0.2f), new Color(0f, 0f, 0f, 0.2f));
                            }
                            else
                            {
                                DrawUtil.HandlesDrawSolidRectangleWithOutline(visual_tile_rect, new Color(0f, 0f, 0f, 0.1f), new Color(0f, 0f, 0f, 0.1f));
                                TileSetEditor.DoGUIDrawTileFromTileData(visual_tile_rect, tileData, tileSet);
                            }
                            Rect tile_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), (tileSet.visual_tile_size.x + visual_tile_padding), (tileSet.visual_tile_size.y + visual_tile_padding));

                            if (visual_tile_rect.Contains(e.mousePosition))         //当前tile的矩形包含鼠标
                            {
                                if (e.type == EventType.MouseDrag && e.button == 0) //鼠标左键点击并且拖动,设置sharedTileSetData.pointed_tile_index_rect
                                {
                                    sharedTileSetData.pointed_tile_index_rect = new KeyValuePair <int, Rect>(visible_tile_count, tile_rect);
                                }
                                else if (e.type == EventType.MouseDown && e.button == 0)//鼠标左键按下,设置sharedTileSetData.start_drag_tile_index_rect,sharedTileSetData.pointed_tile_index_rect, sharedTileSetData.end_drag_tile_index_rect
                                {
                                    sharedTileSetData.start_drag_tile_index_rect   = sharedTileSetData.pointed_tile_index_rect =
                                        sharedTileSetData.end_drag_tile_index_rect =
                                            new KeyValuePair <int, Rect>(visible_tile_count, tile_rect);
                                }
                                else if (e.type == EventType.MouseUp && e.button == 0)//放开鼠标左键,设置sharedTileSetData.end_drag_tile_index_rect
                                {
                                    sharedTileSetData.end_drag_tile_index_rect = new KeyValuePair <int, Rect>(visible_tile_count, tile_rect);
                                    DoSetTileSelection();
                                }
                            }

                            if ((is_left_mouse_released || is_right_mouse_released) && //放开鼠标左键或者右键
                                is_mouse_inside_tile_palette_scroll_area && visual_tile_rect.Contains(e.mousePosition) && //鼠标位置处在该tile中
                                (sharedTileSetData.start_drag_tile_index_rect.Key == sharedTileSetData.end_drag_tile_index_rect.Key) && // 没有拖动
                                tile_scroll_size_rect.Contains(e.mousePosition - sharedTileSetData.tile_palette_scroll_pos))// and it's inside the scroll area
                            {
                                tileSet.selected_tileId = tileId;

                                //Give focus to SceneView to get key events
                                FocusSceneView();
                                //放开鼠标右键,打开显示tile的属性面板
                                if (is_right_mouse_released)
                                {
                                    TilePropertiesEditor.Show(tileSet);
                                }
                            }
                            else if (tile != null && tileSet.selected_tileId == tileId)//用黄色外边框选中的tile
                            {
                                DrawUtil.HandlesDrawSolidRectangleWithOutline(tile_rect, new Color(0f, 0f, 0f, 0.1f),
                                                                              new Color(1f, 1f, 0f, 1f));
                            }
                        }
                        ++visible_tile_count;
                    }
                    this.visible_tile_list = visible_tile_list;
                    // 画选中的矩形 用于TileSelection
                    if (sharedTileSetData.start_drag_tile_index_rect.Key != sharedTileSetData.pointed_tile_index_rect.Key)
                    {
                        Rect selection_rect = new Rect(sharedTileSetData.start_drag_tile_index_rect.Value.center, sharedTileSetData.pointed_tile_index_rect.Value.center - sharedTileSetData.start_drag_tile_index_rect.Value.center);
                        selection_rect.Set(Mathf.Min(selection_rect.xMin, selection_rect.xMax), Mathf.Min(selection_rect.yMin, selection_rect.yMax), Mathf.Abs(selection_rect.width), Mathf.Abs(selection_rect.height));
                        selection_rect.xMin -= sharedTileSetData.start_drag_tile_index_rect.Value.width / 2;
                        selection_rect.xMax += sharedTileSetData.start_drag_tile_index_rect.Value.width / 2;
                        selection_rect.yMin -= sharedTileSetData.start_drag_tile_index_rect.Value.height / 2;
                        selection_rect.yMax += sharedTileSetData.start_drag_tile_index_rect.Value.height / 2;
                        DrawUtil.HandlesDrawSolidRectangleWithOutline(selection_rect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                    }
                }
            }
            if (e.type == EventType.Repaint)
            {
                tile_palette_scroll_area_rect  = GUILayoutUtility.GetLastRect();
                tile_scroll_size_rect          = tile_palette_scroll_area_rect;
                tile_scroll_size_rect.position = Vector2.zero; // reset position to the Contains and Overlaps inside the tile scroll view without repositioning the position of local positions
                if (tile_area_width > tile_scroll_size_rect.width)
                {
                    tile_scroll_size_rect.height -= GUI.skin.verticalScrollbar.fixedWidth;
                }
                if (tile_area_height > tile_scroll_size_rect.height)
                {
                    tile_scroll_size_rect.width -= GUI.skin.verticalScrollbar.fixedWidth;
                }
            }
        }