Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorTools / MarkAreaTool.cs
@小李xl 小李xl on 20 Aug 2023 10 KB 创建房间标记, 开发中
  1. using Godot;
  2. using UI.MapEditor;
  3. using Color = Godot.Color;
  4.  
  5. namespace UI.MapEditorTools;
  6.  
  7. public partial class MarkAreaTool : Node2D
  8. {
  9. //鼠标悬停颜色
  10. private static Color _sideColor = new Color(1, 1, 1, 1);
  11. private static Color _sideHoverColor = new Color(0.65f, 0.65f, 0.65f, 1);
  12. private static Color _cornerColor = new Color(1, 1, 1, 1);
  13. private static Color _cornerHoverColor = new Color(0.65f, 0.65f, 0.65f, 1);
  14. //鼠标悬停状态
  15. private bool _mouseInL = false;
  16. private bool _mouseInR = false;
  17. private bool _mouseInT = false;
  18. private bool _mouseInB = false;
  19. private bool _mouseInLT = false;
  20. private bool _mouseInRT = false;
  21. private bool _mouseInLB = false;
  22. private bool _mouseInRB = false;
  23. //是否绘制角
  24. private bool _showCornerBlock = false;
  25.  
  26. private MarkInfo _markInfo;
  27. private MarkTool _markTool;
  28. private MapEditorTools.ToolRoot _toolRoot;
  29. /// <summary>
  30. /// 是否正在拖拽中
  31. /// </summary>
  32. public bool IsDrag { get; private set; } = false;
  33. private Vector2 _startMousePosition;
  34. private Vector2 _prevMousePosition;
  35. private Vector2 _startPosition;
  36. private float _startWidth;
  37. private float _startHeight;
  38. public void InitData(MapEditorTools.ToolRoot toolRoot, MarkTool markTool)
  39. {
  40. _toolRoot = toolRoot;
  41. _markTool = markTool;
  42. _markInfo = markTool.MarkInfo;
  43. }
  44.  
  45. public override void _Process(double delta)
  46. {
  47. if (!Visible || _markInfo == null || _toolRoot.UiPanel.IsOpenPopUps || !_toolRoot.UiPanel.S_ToolRoot.Instance.Visible)
  48. {
  49. return;
  50. }
  51.  
  52. _showCornerBlock = _markInfo.Size.X >= 4 && _markInfo.Size.Y >= 4;
  53. var globalMousePosition = GetGlobalMousePosition();
  54. if (IsDrag) //按下拖拽按钮
  55. {
  56. if (!Input.IsMouseButtonPressed(MouseButton.Left)) //松开拖拽
  57. {
  58. IsDrag = false;
  59. }
  60. else //拖拽中
  61. {
  62. var pos = globalMousePosition;
  63. if (pos != _prevMousePosition)
  64. {
  65.  
  66. if (_mouseInL)
  67. {
  68. var offset = globalMousePosition - _startMousePosition;
  69. offset = offset / _toolRoot.Instance.Scale * 2f;
  70. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  71. _markInfo.Size = new SerializeVector2(newWidth, _markInfo.Size.Y);
  72. }
  73. else if (_mouseInR)
  74. {
  75. var offset = _startMousePosition - globalMousePosition;
  76. offset = offset / _toolRoot.Instance.Scale * 2f;
  77. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  78. _markInfo.Size = new SerializeVector2(newWidth, _markInfo.Size.Y);
  79. }
  80. else if (_mouseInT)
  81. {
  82. var offset = globalMousePosition - _startMousePosition;
  83. offset = offset / _toolRoot.Instance.Scale * 2f;
  84. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  85. _markInfo.Size = new SerializeVector2(_markInfo.Size.X, newHeight);
  86. }
  87. else if (_mouseInB)
  88. {
  89. var offset = _startMousePosition - globalMousePosition;
  90. offset = offset / _toolRoot.Instance.Scale * 2f;
  91. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  92. _markInfo.Size = new SerializeVector2(_markInfo.Size.X, newHeight);
  93. }
  94. //----------------------------------------------------------------------------------
  95. else if (_mouseInLT)
  96. {
  97. var offset = globalMousePosition - _startMousePosition;
  98. offset = offset / _toolRoot.Instance.Scale * 2f;
  99. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  100. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  101. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  102. }
  103. else if (_mouseInLB)
  104. {
  105. var offsetX = (globalMousePosition.X - _startMousePosition.X) / _toolRoot.Instance.Scale.X * 2f;
  106. var offsetY = (_startMousePosition.Y - globalMousePosition.Y) / _toolRoot.Instance.Scale.Y * 2f;
  107. var newWidth = Mathf.Max(0, (int)(_startWidth - offsetX));
  108. var newHeight = Mathf.Max(0, (int)(_startHeight - offsetY));
  109. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  110. }
  111. else if (_mouseInRT)
  112. {
  113. var offsetX = (_startMousePosition.X - globalMousePosition.X) / _toolRoot.Instance.Scale.X * 2f;
  114. var offsetY = (globalMousePosition.Y - _startMousePosition.Y) / _toolRoot.Instance.Scale.Y * 2f;
  115. var newWidth = Mathf.Max(0, (int)(_startWidth - offsetX));
  116. var newHeight = Mathf.Max(0, (int)(_startHeight - offsetY));
  117. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  118. }
  119. else if (_mouseInRB)
  120. {
  121. var offset = _startMousePosition - globalMousePosition;
  122. offset = offset / _toolRoot.Instance.Scale * 2f;
  123. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  124. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  125. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  126. }
  127. _prevMousePosition = pos;
  128. }
  129. }
  130. }
  131. else //未被拖拽
  132. {
  133. _mouseInL = false;
  134. _mouseInR = false;
  135. _mouseInT = false;
  136. _mouseInB = false;
  137. _mouseInLT = false;
  138. _mouseInRT = false;
  139. _mouseInLB = false;
  140. _mouseInRB = false;
  141.  
  142. var flag = false;
  143. //必须要选中
  144. if (_toolRoot.UiPanel.EditorMap.Instance.MouseType == EditorTileMap.MouseButtonType.Edit)
  145. {
  146. var mousePosition = GetLocalMousePosition();
  147. //判断鼠标是否在点上
  148. if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetLeftTopRect()))
  149. {
  150. _mouseInLT = true;
  151. flag = true;
  152. }
  153. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetRightTopRect()))
  154. {
  155. _mouseInRT = true;
  156. flag = true;
  157. }
  158. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetLeftBottomRect()))
  159. {
  160. _mouseInLB = true;
  161. flag = true;
  162. }
  163. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetRightBottomRect()))
  164. {
  165. _mouseInRB = true;
  166. flag = true;
  167. }
  168. else if (Utils.IsPositionInRect(mousePosition, GetLeftRect()))
  169. {
  170. _mouseInL = true;
  171. flag = true;
  172. }
  173. else if (Utils.IsPositionInRect(mousePosition, GetRightRect()))
  174. {
  175. _mouseInR = true;
  176. flag = true;
  177. }
  178. else if (Utils.IsPositionInRect(mousePosition, GetTopRect()))
  179. {
  180. _mouseInT = true;
  181. flag = true;
  182. }
  183. else if (Utils.IsPositionInRect(mousePosition, GetBottomRect()))
  184. {
  185. _mouseInB = true;
  186. flag = true;
  187. }
  188. }
  189.  
  190. if (flag)
  191. {
  192. if (Input.IsMouseButtonPressed(MouseButton.Left))
  193. {
  194. IsDrag = true;
  195. _startMousePosition = globalMousePosition;
  196. _prevMousePosition = _startMousePosition;
  197. _startPosition = _markTool.Position;
  198. _startWidth = _markInfo.Size.X;
  199. _startHeight = _markInfo.Size.Y;
  200. }
  201. }
  202. }
  203.  
  204. if (Visible)
  205. {
  206. QueueRedraw();
  207. }
  208. }
  209.  
  210. public override void _Draw()
  211. {
  212. //绘制边框
  213. DrawRect(GetTopRect(), _mouseInT ? _sideHoverColor : _sideColor);
  214. DrawRect(GetBottomRect(), _mouseInB ? _sideHoverColor : _sideColor);
  215. DrawRect(GetLeftRect(), _mouseInL ? _sideHoverColor : _sideColor);
  216. DrawRect(GetRightRect(), _mouseInR ? _sideHoverColor : _sideColor);
  217. if (_showCornerBlock)
  218. {
  219. //绘制角
  220. DrawRect(GetLeftTopRect(), _mouseInLT ? _cornerHoverColor : _cornerColor);
  221. DrawRect(GetLeftBottomRect(), _mouseInLB ? _cornerHoverColor : _cornerColor);
  222. DrawRect(GetRightTopRect(), _mouseInRT ? _cornerHoverColor : _cornerColor);
  223. DrawRect(GetRightBottomRect(), _mouseInRB ? _cornerHoverColor : _cornerColor);
  224. }
  225. }
  226.  
  227. private Rect2 GetTopRect()
  228. {
  229. return new Rect2(-(_markInfo.Size.X + 2) / 2f + 0.5f, -(_markInfo.Size.Y + 2) / 2f - 0.5f, (_markInfo.Size.X + 2) - 1, 1);
  230. }
  231. private Rect2 GetBottomRect()
  232. {
  233. return new Rect2(-(_markInfo.Size.X + 2) / 2f + 0.5f, (_markInfo.Size.Y + 2) / 2f - 0.5f, (_markInfo.Size.X + 2) - 1, 1);
  234. }
  235.  
  236. private Rect2 GetLeftRect()
  237. {
  238. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 0.5f, -(_markInfo.Size.Y + 2) / 2f + 0.5f, 1, (_markInfo.Size.Y + 2) - 1);
  239. }
  240. private Rect2 GetRightRect()
  241. {
  242. return new Rect2((_markInfo.Size.X + 2) / 2f - 0.5f, -(_markInfo.Size.Y + 2) / 2f + 0.5f, 1, (_markInfo.Size.Y + 2) - 1);
  243. }
  244.  
  245. private Rect2 GetLeftTopRect()
  246. {
  247. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 1.5f, -(_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  248. }
  249. private Rect2 GetLeftBottomRect()
  250. {
  251. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 1.5f, (_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  252. }
  253. private Rect2 GetRightTopRect()
  254. {
  255. return new Rect2((_markInfo.Size.X + 2) / 2f - 1.5f, -(_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  256. }
  257. private Rect2 GetRightBottomRect()
  258. {
  259. return new Rect2((_markInfo.Size.X + 2) / 2f - 1.5f, (_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  260. }
  261. }