编辑门区域动态调整大小
1 parent efee171 commit 9f69a3be3d1e60d71770c2ed5c87fd576b6ed42b
@小李xl 小李xl authored on 30 Jul 2023
Showing 8 changed files
View
14
DungeonShooting_Godot/prefab/ui/MapEditorTools.tscn
anchor_bottom = 1.0
offset_top = -36.0
offset_right = 160.0
grow_vertical = 0
color = Color(1, 1, 1, 0)
color = Color(1, 1, 1, 0.313726)
script = ExtResource("2_6qfy3")
 
[node name="E_HoverRoot" type="Control" parent="DoorToolRoot"]
anchors_preset = 0
anchor_bottom = 1.0
offset_top = -36.0
offset_right = 160.0
grow_vertical = 0
color = Color(1, 1, 1, 0)
color = Color(1, 1, 1, 0.313726)
script = ExtResource("2_6qfy3")
 
[node name="S_HoverRoot" type="Control" parent="DoorToolRoot"]
anchors_preset = 0
offset_left = -9.12665e-05
offset_top = -36.0
offset_right = 160.0
grow_vertical = 0
color = Color(1, 1, 1, 0)
color = Color(1, 1, 1, 0.313726)
script = ExtResource("2_6qfy3")
 
[node name="W_HoverRoot" type="Control" parent="DoorToolRoot"]
anchors_preset = 0
anchor_bottom = 1.0
offset_top = -36.0
offset_right = 160.0
grow_vertical = 0
color = Color(1, 1, 1, 0)
color = Color(1, 1, 1, 0.313726)
script = ExtResource("2_6qfy3")
 
[node name="HoverPreviewRoot" type="Control" parent="DoorToolRoot"]
visible = false
texture_normal = ExtResource("2_rwvbg")
texture_pressed = ExtResource("2_rwvbg")
texture_hover = ExtResource("2_rwvbg")
 
[node name="DoorTool" type="TextureButton" parent="HBoxContainer"]
layout_mode = 2
texture_normal = ExtResource("2_rwvbg")
texture_pressed = ExtResource("2_rwvbg")
texture_hover = ExtResource("2_rwvbg")
 
[node name="CenterTool" type="TextureButton" parent="HBoxContainer"]
layout_mode = 2
texture_normal = ExtResource("2_rwvbg")
texture_pressed = ExtResource("2_rwvbg")
View
175
DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMap.cs
 
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>
/// 聚焦
View
2
■■■
DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMapBar.cs
var mapEditorToolsPanel = _editorPanel.S_MapEditorTools.Instance;
mapEditorToolsPanel.S_HandTool.Instance.Pressed += _editorTileMap.Instance.OnSelectHandTool;
mapEditorToolsPanel.S_PenTool.Instance.Pressed += _editorTileMap.Instance.OnSelectPenTool;
mapEditorToolsPanel.S_RectTool.Instance.Pressed += _editorTileMap.Instance.OnSelectRectTool;
mapEditorToolsPanel.S_DoorTool.Instance.Pressed += _editorTileMap.Instance.OnSelectDoorTool;
mapEditorToolsPanel.S_CenterTool.Instance.Pressed += _editorTileMap.Instance.OnClickCenterTool;
}
 
public void OnHide()
var mapEditorToolsPanel = _editorPanel.S_MapEditorTools.Instance;
mapEditorToolsPanel.S_HandTool.Instance.Pressed -= _editorTileMap.Instance.OnSelectHandTool;
mapEditorToolsPanel.S_PenTool.Instance.Pressed -= _editorTileMap.Instance.OnSelectPenTool;
mapEditorToolsPanel.S_RectTool.Instance.Pressed -= _editorTileMap.Instance.OnSelectRectTool;
mapEditorToolsPanel.S_DoorTool.Instance.Pressed -= _editorTileMap.Instance.OnSelectDoorTool;
mapEditorToolsPanel.S_CenterTool.Instance.Pressed -= _editorTileMap.Instance.OnClickCenterTool;
}
 
public void Process(float delta)
View
DungeonShooting_Godot/src/game/ui/mapEditorTools/DoorDragArea.cs
View
DungeonShooting_Godot/src/game/ui/mapEditorTools/DoorDragButton.cs
View
DungeonShooting_Godot/src/game/ui/mapEditorTools/DoorHoverArea.cs
View
DungeonShooting_Godot/src/game/ui/mapEditorTools/MapEditorTools.cs
View
DungeonShooting_Godot/src/game/ui/mapEditorTools/MapEditorToolsPanel.cs