Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / mapEditorTools / MarkTool.cs
@小李xl 小李xl on 6 Jan 2024 5 KB 保存TileSet数据,开发中
  1. using Godot;
  2. using UI.MapEditor;
  3.  
  4. namespace UI.MapEditorTools;
  5.  
  6. /// <summary>
  7. /// 标记工具
  8. /// </summary>
  9. public partial class MarkTool : TextureRect, IUiNodeScript
  10. {
  11. /// <summary>
  12. /// 绑定的数据
  13. /// </summary>
  14. public MarkInfo MarkInfo { get; private set; }
  15.  
  16. /// <summary>
  17. /// 是否拖拽中
  18. /// </summary>
  19. public bool IsDrag { get; private set; }
  20. /// <summary>
  21. /// 标记区域
  22. /// </summary>
  23. public MarkAreaTool MarkAreaTool { get; private set; }
  24.  
  25. private bool _enter;
  26. private MapEditorTools.MarkTemplate _toolNode;
  27. private bool _isDown;
  28. private Vector2 _offset;
  29. private Vector2 _startPos;
  30. public void SetUiNode(IUiNode uiNode)
  31. {
  32. _toolNode = (MapEditorTools.MarkTemplate)uiNode;
  33. _toolNode.Instance.MouseEntered += OnMouseEntered;
  34. _toolNode.Instance.MouseExited += OnMouseExited;
  35. MarkAreaTool = new MarkAreaTool();
  36. MarkAreaTool.Position = Size / 2;
  37. MarkAreaTool.Visible = false;
  38. AddChild(MarkAreaTool);
  39. }
  40.  
  41. public void OnDestroy()
  42. {
  43. }
  44.  
  45. public override void _Process(double delta)
  46. {
  47. if (_toolNode != null && MarkInfo != null && _toolNode.UiPanel.S_ToolRoot.Instance.Visible && !DoorHoverArea.IsDrag)
  48. {
  49. if (_isDown)
  50. {
  51. //松开鼠标或者在拖拽区域
  52. if (!Input.IsMouseButtonPressed(MouseButton.Left) || MarkAreaTool.IsDrag)
  53. {
  54. _isDown = false;
  55. IsDrag = false;
  56. //移动过, 就派发修改事件
  57. var pos = GlobalPosition;
  58. if (_startPos != pos)
  59. {
  60. _startPos = pos;
  61. EventManager.EmitEvent(EventEnum.OnTileMapDirty);
  62. }
  63. }
  64. }
  65. else if (_enter && !_isDown)
  66. {
  67. //判断是否可以选中
  68. var activeMark = _toolNode.UiPanel.ActiveMark;
  69. if ((activeMark == null || (!activeMark.IsDrag && !activeMark.MarkAreaTool.IsDrag)) &&
  70. !MarkAreaTool.IsDrag && Input.IsMouseButtonPressed(MouseButton.Left) &&
  71. _toolNode.UiPanel.EditorMap.Instance.MouseType == EditorTileMap.MouseButtonType.Edit)
  72. {
  73. _isDown = true;
  74. if (_toolNode.UiPanel.ActiveMark != this)
  75. {
  76. IsDrag = false;
  77. _toolNode.UiPanel.SetActiveMark(this);
  78. }
  79. else
  80. {
  81. _offset = GlobalPosition - GetGlobalMousePosition();
  82. IsDrag = true;
  83. _startPos = GlobalPosition;
  84. }
  85. }
  86. }
  87.  
  88. //拖拽中
  89. if (IsDrag && _toolNode.UiPanel.ActiveMark == this)
  90. {
  91. GlobalPosition = _offset + GetGlobalMousePosition().Round();
  92. MarkInfo.Position = new SerializeVector2((Position + (Size / 2).Ceil()).Round());
  93. }
  94.  
  95. //只有选中物体才会显示拖拽区域
  96. if (_toolNode.UiPanel.ActiveMark == this)
  97. {
  98. MarkAreaTool.Visible = true;
  99. }
  100. else
  101. {
  102. MarkAreaTool.Visible = false;
  103. }
  104. QueueRedraw();
  105. }
  106. }
  107. /// <summary>
  108. /// 初始化数据
  109. /// </summary>
  110. public void InitData(MarkInfo markInfo)
  111. {
  112. MarkInfo = markInfo;
  113. Position = markInfo.Position.AsVector2() - (Size / 2).Ceil();
  114. _startPos = GlobalPosition;
  115. MarkAreaTool.InitData(_toolNode.UiPanel.S_ToolRoot, this);
  116. //显示图标
  117. Texture = ResourceManager.GetMarkIcon(markInfo);
  118. }
  119.  
  120. /// <summary>
  121. /// 刷新标记数据
  122. /// </summary>
  123. public void RefreshData()
  124. {
  125. //更新坐标
  126. Position = MarkInfo.Position.AsVector2();
  127. //更新icon
  128. Texture = ResourceManager.GetMarkIcon(MarkInfo);
  129. }
  130. private void OnMouseEntered()
  131. {
  132. _enter = true;
  133. }
  134.  
  135. private void OnMouseExited()
  136. {
  137. _enter = false;
  138. }
  139.  
  140. public override void _Draw()
  141. {
  142. if (MarkInfo != null && !MarkAreaTool.Visible)
  143. {
  144. var size = new Vector2(MarkInfo.Size.X + 2, MarkInfo.Size.Y + 2);
  145. DrawRect(new Rect2(-size / 2 + Size / 2, size.X, size.Y), new Color(1, 1, 1, 0.7f), false, 1);
  146. }
  147. }
  148.  
  149. /// <summary>
  150. /// 选中标记
  151. /// </summary>
  152. public void OnSelect()
  153. {
  154. var a = Modulate.A;
  155. Modulate = new Color(0, 1, 0, a);
  156. }
  157.  
  158. /// <summary>
  159. /// 取消选中标记
  160. /// </summary>
  161. public void OnUnSelect()
  162. {
  163. var a = Modulate.A;
  164. Modulate = new Color(1, 1, 1, a);
  165. }
  166.  
  167. /// <summary>
  168. /// 设置透明度值
  169. /// </summary>
  170. public void SetModulateAlpha(float a)
  171. {
  172. var m = Modulate;
  173. m.A = a;
  174. Modulate = m;
  175. }
  176. }