Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / tileSetEditorCombination / GridBg.cs
  1. using Godot;
  2.  
  3. namespace UI.TileSetEditorCombination;
  4.  
  5. public abstract partial class GridBg<T> : ColorRect, IUiNodeScript where T : IUiNode
  6. {
  7. public ColorRect Grid { get; protected set; }
  8. public Control ContainerRoot { get; protected set; }
  9. public T UiNode { get; private set; }
  10. public virtual void SetUiNode(IUiNode uiNode)
  11. {
  12. UiNode = (T)uiNode;
  13. this.AddDragListener(DragButtonEnum.Middle, OnDrag);
  14. Resized += RefreshGridTrans;
  15. }
  16.  
  17. public virtual void OnDestroy()
  18. {
  19. }
  20. /// <summary>
  21. /// 当前Ui被显示出来时调用
  22. /// </summary>
  23. public void OnShow()
  24. {
  25. RefreshGridTrans();
  26. }
  27.  
  28. public override void _GuiInput(InputEvent @event)
  29. {
  30. if (@event is InputEventMouseButton mouseButton)
  31. {
  32. AcceptEvent();
  33. if (mouseButton.ButtonIndex == MouseButton.WheelDown)
  34. {
  35. //缩小
  36. if (Utils.DoShrinkByMousePosition(ContainerRoot, 0.4f))
  37. {
  38. SetGridTransform(ContainerRoot.Position, ContainerRoot.Scale.X);
  39. }
  40. }
  41. else if (mouseButton.ButtonIndex == MouseButton.WheelUp)
  42. {
  43. //放大
  44. if (Utils.DoMagnifyByMousePosition(ContainerRoot, 20))
  45. {
  46. SetGridTransform(ContainerRoot.Position, ContainerRoot.Scale.X);
  47. }
  48. }
  49. }
  50. }
  51. //拖拽回调
  52. private void OnDrag(DragState state, Vector2 pos)
  53. {
  54. if (state == DragState.DragMove)
  55. {
  56. ContainerRoot.Position += pos;
  57. RefreshGridTrans();
  58. }
  59. }
  60. /// <summary>
  61. /// 刷新背景网格位置和缩放
  62. /// </summary>
  63. public void RefreshGridTrans()
  64. {
  65. Grid.Material.SetShaderMaterialParameter(ShaderParamNames.Size, Size);
  66. SetGridTransform(ContainerRoot.Position, ContainerRoot.Scale.X);
  67. }
  68. //设置网格位置和缩放
  69. private void SetGridTransform(Vector2 pos, float scale)
  70. {
  71. Grid.Material.SetShaderMaterialParameter(ShaderParamNames.GridSize, GameConfig.TileCellSize * scale);
  72. Grid.Material.SetShaderMaterialParameter(ShaderParamNames.Offset, -pos);
  73. }
  74. }