diff --git a/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkAreaTool.cs b/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkAreaTool.cs index 8343a4e..d446319 100644 --- a/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkAreaTool.cs +++ b/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkAreaTool.cs @@ -1,25 +1,17 @@ -using System.Drawing; -using Godot; +using Godot; using Color = Godot.Color; namespace UI.MapEditorTools; public partial class MarkAreaTool : Node2D { - /// - /// 宽度 - /// - public int Width { get; private set; } - /// - /// 高度 - /// - public int Height { get; private set; } - - private static Color _sideColor = new Color(0, 0, 0, 0); - private static Color _sideHoverColor = new Color(0, 0, 0, 0.3f); + //鼠标悬停颜色 + private static Color _sideColor = new Color(1, 1, 1, 1); + private static Color _sideHoverColor = new Color(0.65f, 0.65f, 0.65f, 1); private static Color _cornerColor = new Color(1, 1, 1, 1); private static Color _cornerHoverColor = new Color(0.65f, 0.65f, 0.65f, 1); + //鼠标悬停状态 private bool _mouseInL = false; private bool _mouseInR = false; private bool _mouseInT = false; @@ -28,62 +20,126 @@ private bool _mouseInRT = false; private bool _mouseInLB = false; private bool _mouseInRB = false; + + private MarkInfo _markInfo; + private MarkTool _markTool; + private MapEditorTools.ToolRoot _toolRoot; - public void SetSize(int width, int height) + /// + /// 是否正在拖拽中 + /// + public bool IsDrag { get; private set; } = false; + private Vector2 _startMousePosition; + private Vector2 _prevMousePosition; + private Vector2 _startPosition; + private float _startWidth; + + public void InitData(MapEditorTools.ToolRoot toolRoot, MarkTool markTool) { - Width = width; - Height = height; + _toolRoot = toolRoot; + _markTool = markTool; + _markInfo = markTool.MarkInfo; } public override void _Process(double delta) { - if (!Visible) + if (!Visible || _markInfo == null) { return; } - - _mouseInL = false; - _mouseInR = false; - _mouseInT = false; - _mouseInB = false; - _mouseInLT = false; - _mouseInRT = false; - _mouseInLB = false; - _mouseInRB = false; - var mousePosition = GetLocalMousePosition(); - //判断鼠标是否在点上 - if (Utils.IsPositionInRect(mousePosition, GetLeftTopRect())) + if (IsDrag) //按下拖拽按钮 { - _mouseInLT = true; + if (!Input.IsMouseButtonPressed(MouseButton.Left)) //松开拖拽 + { + IsDrag = false; + } + else //拖拽中 + { + var pos = GetGlobalMousePosition(); + if (pos != _prevMousePosition) + { + if (_mouseInL) + { + var offset = GetGlobalMousePosition() - _startMousePosition; + offset = offset / _toolRoot.Instance.Scale; + var newWidth = Mathf.Max(1, (int)(_startWidth - offset.X)); + _markInfo.Size = new SerializeVector2(newWidth, _markInfo.Size.Y); + var end = (int)(_startPosition.X + _startWidth / 2f); + var newX = (int)(end - newWidth / 2f); + _markTool.Position = new Vector2(newX, _markTool.Position.Y); + GD.Print("newWidth: " + newWidth); + } + _prevMousePosition = pos; + } + } } - else if (Utils.IsPositionInRect(mousePosition, GetRightTopRect())) + else //未被拖拽 { - _mouseInRT = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetLeftBottomRect())) - { - _mouseInLB = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetRightBottomRect())) - { - _mouseInRB = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetLeftRect())) - { - _mouseInL = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetRightRect())) - { - _mouseInR = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetTopRect())) - { - _mouseInT = true; - } - else if (Utils.IsPositionInRect(mousePosition, GetBottomRect())) - { - _mouseInB = true; + _mouseInL = false; + _mouseInR = false; + _mouseInT = false; + _mouseInB = false; + _mouseInLT = false; + _mouseInRT = false; + _mouseInLB = false; + _mouseInRB = false; + + var flag = false; + var mousePosition = GetLocalMousePosition(); + //判断鼠标是否在点上 + if (Utils.IsPositionInRect(mousePosition, GetLeftTopRect())) + { + _mouseInLT = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetRightTopRect())) + { + _mouseInRT = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetLeftBottomRect())) + { + _mouseInLB = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetRightBottomRect())) + { + _mouseInRB = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetLeftRect())) + { + _mouseInL = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetRightRect())) + { + _mouseInR = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetTopRect())) + { + _mouseInT = true; + flag = true; + } + else if (Utils.IsPositionInRect(mousePosition, GetBottomRect())) + { + _mouseInB = true; + flag = true; + } + + if (flag) + { + if (Input.IsMouseButtonPressed(MouseButton.Left)) + { + IsDrag = true; + _startMousePosition = GetGlobalMousePosition(); + _prevMousePosition = _startMousePosition; + _startPosition = _markTool.Position; + _startWidth = _markInfo.Size.X; + } + } } if (Visible) @@ -108,41 +164,41 @@ private Rect2 GetTopRect() { - return new Rect2(-Width / 2f + 0.5f, -Height / 2f - 0.5f, Width - 1, 1); + return new Rect2(-_markInfo.Size.X / 2f + 0.5f, -_markInfo.Size.Y / 2f - 0.5f, _markInfo.Size.X - 1, 1); } private Rect2 GetBottomRect() { - return new Rect2(-Width / 2f + 0.5f, Height / 2f - 0.5f, Width - 1, 1); + return new Rect2(-_markInfo.Size.X / 2f + 0.5f, _markInfo.Size.Y / 2f - 0.5f, _markInfo.Size.X - 1, 1); } private Rect2 GetLeftRect() { - return new Rect2(-Width / 2f - 0.5f, -Height / 2f + 0.5f, 1, Height - 1); + return new Rect2(-_markInfo.Size.X / 2f - 0.5f, -_markInfo.Size.Y / 2f + 0.5f, 1, _markInfo.Size.Y - 1); } private Rect2 GetRightRect() { - return new Rect2(Width / 2f - 0.5f, -Height / 2f + 0.5f, 1, Height - 1); + return new Rect2(_markInfo.Size.X / 2f - 0.5f, -_markInfo.Size.Y / 2f + 0.5f, 1, _markInfo.Size.Y - 1); } private Rect2 GetLeftTopRect() { - return new Rect2(-Width / 2f - 1.5f, -Height / 2f - 1.5f, 3, 3); + return new Rect2(-_markInfo.Size.X / 2f - 1.5f, -_markInfo.Size.Y / 2f - 1.5f, 3, 3); } private Rect2 GetLeftBottomRect() { - return new Rect2(-Width / 2f - 1.5f, Height / 2f - 1.5f, 3, 3); + return new Rect2(-_markInfo.Size.X / 2f - 1.5f, _markInfo.Size.Y / 2f - 1.5f, 3, 3); } private Rect2 GetRightTopRect() { - return new Rect2(Width / 2f - 1.5f, -Height / 2f - 1.5f, 3, 3); + return new Rect2(_markInfo.Size.X / 2f - 1.5f, -_markInfo.Size.Y / 2f - 1.5f, 3, 3); } private Rect2 GetRightBottomRect() { - return new Rect2(Width / 2f - 1.5f, Height / 2f - 1.5f, 3, 3); + return new Rect2(_markInfo.Size.X / 2f - 1.5f, _markInfo.Size.Y / 2f - 1.5f, 3, 3); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkTool.cs b/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkTool.cs index 40f4837..fedda46 100644 --- a/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkTool.cs +++ b/DungeonShooting_Godot/src/game/ui/mapEditorTools/MarkTool.cs @@ -11,8 +11,13 @@ /// 绑定的数据 /// public MarkInfo MarkInfo { get; private set; } + + /// + /// 是否拖拽中 + /// + public bool IsDrag { get; private set; } + private bool _enter; - private bool _isMOve; private MapEditorTools.MarkTemplate _toolNode; private bool _isDown; private Vector2 _offset; @@ -38,44 +43,51 @@ { if (_isDown) { - //松开鼠标 - if (!Input.IsMouseButtonPressed(MouseButton.Left)) + //松开鼠标或者在拖拽区域 + if (!Input.IsMouseButtonPressed(MouseButton.Left) || _markAreaToolUp.IsDrag) { _isDown = false; - _isMOve = false; + IsDrag = false; } } else if (!_isDown) { //按下鼠标 - if (Input.IsMouseButtonPressed(MouseButton.Left)) + if (!_markAreaToolUp.IsDrag && Input.IsMouseButtonPressed(MouseButton.Left)) { _isDown = true; if (_toolNode.UiPanel.ActiveMark != this) { - _isMOve = false; + IsDrag = false; _toolNode.UiPanel.SetActiveMark(this); } else { _offset = GlobalPosition - GetGlobalMousePosition(); - _isMOve = true; + IsDrag = true; } } } } - //移动中 - if (_isMOve && _toolNode.UiPanel.ActiveMark == this) + //拖拽中 + if (IsDrag && _toolNode.UiPanel.ActiveMark == this) { GlobalPosition = _offset + GetGlobalMousePosition().Round(); MarkInfo.Position = new SerializeVector2((Position + (Size / 2).Ceil()).Round()); } + //只有选中物体才会显示拖拽区域 + if (_toolNode.UiPanel.ActiveMark == this && MarkInfo.Size.X > 0 && MarkInfo.Size.Y > 0) + { + _markAreaToolUp.Visible = true; + } + else + { + _markAreaToolUp.Visible = false; + } QueueRedraw(); } - - RefreshAreaTool(); } /// @@ -85,7 +97,7 @@ { MarkInfo = markInfo; Position = markInfo.Position.AsVector2() - (Size / 2).Ceil(); - RefreshAreaTool(); + _markAreaToolUp.InitData(_toolNode.UiPanel.S_ToolRoot, this); } private void OnMouseEntered() @@ -100,24 +112,10 @@ public override void _Draw() { - if (MarkInfo != null && MarkInfo.Size.X > 0 && MarkInfo.Size.Y > 0) + if (MarkInfo != null && MarkInfo.Size.X > 0 && MarkInfo.Size.Y > 0 && !_markAreaToolUp.Visible) { var size = MarkInfo.Size.AsVector2(); DrawRect(new Rect2(-size / 2 + Size / 2, size.X, size.Y), new Color(1, 1, 1, 0.3f), false, 1); } } - - //刷新区域标记工具数据 - private void RefreshAreaTool() - { - if (_toolNode.UiPanel.ActiveMark == this && MarkInfo.Size.X > 0 && MarkInfo.Size.Y > 0) - { - _markAreaToolUp.Visible = true; - _markAreaToolUp.SetSize((int)MarkInfo.Size.X, (int)MarkInfo.Size.Y); - } - else - { - _markAreaToolUp.Visible = false; - } - } } \ No newline at end of file