Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorTools / MarkAreaTool.cs
  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. _startWidth = _markInfo.Size.X;
  44. _startHeight = _markInfo.Size.Y;
  45. }
  46.  
  47. public override void _Process(double delta)
  48. {
  49. if (!Visible || _markInfo == null || _toolRoot.UiPanel.IsOpenPopUps || !_toolRoot.UiPanel.S_ToolRoot.Instance.Visible)
  50. {
  51. return;
  52. }
  53.  
  54. _showCornerBlock = _markInfo.Size.X >= 4 && _markInfo.Size.Y >= 4;
  55. var globalMousePosition = GetGlobalMousePosition();
  56. if (IsDrag) //按下拖拽按钮
  57. {
  58. if (!Input.IsMouseButtonPressed(MouseButton.Left)) //松开拖拽
  59. {
  60. if (_markInfo.Size.X != _startWidth || _markInfo.Size.Y != _startHeight)
  61. {
  62. _startWidth = _markInfo.Size.X;
  63. _startHeight = _markInfo.Size.Y;
  64. //派发修改事件
  65. EventManager.EmitEvent(EventEnum.OnEditorDirty);
  66. }
  67. IsDrag = false;
  68. }
  69. else //拖拽中
  70. {
  71. var pos = globalMousePosition;
  72. if (pos != _prevMousePosition)
  73. {
  74.  
  75. if (_mouseInL)
  76. {
  77. var offset = globalMousePosition - _startMousePosition;
  78. offset = offset / _toolRoot.Instance.Scale * 2f;
  79. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  80. _markInfo.Size = new SerializeVector2(newWidth, _markInfo.Size.Y);
  81. }
  82. else if (_mouseInR)
  83. {
  84. var offset = _startMousePosition - globalMousePosition;
  85. offset = offset / _toolRoot.Instance.Scale * 2f;
  86. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  87. _markInfo.Size = new SerializeVector2(newWidth, _markInfo.Size.Y);
  88. }
  89. else if (_mouseInT)
  90. {
  91. var offset = globalMousePosition - _startMousePosition;
  92. offset = offset / _toolRoot.Instance.Scale * 2f;
  93. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  94. _markInfo.Size = new SerializeVector2(_markInfo.Size.X, newHeight);
  95. }
  96. else if (_mouseInB)
  97. {
  98. var offset = _startMousePosition - globalMousePosition;
  99. offset = offset / _toolRoot.Instance.Scale * 2f;
  100. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  101. _markInfo.Size = new SerializeVector2(_markInfo.Size.X, newHeight);
  102. }
  103. //----------------------------------------------------------------------------------
  104. else if (_mouseInLT)
  105. {
  106. var offset = globalMousePosition - _startMousePosition;
  107. offset = offset / _toolRoot.Instance.Scale * 2f;
  108. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  109. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  110. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  111. }
  112. else if (_mouseInLB)
  113. {
  114. var offsetX = (globalMousePosition.X - _startMousePosition.X) / _toolRoot.Instance.Scale.X * 2f;
  115. var offsetY = (_startMousePosition.Y - globalMousePosition.Y) / _toolRoot.Instance.Scale.Y * 2f;
  116. var newWidth = Mathf.Max(0, (int)(_startWidth - offsetX));
  117. var newHeight = Mathf.Max(0, (int)(_startHeight - offsetY));
  118. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  119. }
  120. else if (_mouseInRT)
  121. {
  122. var offsetX = (_startMousePosition.X - globalMousePosition.X) / _toolRoot.Instance.Scale.X * 2f;
  123. var offsetY = (globalMousePosition.Y - _startMousePosition.Y) / _toolRoot.Instance.Scale.Y * 2f;
  124. var newWidth = Mathf.Max(0, (int)(_startWidth - offsetX));
  125. var newHeight = Mathf.Max(0, (int)(_startHeight - offsetY));
  126. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  127. }
  128. else if (_mouseInRB)
  129. {
  130. var offset = _startMousePosition - globalMousePosition;
  131. offset = offset / _toolRoot.Instance.Scale * 2f;
  132. var newWidth = Mathf.Max(0, (int)(_startWidth - offset.X));
  133. var newHeight = Mathf.Max(0, (int)(_startHeight - offset.Y));
  134. _markInfo.Size = new SerializeVector2(newWidth, newHeight);
  135. }
  136. _prevMousePosition = pos;
  137. }
  138. }
  139. }
  140. else //未被拖拽
  141. {
  142. _mouseInL = false;
  143. _mouseInR = false;
  144. _mouseInT = false;
  145. _mouseInB = false;
  146. _mouseInLT = false;
  147. _mouseInRT = false;
  148. _mouseInLB = false;
  149. _mouseInRB = false;
  150.  
  151. var flag = false;
  152. //必须要选中
  153. if (_toolRoot.UiPanel.EditorMap.Instance.MouseType == EditorTileMap.MouseButtonType.Edit)
  154. {
  155. var mousePosition = GetLocalMousePosition();
  156. //判断鼠标是否在点上
  157. if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetLeftTopRect()))
  158. {
  159. _mouseInLT = true;
  160. flag = true;
  161. }
  162. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetRightTopRect()))
  163. {
  164. _mouseInRT = true;
  165. flag = true;
  166. }
  167. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetLeftBottomRect()))
  168. {
  169. _mouseInLB = true;
  170. flag = true;
  171. }
  172. else if (_showCornerBlock && Utils.IsPositionInRect(mousePosition, GetRightBottomRect()))
  173. {
  174. _mouseInRB = true;
  175. flag = true;
  176. }
  177. else if (Utils.IsPositionInRect(mousePosition, GetLeftRect()))
  178. {
  179. _mouseInL = true;
  180. flag = true;
  181. }
  182. else if (Utils.IsPositionInRect(mousePosition, GetRightRect()))
  183. {
  184. _mouseInR = true;
  185. flag = true;
  186. }
  187. else if (Utils.IsPositionInRect(mousePosition, GetTopRect()))
  188. {
  189. _mouseInT = true;
  190. flag = true;
  191. }
  192. else if (Utils.IsPositionInRect(mousePosition, GetBottomRect()))
  193. {
  194. _mouseInB = true;
  195. flag = true;
  196. }
  197. }
  198.  
  199. if (flag)
  200. {
  201. if (Input.IsMouseButtonPressed(MouseButton.Left))
  202. {
  203. IsDrag = true;
  204. _startMousePosition = globalMousePosition;
  205. _prevMousePosition = _startMousePosition;
  206. _startPosition = _markTool.Position;
  207. _startWidth = _markInfo.Size.X;
  208. _startHeight = _markInfo.Size.Y;
  209. }
  210. }
  211. }
  212.  
  213. if (Visible)
  214. {
  215. QueueRedraw();
  216. }
  217. }
  218.  
  219. public override void _Draw()
  220. {
  221. //绘制边框
  222. DrawRect(GetTopRect(), _mouseInT ? _sideHoverColor : _sideColor);
  223. DrawRect(GetBottomRect(), _mouseInB ? _sideHoverColor : _sideColor);
  224. DrawRect(GetLeftRect(), _mouseInL ? _sideHoverColor : _sideColor);
  225. DrawRect(GetRightRect(), _mouseInR ? _sideHoverColor : _sideColor);
  226. if (_showCornerBlock)
  227. {
  228. //绘制角
  229. DrawRect(GetLeftTopRect(), _mouseInLT ? _cornerHoverColor : _cornerColor);
  230. DrawRect(GetLeftBottomRect(), _mouseInLB ? _cornerHoverColor : _cornerColor);
  231. DrawRect(GetRightTopRect(), _mouseInRT ? _cornerHoverColor : _cornerColor);
  232. DrawRect(GetRightBottomRect(), _mouseInRB ? _cornerHoverColor : _cornerColor);
  233. }
  234. }
  235.  
  236. private Rect2 GetTopRect()
  237. {
  238. return new Rect2(-(_markInfo.Size.X + 2) / 2f + 0.5f, -(_markInfo.Size.Y + 2) / 2f - 0.5f, (_markInfo.Size.X + 2) - 1, 1);
  239. }
  240. private Rect2 GetBottomRect()
  241. {
  242. return new Rect2(-(_markInfo.Size.X + 2) / 2f + 0.5f, (_markInfo.Size.Y + 2) / 2f - 0.5f, (_markInfo.Size.X + 2) - 1, 1);
  243. }
  244.  
  245. private Rect2 GetLeftRect()
  246. {
  247. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 0.5f, -(_markInfo.Size.Y + 2) / 2f + 0.5f, 1, (_markInfo.Size.Y + 2) - 1);
  248. }
  249. private Rect2 GetRightRect()
  250. {
  251. return new Rect2((_markInfo.Size.X + 2) / 2f - 0.5f, -(_markInfo.Size.Y + 2) / 2f + 0.5f, 1, (_markInfo.Size.Y + 2) - 1);
  252. }
  253.  
  254. private Rect2 GetLeftTopRect()
  255. {
  256. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 1.5f, -(_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  257. }
  258. private Rect2 GetLeftBottomRect()
  259. {
  260. return new Rect2(-(_markInfo.Size.X + 2) / 2f - 1.5f, (_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  261. }
  262. private Rect2 GetRightTopRect()
  263. {
  264. return new Rect2((_markInfo.Size.X + 2) / 2f - 1.5f, -(_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  265. }
  266. private Rect2 GetRightBottomRect()
  267. {
  268. return new Rect2((_markInfo.Size.X + 2) / 2f - 1.5f, (_markInfo.Size.Y + 2) / 2f - 1.5f, 3, 3);
  269. }
  270. }