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