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