| |
---|
| | |
---|
| | public partial class EditorTileMap : TileMap |
---|
| | { |
---|
| | |
---|
| | public enum MouseLeftButtonType |
---|
| | public enum MouseButtonType |
---|
| | { |
---|
| | /// <summary> |
---|
| | /// 无状态 |
---|
| | /// </summary> |
---|
| |
---|
| | /// 笔 |
---|
| | /// </summary> |
---|
| | Pen, |
---|
| | /// <summary> |
---|
| | /// 区域模式 |
---|
| | /// 绘制区域模式 |
---|
| | /// </summary> |
---|
| | Area |
---|
| | Area, |
---|
| | /// <summary> |
---|
| | /// 编辑门区域模式 |
---|
| | /// </summary> |
---|
| | Door, |
---|
| | } |
---|
| | |
---|
| | /// <summary> |
---|
| | /// 自动图块地板层 |
---|
| |
---|
| | /// 编辑器工具UI |
---|
| | /// </summary> |
---|
| | public MapEditorToolsPanel MapEditorToolsPanel { get; set; } |
---|
| | |
---|
| | //是否启用绘制图块 |
---|
| | private MouseLeftButtonType _mouseLeftButtonType = MouseLeftButtonType.None; |
---|
| | /// <summary> |
---|
| | /// 左键功能 |
---|
| | /// </summary> |
---|
| | public MouseButtonType MouseType { get; set; } = MouseButtonType.Area; |
---|
| | |
---|
| | //鼠标坐标 |
---|
| | private Vector2 _mousePosition; |
---|
| | //鼠标所在的cell坐标 |
---|
| | private Vector2I _mouseCellPosition; |
---|
| |
---|
| | |
---|
| | if (!MapEditorToolsPanel.S_HBoxContainer.Instance.IsPositionOver(GetGlobalMousePosition())) //不在Ui节点上 |
---|
| | { |
---|
| | //左键绘制 |
---|
| | if (_isLeftPressed && _mouseLeftButtonType == MouseLeftButtonType.Pen) |
---|
| | { |
---|
| | if (Input.IsKeyPressed(Key.Shift)) //按住shift绘制矩形 |
---|
| | if (_isLeftPressed) |
---|
| | { |
---|
| | if (MouseType == MouseButtonType.Pen) //绘制单格 |
---|
| | { |
---|
| | if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过 |
---|
| | { |
---|
| | _changeFlag = true; |
---|
| | _prevMouseCellPosition = _mouseCellPosition; |
---|
| | //绘制自动图块 |
---|
| | SetSingleAutoCell(_mouseCellPosition); |
---|
| | } |
---|
| | } |
---|
| | else if (MouseType == MouseButtonType.Area) //绘制区域 |
---|
| | { |
---|
| | _drawFullRect = true; |
---|
| | } |
---|
| | else if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过 |
---|
| | { |
---|
| | _changeFlag = true; |
---|
| | _prevMouseCellPosition = _mouseCellPosition; |
---|
| | //绘制自动图块 |
---|
| | SetSingleAutoCell(_mouseCellPosition); |
---|
| | } |
---|
| | } |
---|
| | else if (_isRightPressed && _mouseLeftButtonType == MouseLeftButtonType.Pen) //右键擦除 |
---|
| | { |
---|
| | if (Input.IsKeyPressed(Key.Shift)) //按住shift擦除矩形 |
---|
| | else if (MouseType == MouseButtonType.Drag) //拖拽 |
---|
| | { |
---|
| | SetMapPosition(GetGlobalMousePosition() + _moveOffset); |
---|
| | } |
---|
| | } |
---|
| | else if (_isRightPressed) //右键擦除 |
---|
| | { |
---|
| | if (MouseType == MouseButtonType.Pen) //绘制单格 |
---|
| | { |
---|
| | if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过 |
---|
| | { |
---|
| | _changeFlag = true; |
---|
| | _prevMouseCellPosition = _mouseCellPosition; |
---|
| | EraseSingleAutoCell(_mouseCellPosition); |
---|
| | } |
---|
| | } |
---|
| | else if (MouseType == MouseButtonType.Area) //绘制区域 |
---|
| | { |
---|
| | _drawFullRect = true; |
---|
| | } |
---|
| | else if (_prevMouseCellPosition != _mouseCellPosition || !_changeFlag) //鼠标位置变过 |
---|
| | { |
---|
| | _changeFlag = true; |
---|
| | _prevMouseCellPosition = _mouseCellPosition; |
---|
| | EraseSingleAutoCell(_mouseCellPosition); |
---|
| | else if (MouseType == MouseButtonType.Drag) //拖拽 |
---|
| | { |
---|
| | SetMapPosition(GetGlobalMousePosition() + _moveOffset); |
---|
| | } |
---|
| | } |
---|
| | else if (_isMiddlePressed) //中键移动 |
---|
| | { |
---|
| | //GD.Print("移动..."); |
---|
| | SetMapPosition(GetGlobalMousePosition() + _moveOffset); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| |
---|
| | _generateTimer -= newDelta; |
---|
| | if (_generateTimer <= 0) |
---|
| | { |
---|
| | //计算区域 |
---|
| | CalcTileRect(); |
---|
| | CalcTileRect(false); |
---|
| | GD.Print("开始检测是否可以生成地形..."); |
---|
| | if (CheckTerrain()) |
---|
| | { |
---|
| | GD.Print("开始绘制导航网格..."); |
---|
| |
---|
| | Utils.DrawNavigationPolygon(canvasItem, polygonData, 3f / Scale.X); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | if (_mouseLeftButtonType == MouseLeftButtonType.Pen || _mouseLeftButtonType == MouseLeftButtonType.Area) |
---|
| | if (MouseType == MouseButtonType.Pen || MouseType == MouseButtonType.Area) |
---|
| | { |
---|
| | if (_drawFullRect) //绘制填充矩形 |
---|
| | { |
---|
| | var size = TileSet.TileSize; |
---|
| |
---|
| | if (mouseButton.ButtonIndex == MouseButton.Left) //左键 |
---|
| | { |
---|
| | if (mouseButton.Pressed) //按下 |
---|
| | { |
---|
| | _moveOffset = Position - GetGlobalMousePosition(); |
---|
| | _mouseStartCellPosition = LocalToMap(GetLocalMousePosition()); |
---|
| | } |
---|
| | else |
---|
| | { |
---|
| |
---|
| | else if (mouseButton.ButtonIndex == MouseButton.Right) //右键 |
---|
| | { |
---|
| | if (mouseButton.Pressed) //按下 |
---|
| | { |
---|
| | _moveOffset = Position - GetGlobalMousePosition(); |
---|
| | _mouseStartCellPosition = LocalToMap(GetLocalMousePosition()); |
---|
| | } |
---|
| | else |
---|
| | { |
---|
| |
---|
| | |
---|
| | //聚焦 |
---|
| | //MapEditorPanel.CallDelay(0.1f, OnClickCenterTool); |
---|
| | CallDeferred(nameof(OnClickCenterTool)); |
---|
| | |
---|
| | // var doorPos = (_roomPosition + new Vector2I(_roomSize.X - 1, 1)) * GameConfig.TileCellSize; |
---|
| | // MapEditorToolsPanel.CreateDoorTool( |
---|
| | // doorPos, DoorDirection.E, |
---|
| | // 0 * GameConfig.TileCellSize, 4 * GameConfig.TileCellSize |
---|
| | // ); |
---|
| | |
---|
| | //加载门编辑区域 |
---|
| | foreach (var doorAreaInfo in _doorConfigs) |
---|
| | { |
---|
| | MapEditorToolsPanel.CreateDoorTool(doorAreaInfo); |
---|
| |
---|
| | ClearLayer(AutoTopLayer); |
---|
| | ClearLayer(AutoMiddleLayer); |
---|
| | } |
---|
| | |
---|
| | //从新计算房间区域 |
---|
| | private void CalcTileRect() |
---|
| | //重新计算房间区域 |
---|
| | private void CalcTileRect(bool sendMessage) |
---|
| | { |
---|
| | var rect = GetUsedRect(); |
---|
| | _roomPosition = rect.Position; |
---|
| | SetMapSize(rect.Size); |
---|
| | if (sendMessage) |
---|
| | { |
---|
| | SetMapSize(rect.Size); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | //检测是否有不合规的图块, 返回true表示图块正常 |
---|
| | private bool CheckTerrain() |
---|
| |
---|
| | { |
---|
| | var pos = new Vector2I(x + i, y + j); |
---|
| | if (GetCellSourceId(AutoFloorLayer, pos) == -1) |
---|
| | { |
---|
| | //先检测对角是否有地板 |
---|
| | var left = _autoCellLayerGrid.Get(pos.X - 1, pos.Y); |
---|
| | var right = _autoCellLayerGrid.Get(pos.X + 1, pos.Y); |
---|
| | var top = _autoCellLayerGrid.Get(pos.X, pos.Y + 1); |
---|
| | var down = _autoCellLayerGrid.Get(pos.X, pos.Y - 1); |
---|
| | |
---|
| | if ((left && right) || (top && down)) |
---|
| | //先检测对边是否有地板 |
---|
| | if ((_autoCellLayerGrid.Get(pos.X - 1, pos.Y) && _autoCellLayerGrid.Get(pos.X + 1, pos.Y)) //left & right |
---|
| | || (_autoCellLayerGrid.Get(pos.X, pos.Y + 1) && _autoCellLayerGrid.Get(pos.X, pos.Y - 1))) //top & down |
---|
| | { |
---|
| | _checkTerrainFlag = false; |
---|
| | _checkTerrainErrorPosition = pos; |
---|
| | return false; |
---|
| | } |
---|
| | |
---|
| | //再检测对角是否有地板 |
---|
| | var topLeft = _autoCellLayerGrid.Get(pos.X - 1, pos.Y + 1); //top-left |
---|
| | var downRight = _autoCellLayerGrid.Get(pos.X + 1, pos.Y - 1); //down-right |
---|
| | var downLeft = _autoCellLayerGrid.Get(pos.X - 1, pos.Y - 1); //down-left |
---|
| | var topRight = _autoCellLayerGrid.Get(pos.X + 1, pos.Y + 1); //top-right |
---|
| | if ((topLeft && downRight && !downLeft && !topRight) || (!topLeft && !downRight && downLeft && topRight)) |
---|
| | { |
---|
| | _checkTerrainFlag = false; |
---|
| | _checkTerrainErrorPosition = pos; |
---|
| | return false; |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| |
---|
| | var arr = new Array<Vector2I>(list); |
---|
| | //绘制自动图块 |
---|
| | SetCellsTerrainConnect(AutoFloorLayer, arr, _terrainSet, _terrain, false); |
---|
| | //计算区域 |
---|
| | CalcTileRect(); |
---|
| | CalcTileRect(true); |
---|
| | //将墙壁移动到指定层 |
---|
| | MoveTerrainCell(); |
---|
| | } |
---|
| | |
---|
| |
---|
| | /// 选中拖拽功能 |
---|
| | /// </summary> |
---|
| | public void OnSelectHandTool() |
---|
| | { |
---|
| | |
---|
| | MouseType = MouseButtonType.Drag; |
---|
| | } |
---|
| | |
---|
| | /// <summary> |
---|
| | /// 选中画笔攻击 |
---|
| | /// </summary> |
---|
| | public void OnSelectPenTool() |
---|
| | { |
---|
| | |
---|
| | MouseType = MouseButtonType.Pen; |
---|
| | } |
---|
| | |
---|
| | /// <summary> |
---|
| | /// 选中绘制区域功能 |
---|
| | /// </summary> |
---|
| | public void OnSelectRectTool() |
---|
| | { |
---|
| | |
---|
| | MouseType = MouseButtonType.Area; |
---|
| | } |
---|
| | |
---|
| | /// <summary> |
---|
| | /// 选择编辑门区域 |
---|
| | /// </summary> |
---|
| | public void OnSelectDoorTool() |
---|
| | { |
---|
| | MouseType = MouseButtonType.Door; |
---|
| | } |
---|
| | |
---|
| | /// <summary> |
---|
| | /// 聚焦 |
---|
| |
---|
| | |